Skip to content

Catch unsupported operation errors #16

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions regexploit/ast/sre.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
SreOp = Tuple[SreConstant, SreOpData]


class UnsupportedSreOpException(Exception):
pass


class SreOpParser:
def __init__(self):
self._groups = {}
Expand All @@ -24,6 +28,8 @@ def parse_sre(self, pattern: str, flags: int = 0):
return self.sequence_or_singleton(sre_parse.parse(pattern, flags))

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

def sequence_or_singleton(self, ops: List[SreOp]):
Expand Down
10 changes: 7 additions & 3 deletions regexploit/bin/regexploit-python-env
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def main():
hooked_regex: regexploit.hook.CompiledRegex
for hooked_regex in regexploit.hook.get_and_clear_regexes():
output.next()
parsed = SreOpParser().parse_sre(
hooked_regex.pattern, hooked_regex.flags
)
try:
parsed = SreOpParser().parse_sre(
hooked_regex.pattern, hooked_regex.flags
)
except Exception as e:
print(f"Error parsing regex {hooked_regex.pattern} from {p}: {e}")
continue
Comment on lines +42 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same patch is also required for the yaml executable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened stsewd#1 to your repository, so it can end up in the same pull request here

for redos in find(parsed):
if redos.starriness > 2:
output.record(
Expand Down
5 changes: 4 additions & 1 deletion regexploit/bin/regexploit_python_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import traceback
import warnings

from regexploit.ast.sre import SreOpParser
from regexploit.ast.sre import SreOpParser, UnsupportedSreOpException
from regexploit.bin.files import file_generator
from regexploit.languages.python_node_visitor import PythonNodeVisitor
from regexploit.output.text import TextOutput
Expand All @@ -31,6 +31,9 @@ def handle_file(filename: str, output: TextOutput):
parsed = SreOpParser().parse_sre(regex.pattern, regex.flags)
except re.error:
continue # We will have many strings which aren't actually regexes
except UnsupportedSreOpException as e:
print(f"Error parsing regex {regex.pattern} from {filename}: {e}")
continue
try:
output.next()
for redos in find(parsed):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_at.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from regexploit.ast.at import EndOfString
from regexploit.ast.sre import SreOpParser
from regexploit.ast.sre import SreOpParser, UnsupportedSreOpException


def from_regex(pattern: str):
Expand Down Expand Up @@ -60,3 +60,16 @@ def test_real():
dollar = EndOfString()
dollar.set_character(from_regex(r"-\d+(\s*\s*\s*)").elements)
assert dollar.character == from_regex(r"[\s]")


@pytest.mark.parametrize(
"r",
[
r"a++b",
r"a?+b",
r"a*+b",
],
)
def test_unsupported_op(r):
with pytest.raises(UnsupportedSreOpException):
from_regex(r)