Skip to content

fix(router): keep a Tree hostname's regex source out of IDNA folding - #1385

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
fix/idna-folds-regex-escapes
Sep 21, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
fix/idna-folds-regex-escapes

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

idna::domain_to_ascii was applied to a whole Tree hostname, including any /…/ regex
segment, and it case-folds the escape — inverting \D into \d. The rule then matched the
exact complement of what the operator wrote.

Measured on the unfixed tree:

"/[^\D]/.example.com"  -> "/[^\d]/.example.com"
"/\D+/.example.com"    -> "/\d+/.example.com"
lookup "abc.rx.example.com" -> None
lookup "777.rx.example.com" -> Some(...)     <- \D matched a digit

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_rule already makes
for Pre/Post and the one insert_recursive uses to address a regexps entry. Per-label and
whole-domain IDNA were measured to agree on a label (MÜNCHEN → xn--mnchen-3ya either
way), so splitting loses no IDNA. A hostname with no /, or one whose / does not parse as
that grammar, falls back to the unchanged whole-string call byte for byte.

Two halves, both required. tree_hostname_to_ascii sends literal labels through
idna::domain_to_ascii and copies /…/ segments verbatim; compiled_segment wraps
anchored_segment with case_insensitive(true) at the single compile site, because not
folding the source would otherwise stop /API[0-9]/ matching api7.

anchored_segment is untouched and stays the single source of a regexps entry's identity
— RegexBuilder leaves as_str() as handed, asserted by a debug_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:

`\D+` must NOT match a run of digits; folded to `\d+` it matched exactly this
the literal label must still be punycoded and lowercased

Both exit 101 with a test result: line, so real failures rather than compile errors. The
second 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

`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
FlorentinDUBOIS merged commit d7e4400 into main Sep 21, 2026
43 of 44 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/idna-folds-regex-escapes branch September 21, 2026 02:51
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.

router: idna::domain_to_ascii folds regex escapes in a Tree hostname, inverting \D into \d

1 participant