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

Catastrophic backtracking in regular expression #723

Open
ericwb opened this issue Apr 2, 2024 · 0 comments
Open

Catastrophic backtracking in regular expression #723

ericwb opened this issue Apr 2, 2024 · 0 comments

Comments

@ericwb
Copy link

ericwb commented Apr 2, 2024

While doing some static analysis, found that this bit of code is vulnerable to a ReDoS (Regular expression denial-of-service).

https://github.com/nficano/python-lambda/blob/master/aws_lambda/helpers.py#L41

⛔️ Error on line 41 in https://github.com/nficano/python-lambda/blob/master/aws_lambda/helpers.py#L41
PY033: Inefficient Regular Expression Complexity
The call to 're.search'' with regex pattern 'r"^\${(?P<environment_key_name>\w+)*}$"'' is susceptible to catastrophic backtracking and may cause performance degradation.
  40     if val is not None and isinstance(val, str):                                                      
❱ 41         match = re.search(r"^\${(?P<environment_key_name>\w+)*}$", val)                               
  42         if match is not None:                                                                         

To recreate in a small sample:

import re

val = "${aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!}"
match = re.search(r"^\${(?P<environment_key_name>\w+)*}$", val)

Running this code will hang for a very long time.

Thus in the original code, if an environment variable is set to "${aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!}" you'll get a similar denial-of-service.

https://docs.securesauce.dev/rules/PY033

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant