You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
router: an alternation in a hostname regex segment is only half-anchored, re-opening the substring match #1356
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 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.
Activity
fix(router): anchor every branch of an alternation in a hostname regex
fix(router): anchor every branch of an alternation in a hostname regex
test(router): quickcheck property harness for hostname resolution
test(router): quickcheck property harness for hostname resolution (#1407