Skip to content

fix: prevent ReDoS in URL regex pattern#1

Merged
DontFretBrett merged 2 commits into
developfrom
fix/url-regex-with-redos-vulnerability
May 20, 2026
Merged

fix: prevent ReDoS in URL regex pattern#1
DontFretBrett merged 2 commits into
developfrom
fix/url-regex-with-redos-vulnerability

Conversation

@DontFretBrett
Copy link
Copy Markdown
Owner

@DontFretBrett DontFretBrett commented May 20, 2026

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

  • yarn test:single-process was run under Node 20 in this environment because the system default Node 26 breaks the repos existing Mocha setup before the change is applied.

Summary by Sourcery

Bug Fixes:

  • Prevent potential ReDoS in the Abide URL validation by replacing the vulnerable regex pattern.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 20, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates 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

Change Details Files
Refactor URL validation regex to avoid ReDoS and construct it from safer sub-patterns.
  • Replace single large URL regex literal with an IIFE that builds sub-pattern strings for protocol, www prefix, domain, and body.
  • Create the final URL matcher via new RegExp using a composed pattern string instead of a literal to reduce catastrophic backtracking risk.
  • Preserve the previous matching semantics while adding the case-insensitive flag on the URL regex.
js/foundation.abide.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@DontFretBrett DontFretBrett merged commit bfe2a22 into develop May 20, 2026
1 check passed
@DontFretBrett DontFretBrett deleted the fix/url-regex-with-redos-vulnerability branch May 20, 2026 18:05
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.defaults instead 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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread js/foundation.abide.js
// 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() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread js/foundation.abide.js
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');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant