Skip to content

fix(listener): resolve HSTS inheritance against each listener's own default - #1697

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/hsts-inheritance-on-copied-frontends
Sep 29, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/hsts-inheritance-on-copied-frontends

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

What

A policy-carrying HTTPS frontend (tags, headers, redirect, rewrite, auth) with no hsts block of its own, added while its listener had no HSTS default, was stored with inherits_listener_hsts = false. Two consequences:

Both contradict the documented contract (doc/configure.md, HSTS hot-reconfig): a frontend with no per-frontend [hsts] block at add time inherits the listener default.

Fix

  • HttpsProxy::add_https_frontend (lib/src/https.rs) now records the frontend's own intent. If the frontend declared no hsts, the origin is HstsOrigin::InheritedFromListenerDefault, and front.hsts takes the listener default, which may be None. It no longer checks whether a default existed.
  • Frontend::new (lib/src/router/mod.rs) drops the && hsts.is_some() condition, so inherits_listener_hsts reflects only that intent.
  • Router::refresh_inheriting_hsts is unchanged. It already runs on the copy in add_listener and on every listener HSTS patch, and it now reaches these frontends too.

No new allocation, and nothing changes on the request path. The only extra work is the refresh walk, which now also rebuilds these frontends during a listener patch or a listener add.

Behaviour change on a single listener

This affects a policy-carrying frontend with no hsts of its own, added before the listener had an HSTS default. When a patch later enables the default, that frontend now gets the header. Before, it did not; frontends without any policy already did, through route promotion. Two cases are unchanged:

  • A frontend with its own hsts (including enabled = false).
  • A frontend added while the listener already had a default.

Tests (lib/src/https.rs, listener_sibling_tests)

  • a_copied_frontend_without_hsts_inherits_the_new_listener_default: two HTTPS listeners on one address (the second on interface = "lo"). A tagged frontend without hsts is added while the first listener has no default, then the second listener is added with max-age=100. Each listener must serve its own default, including after a separate patch to each listener.
  • a_frontend_with_explicit_hsts_is_never_overridden_by_a_listener_default: guard. A frontend with explicit hsts keeps it on both listeners, at copy time and after patches. It is green both before and after the fix.
  • a_listener_hsts_patch_refreshes_every_frontend_without_its_own_hsts: a single listener, starting with no default and with a default. A patch that enables HSTS reaches the tagged frontend, and a patch that disables it removes the header.

RED on origin/main (d9ce2a0) with the tests only:

test https::listener_sibling_tests::a_listener_hsts_patch_refreshes_every_frontend_without_its_own_hsts ... FAILED
test https::listener_sibling_tests::a_copied_frontend_without_hsts_inherits_the_new_listener_default ... FAILED
test https::listener_sibling_tests::a_frontend_with_explicit_hsts_is_never_overridden_by_a_listener_default ... ok
assertion `left == right` failed
  left: None
 right: Some("max-age=700")
assertion `left == right` failed: the listener added after the frontend must serve its own HSTS default
  left: None
 right: Some("max-age=100")
test result: FAILED. 5 passed; 2 failed; 0 ignored; 0 measured; 1354 filtered out

GREEN with the fix:

test https::listener_sibling_tests::a_copied_frontend_without_hsts_inherits_the_new_listener_default ... ok
test https::listener_sibling_tests::a_listener_hsts_patch_refreshes_every_frontend_without_its_own_hsts ... ok
test https::listener_sibling_tests::a_frontend_with_explicit_hsts_is_never_overridden_by_a_listener_default ... ok
test result: ok. 1361 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Validation (each by its own exit code, all 0)

  • cargo build --all-features
  • cargo clippy --all-features --all-targets -- -D warnings
  • cargo +nightly fmt --all -- --check
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked
  • cargo test -p sozu-lib: 1371 passed
  • cargo test -p sozu: 242 passed
  • cargo test -p sozu-command-lib: 210 passed
  • e2e cargo test -p sozu-e2e -- <group>: hsts 7, https 6, tls_tests 9, listener 27, all passed
  • check_doc_citations.py in all four modes: --self-test, --root ., --base origin/main, --audit --root .

Docs

  • doc/configure.md, HSTS hot-reconfig: defines an inheriting frontend and describes how a listener added on a shared address resolves inheritance.
  • CHANGELOG.md, Unreleased / Fixed.
  • doc/configure_admin_ops.md: one line citation renumbered (doc/configure.md:1266 becomes :1268), because the docs insertion moved that line.

Not addressed (pre-existing, follow-ups)

Closes #1691

…efault

A policy-carrying frontend (tags, headers, redirect, rewrite, auth) with
no hsts block of its own, added while its HTTPS listener had no HSTS
default, was recorded as an explicit override (inherits_listener_hsts =
false). A listener later added on the same address copied it without
its own default, and a later listener HSTS patch did not reach it,
contrary to the documented contract that every frontend without a
per-frontend [hsts] block at add time inherits the listener default.

