-
Notifications
You must be signed in to change notification settings - Fork 1
fix: parameterize SQON-to-SQL filter values, closing a SQL injection #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
justincorrigible
wants to merge
6
commits into
main
Choose a base branch
from
fix/sqon-sql-injection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5684037
fix: parameterize SQON-to-SQL filter values, closing a SQL injection
justincorrigible 4d924d2
fix: parameterize the same unescaped JSONB filter in getSubmittedData…
justincorrigible 5fce55f
test: broaden adversarial coverage for the SQON filter fix, drop stal…
justincorrigible aab0c6c
docs: log tech-debt for this fix's remaining scope
justincorrigible 5c7d473
fix: use inArray for IN-clause construction, add multi-element test
justincorrigible a0ac93f
docs: add session-file entry for the SQL injection fix
justincorrigible File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Closed a SQL injection reachable through any SQON filter, plus a stored variant reachable through submitted data. | ||
|
|
||
| - `packages/data-provider/src/utils/convertSqonToQuery.ts`: `processFilterOperator` spliced `fieldName`/`value` into `sql.raw()` text via an unescaping helper; a crafted value could alter the WHERE clause's boolean structure. Bound both through drizzle's `sql` template and `inArray()` instead, matching the `IN`-clause convention already used elsewhere in this codebase; removed the unescaping helper entirely rather than hardening it. Dropped a stale `TODO: SQL sanitization` comment this closes. | ||
| - `packages/data-provider/src/repository/submittedRepository.ts`: `getSubmittedDataFiltered` had the same unescaped-splice pattern, more severe here since `dataValue` is read back out of a submitter's own previously-submitted record (foreign-key relationship resolution during compound-view reads and submission processing), so a malicious value submitted once would have poisoned every later query walking that record's relationships. Extracted the filter-building into `buildDataFieldFilter` and fixed the same way. | ||
| - New tests: `test/unit/utils/convertSqonToQuery.spec.ts` and `test/unit/repository/submittedRepository.spec.ts` cover injection-shaped input (quotes, statement terminators, comment markers) on both fixes, a multi-element `in` array (the prior single-element-only coverage couldn't distinguish comma-expansion from an invalid single-array-parameter bind), and parameter-binding parity for ordinary input. | ||
| - `.dev/tech-debt.md`: logged that issue #43 covers three sanitization items and only the SQON one is fixed here, and that SQON `fieldName` still has no allowlist against the active dictionary's schema (harmless now that it's parameterized, but worth closing for correctness). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/data-provider/test/unit/repository/submittedRepository.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { expect } from 'chai'; | ||
| import { PgDialect } from 'drizzle-orm/pg-core'; | ||
| import { describe, it } from 'mocha'; | ||
|
|
||
| import { buildDataFieldFilter } from '../../../src/repository/submittedRepository.js'; | ||
|
|
||
| const dialect = new PgDialect(); | ||
|
|
||
| describe('submittedRepository', () => { | ||
| describe('buildDataFieldFilter', () => { | ||
| it('should build a parameterized filter comparing a JSONB field against a value', () => { | ||
| const result = buildDataFieldFilter('submitter_donor_id', 'DO-01'); | ||
| const { sql, params } = dialect.sqlToQuery(result); | ||
|
|
||
| expect(sql).to.eql('data ->> $1 in ($2)'); | ||
| expect(params).to.eql(['submitter_donor_id', 'DO-01']); | ||
| }); | ||
|
|
||
| it('should bind a dataField containing SQL metacharacters as a parameter, not as SQL text', () => { | ||
| // Regression coverage for a stored SQL injection: dataField/dataValue here can originate | ||
| // from a submitter's own submitted data (see searchDataRelations.ts, viewMode.ts), so they | ||
| // must never be spliced into the query text. | ||
| const result = buildDataFieldFilter(`x' OR '1'='1`, 'a'); | ||
| const { sql, params } = dialect.sqlToQuery(result); | ||
|
|
||
| expect(sql).to.eql('data ->> $1 in ($2)'); | ||
| expect(params).to.eql([`x' OR '1'='1`, 'a']); | ||
| }); | ||
|
|
||
| it('should bind a dataValue containing a statement terminator and comment marker as a parameter', () => { | ||
| const result = buildDataFieldFilter('submitter_donor_id', `DO-01'; DROP TABLE submitted_data; --`); | ||
| const { sql, params } = dialect.sqlToQuery(result); | ||
|
|
||
| expect(sql).to.eql('data ->> $1 in ($2)'); | ||
| expect(params).to.eql(['submitter_donor_id', `DO-01'; DROP TABLE submitted_data; --`]); | ||
| }); | ||
|
|
||
| it('should coerce an undefined dataValue to the string "undefined", matching prior behaviour', () => { | ||
| const result = buildDataFieldFilter('submitter_donor_id', undefined); | ||
| const { params } = dialect.sqlToQuery(result); | ||
|
|
||
| expect(params).to.eql(['submitter_donor_id', 'undefined']); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😉