Repository navigation
fix(router): keep a Tree hostname's regex source out of IDNA folding - #1385
Merged
Merged
Conversation
`AddCertificate.certificate.names` is a free `Vec<String>` that `CertificateResolver::add_certificate` inserted into the SNI `TrieNode<Fingerprint>` verbatim. That trie is the same `lib/src/router/pattern_trie.rs` the HTTP router uses, where a dot-separated label wrapped in `/.../` is compiled as a REGEX segment, so a certificate declared for `/te.*/.example.com` was served for `test.example.com` and for `tenant.example.com` alike -- one certificate bound to an open-ended class of SNI values, with an inventory that shows a SAN-shaped string. The validation that existed could not catch it: it dry-runs every name into a scratch trie and asks only whether the name INSERTS, which a regex segment does. The check has to be on the name's bytes. `add_certificate` now also refuses a name containing `/`, in the SAME pre-mutation block as the `MAX_HOSTNAME_LENGTH` bound, returning `CertificateResolverError::InvalidName`; nothing is half-registered. A `/` cannot occur in a DNS name, so no legitimate SAN is lost, and an ordinary `*.example.com` wildcard is not a regex segment and still loads. This is the rule `config::validate_sni_pattern` already applies to every path into the TCP SNI route table; the certificate trie was the one SNI table left open. Removal is unaffected: `remove_certificate` re-inserts the names a certificate already carries and never re-validates them, so a certificate an earlier build accepted stays removable. `doc/configure.md` states the rule under "Hostname case" and `CHANGELOG.md` tells an operator how to find an affected configuration -- the bundled CLI never sets the `names` override, so only a control plane that fills it, or a saved state replaying one, can be affected. Reported in #1378. Signed-off-by: Florentin Dubois <[email protected]>
`add_tree_rule`, `remove_tree_rule` and `has_hostname` ran the WHOLE configured hostname through `idna::domain_to_ascii`, regex segments included. That function ASCII-lowercases, and lowercasing an uppercase regex escape inverts the character class it names: measured, `"/\D+/.example.com"` normalised to `"/\d+/.example.com"` and `"/[^\D]/.example.com"` to `"/[^\d]/.example.com"`. `\D` is "not a digit" and `\d` is "a digit", so the installed rule matched the exact COMPLEMENT of what the operator wrote while `sozu query frontends` still echoed the original spelling. Measured on the tree as it stood, the frontend `/\D+/.rx.example.com` served `777.rx.example.com` and did not serve `abc.rx.example.com`. `\W`/`\w` and `\S`/`\s` invert the same way. Pre and Post never carried it: `convert_regex_domain_rule` copies a segment's source verbatim and `DomainRule::from_str` takes its case insensitivity from the `RegexBuilder` instead. `tree_hostname_to_ascii` normalises the literal labels only and copies a `/`-delimited segment byte for byte -- the split `convert_regex_domain_rule` already makes and the one `pattern_trie.rs`'s `insert_recursive` addresses a `regexps` entry by. Per-label and whole-domain IDNA agree on a label (measured: `"MUNCHEN"` with an umlaut maps to `"xn--mnchen-3ya"` either way), so a genuine unicode label in a hostname that also carries a regex segment is still punycoded. A hostname with no `/` at all, and one whose `/` does not parse as that grammar (`abc/[0-9]+/.example.com`, `example.com/`), both keep the historical whole-string call byte for byte. Leaving the source unfolded moves the case fold onto the compile, and that half is load-bearing: an uppercase literal such as `/API[0-9]/` used to meet the ASCII-lowercased lookup key only because the source had been lowercased too. `pattern_trie`'s `compiled_segment` wraps `anchored_segment` and sets `case_insensitive(true)`, exactly as `DomainRule::from_str` does, so literals fold and `\d`/`\D`, `\w`/`\W` and `\s`/`\S` do not. `RegexBuilder` leaves `as_str()` as the pattern it was handed, so `anchored_segment` remains the single source of a `regexps` entry's identity, a stored segment stays removable, and `captures_len` -- and therefore every `$HOST[n]` rewrite index -- is unchanged. This changes existing rules. A trie-routed frontend spelling `\D`, `\W` or `\S` has been matching the complement of its source and now matches what it says; a rule tuned against the folded behaviour must be respelled with the lowercase escape it actually meant. `/API[0-9]/` keeps matching exactly the hosts it matched before. `doc/configure.md` and `CHANGELOG.md` carry the migration note in this changeset. Regression tests, each seen red on the unfixed tree: `a_tree_hostname_regex_escape_is_not_case_folded`, `every_inverting_uppercase_regex_escape_survives_a_tree_insert`, `a_unicode_label_is_punycoded_while_its_sibling_regex_segment_is_not`, and `compiled_segment_folds_case_without_respelling_its_identity`. Closes #1377 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 20, 2026 22:14
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.
idna::domain_to_asciiwas applied to a whole Tree hostname, including any/…/regexsegment, and it case-folds the escape — inverting
\Dinto\d. The rule then matched theexact complement of what the operator wrote.
Measured on the unfixed tree:
A label the operator wrapped in slashes is regex source; every other label is a domain
label. That is not a new rule — it is the split
convert_regex_domain_rulealready makesfor Pre/Post and the one
insert_recursiveuses to address aregexpsentry. Per-label andwhole-domain IDNA were measured to agree on a label (
MÜNCHEN→xn--mnchen-3yaeitherway), so splitting loses no IDNA. A hostname with no
/, or one whose/does not parse asthat grammar, falls back to the unchanged whole-string call byte for byte.
Two halves, both required.
tree_hostname_to_asciisends literal labels throughidna::domain_to_asciiand copies/…/segments verbatim;compiled_segmentwrapsanchored_segmentwithcase_insensitive(true)at the single compile site, because notfolding the source would otherwise stop
/API[0-9]/matchingapi7.anchored_segmentis untouched and stays the single source of aregexpsentry's identity—
RegexBuilderleavesas_str()as handed, asserted by adebug_assert_eq!.captures_len()is pinned on the production-compiled regex at 1 (
a|b|c) and 2 (cdn([0-9]+)), so no$HOST[n]rewrite index shifts.Red-then-green, re-verified independently of the authoring run by disabling the split path:
Both exit 101 with a
test result:line, so real failures rather than compile errors. Thesecond is the one that matters: the fix spares only regex source, it does not skip IDNA.
BUILD=0 CLIPPY=0 TEST=0 FMT=0—test result: ok. 915 passed; 0 failed.Stacked on #1384; review the top commit. The case-insensitive compile also reaches the
certificate-name trie, which #1384 closes by rejecting
/in certificate names outright.Closes #1377