寻找有关我应该在正则表达式中转义哪些字符以防止漏洞利用的提示。
例如
Regular Expression Injection
The PCRE function preg_replace() function in PHP allows for an “e” (PREG_REPLACE_EVAL) modifier which means the replacement string will be evaluated as PHP after subsitution. Untrusted input used in the replacement string could therefore inject PHP code to be executed.
或此处:http://hauser-wenz.de/playground/papers/RegExInjection.pdf
一般来说,对于 sql 注入(inject),有很多指南和技巧可以遵循,但找不到太多关于正则表达式注入(inject)的信息
最佳答案
简单的答案:使用preg_replace_callback()
相反(它不依赖于用户代码的 eval
)。这就是 PHP 5.5 建议您在使用 /e
时执行的操作。
$str = 'CAPS';
$str = preg_replace_callback('/[A-Z]/', function($match) { return strtolower($match[0]); }, $str);
echo $str;
关于php - 如何防范PHP中的正则表达式注入(inject)?为 php 的所有正则表达式函数清理输入的一般规则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21114365/