Skip to content

Commit f8f014d

Browse files
committed
Catch unsupported operation errors
1 parent e0ad915 commit f8f014d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

regexploit/ast/sre.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
SreOp = Tuple[SreConstant, SreOpData]
1616

1717

18+
class UnsupportedSreOpException(Exception):
19+
pass
20+
21+
1822
class SreOpParser:
1923
def __init__(self):
2024
self._groups = {}
@@ -24,6 +28,8 @@ def parse_sre(self, pattern: str, flags: int = 0):
2428
return self.sequence_or_singleton(sre_parse.parse(pattern, flags))
2529

2630
def parse_op(self, op: SreConstant, data: SreOpData):
31+
if not hasattr(self, f"from_{op.name}"):
32+
raise UnsupportedSreOpException(f"Unsupported SRE op: {op.name}")
2733
return getattr(self, f"from_{op.name}")(data)
2834

2935
def sequence_or_singleton(self, ops: List[SreOp]):

regexploit/bin/regexploit_python_ast.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import traceback
77
import warnings
88

9-
from regexploit.ast.sre import SreOpParser
9+
from regexploit.ast.sre import SreOpParser, UnsupportedSreOpException
1010
from regexploit.bin.files import file_generator
1111
from regexploit.languages.python_node_visitor import PythonNodeVisitor
1212
from regexploit.output.text import TextOutput
@@ -31,6 +31,9 @@ def handle_file(filename: str, output: TextOutput):
3131
parsed = SreOpParser().parse_sre(regex.pattern, regex.flags)
3232
except re.error:
3333
continue # We will have many strings which aren't actually regexes
34+
except UnsupportedSreOpException as e:
35+
print(f"Error parsing regex: {regex.pattern} from {filename}: {e}")
36+
continue
3437
try:
3538
output.next()
3639
for redos in find(parsed):

tests/test_at.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from regexploit.ast.at import EndOfString
4-
from regexploit.ast.sre import SreOpParser
4+
from regexploit.ast.sre import SreOpParser, UnsupportedSreOpException
55

66

77
def from_regex(pattern: str):
@@ -60,3 +60,16 @@ def test_real():
6060
dollar = EndOfString()
6161
dollar.set_character(from_regex(r"-\d+(\s*\s*\s*)").elements)
6262
assert dollar.character == from_regex(r"[\s]")
63+
64+
65+
@pytest.mark.parametrize(
66+
"r",
67+
[
68+
r"a++b",
69+
r"a?+b",
70+
r"a*+b",
71+
],
72+
)
73+
def test_unsupported_op(r):
74+
with pytest.raises(UnsupportedSreOpException):
75+
from_regex(r)

0 commit comments

Comments
 (0)