Skip to content

Commit 70f89be

Browse files
authored
fix redos-able regex and add poc code to tests (#182)
1 parent d8f88e7 commit 70f89be

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

eth_account/_utils/structured_data/validation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Regexes
88
IDENTIFIER_REGEX = r"^[a-zA-Z_$][a-zA-Z_$0-9]*$"
9-
TYPE_REGEX = r"^[a-zA-Z_$][a-zA-Z_$0-9]*(\[([1-9]\d*)*\])*$"
9+
TYPE_REGEX = r"^[a-zA-Z_$][a-zA-Z_$0-9]*(\[([1-9]\d*\b)*\])*$"
1010

1111

1212
def validate_has_attribute(attr_name, dict_data):

newsfragments/178.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix DoS-able regex pattern

tests/core/test_structured_data_signing.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import pytest
33
import re
4+
import time
45

56
from eth_abi.exceptions import (
67
ABITypeError,
@@ -199,6 +200,29 @@ def test_type_regex(type, valid):
199200
assert re.match(TYPE_REGEX, type) is None
200201

201202

203+
def test_type_regex_for_redos():
204+
start = time.time()
205+
# len 30 string is long enough to cause > 1 second delay if the regex is bad
206+
long = '1' * 30
207+
invalid_structured_data_string = f"""{{
208+
"types": {{
209+
"EIP712Domain": [
210+
{{"name": "aaaa", "type": "$[{long}0"}},
211+
{{"name": "version", "type": "string"}},
212+
{{"name": "chainId", "type": "uint256"}},
213+
{{"name": "verifyingContract", "type": "address"}}
214+
]
215+
}}
216+
}}"""
217+
218+
with pytest.raises(re.error, match="unterminated character set at position 15"):
219+
with pytest.raises(ValidationError, match=f"Invalid Type `$[{long}0` in `EIP712Domain`"):
220+
load_and_validate_structured_message(invalid_structured_data_string)
221+
222+
done = time.time() - start
223+
assert done < 1
224+
225+
202226
def test_structured_data_invalid_identifier_filtered_by_regex():
203227
invalid_structured_data_string = open(
204228
"tests/fixtures/invalid_struct_identifier_message.json"

0 commit comments

Comments
 (0)