add_https_frontend now records the frontend's own intent (no hsts
declared) instead of whether a default existed, and Frontend::new no
longer requires a resolved value, so Router::refresh_inheriting_hsts
resolves it against each listener's default, both when
HttpsProxy::add_listener copies a sibling's routes and on every later
patch. Explicit per-frontend hsts, including enabled = false, is never
overridden.

Behaviour change on a single listener: such a frontend added before
the listener had an HSTS default now receives the header once a patch
enables it, as frontends without any policy already did.

Closes #1691

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 6c9b566 into main Sep 29, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/hsts-inheritance-on-copied-frontends branch September 29, 2026 19:04
FlorentinDUBOIS added a commit that referenced this pull request Sep 29, 2026
#1695)

Closes #1689

## Problem (reproduced)

kawa's H1 parser pushes a chunked request's trailer fields after its last chunk with no callback, so `HttpContext::on_request_headers` never sees them. On `main` (`3ed6e8a6`) the new e2e test shows the backend receiving them verbatim:

```
TRAILER-SPOOF: backend received "POST /api HTTP/1.1\r\n...X-Forwarded-For: 127.0.0.1\r\n...\r\n\r\n5\r\nHello\r\n0\r\nX-Forwarded-For: 6.6.6.6\r\nForwarded: for=6.6.6.6\r\nX-Real-IP: 6.6.6.6\r\nX-Request-Id: 6.6.6.6\r\nX-Forwarded-Proto: 6.6.6.6\r\nX-Forwarded-Port: 6.6.6.6\r\nX-Forwarded-Host: 6.6.6.6\r\nGrpc-Status: 0\r\n\r\n"
```

A backend that merges trailers into its header view then sees a forged client address (RFC 9110 §6.5.1, CWE-348).

## Fix

- `TRAILER_SPOOF_VECTOR_HEADERS` (`lib/src/protocol/kawa_h1/editor.rs`) is now the **single list** for both frontends: `x-real-ip`, `x-forwarded-for`, `forwarded`, `x-request-id`, `x-forwarded-proto`, `x-forwarded-port`, `x-forwarded-host`. `pkawa::handle_trailer` reads it instead of the inline seven-name `matches!` that #1685 (merged as `5471bfba`) added, so H1 and H2 cannot drift. H2 behaviour is unchanged. This branch is rebased on `origin/main` (`a6419607`), which includes #1685, #1696, #1697 and #1699. The `editor.rs` conflict with #1696 (both appended tests at the end of the test module) keeps both sets of tests, and the `pkawa.rs` comment keeps #1699's mode wording while naming the shared constant. The `pkawa.rs` conflict was resolved in favour of the shared constant, keeping #1685's `forwarded_headers`-mode wording in the doc comments, the test rationale and the `doc/configure.md` paragraph (which now also names the H1 path).
- `elide_request_trailer_spoof_vectors` (same file) runs after both frontend `kawa::h1::parse` sites of `ConnectionH1`: `readable`, and the keep-alive branch of `writable` that parses a pipelined request. Each call site reads `kawa.blocks.len()` just before the parse and passes it as `first_new_block`. The walk goes back only over the blocks that parse appended (closing `Flags` + trailer fields), stopping at the `end_body` marker or at `first_new_block`. It elides matching names in any case with `Pair::elide`.
- **Linear cost (review item):** a parse only appends blocks and `prepare` only drains from the front between parses, so every older field was already examined by the call after the parse that queued it. A client that trickles one trailer line per segment while the backend isn't writable costs one block per line, not an O(n²) re-walk. This holds when earlier trailer fields were already drained to the backend.
- **Framing:** kawa's H1 and H2 converters skip elided pairs. The last chunk and the closing empty line are still written, so the framing stays valid even when every field is dropped. The e2e test asserts `...0\r\nGrpc-Status: 0\r\n\r\n` on the wire.
- **Cost:** the function returns on the `kind` / `body_size == Chunked` / `parsing_phase ∈ {Trailers, Terminated}` checks before reading any block. Requests without trailers pay three field compares.
- **Responses are not filtered.** A response trailer goes to the client, which does not take client attribution from it, so the spoofing risk this issue describes does not apply.
- New counter `http.trailer.spoof_vector_elided` (one increment per elided H1 field), documented in `doc/configure.md`.
- Docs: `doc/configure.md` (anti-spoof paragraph + metric row), `lib/src/protocol/kawa_h1/LIFECYCLE.md` §2.3, `CHANGELOG.md` (Unreleased / Security).

## Tests (red, then green)

