Skip to content

fix(router): prevent panic in DomainRule::Wildcard::matches - #1232

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/router-wildcard-overflow
Apr 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/router-wildcard-overflow

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

DomainRule::Wildcard::matches in lib/src/router/mod.rs computed
hostname.len() - s.len() + 1 unconditionally, underflowing usize and
panicking the HTTP worker thread with attempt to subtract with overflow
whenever 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 on
ends_with(suffix), so the false-positive in §1 of the issue is rare —
but the latent OOB slice would itself panic if ends_with ever returned
true on the wrapped value. CWE-191 → CWE-754, remote-reachable DoS.

Closes #1223.

Fix

Replace the unguarded subtraction with <[u8]>::strip_suffix and
validate the leftmost-prefix invariants on the returned slice:

DomainRule::Wildcard(s) => {
    let suffix = &s.as_bytes()[1..];
    hostname
        .strip_suffix(suffix)
        .is_some_and(|prefix| !prefix.is_empty() && !prefix.contains(&b'.'))
}

There is no length arithmetic to underflow. !prefix.is_empty() enforces
"leftmost label must be non-empty" — the boundary Host: .foo.example.com
against *.foo.example.com is now rejected as an invalid DNS label per
RFC 1035 §3.1. !prefix.contains(&b'.') preserves single-label wildcard
semantics. A /// doc-comment on enum DomainRule::Wildcard records the
implementation contract.

Behavior changes

Hostname vs *.foo.example.com Before After
b"", b"x", b"a.b" panic in debug, false in release (wrap masked by && short-circuit on ends_with) false
b".foo.example.com" true (buggy: empty prefix slice) false (empty leftmost label rejected)
b"x.foo.example.com" true true (unchanged)
b"y.x.foo.example.com" false false (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

  • H1: HttpListener::frontend_from_request → Router::lookup → linear
    pre/post DomainRule::matches. hostname_and_port accepts an empty
    hostname via take_while, so a literal empty Host: reaches the bug.
  • H2: :authority is rejected when empty by pkawa.rs, but a 1-byte
    authority still triggered the underflow against any wildcard of length
    ≥ 3.
  • Operator-driven: Router::has_hostname (frontend-removal cleanup)
    shares the arm and benefits identically.

The trie path (pattern_trie::lookup) has its own is_empty() guard and
is not affected.

Tests

Two new unit tests in lib/src/router/mod.rs:

  • match_domain_rule_wildcard_short_hostname_does_not_panic exercises
    DomainRule::matches directly with b"", b"x", b"a.b", the
    boundary b".foo.example.com", multi-label b"y.x.foo.example.com",
    and the canonical happy case b"x.foo.example.com". The first three
    would panic in debug builds on main today.
  • router_lookup_wildcard_pre_rule_short_hostname_does_not_panic
    exercises the full Router::lookup integration with a Wildcard
    pre-rule. The pre-existing match_router test only puts Wildcard
    in add_tree_rule (which routes through pattern_trie::lookup and
    bypasses DomainRule::Wildcard::matches), so this new test closes
    the 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 --locked
  • cargo +nightly fmt --all -- --check
  • cargo clippy --all-targets --locked — no issues
  • cargo test -p sozu-lib --lib router::tests — 20 passed (was 18; +2 new)
  • cargo test --workspace --doc --locked — 5 passed, 9 ignored
  • cargo test --workspace --locked --lib --tests — 862 passed, 6 suites

Backport

The bug arm has been unchanged since a9dd46e9 (2019-06-04, "implement
the new Router") — git blame on the modified region shows no functional
edits since introduction. All released sozu-lib versions (≤ 1.1.1) are
affected. 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)

  • Tighten FromStr for DomainRule to reject *foo / ** patterns
    (require *. prefix). The fixed matches arm already handles those
    patterns safely.
  • Reject empty hostname at the H1 parser layer (hostname_and_port).
  • Add a libfuzzer target over Router::lookup / DomainRule::matches.
  • Enable #![deny(clippy::arithmetic_side_effects)] on the router module.

Changelog

### Security entry under [Unreleased] describing the bug, fix,
behavior change, reachability, and affected versions.

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]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit e3fa163 into main Apr 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/router-wildcard-overflow branch April 28, 2026 12:03
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
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.

Panic in DomainRule::Wildcard::matches — "attempt to subtract with overflow" when hostname shorter than wildcard

1 participant