Skip to content

fix: SQL Injection via parameterized queries (#4169)#12

Open
aglichandrap wants to merge 1 commit into
IntersectMBO:mainfrom
aglichandrap:fix/4169-sql-injection-parameterized-queries
Open

fix: SQL Injection via parameterized queries (#4169)#12
aglichandrap wants to merge 1 commit into
IntersectMBO:mainfrom
aglichandrap:fix/4169-sql-injection-parameterized-queries

Conversation

@aglichandrap
Copy link
Copy Markdown

Fix: SQL Injection via Parameterized Queries

Closes IntersectMBO/govtool#4169

Problem

The GET /voters/:stakeKey/delegation endpoint was vulnerable to SQL injection (CVSS 8.6). The stakeKey URL parameter was interpolated directly into a raw SQL template literal, allowing UNION-based injection to read the entire database.

Secondary vectors existed in drep.service.ts where search queries used weak single-quote doubling and DRep view arrays were interpolated without escaping.

Changes

backend/src/voter/voter.service.ts (Primary fix):

  • Converted DECODE('${stakeKey}', 'hex')DECODE($1, 'hex') with [stakeKey] parameter array
  • Added hex input validation: rejects non-hex characters before query execution
  • Throws BadRequestException for invalid input

backend/src/drep/drep.service.ts (Secondary fixes):

  • Converted search ILIKE from string interpolation to $1/$2 parameterized queries
  • Added proper single-quote escaping for nameFilteredDRepViews array interpolation
  • Added proper single-quote escaping for dRepViews array interpolation
  • Pass searchParams array to query execution calls

backend/src/queries/getDReps.ts:

  • Added queryParams parameter to getAllDRepsQuery function signature

Test files added:

  • backend/src/voter/__tests__/voter.service.spec.ts — Tests SQL injection payloads (UNION, OR 1=1, special chars) are rejected; verifies parameterized $1 usage
  • backend/src/drep/__tests__/drep.service.spec.ts — Tests search parameterization and array escaping

Security Notes

  • The parameterized query approach prevents all forms of SQL injection by ensuring user input is never interpolated into SQL strings
  • The hex validation provides defense-in-depth by rejecting malformed input before it reaches the database layer
  • No authentication bypass is possible as the fix applies at the query layer

- voter.service.ts: Convert stakeKey from string interpolation to $1 parameterized query, add hex validation
- drep.service.ts: Convert search query from single-quote doubling to proper parameterized queries, escape array interpolations
- getDReps.ts: Add queryParams parameter to support parameterized search
- Add unit tests proving injection payloads are rejected/parameterized

Closes IntersectMBO/govtool#4169
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.

🐛 [Bounty] - SQL Injection in GET /voters/:stakeKey/delegation allows any attacker to read the entire Cardano blockchain state database

1 participant