decode non-utf8 sql bytes as latin-1 not unicode-escape#852
Open
alhudz wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #852 +/- ##
==========================================
+ Coverage 97.04% 97.27% +0.22%
==========================================
Files 20 31 +11
Lines 1558 3664 +2106
Branches 0 328 +328
==========================================
+ Hits 1512 3564 +2052
- Misses 46 59 +13
- Partials 0 41 +41 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Repro:
sqlparse.parse(b"SELECT '\x41', '\n' \xff")where the input is bytes that aren't valid UTF-8 (a single stray\xffis enough to take the fallback branch).Cause: the non-UTF-8 fallback in
Lexer.get_tokensdecodes viaunicode-escape, which evaluates backslash escape sequences in the SQL bytes (\x41becomesA,\nbecomes a newline, plus octal and\u…). The parsed token stream then no longer matches the raw bytes the database receives, so anything inspecting or sanitising the SQL bytes sees a different statement from the one that runs.Fix: decode the fallback as
latin-1, which maps all 256 byte values one-to-one without evaluating escapes. For input without backslashes the result is byte-identical to today; only the escape reinterpretation is dropped.pytest)ruff)