Repository navigation
Bind query parameters instead of interpolating them (SQL injection fix) - #723
Open
chrisclark wants to merge 1 commit into
Open
chrisclark wants to merge 1 commit into
chrisclark wants to merge 1 commit into
Conversation
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes the SQL injection through
$$param$$substitution. Critical.swap_paramsinterpolated parameter values into the SQL withre.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 querySELECT '$$who$$' AS who:Since
?show=1(and plainGET /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_paramsgained abindargument and now returns(sql, bound_values).The display-only path still interpolates, deliberately:
final_sql()feeds query logging and thepasses_blacklistcheck, neither of which reaches the database.Details worth reviewing:
'$$id$$'is quoted in the docs and in existing tests. It becomes a bare placeholder, otherwise the driver would see the literal string?.int/float, soWHERE id = $$id$$still compares numbers for text drivers. Without this,select 6870+4 where 1=$$myparam:1$$returned no rows.%still works. Drivers usingformat/pyformatre-interpolate the SQL when values are supplied, so a literal%in a non-parameterizedLIKE '%foo%'is escaped. Only applied when binding actually happens;qmarkis untouched.Breaking change
Parameters are data now. The common cases are unaffected —
WHERE col = '$$value$$',LIMIT $$n$$andOFFSET $$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 formORDER BY '$$sort$$'was already broken before this change — it is a constant too.)Called out in
HISTORY.rst.Tests
swap_paramsreturn type updated in the three existing tests.C:\path\1), numeric type preservation,%escaping.re.errorfromregex.suband 500'd every render (the view only catchesDatabaseError). Now 200.Full suite: 301 tests, OK.
ruff check explorer/: clean.