Skip to content

Commit

Permalink
Protection against ReDoS
Browse files Browse the repository at this point in the history
The regex module is compatible with the re module (VERSION0 flag).
It is also faster.

```python
>>> import re
>>> import regex
>>> import timeit
>>> pattert = "(a+)+b"
>>> input = "a" * 25
>>> timeit.timeit(lambda: re.search(pattern, input), number=10)
32.332445038000515
>>> timeit.timeit(lambda: regex.search(pattern, input, flags=regex.VERSION0), number=10)
0.003861578001306043
>>> input = "a" * 10000
>>> regex.search(pattern, input, flags=regex.VERSION0, timeout=5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stsewd/.pyenv/versions/readthedocs.org/lib/python3.6/site-packages/regex/regex.py", line 266, in search
    concurrent, partial, timeout)
TimeoutError: regex timed out
```
  • Loading branch information
stsewd committed Nov 6, 2019
1 parent f30eb28 commit 7cc0b47
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
from shutil import rmtree

import regex
from django.conf import settings
from django.db import models
from django.db.models import F
Expand Down Expand Up @@ -1137,6 +1138,8 @@ def __str__(self):

class RegexAutomationRule(VersionAutomationRule):

TIMEOUT = 15 # timout in seconds

allowed_actions = {
VersionAutomationRule.ACTIVATE_VERSION_ACTION: actions.activate_version,
VersionAutomationRule.SET_DEFAULT_VERSION_ACTION: actions.set_default_version,
Expand All @@ -1146,11 +1149,28 @@ class Meta:
proxy = True

def match(self, version, match_arg):
"""
Find a match using regex.search.
.. note::
We use the regex module with the timeout
arg to avoid ReDoS.
"""
try:
match = re.search(
match_arg, version.verbose_name
match = regex.search(
match_arg,
version.verbose_name,
# Compatible with the re module
flags=regex.VERSION0,
timeout=self.TIMEOUT,
)
return bool(match), match
except TimeoutError:
log.warning(
'Timeout while parsing regex. pattern=%s, input=%s',
match_arg, version.verbose_name,
)
except Exception as e:
log.info('Error parsing regex: %s', e)
return False, None
return False, None
1 change: 1 addition & 0 deletions requirements/pip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Unipath==1.1
django-kombu==0.9.4
mock==3.0.5
stripe==2.37.2
regex==2019.11.1

# unicode-slugify==0.1.5 is not released on PyPI yet
git+https://github.com/mozilla/unicode-slugify@b696c37#egg=unicode-slugify==0.1.5
Expand Down

0 comments on commit 7cc0b47

Please sign in to comment.