Skip to content

Redirection and URL rewrite - #1162

Closed
Wonshtrum wants to merge 18 commits into
mainfrom
devel/edemolis/feat/redirections
Closed

Wonshtrum wants to merge 18 commits into
mainfrom
devel/edemolis/feat/redirections

Conversation

@Wonshtrum

Copy link
Copy Markdown
Member

Implement redirections and URL rewrite proposal of #1161.

@Wonshtrum
Wonshtrum force-pushed the devel/edemolis/feat/redirections branch from 6701c49 to 663b67f Compare December 17, 2024 20:30
@Wonshtrum
Wonshtrum marked this pull request as ready for review December 19, 2024 14:47
Signed-off-by: Eloi DEMOLIS <[email protected]>
@Wonshtrum
Wonshtrum force-pushed the devel/edemolis/feat/redirections branch from 7b846d1 to 28ea2c6 Compare January 8, 2025 09:42
I feel like it's going nowhere...
Backend logic is messy and unclear, buffers are needlessly kept alive,
borrowing subsets of the HTTP session is harder and harder, reseting
default answers request is way too hard for what it's worth (it's
probably broken right now), gauges are desynched and we are cloning
again and again String ids...

Signed-off-by: Eloi DEMOLIS <[email protected]>
@Wonshtrum
Wonshtrum force-pushed the devel/edemolis/feat/redirections branch from 28ea2c6 to 33513c4 Compare January 8, 2025 09:43
- Fix non idempotent connect_to_backend (probably suboptimal)
- Explicitely exclude body when no length info is received, add a
  `Content-Length: 0` to avoid message smuggling (may break some apps?)

Signed-off-by: Eloi DEMOLIS <[email protected]>
- Use Sha256 hashes
- Add WWW_AUTHENTICATE variable to 401 templates

Signed-off-by: Eloi DEMOLIS <[email protected]>
- set default frontend rule position to TREE for config
- remove debug warnings
- apply clippy suggestions

Signed-off-by: Eloi DEMOLIS <[email protected]>
- Move tags from L7 listeners to L7 frontends
- Keep tags on L4 listeners assuming a single frontend

Signed-off-by: Eloi DEMOLIS <[email protected]>
- fix path regex captures indexes
- replace missing optional captures by empty string (instead of panic)
- skip first dot in host subdomain regex
- force full match on host regex
- use cluster https port for custom https redirections
- add a config to test regex rewrites

Signed-off-by: Eloi DEMOLIS <[email protected]>
@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closing as superseded by the unified-mux re-implementation that landed on main since this PR was opened. Each proposal item from #1161 has a corresponding home on main; pointers below for reference.

Where the proposal landed on main

  • Per-frontend redirect / redirect_scheme / rewrite_host / rewrite_path / rewrite_port / redirect_template — command/src/command.proto::RequestHttpFrontend carries the new fields and the RedirectPolicy / RedirectScheme enums; lib/src/router/mod.rs::Frontend + RouteResult + RewriteParts parse them at frontend registration; lib/src/protocol/mux/router.rs::route_from_request is the dispatch chokepoint (covers H1 and H2 frontends through the unified mux).
  • Per-listener / per-cluster answers map and the template variables (%ROUTE, %REQUEST_ID, %CLUSTER_ID, %REDIRECT_LOCATION, %TEMPLATE_NAME, plus diagnostic variables on the error paths) — lib/src/protocol/kawa_h1/answers.rs::HttpAnswers (template engine + per-cluster + per-listener override + frontend redirect_template + fallback chain). On a RedirectPolicy::Permanent request the frontend's redirect_template is compiled on demand via HttpAnswers::render_inline_301, with a fall-back to the listener default on compile failure.
  • Cluster https_redirect + https_redirect_port — kept as the legacy HTTP→HTTPS short-circuit at lib/src/protocol/mux/router.rs; resolved BEFORE per-(cluster, source-IP) accounting so a redirect-only request never consumes an IP slot.
  • Clusterless frontends — legacy Forward + None defaults to 401, preserving the prior deny-by-default. Clusterless Permanent (the "moved permanently to a different system" shape from this issue's discussion) is reachable as of fix(router,mux,answers): close PR #1162 follow-up gaps on main #1235.
  • $HOST[n] / $PATH[n] capture grammar — lib/src/router/mod.rs::RewriteParts parses the template at registration time and validates indices against the rule's capture cap, so out-of-bounds references fail config-load rather than at runtime. Missing optional captures substitute to empty string.
  • e2e cardinality matrix — e2e/src/tests/redirect_rewrite_auth_tests.rs covers H1↔H1, H1↔H2, H2↔H1 and H2↔H2 across redirect, rewrite, basic-auth and header injection.

Regex correctness fixes from 448ece33

The two host-regex correctness fixes from this branch's last commit (Rewriting fixes, 2025-10-02) — \A/\z anchoring of convert_regex_domain_rule output and the break in the inner /-finding loop — landed in #1235 with the matching regression tests. The unanchored-regex routing-bypass (operator's /example\.com/ matching attacker.example.com.evil.org) and the multi-segment hostname mis-parse (/seg1/.foo./seg2/.com swallowing . separators) were both genuine gaps on main.

