fix: prevent ReDoS in URL regex pattern#1
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates Abide’s URL validation to a safer, equivalent regex construction that mitigates a potential ReDoS vulnerability by rebuilding the pattern via RegExp instead of a single complex literal, while keeping existing behavior and adding case-insensitive matching. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The new URL regex adds an
i(case-insensitive) flag that the original pattern did not have; please confirm this behavior change is intentional for all consumers of this validator. - Consider extracting the URL regex construction into a named constant or helper outside
Abide.defaultsinstead of an inline IIFE to make the pattern easier to read, reason about, and potentially reuse.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new URL regex adds an `i` (case-insensitive) flag that the original pattern did not have; please confirm this behavior change is intentional for all consumers of this validator.
- Consider extracting the URL regex construction into a named constant or helper outside `Abide.defaults` instead of an inline IIFE to make the pattern easier to read, reason about, and potentially reuse.
## Individual Comments
### Comment 1
<location path="js/foundation.abide.js" line_range="864" />
<code_context>
// For more restrictive URL Regexs, see https://mathiasbynens.be/demo/url-regex.
- url: /^((?:(https?|ftps?|file|ssh|sftp):\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))$/,
-
+ url: (function() {
+ const protocol = '(?:https?|ftps?|file|ssh|sftp):\\/\\/';
+ const www = 'www\\d{0,3}[.]';
</code_context>
<issue_to_address>
**issue (bug_risk):** New use of `const` and template literals introduces ES2015 features into what appears to be an ES5-oriented file.
The new `const`, template literals, and IIFE-based `RegExp` mean this file now relies on ES2015 syntax. If this bundle must run in ES5-only environments (e.g., IE11 or non-transpiled tooling), this can cause syntax errors. If ES2015+ is required/guaranteed, no change needed; otherwise, please revert to ES5 constructs or ensure this file is transpiled.
</issue_to_address>
### Comment 2
<location path="js/foundation.abide.js" line_range="870" />
<code_context>
+ const domain = '[a-z0-9.\\-]+[.][a-z]{2,4}\\/';
+ const body = '(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]+\\)))*\\))+' +
+ '(?:\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'".,<>?\\xab\\xbb\\u201c\\u201d\\u2018\\u2019])';
+ return new RegExp(`^((?:${protocol}|${www}|${domain})${body})$`, 'i');
+ })(),
+
</code_context>
<issue_to_address>
**question (bug_risk):** The added `i` flag changes the URL validation semantics to be case-insensitive compared to the original regex.
The original regex was case-sensitive; constructing it with `RegExp(..., 'i')` makes the entire match case-insensitive (e.g., uppercase protocol/host now match). Please confirm this broader matching is expected by all callers, especially given the reference to the CommonRegexJS pattern. If not, remove the `i` flag to preserve the previous behavior.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| // For more restrictive URL Regexs, see https://mathiasbynens.be/demo/url-regex. | ||
| url: /^((?:(https?|ftps?|file|ssh|sftp):\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))$/, | ||
|
|
||
| url: (function() { |
There was a problem hiding this comment.
issue (bug_risk): New use of const and template literals introduces ES2015 features into what appears to be an ES5-oriented file.
The new const, template literals, and IIFE-based RegExp mean this file now relies on ES2015 syntax. If this bundle must run in ES5-only environments (e.g., IE11 or non-transpiled tooling), this can cause syntax errors. If ES2015+ is required/guaranteed, no change needed; otherwise, please revert to ES5 constructs or ensure this file is transpiled.
| const domain = '[a-z0-9.\\-]+[.][a-z]{2,4}\\/'; | ||
| const body = '(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]+\\)))*\\))+' + | ||
| '(?:\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'".,<>?\\xab\\xbb\\u201c\\u201d\\u2018\\u2019])'; | ||
| return new RegExp(`^((?:${protocol}|${www}|${domain})${body})$`, 'i'); |
There was a problem hiding this comment.
question (bug_risk): The added i flag changes the URL validation semantics to be case-insensitive compared to the original regex.
The original regex was case-sensitive; constructing it with RegExp(..., 'i') makes the entire match case-insensitive (e.g., uppercase protocol/host now match). Please confirm this broader matching is expected by all callers, especially given the reference to the CommonRegexJS pattern. If not, remove the i flag to preserve the previous behavior.
Description
Ports the upstream fix from foundation#15531 into this fork develop branch.
This updates the URL validation regex in js/foundation.abide.js to avoid the ReDoS issue while preserving the previous matching behavior.
Upstream reference
Validation
Summary by Sourcery
Bug Fixes: