Problem/Motivation
The methods isNeededUrls() and isUaWhitelisted() in CookieBotProtectionMiddleware pass configuration patterns directly to preg_match() without any prior validation:
if ($pattern_found |= (preg_match('{' . $pcre_pattern . '}u', $user_agent, $matches) > 0)) {
If an administrator enters an invalid regular expression in the settings form (e.g. an unclosed group like (foo), preg_match() will emit a PHP Warning or throw an Error on every request matching a protected URL, until the configuration is manually fixed.
Steps to reproduce
- Install the module and go to the settings form.
- Enter
(fooin the Url protected patterns or User-Agent whitelist patterns field and save. - Visit a URL that matches the intended pattern.
- Observe a PHP warning or error in the logs.
Proposed resolution
Add a validateForm() method in src/Form/SettingsForm.php that iterates over each line of the url_protected_patterns and ua_whitelist_patterns fields and tests each pattern before the configuration is saved:
set_error_handler(static function (): bool { return TRUE; });
$valid = preg_match('{' . $pattern . '}u', '') !== FALSE;
restore_error_handler();If a pattern is invalid, $form_state->setError() is called on the corresponding field with a message indicating which pattern failed.
Remaining tasks
- Implement
validateForm()insrc/Form/SettingsForm.php - Add test coverage
User interface changes
A validation error message is displayed on the Url protected patterns and/or User-Agent whitelist patterns fields when an invalid regular expression is submitted.
API changes
None.
Data model changes
None.
Issue fork cookie_bot_protection-3595565
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
macsim commentedComment #4
macsim commentedComment #6
macsim commented