Skip to content

fix(router,mux,answers): close PR #1162 follow-up gaps on main - #1235

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/redirect-regex-template-drift
Apr 30, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/redirect-regex-template-drift

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Four gaps remained on main after the redirect / URL-rewrite / answer-template / Basic-auth stack landed via the unified mux. PR #1162 ("Redirection and URL rewrite", base main) is structurally superseded — the bulk of its proposal landed via the unified-mux re-implementation — but its last commit 448ece33 Rewriting fixes carried two genuine host-regex correctness fixes that did not make it across, and the same review pass surfaced a clusterless-redirect ordering bug and a doc/code drift on %STATUS_CODE. This PR closes those four gaps.

Changes

Clusterless RedirectPolicy::Permanent now reachable

lib/src/protocol/mux/router.rs::route_from_request previously short-circuited every cluster_id == None route to RetrieveClusterError::UnauthorizedRoute BEFORE the RedirectPolicy::Permanent branch, so a frontend declared with redirect = permanent and no backing cluster (the canonical "this hostname has moved, no service remains" shape from #1161) returned 401 instead of 301. The Permanent branch is now resolved before the clusterless-deny short-circuit.

The Permanent block does not read cluster_id; the cluster-derived knobs (https_redirect_port, www_authenticate, authorized_hashes) already default to safe sentinels at the cluster lookup when cluster_id is None. The let Some(cluster_id) = cluster_id else { unreachable!() } binding moves below the deny block; its invariant still holds because the deny arm guarantees cluster_id is Some by the time control reaches the binding.

Non-trie host regex anchored at both ends

lib/src/router/mod.rs::convert_regex_domain_rule previously emitted an unanchored regex string. The regex crate's Regex::is_match is unanchored by default — per the crate docs, "All searching is done with an implicit (?s:.)*? at the beginning and end of a pattern." So an operator's /example\.com/ regex matched any hostname containing example.com as a substring — attacker.example.com.evil.org, example.commercial, prefixexample.com — letting an attacker-controlled domain reach a frontend that should only serve example.com (CWE-1023, routing bypass).

The compiled regex now opens with \A and closes with \z so Regex::is_match only succeeds on a full-host match. The trie path (lib/src/router/pattern_trie.rs:140-167) was already anchored — its inline comment was prior-art evidence of intent ("Without \A...\z, a pattern like cdn[0-9]+ would match cdn123xxx, which silently widens the routing surface"). The non-trie path now matches that contract.

Multi-segment regex hostnames keep . separators

The inner /-finding loop in convert_regex_domain_rule did not break after the first matching /. For a hostname pattern like /seg1/.foo./seg2/.com, every later / overwrote the segment index, and the literal . separators between regex segments were swallowed into the regex segment itself — producing a regex that matched far more strings than the operator intended. The inner loop now breaks after found = true, so the expected \Aseg1\.foo\.seg2\.com\z is produced.

%STATUS_CODE doc/code drift removed

doc/configure.md, the redirect-template CLI help in bin/src/cli.rs, and the redirect_template doc comments in command/src/command.proto and command/src/config.rs advertised a %STATUS_CODE placeholder that lib/src/protocol/kawa_h1/answers.rs::HttpAnswers::template never defined. An operator who put %STATUS_CODE in a custom template would see the literal string on the wire — the parser passes unknown variable names through as bytes without error. References to the placeholder are stripped so operators no longer see a promised variable that silently no-ops. Implementation of an actual STATUS_CODE variable is deferred to the same change that adds Temporary (302) and 308 for #1009.

Tests

  • Updated convert_regex (existing) for the anchored shape.
  • New regex_domain_rule_rejects_suffix_and_prefix — asserts /example\.com/ does NOT match attacker.example.com, example.com.evil.org, prefixexample.com, example.commercial.
  • New regex_domain_rule_multi_segment_segments_are_isolated — asserts the multi-/.../ pattern produces the correctly-ordered anchored output.
  • New e2e try_clusterless_permanent_redirect_emits_301 — clusterless frontend with redirect = Permanent + rewrite_host = "new.example.com" + redirect_scheme = UseSame asserts HTTP/1.1 301 Moved Permanently and Location: http://new.example.com/path. Regression catches the ordering bug.

Validation

  • cargo build --all-features --locked — built (237 crates, 1m 39s)
  • cargo clippy --all-targets --locked — clean
  • cargo +nightly fmt --all -- --check — clean
  • cargo test --workspace --locked — 955 passed, 7 ignored, 0 failed (5m 53s)
  • cargo test -p sozu-e2e -- redirect — 19 passed (including the new clusterless test)

Issue references

Test plan

  • CI green (cargo build, cargo clippy, cargo +nightly fmt --check, cargo test --workspace).
  • Manual: register a clusterless frontend with redirect = permanent and rewrite_host, send GET to the front address, observe 301 with Location: <scheme>://<rewrite_host><path>.
  • Manual: register a frontend with regex hostname /example\.com/, send GET with Host: attacker.example.com, observe 404 (no match) — not 200/301.
  • Confirm the redirect-template CLI help no longer mentions %STATUS_CODE.

Four gaps identified after the redirect/rewrite/answer-template/auth
stack landed on `main`. PR #1162 ("Redirection and URL rewrite",
@Wonshtrum, base `main`) is structurally superseded by the unified mux
implementation but its last commit `448ece33 Rewriting fixes` carried
two genuine correctness fixes that did not make it to `main`. The
clusterless-redirect ordering bug and the doc/code drift on
`%STATUS_CODE` are independent gaps surfaced by the same review pass.

