Skip to content

Commit d05095f

Browse files
committed
Avoid excessive backtracking when parsing unterminated strings
1 parent 5f87fad commit d05095f

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎NEWS‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# 1.12.2
2+
3+
* Avoid excessive backtracking when parsing an unterminated string with many
4+
escape sequences. The previous behaviour would allow maliciously crafted
5+
documents to cause a denial of service.
6+
7+
Note that it is still strongly recommended to process untrusted documents in
8+
a separate thread with a timeout to avoid potential similar issues.
9+
110
# 1.12.1
211

312
* Fix: on Windows, when an image's content type includes a backslash in the

‎mammoth/styles/parser/tokeniser.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class TokenType(object):
1313
UNTERMINATED_STRING = "unterminated string"
1414
INTEGER = "integer"
1515
END = "end"
16-
16+
1717

1818

1919
def regex_tokeniser(rules):
2020
rules = [(token_type, _to_regex(regex)) for token_type, regex in rules]
2121
rules.append(("unknown", re.compile(".")))
22-
22+
2323
def tokenise(value):
2424
tokens = []
2525
index = 0
@@ -39,7 +39,7 @@ def tokenise(value):
3939
return tokens
4040

4141
return tokenise
42-
42+
4343

4444
def _to_regex(value):
4545
if hasattr(value, "match"):
@@ -48,7 +48,7 @@ def _to_regex(value):
4848
return re.compile(value)
4949

5050

51-
_string_prefix = r"'(?:\\.|[^'])*"
51+
_string_prefix = r"'(?:\\(?:.|$)|[^'\\])*"
5252
_identifier_character = r"(?:[a-zA-Z\-_]|\\.)"
5353

5454
tokenise = regex_tokeniser([

‎tests/styles/parser/tokeniser_tests.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,26 @@ def test_strings_are_tokenised():
3131
assert_tokens("'Tristan'", is_token("string", "'Tristan'"))
3232

3333

34-
def test_escape_sequences_in_strings_are_tokenised():
34+
def test_escaped_string_terminators_in_strings_are_tokenised():
3535
assert_tokens(r"'Tristan\''", is_token("string", r"'Tristan\''"))
3636

3737

38+
def test_escape_sequences_in_strings_are_tokenised():
39+
assert_tokens(r"'Tristan\\'", is_token("string", r"'Tristan\\'"))
40+
41+
3842
def test_unterminated_strings_are_tokenised():
3943
assert_tokens("'Tristan", is_token("unterminated string", "'Tristan"))
4044

4145

46+
def test_unterminated_strings_with_unterminated_escape_are_tokenised():
47+
assert_tokens("'Tristan\\", is_token("unterminated string", "'Tristan\\"))
48+
49+
50+
def test_unterminated_strings_with_many_escape_sequences_do_not_cause_excessive_backtracking():
51+
assert_tokens("'" + "\\a" * 50, is_token("unterminated string", "'" + "\\a" * 50))
52+
53+
4254
def test_arrows_are_tokenised():
4355
assert_tokens("=>=>", is_token("symbol", "=>"), is_token("symbol", "=>"))
4456

0 commit comments

Comments
 (0)