Repository navigation
fix(router): prevent panic in DomainRule::Wildcard::matches - #1232
Merged
Merged
Conversation
DomainRule::Wildcard::matches in lib/src/router/mod.rs computed hostname.len() - s.len() + 1 unconditionally, underflowing usize and panicking the HTTP worker when an incoming hostname was shorter than the configured wildcard pattern. Reachable from H1, H2, and operator-driven Router::has_hostname (CWE-191 -> CWE-754, remote-reachable DoS). Replace the unguarded subtraction with <[u8]>::strip_suffix; validate non-empty, dot-free leftmost prefix on the returned slice. The boundary case hostname == s[1..] (empty leftmost label) is now rejected per RFC 1035 section 3.1. Add a doc-comment on enum DomainRule::Wildcard recording the contract, plus two regression tests covering both DomainRule::matches directly and Router::lookup end-to-end. Closes #1223 Signed-off-by: Florentin Dubois <[email protected]>
jrcribb
pushed a commit
to jrcribb/sozu
that referenced
this pull request
Aug 10, 2026
…d-overflow fix(router): prevent panic in DomainRule::Wildcard::matches
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.
Summary
DomainRule::Wildcard::matchesinlib/src/router/mod.rscomputedhostname.len() - s.len() + 1unconditionally, underflowingusizeandpanicking the HTTP worker thread with
attempt to subtract with overflowwhenever the incoming hostname was shorter than the configured wildcard
pattern. An unauthenticated client can trigger this with a short
Host:header (H1) or a 1-byte
:authority(H2). Debug builds always panic;release builds wrap and are masked by the
&&short-circuit onends_with(suffix), so the false-positive in §1 of the issue is rare —but the latent OOB slice would itself panic if
ends_withever returnedtrue on the wrapped value. CWE-191 → CWE-754, remote-reachable DoS.
Closes #1223.
Fix
Replace the unguarded subtraction with
<[u8]>::strip_suffixandvalidate the leftmost-prefix invariants on the returned slice:
There is no length arithmetic to underflow.
!prefix.is_empty()enforces"leftmost label must be non-empty" — the boundary
Host: .foo.example.comagainst
*.foo.example.comis now rejected as an invalid DNS label perRFC 1035 §3.1.
!prefix.contains(&b'.')preserves single-label wildcardsemantics. A
///doc-comment onenum DomainRule::Wildcardrecords theimplementation contract.
Behavior changes
*.foo.example.comb"",b"x",b"a.b"falsein release (wrap masked by&&short-circuit onends_with)falseb".foo.example.com"true(buggy: empty prefix slice)false(empty leftmost label rejected)b"x.foo.example.com"truetrue(unchanged)b"y.x.foo.example.com"falsefalse(unchanged)Production-realistic traffic semantics are unchanged. The only flip is
the boundary case
hostname == s[1..], which previously crashed (debug)or accepted-via-empty-slice (release) an invalid DNS label.
Reachability
HttpListener::frontend_from_request→Router::lookup→ linearpre/post
DomainRule::matches.hostname_and_portaccepts an emptyhostname via
take_while, so a literal emptyHost:reaches the bug.:authorityis rejected when empty bypkawa.rs, but a 1-byteauthority still triggered the underflow against any wildcard of length
≥ 3.
Router::has_hostname(frontend-removal cleanup)shares the arm and benefits identically.
The trie path (
pattern_trie::lookup) has its ownis_empty()guard andis not affected.
Tests
Two new unit tests in
lib/src/router/mod.rs:match_domain_rule_wildcard_short_hostname_does_not_panicexercisesDomainRule::matchesdirectly withb"",b"x",b"a.b", theboundary
b".foo.example.com", multi-labelb"y.x.foo.example.com",and the canonical happy case
b"x.foo.example.com". The first threewould panic in debug builds on
maintoday.router_lookup_wildcard_pre_rule_short_hostname_does_not_panicexercises the full
Router::lookupintegration with aWildcardpre-rule. The pre-existing
match_routertest only putsWildcardin
add_tree_rule(which routes throughpattern_trie::lookupandbypasses
DomainRule::Wildcard::matches), so this new test closesthe only path actually affected by Panic in DomainRule::Wildcard::matches — "attempt to subtract with overflow" when hostname shorter than wildcard #1223.
Validation
cargo build --all-features --lockedcargo +nightly fmt --all -- --checkcargo clippy --all-targets --locked— no issuescargo test -p sozu-lib --lib router::tests— 20 passed (was 18; +2 new)cargo test --workspace --doc --locked— 5 passed, 9 ignoredcargo test --workspace --locked --lib --tests— 862 passed, 6 suitesBackport
The bug arm has been unchanged since
a9dd46e9(2019-06-04, "implementthe new Router") —
git blameon the modified region shows no functionaledits since introduction. All released
sozu-libversions (≤ 1.1.1) areaffected. The patch cherry-picks cleanly to a 1.1.x point release if
maintainers cut one.
Out of scope (filed for separate follow-ups, not this PR)
FromStr for DomainRuleto reject*foo/**patterns(require
*.prefix). The fixedmatchesarm already handles thosepatterns safely.
hostname_and_port).Router::lookup/DomainRule::matches.#![deny(clippy::arithmetic_side_effects)]on the router module.Changelog
### Securityentry under[Unreleased]describing the bug, fix,behavior change, reachability, and affected versions.