* Clusterless `RedirectPolicy::Permanent` now reachable. In
  `lib/src/protocol/mux/router.rs::route_from_request` the
  `RedirectPolicy::Permanent` branch is moved ahead of the
  `Unauthorized || cluster_id.is_none()` deny, so a frontend declared
  with `redirect = permanent` and no backing cluster (the canonical
  "this hostname has moved, no service remains" shape from #1161)
  emits 301 instead of 401. The `Permanent` block does not read
  `cluster_id`; the cluster-derived knobs already default to safe
  sentinels at the cluster lookup when `cluster_id` is `None`. The
  `let Some(cluster_id) = cluster_id else { unreachable!() }` binding
  moves below the deny block; its invariant still holds.

* Non-trie host regex anchored at both ends. In
  `lib/src/router/mod.rs::convert_regex_domain_rule` the compiled
  regex now opens with `\A` and closes with `\z`. Without anchors,
  `Regex::is_match` is unanchored, so an operator's `/example\.com/`
  matched any hostname containing `example.com` as a substring,
  including `attacker.example.com.evil.org` — letting an
  attacker-controlled domain reach a frontend that should only serve
  `example.com` (CWE-1023, routing bypass). The trie path
  (`lib/src/router/pattern_trie.rs`) was already anchored.

* Inner `/`-finding loop terminates on first match. Same function;
  the inner loop now `break`s after `found = true`, so a multi-segment
  regex hostname like `/seg1/.foo./seg2/.com` no longer overwrites
  `index` on every later `/` and the literal `.` separators between
  regex segments are kept in their correct position
  (`\Aseg1\.foo\.seg2\.com\z`).

* `%STATUS_CODE` doc/code drift removed. The placeholder was
  advertised in `doc/configure.md`, the `redirect-template` CLI help
  in `bin/src/cli.rs`, and the `redirect_template` doc comments in
  `command/src/command.proto` and `command/src/config.rs`, but
  `lib/src/protocol/kawa_h1/answers.rs::HttpAnswers::template` never
  defined a `STATUS_CODE` variable. References are stripped so
  operators no longer see a promised variable that silently no-ops.
  Implementation deferred to the same change that adds 302/308 for
  #1009.

Tests: `convert_regex` (updated for the anchored shape) plus three new
unit / e2e regressions —
`regex_domain_rule_rejects_suffix_and_prefix`,
`regex_domain_rule_multi_segment_segments_are_isolated`, and
`try_clusterless_permanent_redirect_emits_301` (asserts 301 + Location
on the rewritten host).

Closes parts of #1161 (proposal — clusterless permanent redirect was
the last reachable gap) and #1154 (modify Host header — already
addressed by `rewrite_host`, now closeable). Leaves #1009 partially
open: 302 (`Temporary`) and 308 are still missing on `main`.

Validation:
* cargo build --all-features --locked
* cargo clippy --all-targets --locked
* cargo +nightly fmt --all -- --check
* cargo test --workspace --locked  (955 passed, 7 ignored)
* cargo test -p sozu-e2e -- redirect  (19 passed)

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS FlorentinDUBOIS self-assigned this Apr 30, 2026
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 41b69cc into main Apr 30, 2026
36 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/redirect-regex-template-drift branch April 30, 2026 19:55
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.

1 participant