New tests: `a_chunked_request_trailer_section_loses_its_spoof_vector_fields`, `a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered`, `a_trailer_section_split_across_reads_is_filtered_in_each_part`, `a_trailer_walk_stops_at_the_blocks_queued_before_the_parse`, `trailer_elision_leaves_headers_and_responses_alone` (editor unit tests). The e2e tests are `test_h1_trailer_spoof_headers_dropped`, `test_h1_trailer_spoof_headers_dropped_split` (trailers in a separate write), `test_h1_trailer_spoof_headers_dropped_split_inside_trailers` (`X-Forwarded-For` in one write, the rest in the next) and `test_h1_pipelined_trailer_spoof_headers_dropped`. `handle_trailer_drops_each_spoof_header_individually` now iterates the shared list.

RED, e2e on unfixed code:
```
TRAILER-SPOOF: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped ... FAILED
TRAILER-SPOOF-SPLIT: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split ... FAILED
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 504 filtered out
```
RED, unit tests: `elide_request_trailer_spoof_vectors` stubbed to `return 0`, and pkawa restored to its old four-name `matches!`:
```
test protocol::mux::pkawa::tests::handle_trailer_drops_each_spoof_header_individually ... FAILED
test protocol::kawa_h1::editor::tests::a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered ... FAILED
test protocol::kawa_h1::editor::tests::a_chunked_request_trailer_section_loses_its_spoof_vector_fields ... FAILED
test protocol::kawa_h1::editor::tests::trailer_elision_leaves_headers_and_responses_alone ... ok
trailer with only x-forwarded-proto should leave no surviving block
test result: FAILED. 1 passed; 3 failed; 0 ignored; 0 measured; 1348 filtered out
```
RED, pipelined path: only the keep-alive call site in `mux/h1.rs` disabled:
```
TRAILER-SPOOF-PIPELINED: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_pipelined_trailer_spoof_headers_dropped ... FAILED
```
RED, review items after the rebase:

Walk ignoring `first_new_block` (`range_mut(0..)`). The function's own post-condition `debug_assert!` ("only blocks appended by the last parse are elided") fires before the test's assertion:
```
test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... ok
test protocol::kawa_h1::editor::tests::a_trailer_walk_stops_at_the_blocks_queued_before_the_parse ... FAILED
test result: FAILED. 1 passed; 1 failed
```
Elision disabled (`return 0`):
```
test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... FAILED
assertion `left == right` failed: the first trailer part is filtered on its own
  left: 0
 right: 1
TRAILER-SPOOF-SPLIT-INSIDE: FAIL — a spoofed forwarding trailer reached the backend
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split_inside_trailers ... FAILED
```

GREEN:
```
test protocol::mux::pkawa::tests::handle_trailer_drops_each_spoof_header_individually ... ok
test protocol::kawa_h1::editor::tests::a_chunked_request_trailer_section_loses_its_spoof_vector_fields ... ok
test protocol::kawa_h1::editor::tests::a_trailer_section_parsed_after_the_last_chunk_was_forwarded_is_filtered ... ok
test protocol::kawa_h1::editor::tests::trailer_elision_leaves_headers_and_responses_alone ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split ... ok
test tests::h1_security_tests::test_h1_trailer_spoof_headers_dropped_split_inside_trailers ... ok
test protocol::kawa_h1::editor::tests::a_trailer_section_split_across_reads_is_filtered_in_each_part ... ok
test protocol::kawa_h1::editor::tests::a_trailer_walk_stops_at_the_blocks_queued_before_the_parse ... ok
test tests::h1_security_tests::test_h1_pipelined_trailer_spoof_headers_dropped ... ok
```

## Validation (each by its own exit code)

- `cargo build --all-features`: 0
- `cargo clippy --all-features --all-targets -- -D warnings`: 0
- `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` (MSRV): 0
- `cargo +nightly fmt --all -- --check`: 0
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked`: 0
- `cargo test -p sozu-lib`: 0 (1381 passed)
- `cargo test -p sozu`: 0
- `cargo test -p sozu-command-lib`: 0
- `cargo test -p sozu-e2e -- trailer h1_security mux_tests forward connection`: 0 (104 passed)
- `check_doc_citations.py` `--self-test` / `--root .` / `--base origin/main` / `--audit --root .`: all 0 (the audit's three advisory findings are in files this PR does not touch)

## Residual risks

- The advisory `Trailer:` request header still lists the dropped names. It is informational only, and this PR leaves it as sent.
- An H2-frontend request forwarded to an H1 backend goes through `pkawa::handle_trailer`, not this walk. It uses the same list.
- The bounded walk assumes kawa 0.7.2's invariants: parsing only appends blocks, and draining happens only from the front between parses. A debug post-condition guards this.




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.

listener: a frontend copied to a new listener on the same address does not inherit that listener's HSTS default

1 participant