Repository navigation
fix(router): stop an exact-host rule attaching to a matching regex segment - #1367
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 20, 2026 11:41
…dence `add_tree_rule` reaches the node a hostname owns through `TrieNode::domain_lookup_mut` and inserts only when that answers `None`. That resolver walked a literal segment through the first regex segment matching it, so adding `test4.example.com` after `/test[0-9]/.example.com` never created a node for the exact host: the rule was appended to the regex segment's list and every host the pattern matches served it. A rule scoped to one host answered for a family, while `sozu query frontends` showed exactly what the operator had typed, and declaring the exact name first avoided it entirely. `lookup_mut` is key-addressed now: with `accept_wildcard: false` a literal key may not resolve into a non-literal entry. That is the guard `insert_sni_route` already documented for the `*` slot, extended to the regex list, which had nothing equivalent; the `*` case and the trailing-`/` case of the same function already matched by identity. Hostname resolution is stated and asserted most specific first — exact name, then regex segments in declaration order, then the `*` wildcard — in one backtracking walk, `TrieNode::lookup_recursive`, that `lookup` and `lookup_with_path` share so routing and SNI certificate selection cannot disagree. The exact-child arm had no fallback and the wildcard was consulted before the regex list; reordering without the fallback would have broken a regex segment holding no value for the host it matched. `Router::lookup` hands the trie the same `select_tree_rule` it runs on the winner, so a hostname candidate whose rules all reject this path or method is skipped rather than ending the lookup — which is the reported 404 where a first-declared POST-only segment shadowed a later GET one. This narrows routing for configurations where a family was being served an exact host's rule and broadens it where a request now falls through to a less specific frontend instead of missing; `CHANGELOG.md` states both and `doc/configure.md` states the order under "Hostname precedence". State replay needs no migration, and a new `command/src/state.rs` test pins why: the `BTreeMap` key sorts a regex segment before the exact name it matches, so a forked worker always took the leaking order and both orders now build the same table. The leak was order-dependent only in one process, which is how the report reproduced it; the corollary on an affected build is that a correct table built live by an exact-first API sequence is re-corrupted at the next fork. The TCP SNI route table cannot hold a regex segment — `validate_sni_pattern` rejects `/` — but the certificate trie can, through an operator-supplied `names` override, so the reorder does reach certificate selection there. Closes #1351 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/trie-exact-host-precedence
branch
from
September 20, 2026 11:46
5875bb9 to
2ea3551
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1351.
The leak
Adding an exact-hostname frontend after a regex hostname segment that matches it did not create an
exact-host rule.
TrieNode::lookup_mutresolved a literal segment through the firstregexpsentrywhose pattern matched it, and
add_tree_rule,Router::remove_tree_rule,TcpListener::insert_sni_routeandremove_sni_routeall address a leaf throughdomain_lookup_mutand insert only on
None— so the exact host never got a node and its rule was appended to the regexleaf's
Vec, then served for every host that regex matched.It was not declaration-order-dependent in production — it was universal
The issue says "Declaration order decides whether this happens at all". That is true of the direct API,
which is what its reproduction measures, and false of the production path.
The main process replays
ConfigStateinto every worker from aBTreeMap<String, HttpFrontend>keyed{address};{hostname};P{path}[;{method}]./is0x2F, below every character legal at the start of aDNS label, so at the first differing byte a regex key always sorts before the exact host it matches.
The full chain was traced with no sort, partition or grouping anywhere:
So every worker took the adds in the leaking order, at every start — and a correct table built live by
an exact-first API sequence was re-corrupted at the next fork or upgrade. Deterministic and
universal for any configuration carrying an exact host and a matching regex segment on the same
listener address.
The fix, in two halves that must ship together
The add path.
lookup_mutis now key-addressed:if !accept_wildcard { return None; }in front ofthe
regexpsscan. This is the guardinsert_sni_routealready documented for the*slot — "an exactkey must never fall back to a sibling wildcard's entry" — extended to the regex list, which had nothing
equivalent. Fixing it in the trie also closes the identical latent bug on the TCP SNI route table.
The lookup path.
lookupandlookup_with_pathnow share one backtracking walk,TrieNode::lookup_recursive, so the order is stated once and the SNI and HTTP paths cannot drift. Ittakes an
acceptpredicate, andRouter::lookuppasses the sameselect_tree_ruleit will run on thewinner — which turns the order into a search rather than a filter, closing the method-404 shape the
issue reports as its second finding.
Precedence, stated and asserted
Per level, most specific first: exact literal child → regex segments (declaration order among
overlapping) →
*wildcard, leftmost label only, with fall-through between every pair.Four mutations were run; no transition survives:
Red on the unfixed tree with the tests alone injected:
FAILED. 108 passed; 7 failed.Cost, measured rather than argued
The unified walk backtracks over regex segments at every level, so it was measured on both axes.
Attacker-controlled dimension, configuration fixed at 30 nodes:
1.10× for a 32× longer hostname. Cost is linear in configuration node count (~11-13 µs/node),
not in attacker input. Recursion depth is unchanged — one frame per label. No datapath DoS.
Operator impact
Two behaviours change. The narrowing is the fix: a rule written for one host stops being served to a
regex family. The broadening is the one to read: an exact frontend no longer shadows
*.example.comfor paths it does not itself carry, so an operator using a narrow exact frontend as adeny-by-404 shadow loses that. Both are in
CHANGELOG.md, anddoc/configure.mdgains a### Hostname precedencesection stating the order.No migration is needed: the two declaration orders now build the same table, so the same replay yields
corrected routing on the new binary.
Reach
lib/src/tls.rsandlib/src/protocol/tcp_preread/mod.rswalk separateTrieNodeinstancessharing only generic method code, and both consume
TrieNode::lookup, so certificate selection followsthe same order. The TCP SNI route trie provably cannot hold a regex segment —
validate_sni_patternrejects
/on every insert path, with tests. The certificate trie is not so closed:add_certificatevalidates only length and a hard
InsertResult::Failed, and its names may be an operator-suppliedoverride, so a regex-shaped name can land there. No test covers the new order on that path.
Validation
BUILD=0 CLIPPY=0 TEST=0 FMT=0,test result: ok. 871 passed; 0 failed.Merged with
fix/anchor-hostname-regex: builds, 880 passed, clippy clean.