The two moschroe regressions (#1161 thread, June 2025) — synthetic capture-group-0 indexing and the missing-optional-capture panic — are addressed defensively on main at parse time via RewriteParts::parse (rejects out-of-bounds indices at registration) and at runtime via empty-string substitution.

Issues

Thank you for the original proposal and the implementation branch — the Frontend / RouteResult / RewriteParts shape and the template-variable schema directly informed the main-side design.

FlorentinDUBOIS added a commit that referenced this pull request Apr 30, 2026
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 added a commit that referenced this pull request May 2, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks —
`## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs #1162, #1191, #1192,
#1193, #1194, #1196, #1202, #1223), `## feat/answer-templates-rewrite-auth -
Unreleased` (answer templates, URL rewrite, basic auth), and
`## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR #1209). All
three feature stacks are on `main`; the per-branch dividers were release-
engineering staging that no longer reflects current state.

Changes:
- Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and
  `## feat/h2-mux - Unreleased` dividers; their bodies now appear under
  `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`,
  `### Added — H2 multiplexer (PR #1209)`).
- Promote the breaking changes (proto-tag renumbers, dashboard metric renames,
  `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header
  semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to
  a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of
  the section so operators see them first.
- Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed,
  ✍️ → Changed, 📚 → Documentation, 🤖 → CI, ⚠️ → Behaviour-affecting). Per-
  commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they
  don't conflict with Keep-a-Changelog vocabulary.
- Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim;
  they are released and immutable.

Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut
in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple
`### Added`/`### Changed`/`### Fixed` sub-blocks into one each per
Keep-a-Changelog strict shape; this commit is the structural collapse only.

Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse).
Signed-off-by: Florentin Dubois <[email protected]>
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
Four gaps identified after the redirect/rewrite/answer-template/auth
stack landed on `main`. PR sozu-proxy#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 sozu-proxy#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
  sozu-proxy#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 sozu-proxy#1161 (proposal — clusterless permanent redirect was
the last reachable gap) and sozu-proxy#1154 (modify Host header — already
addressed by `rewrite_host`, now closeable). Leaves sozu-proxy#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]>
Co-authored-by: Eloi Démolis <[email protected]>
Co-authored-by: hcaumeil <[email protected]>
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks —
`## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs sozu-proxy#1162, sozu-proxy#1191, sozu-proxy#1192,
sozu-proxy#1193, sozu-proxy#1194, sozu-proxy#1196, sozu-proxy#1202, sozu-proxy#1223), `## feat/answer-templates-rewrite-auth -
Unreleased` (answer templates, URL rewrite, basic auth), and
`## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR sozu-proxy#1209). All
three feature stacks are on `main`; the per-branch dividers were release-
engineering staging that no longer reflects current state.

Changes:
- Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and
  `## feat/h2-mux - Unreleased` dividers; their bodies now appear under
  `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`,
  `### Added — H2 multiplexer (PR sozu-proxy#1209)`).
- Promote the breaking changes (proto-tag renumbers, dashboard metric renames,
  `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header
  semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to
  a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of
  the section so operators see them first.
- Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed,
  ✍️ → Changed, 📚 → Documentation, 🤖 → CI, ⚠️ → Behaviour-affecting). Per-
  commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they
  don't conflict with Keep-a-Changelog vocabulary.
- Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim;
  they are released and immutable.

Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut
in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple
`### Added`/`### Changed`/`### Fixed` sub-blocks into one each per
Keep-a-Changelog strict shape; this commit is the structural collapse only.

Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse).
Signed-off-by: Florentin Dubois <[email protected]>
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.

2 participants