Skip to content

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

Closed

Description

@Shine-neko

Hello,

I ran into a panic in the router while wiring up wildcard hostnames.

Summary

DomainRule::Wildcard::matches panics with attempt to subtract with overflow
when the incoming hostname is shorter than the configured wildcard suffix.

The HTTP worker thread dies and the proxy stops serving traffic.

Location

lib/src/router/mod.rs:430 (sozu-lib 1.1.1, also present in earlier versions):

DomainRule::Wildcard(s) => {                                                                                                                                                                                                                
    let len_without_suffix = hostname.len() - s.len() + 1;
    hostname.ends_with(s[1..].as_bytes())
        && !&hostname[..len_without_suffix].contains(&b'.')                                                                                                                                                                                 
}                                                        
                                                                                                                                                                                                                                            
hostname.len() - s.len() underflows (usize) whenever
hostname.len() < s.len().                          

Reproduction

Configuration:

  • Wildcard frontend: *.wildcard-test.localhost (24 bytes)
  • Any other frontend on the same listener

Then send a request whose Host header is shorter than the wildcard:

GET / HTTP/1.1
Host: a.b

The router iterates over all DomainRules for the listener and, when it hits
the wildcard entry, computes 3 - 24 → overflow → panic in debug builds
(release builds happen to be safe because the short-circuit && prevents the
out-of-bounds slice from being evaluated, but the arithmetic is still broken).

In my case I hit this accidentally: the wildcard route was for domain A, but
a different request arrived for domain B on the same listener. During route
matching the wildcard rule was evaluated against B's short Host and panicked.

Impact

  • HTTP worker thread dies.
  • ERROR sozune::proxy::sozu: HTTP worker thread panicked
  • Subsequent requests fail with connection refused.
  • Recovery requires restarting the proxy.

Environment

  • sozu-lib 1.1.1 (from crates.io)
  • Called via sozu-command-lib 1.1.1 frontend routing
  • Rust: rustc 1.94.1
  • Debug build (panic visible). Release builds don't panic here because the
    short-circuit && returns false before the wrapped index is used as a slice
    bound, but this correctness is accidental.

Activity

  1. FlorentinDUBOIS commented on Apr 21, 2026

    @FlorentinDUBOIS
    Collaborator

    Thanks for reporting the issue! I will take a look.

  2. added a commit that references this issue on Apr 28, 2026
    4e102b4
  3. added 2 commits that reference this issue on Aug 10, 2026
    b00ea2e
    63f6576
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions