Skip to content
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

more explicitly define escape sequencies in JsonLexer (fix #1065) #1528

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pygments/lexers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],


Expand All @@ -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'),
],

Expand Down
28 changes: 28 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down