Detect potentionally unsafe regex patterns
This commit is contained in:
parent
3f5a5b4f9b
commit
bf41c7b651
1 changed files with 20 additions and 0 deletions
20
src/util/SafeRe.py
Normal file
20
src/util/SafeRe.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class UnsafePatternError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def isSafePattern(pattern):
|
||||||
|
if len(pattern) > 255:
|
||||||
|
raise UnsafePatternError("Pattern too long: %s characters" % len(pattern))
|
||||||
|
|
||||||
|
unsafe_pattern_match = re.search("[^\.][\*\{\+]", pattern) # Always should be "." before "*{+" characters to avoid ReDoS
|
||||||
|
if unsafe_pattern_match:
|
||||||
|
raise UnsafePatternError("Potentially unsafe part of the pattern: %s" % unsafe_pattern_match.group(0))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def match(pattern, *args, **kwargs):
|
||||||
|
if isSafePattern(pattern):
|
||||||
|
return re.match(pattern, *args, **kwargs)
|
Loading…
Reference in a new issue