Fix SQL injection in search by using parameterized queries - #41
Conversation
G4brym
left a comment
There was a problem hiding this comment.
Automated Code Review — APPROVED ✅
Review Scores: 5/5 reviewers approved
CI Status: All checks passed ✅
Summary
Clean, well-implemented security fix that replaces fragile SQL string interpolation with proper parameterized queries. The extracted buildSearchFilters() helper is well-typed, well-tested (12 new tests covering normal usage, edge cases, and injection scenarios), and follows existing codebase conventions.
Review Perspectives
- Correctness: ✅ Parameterized conditions and params are correctly structured; empty/whitespace/invalid inputs handled properly; both main query and count query use consistent filters
- Security: ✅ Replaces fragile
replace(/'/g, "''")escaping with industry-standard?parameter binding; status gets defense-in-depth (whitelist + parameterization) - Performance: ✅ Functionally equivalent SQL queries with negligible overhead from helper function
- Code Quality: ✅ Clean extraction as pure function; net-negative diff in route handler (-15/+5 lines); follows existing patterns in utils.ts
- Testing: ✅ 12 comprehensive unit tests covering normal cases, edge cases, SQL injection scenarios, and special characters; all 159 tests pass
No major, medium, or minor issues found. This is a textbook example of fixing SQL injection properly.
🤖 Automated review by prodboard
The research list search endpoint used string interpolation to build SQL WHERE clauses, which is vulnerable to SQL injection even with manual quote escaping. This replaces the string interpolation with proper parameterized queries via workers-qb's built-in parameter binding. Extracts a `buildSearchFilters` helper that returns parameterized conditions and params, and adds comprehensive unit tests including SQL injection attack scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
48db601 to
faf9d78
Compare
Automated Code Review — Changes Requested 🔄Review Scores: 5/5 reviewers approved CI Failures❌ Changeset CheckLink: https://github.com/G4brym/workers-research/actions/runs/22917017745/job/66505050624 The To fix, run one of the following and commit the generated npx changeset
# or
bunx changesetSelect the appropriate bump type:
Alternatively, create a ---
"workers-research": patch
---
Fix SQL injection in research list search by replacing string interpolation with parameterized queriesCode Quality AssessmentThe code change itself is excellent — all 5 review perspectives approved:
The only blocker is the missing changeset file for CI. Once that's added and CI goes green, this PR is ready to merge. 🤖 Automated review by prodboard |
Adds patch changeset required by CI for the parameterized query fix. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
GET /) with proper parameterized queries viaworkers-qb's built-in parameter bindingLIKEclauses, which is vulnerable to SQL injection edge casesbuildSearchFilters()helper function that returns parameterized conditions and params arraysWhat was changed
src/utils.ts— AddedbuildSearchFilters(q?, status?)that builds parameterizedWHEREconditions:?placeholders instead of string interpolationsrc/index.tsx— Replaced the inline condition-building code with the new helper, passing conditions and params toworkers-qb's.where(conditions, params)method.tests/unit/utils.test.ts— Added 12 unit tests covering:Why this is beneficial
The manual
replace(/'/g, "''")escaping approach is fragile and doesn't protect against all SQL injection vectors. Using parameterized queries is the industry-standard defense and leveragesworkers-qb's existing parameter binding support (already used elsewhere in the codebase, e.g.,where("research_id = ?", id)).Test plan
🤖 Generated with Claude Code