Skip to content

router: an alternation in a hostname regex segment is only half-anchored, re-opening the substring match #1356

Closed

Description

@FlorentinDUBOIS

Summary

Hostname regex segments are anchored by concatenating \A and \z around the operator's pattern,
without grouping it. | is the lowest-precedence regex operator, so an alternation splits the
anchors between its branches: \Aa|b\z parses as (\Aa)|(b\z).

Each branch ends up anchored at one end only, which re-opens the substring match the anchoring
exists to prevent.

Measured

Through the router, on this tree: a hostname rule /a|b/.example.com matches

a.example.com      (intended)
b.example.com      (intended)
axx.example.com    <- unintended, first branch unanchored at its end
xxb.example.com    <- unintended, second branch unanchored at its start

The precedence itself is not engine-specific:

pattern            "a"    "b"    "axx"   "xxb"
\Aa|b\z            true   true   true    true     <- ungrouped
\A(?:a|b)\z        true   true   false   false    <- grouped

Sites

  • lib/src/router/mod.rs:834 convert_regex_domain_rule — String::from("\\A") … push_str("\\z")
  • lib/src/router/pattern_trie.rs:232, :285, :428, :665 — format!("\\A{s}\\z")

Why this matters

The anchoring was added deliberately against exactly this class. The comment above
convert_regex_domain_rule says so:

Anchor at both ends so Regex::is_match only succeeds on a full-host match. Without \A the
pattern /example\.com/ matches any hostname containing example.com as a substring (e.g.
attacker.example.com.evil.org), letting an attacker-controlled domain reach a frontend that
should only serve example.com.

An alternation restores that hole for every branch. A frontend written /api|admin/.example.com
serves apifoo.example.com and xadmin.example.com, neither of which the operator declared.

An operator has no way to notice: the frontend loads, sozu query frontends shows exactly what they
typed, and the extra hosts only appear if someone sends them.

Fix

Wrap the pattern in a non-capturing group before anchoring — \A(?:{pattern})\z — at all five sites.

Non-capturing matters: captures_len() must not change, because $HOST[n] / $PATH[n] rewrite
indices are derived from capture groups. Verified on the path side that (?:…) leaves
captures_len() untouched (/a(b)c → 2, \A(?:/a(b)c)\z → 2).

Provenance

Found while implementing #1350 (anchoring path regexes), where the same wrapper was being written for
the path side. The path implementation uses the grouped form for this reason and pins it with a test;
the hostname sites were deliberately left untouched, as a separate defect with its own operator impact
and its own tests.

Not reproduced against a live proxy — exercised through the router's public surface.

Related: #1350, #1351.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions