How can I safely validate an untrusted regex in Perl?

前端 未结 2 1487
情歌与酒
情歌与酒 2021-01-19 15:20

This answer explains that to validate an arbitrary regular expression, one simply uses eval:

while (<>) {
    eval \"qr/$_/;\"
    print $         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 15:58

    There is some discussion about this over at The Monastery.

    TLDR: use re::engine::RE2 (-strict => 1);

    Make sure at add (-strict => 1) to your use statement or re::engine::RE2 will fall back to perl's re.

    The following is a quote from junyer, owner of the project on github.

    RE2 was designed and implemented with an explicit goal of being able to handle regular expressions from untrusted users without risk. One of its primary guarantees is that the match time is linear in the length of the input string. It was also written with production concerns in mind: the parser, the compiler and the execution engines limit their memory usage by working within a configurable budget – failing gracefully when exhausted – and they avoid stack overflow by eschewing recursion.

提交回复
热议问题