Skip to content

Parameterize gemc-sqlite queries and validate the column list#13

Open
zhaozhiwen wants to merge 1 commit into
gemc:mainfrom
zhaozhiwen:fix/134-sqlite-injection
Open

Parameterize gemc-sqlite queries and validate the column list#13
zhaozhiwen wants to merge 1 commit into
gemc:mainfrom
zhaozhiwen:fix/134-sqlite-injection

Conversation

@zhaozhiwen

Copy link
Copy Markdown
Collaborator

The gemc-sqlite CLI built WHERE clauses and the SELECT column list with raw f-string interpolation of user values (args.ef/vf/sf/rf and -what). Any value with a quote broke the query, and the values were a SQL injection vector (e.g. -ef "' OR '1'='1" returned every row).

Build the WHERE clause from column = ? placeholders and pass values as bound parameters to cursor.execute(query, params). The column list is an identifier and can't be bound, so validate it against the table's real columns (PRAGMA_TABLE_INFO allowlist) and reject unknown columns. Table names remain fixed literals.

Validation: a normal filter returns the right row; a quoted/injection value returns no spurious rows and doesn't crash; "' OR '1'='1" returns nothing (literal, not injected); an unknown -what column is rejected (Python 3.12).

Fixes #7

The gemc-sqlite CLI built WHERE clauses and the SELECT column list with
raw f-string interpolation of user values (args.ef/vf/sf/rf and -what).
Any value with a quote broke the query, and the values were a SQL
injection vector (e.g. -ef "' OR '1'='1" returned every row).

Build the WHERE clause from "column = ?" placeholders and pass the values
as bound parameters to cursor.execute(query, params). The column list is
an identifier and can't be bound, so validate it against the table's real
columns (PRAGMA_TABLE_INFO allowlist) and reject unknown columns. Table
names remain fixed literals ("geometry"/"materials").

Verified: a normal filter returns the right row; a quoted/injection value
returns no spurious rows and does not crash; "' OR '1'='1" returns nothing
(literal, not injected); an unknown -what column is rejected (Python 3.12).

Fixes gemc#7
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.

[Medium] gemc-sqlite CLI builds queries via raw f-strings (breakage + SQL injection)

1 participant