Skip to content
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

Protection against ReDoS #6163

Merged
merged 4 commits into from
Nov 7, 2019
Merged
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
29 changes: 26 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 = 1 # timeout 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,31 @@ 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.

We could use a finite state machine type of regex too,
but there isn't a stable library at the time of writting this code.
"""
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