Repository navigation
fix(router,mux,answers): close PR #1162 follow-up gaps on main - #1235
Merged
Merged
Conversation
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]>
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
Four gaps remained on
mainafter the redirect / URL-rewrite / answer-template / Basic-auth stack landed via the unified mux. PR #1162 ("Redirection and URL rewrite", basemain) is structurally superseded — the bulk of its proposal landed via the unified-mux re-implementation — but its last commit448ece33 Rewriting fixescarried 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::Permanentnow reachablelib/src/protocol/mux/router.rs::route_from_requestpreviously short-circuited everycluster_id == Noneroute toRetrieveClusterError::UnauthorizedRouteBEFORE theRedirectPolicy::Permanentbranch, so a frontend declared withredirect = permanentand no backing cluster (the canonical "this hostname has moved, no service remains" shape from #1161) returned 401 instead of 301. ThePermanentbranch is now resolved before the clusterless-deny short-circuit.The
Permanentblock does not readcluster_id; the cluster-derived knobs (https_redirect_port,www_authenticate,authorized_hashes) already default to safe sentinels at the cluster lookup whencluster_idisNone. Thelet Some(cluster_id) = cluster_id else { unreachable!() }binding moves below the deny block; its invariant still holds because the deny arm guaranteescluster_idisSomeby the time control reaches the binding.Non-trie host regex anchored at both ends
lib/src/router/mod.rs::convert_regex_domain_rulepreviously emitted an unanchored regex string. Theregexcrate'sRegex::is_matchis 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 containingexample.comas a substring —attacker.example.com.evil.org,example.commercial,prefixexample.com— letting an attacker-controlled domain reach a frontend that should only serveexample.com(CWE-1023, routing bypass).The compiled regex now opens with
\Aand closes with\zsoRegex::is_matchonly 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 likecdn[0-9]+would matchcdn123xxx, which silently widens the routing surface"). The non-trie path now matches that contract.Multi-segment regex hostnames keep
.separatorsThe inner
/-finding loop inconvert_regex_domain_ruledid notbreakafter 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 nowbreaks afterfound = true, so the expected\Aseg1\.foo\.seg2\.com\zis produced.%STATUS_CODEdoc/code drift removeddoc/configure.md, theredirect-templateCLI help inbin/src/cli.rs, and theredirect_templatedoc comments incommand/src/command.protoandcommand/src/config.rsadvertised a%STATUS_CODEplaceholder thatlib/src/protocol/kawa_h1/answers.rs::HttpAnswers::templatenever defined. An operator who put%STATUS_CODEin 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 actualSTATUS_CODEvariable is deferred to the same change that addsTemporary(302) and 308 for #1009.Tests
convert_regex(existing) for the anchored shape.regex_domain_rule_rejects_suffix_and_prefix— asserts/example\.com/does NOT matchattacker.example.com,example.com.evil.org,prefixexample.com,example.commercial.regex_domain_rule_multi_segment_segments_are_isolated— asserts the multi-/.../pattern produces the correctly-ordered anchored output.try_clusterless_permanent_redirect_emits_301— clusterless frontend withredirect = Permanent+rewrite_host = "new.example.com"+redirect_scheme = UseSameassertsHTTP/1.1 301 Moved PermanentlyandLocation: 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— cleancargo +nightly fmt --all -- --check— cleancargo 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
rewrite_host).Permanent) and 401 (Unauthorized) are supported; 302 (Temporary) and 308 are still missing onmainand remain a follow-up.devel/edemolis/feat/redirections) — superseded by main and now by this follow-up; safe to close once merged.Test plan
cargo build,cargo clippy,cargo +nightly fmt --check,cargo test --workspace).redirect = permanentandrewrite_host, send GET to the front address, observe 301 withLocation: <scheme>://<rewrite_host><path>./example\.com/, send GET withHost: attacker.example.com, observe 404 (no match) — not 200/301.redirect-templateCLI help no longer mentions%STATUS_CODE.