Skip to content

Bind query parameters instead of interpolating them (SQL injection fix) - #723

Open
chrisclark wants to merge 1 commit into
masterfrom
fix/sql-param-injection
Open

chrisclark wants to merge 1 commit into
masterfrom
fix/sql-param-injection

Conversation

@chrisclark

@chrisclark chrisclark commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the SQL injection through $$param$$ substitution. Critical.

swap_params interpolated parameter values into the SQL with re.sub. Parameter values come from the request (?params=foo:value), which is the documented way to share a parameterized query, so the values were attacker-controlled and became part of the SQL text.

Reproduced before the fix, as a user with view-only permission (EXPLORER_PERMISSION_CHANGE = False) against a query SELECT '$$who$$' AS who:

GET /explorer/1/?params=who:x' UNION SELECT name FROM sqlite_master --
-> 200, sqlite_master contents rendered in the results table

Since ?show=1 (and plain GET /explorer/<id>/) runs and renders in one request, a shared link re-runs the injected SQL on every view, including for visitors who cannot edit queries themselves. Combined with keyword-hiding payloads that the blacklist does not see (/*!50000DELETE*/), this reaches DML/DDL on a connection where the code explicitly checks the blacklist "to catch parameterized SQL".

Fix

Parameters are passed to the driver as bound parameters; they never become part of the SQL text. swap_params gained a bind argument and now returns (sql, bound_values).

The display-only path still interpolates, deliberately: final_sql() feeds query logging and the passes_blacklist check, neither of which reaches the database.

Details worth reviewing:

  • Quoted params. '$$id$$' is quoted in the docs and in existing tests. It becomes a bare placeholder, otherwise the driver would see the literal string ?.
  • Bare params. Injected as unique positional markers, then replaced, so a bare placeholder is not corrupted by the first positional argument.
  • Types. Numeric-looking values bind as int/float, so WHERE id = $$id$$ still compares numbers for text drivers. Without this, select 6870+4 where 1=$$myparam:1$$ returned no rows.
  • % still works. Drivers using format/pyformat re-interpolate the SQL when values are supplied, so a literal % in a non-parameterized LIKE '%foo%' is escaped. Only applied when binding actually happens; qmark is untouched.
  • Byte-identical end state. For every case except malicious input, the SQL the database sees is the same as before (values still appear inline after driver interpolation).

Breaking change

Parameters are data now. The common cases are unaffected — WHERE col = '$$value$$', LIMIT $$n$$ and OFFSET $$n$$ all still work, verified against a real connection.

What stops working is a parameter standing in for a SQL identifier rather than a value. ORDER BY $$sort$$ written as a bare token now sorts by a constant, so rows come back in insertion order. A column name cannot be a bound value, so that query must name the column in the SQL itself. (The quoted form ORDER BY '$$sort$$' was already broken before this change — it is a constant too.)

Called out in HISTORY.rst.

Tests

  • swap_params return type updated in the three existing tests.
  • New: quoted/bare binding, positional placeholders, a value that is not a replacement template (C:\path\1), numeric type preservation, % escaping.
  • New regressions: the injection payload is inert as a quoted param, as a bare param, and end-to-end over HTTP through the download view; one asserts the bind holds even when the blacklist does not catch the payload.
  • Bonus: a parameter value ending in a backslash previously raised re.error from regex.sub and 500'd every render (the view only catches DatabaseError). Now 200.

Full suite: 301 tests, OK. ruff check explorer/: clean.

Query parameters ($$foo$$) were substituted into the SQL as raw text via
regex.sub. Because parameter values come from the request
(?params=foo:value, the documented way to share a parameterized query),
anyone who could get a user to open a crafted link could inject arbitrary
SQL into a saved query -- including a query the visitor has no permission
to edit, and independent of EXPLORER_PERMISSION_CHANGE.

Values are now passed to the driver as bound parameters; swap_params emits
placeholders and collects the values. The display-only path (query log and
the SQL blacklist) still interpolates, since it never reaches the database.

Notes on the implementation:
* A parameter written '$$id$$' -- quoted, as the docs recommend -- becomes a
  bare placeholder, otherwise the driver would see the literal string "?".
  Bare placeholders become unique markers and are then replaced, so they are
  substituted rather than corrupted by the first positional argument.
* Numeric-looking values are bound as int/float so that WHERE id = $$id$$
  keeps comparing numbers rather than text.
* Literal '%' is escaped when the connection will run the SQL through
  %-interpolation, so a non-parameterized LIKE '%foo%' still works.
* swap_params now returns (sql, bound_values).

BREAKING: parameters are data now. A query that relied on a parameter
carrying SQL (e.g. ORDER BY $$sort$$ as a bare token) must be updated.

This branch has not been deployed

No deployments
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.

1 participant