-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Python: fix files being silently dropped over Rust/Python escape mismatch #22443
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
150 changes: 150 additions & 0 deletions
150
python/extractor/tests/parser/unicode_escapes_new.expected
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,150 @@ | ||
| Module: [6, 0] - [29, 0] | ||
| body: [ | ||
| TypeAlias: [6, 0] - [6, 12] | ||
| name: | ||
| Name: [6, 5] - [6, 6] | ||
| variable: Variable('X', None) | ||
| ctx: Store | ||
| type_parameters: [] | ||
| value: | ||
| Name: [6, 9] - [6, 12] | ||
| variable: Variable('int', None) | ||
| ctx: Load | ||
| Assign: [9, 0] - [9, 37] | ||
| targets: [ | ||
| Name: [9, 0] - [9, 4] | ||
| variable: Variable('warn', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [9, 7] - [9, 37] | ||
| s: '⚠️ problem %s: %s' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [10, 0] - [10, 35] | ||
| targets: [ | ||
| Name: [10, 0] - [10, 8] | ||
| variable: Variable('warn_raw', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [10, 11] - [10, 35] | ||
| s: '⚠️ problem %s: %s' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [13, 0] - [13, 19] | ||
| targets: [ | ||
| Name: [13, 0] - [13, 3] | ||
| variable: Variable('zwj', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [13, 6] - [13, 19] | ||
| s: '👨\u200d💻' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [16, 0] - [16, 14] | ||
| targets: [ | ||
| Name: [16, 0] - [16, 3] | ||
| variable: Variable('nfd', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [16, 6] - [16, 14] | ||
| s: 'café' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [17, 0] - [17, 28] | ||
| targets: [ | ||
| Name: [17, 0] - [17, 11] | ||
| variable: Variable('soft_hyphen', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [17, 14] - [17, 28] | ||
| s: 'soft\xadhyphen' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [20, 0] - [20, 12] | ||
| targets: [ | ||
| Name: [20, 0] - [20, 6] | ||
| variable: Variable('café', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Name: [20, 9] - [20, 12] | ||
| variable: Variable('nfd', None) | ||
| ctx: Load | ||
| Assign: [23, 0] - [23, 23] | ||
| targets: [ | ||
| Name: [23, 0] - [23, 3] | ||
| variable: Variable('raw', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [23, 6] - [23, 23] | ||
| s: '⚠️\\u{fe0f}' | ||
| prefix: 'r"' | ||
| implicitly_concatenated_parts: None | ||
| Assign: [24, 0] - [24, 23] | ||
| targets: [ | ||
| Name: [24, 0] - [24, 6] | ||
| variable: Variable('joined', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| JoinedStr: [24, 9] - [24, 23] | ||
| values: [ | ||
| Str: [24, 9] - [24, 12] | ||
| s: '' | ||
| prefix: 'f"' | ||
| implicitly_concatenated_parts: None | ||
| Name: [24, 12] - [24, 15] | ||
| variable: Variable('zwj', None) | ||
| ctx: Load | ||
| Str: [24, 15] - [24, 23] | ||
| s: '⚠️' | ||
| prefix: 'f"' | ||
| implicitly_concatenated_parts: None | ||
| ] | ||
| Assign: [25, 0] - [25, 37] | ||
| targets: [ | ||
| Name: [25, 0] - [25, 12] | ||
| variable: Variable('concatenated', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Str: [25, 15] - [25, 37] | ||
| s: '⚠️👨\u200d💻' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: [ | ||
| StringPart: [25, 15] - [25, 23] | ||
| prefix: '"' | ||
| text: '"⚠️"' | ||
| s: '⚠️' | ||
| StringPart: [25, 24] - [25, 37] | ||
| prefix: '"' | ||
| text: '"👨\u200d💻"' | ||
| s: '👨\u200d💻' | ||
| ] | ||
| Assign: [28, 0] - [28, 17] | ||
| targets: [ | ||
| Name: [28, 0] - [28, 1] | ||
| variable: Variable('d', None) | ||
| ctx: Store | ||
| ] | ||
| value: | ||
| Dict: [28, 4] - [28, 17] | ||
| items: [ | ||
| KeyValuePair: [28, 5] - [28, 16] | ||
| key: | ||
| Str: [28, 5] - [28, 13] | ||
| s: '⚠️' | ||
| prefix: '"' | ||
| implicitly_concatenated_parts: None | ||
| value: | ||
| Num: [28, 15] - [28, 16] | ||
| n: 1 | ||
| text: '1' | ||
| ] | ||
| ] |
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,28 @@ | ||
| # Characters that Rust's `Debug` formatting escapes as `\u{...}` when `tsg-python` serialises the | ||
| # source text. See https://github.com/github/codeql/issues/22435. | ||
|
|
||
| # PEP 695 syntax is what makes the old parser bail out and hand the file to `tsg-python` in the | ||
| # first place, so keep the reported reproducer intact. | ||
| type X = int | ||
|
|
||
| # U+FE0F variation selector, next to a `%` directive. | ||
| warn = "\u26a0\ufe0f problem %s: %s" | ||
| warn_raw = "⚠️ problem %s: %s" | ||
|
|
||
| # U+200D zero width joiner. | ||
| zwj = "👨💻" | ||
|
|
||
| # Combining acute accent (NFD), and a soft hyphen. | ||
| nfd = "café" | ||
| soft_hyphen = "softhyphen" | ||
|
|
||
| # Combining marks are valid in identifiers too. | ||
| café = nfd | ||
|
|
||
| # In f-strings, raw strings and implicit concatenations too. | ||
| raw = r"⚠️\u{fe0f}" | ||
| joined = f"{zwj}⚠️" | ||
| concatenated = "⚠️" "👨💻" | ||
|
|
||
| # ... and outside of string literals. | ||
| d = {"⚠️": 1} # comment with ⚠️ and 👨💻 |
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,85 @@ | ||
| import unittest | ||
|
|
||
| from ast import literal_eval | ||
|
|
||
| from semmle.logging import format_message | ||
| from semmle.python.parser.tsg_parser import evaluate_string, rust_to_python_escapes | ||
|
|
||
|
|
||
| class RustEscapeTest(unittest.TestCase): | ||
| """`tsg-python` serialises strings with Rust's `Debug` formatting, which escapes characters such | ||
| as U+FE0F as `\\u{...}` -- a syntax Python's `literal_eval` does not accept -- and NUL as `\\0`, | ||
| which Python reads as an octal escape.""" | ||
|
|
||
| def test_untouched_without_escapes(self): | ||
| text = '"caf\u00e9 \u2713 \U0001f4be"' | ||
| self.assertEqual(rust_to_python_escapes(text), text) | ||
|
|
||
| def test_basic_multilingual_plane(self): | ||
| self.assertEqual(rust_to_python_escapes(r'"\u{fe0f}"'), r'"\ufe0f"') | ||
| self.assertEqual(rust_to_python_escapes(r'"\u{200d}"'), r'"\u200d"') | ||
|
|
||
| def test_short_and_astral_code_points(self): | ||
| self.assertEqual(rust_to_python_escapes(r'"\u{0}"'), r'"\u0000"') | ||
| self.assertEqual(rust_to_python_escapes(r'"\u{1f4a9}"'), r'"\U0001f4a9"') | ||
|
|
||
| def test_other_escapes_are_preserved(self): | ||
| self.assertEqual(rust_to_python_escapes(r'"a\nb\"c\u{ad}"'), r'"a\nb\"c\u00ad"') | ||
|
|
||
| def test_escaped_backslash_is_not_an_escape_introducer(self): | ||
| # How a raw string `r"\u{fe0f}"` in the analysed source gets serialised: the `\u{fe0f}` is | ||
| # literal text, not an escape, and must survive unchanged. | ||
| self.assertEqual(rust_to_python_escapes(r'"\\u{fe0f}"'), r'"\\u{fe0f}"') | ||
|
|
||
| def test_nul_is_not_left_as_an_octal_escape(self): | ||
| # Rust renders NUL as `\0`; Python would read that as the start of an octal escape and | ||
| # swallow the digits that follow, decoding `"\01"` to U+0001 instead of NUL then `1`. | ||
| self.assertEqual(rust_to_python_escapes(r'"\01"'), r'"\x001"') | ||
|
|
||
| def test_every_escape_shape_round_trips(self): | ||
| # Rust's `Debug for str` only ever emits these escape shapes. Check that each round-trips | ||
| # with every printable ASCII neighbour before and after it. | ||
| for escape_shape, expected in [ | ||
| (r'\0', "\x00"), | ||
| (r'\t', "\t"), | ||
| (r'\n', "\n"), | ||
| (r'\r', "\r"), | ||
| (r'\\', "\\"), | ||
| (r'\"', '"'), | ||
| (r'\u{1}', "\u0001"), | ||
| (r'\u{1f}', "\u001f"), | ||
| (r'\u{300}', "\u0300"), | ||
| (r'\u{fe0f}', "\ufe0f"), | ||
| (r'\u{e0100}', "\U000e0100"), | ||
| (r'\u{10fffe}', "\U0010fffe"), | ||
| ]: | ||
| for neighbour in map(chr, range(0x20, 0x7F)): | ||
| rendered_neighbour = {"\\": r"\\", '"': r'\"'}.get(neighbour, neighbour) | ||
| for position, text, expected_value in [ | ||
| ("before", '"' + rendered_neighbour + escape_shape + '"', neighbour + expected), | ||
| ("after", '"' + escape_shape + rendered_neighbour + '"', expected + neighbour), | ||
| ]: | ||
| with self.subTest( | ||
| escape_shape=escape_shape, | ||
| neighbour=neighbour, | ||
| position=position, | ||
| ): | ||
| self.assertEqual(literal_eval(rust_to_python_escapes(text)), expected_value) | ||
|
|
||
| def test_evaluate_string_on_reported_value(self): | ||
| # The exact value from https://github.com/github/codeql/issues/22435 that used to raise | ||
| # `truncated \uXXXX escape`. | ||
| value = rust_to_python_escapes('"\\"\u26a0\\u{fe0f} problem %s: %s\\""') | ||
| self.assertEqual(evaluate_string(value), "\u26a0\ufe0f problem %s: %s") | ||
|
|
||
|
|
||
| class FormatMessageTest(unittest.TestCase): | ||
| """A pre-formatted log message may contain `%` directives coming from the analysed source, and | ||
| must not be `%`-formatted again.""" | ||
|
|
||
| def test_no_arguments(self): | ||
| message = "Error while parsing value '%s: %s'" | ||
| self.assertEqual(format_message(message, ()), message) | ||
|
|
||
| def test_with_arguments(self): | ||
| self.assertEqual(format_message("%s and %s", ("a", "b")), "a and b") |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.