diff options
| author | Nick Gerner <nick.gerner@gmail.com> | 2020-08-31 07:17:38 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-31 16:17:38 +0200 |
| commit | 9514e794e0c2a5c7c048df97fcfef4a099e05ac3 (patch) | |
| tree | eed514306c5f7ac21d50b97853681ed43c017283 | |
| parent | bc67241664df4082af20fc30f08af8d3b5158fc8 (diff) | |
| download | pygments-git-9514e794e0c2a5c7c048df97fcfef4a099e05ac3.tar.gz | |
more explicitly define escape sequencies in JsonLexer (fix #1065) (#1528)
* more explicitly define escape sequencies in JsonLexer (fix #1065)
* adding test coverage for #1065
| -rw-r--r-- | pygments/lexers/data.py | 4 | ||||
| -rw-r--r-- | tests/test_data.py | 28 |
2 files changed, 30 insertions, 2 deletions
diff --git a/pygments/lexers/data.py b/pygments/lexers/data.py index ebc9f708..fc8897c4 100644 --- a/pygments/lexers/data.py +++ b/pygments/lexers/data.py @@ -471,7 +471,7 @@ class JsonLexer(RegexLexer): '%(exp_part)s|%(frac_part)s)') % vars(), Number.Float), (int_part, Number.Integer), - (r'"(\\\\|\\"|[^"])*"', String.Double), + (r'"(\\(["\\/bfnrt]|u[a-fA-F0-9]]{4})|[^\\"])*"', String.Double), ], @@ -488,7 +488,7 @@ class JsonLexer(RegexLexer): # a json object - { attr, attr, ... } 'objectvalue': [ include('whitespace'), - (r'"(\\\\|\\"|[^"])*"', Name.Tag, 'objectattribute'), + (r'"(\\(["\\/bfnrt]|u[a-fA-F0-9]]{4})|[^\\"])*"', Name.Tag, 'objectattribute'), (r'\}', Punctuation, '#pop'), ], diff --git a/tests/test_data.py b/tests/test_data.py index 23f1d4a0..9724d235 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -56,6 +56,34 @@ def test_basic_json(lexer_json): assert list(lexer_json.get_tokens(fragment)) == tokens +def test_json_escape_backtracking(lexer_json): + # This tests that an (invalid) sequence of escapes doesn't cause the lexer + # to fall into catastrophic backtracking. unfortunately, if it's broken + # this test will hang and that's how we know it's broken :( + fragment = r'{"\u00D0000\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\63CD' + tokens = ( + [(Token.Punctuation, u'{'), + (Token.Error, r'"'), + (Token.Error, '\\'), + (Token.Error, r'u'), + (Token.Error, r'0'), + (Token.Error, r'0'), + (Token.Error, r'D'), + (Token.Error, r'0'), + (Token.Error, r'0'), + (Token.Error, r'0'), + (Token.Error, r'0')] + + [(Token.Error, '\\')] * 178 + + [(Token.Error, r'6'), + (Token.Error, r'3'), + (Token.Error, r'C'), + (Token.Error, r'D'), + (Token.Text, '\n')] + ) + + assert list(lexer_json.get_tokens(fragment)) == tokens + + def test_basic_bare(lexer_bare): # This is the same as testBasic for JsonLexer above, except the # enclosing curly braces are removed. |
