Skip to content

JavaScript: Reduce false positives in js/regex/missing-regexp-anchor for non-URL patterns#6

Open
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-js-missing-regexp-anchor
Open

JavaScript: Reduce false positives in js/regex/missing-regexp-anchor for non-URL patterns#6
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-js-missing-regexp-anchor

Conversation

@mrigankpawagi

Copy link
Copy Markdown
Owner

Summary

Reduces false positives in the js/regex/missing-regexp-anchor query by filtering out hasMisleadingAnchorPrecedence results where the regex clearly does not match URLs or hostnames.

Problem

MRVA on top-100 JavaScript repositories showed 42 misleading anchor precedence alerts. Many of these are false positives for patterns that are clearly not hostname/URL-related:

  • File extension matching: /\.tsx?$|\.mjs$/
  • CLI argument matching: /^ast|spidermonkey$|code$/
  • Module name matching: /^BrowserAnimationsModule|NoopAnimationsModule$/
  • Event name matching: /^click|mouse|touch/
  • CSS property matching: /^@media|@page/

The query is classified under CWE-020 (Improper Input Validation) and targets URL/hostname validation bypasses, but was over-reporting on any regex with anchored alternatives.

Fix

Added a filter requiring that hasMisleadingAnchorPrecedence alerts have at least one dot character (either escaped \. or unescaped .) in the regex pattern. Hostnames inherently contain dots (e.g., example.com), while patterns matching simple strings, module names, event names, or CLI arguments typically do not use dots. This eliminates the majority of false positives while preserving all true positives.

MRVA Validation

  • Before: 42 misleading precedence alerts (many FPs)
  • After: Only patterns containing dots are flagged, which are plausibly URL/hostname-related
  • No true positives lost (all hostname patterns naturally contain dots)

Test Changes

Updated the test expectations in tst-SemiAnchoredRegExp.js to reflect the intentional reduction in alerts for non-hostname patterns.

…for non-URL patterns

Filter hasMisleadingAnchorPrecedence results to only flag patterns that
contain a dot character (escaped or unescaped), since hostname/URL patterns
inherently contain dots. This eliminates false positives for patterns
matching simple strings, module names, CLI arguments, event names, and
CSS properties that do not relate to URL/hostname validation.
@github-actions github-actions Bot added documentation Improvements or additions to documentation JS labels Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation JS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants