I want to add a feature to my website to let users search the texts with RegEx
. But, is it safe to let the users do something like that ?
preg_match('/' . $user_input_regex . '/', $subject);
I want to add a feature to my website to let users search the texts with RegEx
. But, is it safe to let the users do something like that ?
preg_match('/' . $user_input_regex . '/', $subject);
Security wise you should never trust user input, so it depends what you do with the input. In your given case you should at least escape the used delimiter (backslash) in the user input to ensure the regex works.
There is a possible attack on this code called a ReDoS attack (Regular expression Denial of Service).
Specifically with
preg_match
there is a known issue that can cause a PHP Segmentation Fault.So the answer is no, it is not safe because of issues such as these.