Skip to content

fix: anchor RegExp hostname containing alternation - #95

Open
RaphaelFakhri wants to merge 1 commit into
expressjs:masterfrom
RaphaelFakhri:fix-regexp-alternation-anchor
Open

RaphaelFakhri wants to merge 1 commit into
expressjs:masterfrom
RaphaelFakhri:fix-regexp-alternation-anchor

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

A RegExp hostname with a top-level alternation is only partly anchored. hostregexp prepends ^ and appends $ to the source, so /tobi\.com|loki\.com/ becomes /^tobi\.com|loki\.com$/i, and hostnames such as tobi.com.ferret.net and ferret.loki.com match. The README says a RegExp is "forced to match based on the start and end of the hostname".

This change wraps the source in a non-capturing group, ^(?:source)$, for both string and RegExp hostnames. This makes the END_ANCHORED_REGEXP check unnecessary, so the change removes it. Capture group numbering and req.vhost are unchanged, and RegExps that are already anchored behave the same.

Tests

  • should match any alternative: loki.com still matches /tobi\.com|loki\.com/.
  • should match entire hostname with alternation: tobi.com.ferret.net and ferret.loki.com return 404. This test fails on master (expected 404, got 200) and passes with the fix.

npm run lint and npm test pass, and index.js coverage stays at 100%.

Fixes #94

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.

RegExp hostname with alternation is not fully anchored

1 participant