Skip to content

fix(adapter): sanitize page parameter to prevent ArgumentError (#7830) - #7832

Open
UwaisBaig wants to merge 1 commit into
CircuitVerse:masterfrom
UwaisBaig:fix/search-adapter-page-sanitization
Open

fix(adapter): sanitize page parameter to prevent ArgumentError (#7830)#7832
UwaisBaig wants to merge 1 commit into
CircuitVerse:masterfrom
UwaisBaig:fix/search-adapter-page-sanitization

Conversation

@UwaisBaig

@UwaisBaig UwaisBaig commented Sep 2, 2026

Copy link
Copy Markdown

Summary of Changes

Fixes #7830 (Sentry CIRCUITVERSE-CORE-155) where passing non-integer or malicious SQL-like strings in the page query parameter (e.g., "1' AND 1=1 UNION SELECT NULL-- -") caused will_paginate to raise ArgumentError: invalid value for Integer(): ....

Changes made:

  • app/lib/adapters/base_adapter.rb: Added sanitize_page(page) helper method that coerces the input to an integer (page.to_i) and defaults non-positive / unparseable values to 1.
  • app/lib/adapters/pg_adapter.rb: Applied sanitize_page(query_params[:page]) in perform_search before passing to paginate.
  • app/lib/adapters/solr_adapter.rb: Applied sanitize_page(query_params[:page]) in search_project and search_user.
  • spec/lib/adapters/pg_adapter_spec.rb: Added unit tests for page parameter sanitization.

Related Issue

Summary by CodeRabbit

  • Bug Fixes

    • Improved search pagination handling for invalid, negative, zero, and non-numeric page values.
    • Invalid page values now safely return the first page instead of causing unexpected behavior.
    • Valid numeric page values continue to work as expected across project and user searches.
  • Tests

    • Added coverage for valid and invalid pagination inputs, including malformed values.

@coderabbitai

coderabbitai Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The change adds protected page sanitization to BaseAdapter. PostgreSQL and Solr adapters use the sanitized value before pagination. PostgreSQL specs cover injection-style strings, negative values, and valid numeric strings.

Merge Risk: 🟡 Moderate · up to 9bead

The change prevents malformed page values from crashing searches, but requests that omit the page parameter can still produce an application error instead of defaulting to the first page. Merge should wait until missing values are normalized and covered by a regression test.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the adapter fix, page parameter sanitization, and prevented ArgumentError. It matches the main change.
Linked Issues check ✅ Passed The changes address issue #7830 by sanitizing non-integer, SQL-like, zero, and negative page values before pagination. PostgreSQL receives coverage through tests, and the helper is also applied to the…
Out of Scope Changes check ✅ Passed All reported changes support page parameter sanitization and pagination safety. The adapter updates and PostgreSQL tests are within the stated objectives.
Full details: Linked Issues check

Explanation

The changes address issue #7830 by sanitizing non-integer, SQL-like, zero, and negative page values before pagination. PostgreSQL receives coverage through tests, and the helper is also applied to the Solr search methods.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 8fac2f69-962b-4299-8516-f1852db6ec6b

📥 Commits

Reviewing files that changed from the base of the PR and between 79bd963 and 9bead88.

📒 Files selected for processing (4)
  • app/lib/adapters/base_adapter.rb
  • app/lib/adapters/pg_adapter.rb
  • app/lib/adapters/solr_adapter.rb
  • spec/lib/adapters/pg_adapter_spec.rb

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.

protected

def sanitize_page(page)
page_num = page.to_i

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Verify whether production search callers can omit the page parameter.
rg -n -C 8 'search_(project|user)|query_params\[:page\]|page:' app spec

Repository: CircuitVerse/CircuitVerse

Length of output: 39640


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- repository conventions and learnings ---'
find /tmp/coderabbit-repo-knowledge/circuitverse-circuitverse-08827a34 \
  -maxdepth 2 -type f -name '*.md' -print
printf '%s\n' '--- base adapter ---'
cat -n app/lib/adapters/base_adapter.rb
printf '%s\n' '--- direct search flow ---'
cat -n app/lib/adapters/pg_adapter.rb | sed -n '23,55p'
cat -n app/lib/adapters/solr_adapter.rb | sed -n '1,30p'
printf '%s\n' '--- query object and direct callers ---'
rg -n -C 6 'ProjectsQuery|UsersQuery|Adapters::|search_project|search_user' app spec

Repository: CircuitVerse/CircuitVerse

Length of output: 23244


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- query base class ---'
fd -i 'generic_query|search' app/queries app/controllers app/helpers app/lib \
  | sort
cat -n app/queries/generic_query.rb
printf '%s\n' '--- search-related controller flow ---'
rg -n -C 10 'def search|Search.new|query\(' app/controllers app/helpers app/queries app/lib
printf '%s\n' '--- relevant repository learnings ---'
cat /tmp/coderabbit-repo-knowledge/circuitverse-circuitverse-08827a34/learnings/null.md
cat /tmp/coderabbit-repo-knowledge/circuitverse-circuitverse-08827a34/learnings/app-controllers.md

Repository: CircuitVerse/CircuitVerse

Length of output: 12156


Handle an omitted page before calling to_i.

SearchController#search passes raw params to the query objects. If page is omitted, query_params[:page] is nil, and Adapters::PgAdapter#perform_search reaches BaseAdapter#sanitize_page, where nil.to_i raises NoMethodError before the fallback. Normalize the missing value before conversion and add a regression case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ArgumentError: invalid value for Integer(): "1' AND 1=1 UNION SELECT NULL-- -" (ArgumentError)

2 participants