In which languages is it a security hole to use user-supplied regular expression?

后端 未结 8 1538
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 17:00

Edit: tchrist has informed me that my original accusations about Perl\'s insecurity are unfounded. However, the question still stands.

I know that i

8条回答
  •  悲哀的现实
    2020-12-17 17:06

    User-supplied regex, or in general, user input, should never be treated as safe - regardless of the programming language. If your program fails to do so, it is vulnerable to attacks by deliberately crafted inputs.

    In the case of Regex, it can be ReDos: Regex Denial of Service. Basically, a regex which consumes an excessive amount of CPU and memory to process.

    For e.g: if you try to evaluate this regex

    ^(([a-z])+.)+[A-Z]([a-z])+$
    

    on this input:

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!
    

    you'll notice it may hang - it's called catastrophic backtrack. See it for yourself here: https://regex101.com/r/Qhn3Vb/1

    Read more about Regex DoS: https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS


    Bottomline: never assume user input is safe!

提交回复
热议问题