Skip to content

fix(state): record hot x_real_ip listener patches in the main state - #1694

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/state-x-real-ip-patch
Sep 29, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/state-x-real-ip-patch

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

UpdateHttpListenerConfig / UpdateHttpsListenerConfig carry elide_x_real_ip and send_x_real_ip, and the workers apply them (HttpListener::update_config in lib/src/http.rs, and its HTTPS counterpart), but ConfigState::update_http_listener / ConfigState::update_https_listener (command/src/state.rs) never patched either field. On origin/main (3ed6e8a6; still true after #1686 re-keyed the listener maps):

$ git grep -n 'send_x_real_ip\|elide_x_real_ip' origin/main -- command/src/state.rs
(no output)

So the main state kept the creation-time value: sozu listener list and SaveState showed it, and the state replayed to a new worker on upgrade/restart restored it, silently reverting the hot change.

Fix

Patch both flags in both functions, next to the other shared per-connection knobs (after request_timeout), with the same if let Some(v) form the worker uses. Absent fields preserve the recorded value. Control-plane only: no hot-path allocation or syscall. The insertion point is deliberately away from the tail of the two functions, where #1685 adds forwarded_headers, to keep rebasing trivial.

Tests

Rebased onto 05ac4f9c (#1686 listener-by-interface, #1693). The field writes sit after listener_key_for_update resolves the listener, and the tests look listeners up by ListenerKey::from(addr). RED/GREEN below re-run on the rebased tree.

  • command/src/state.rs: update_http_listener_x_real_ip_patch_applied, update_http_listener_x_real_ip_absent_preserves_existing, update_https_listener_x_real_ip_patch_applied, update_https_listener_x_real_ip_absent_preserves_existing, update_listener_x_real_ip_survives_generate_requests_replay.
  • e2e/src/tests/tests.rs: test_x_real_ip_hot_update_survives_upgrade — an HTTP listener starts with both flags off, a hot UpdateHttpListener turns both on, a spoofed X-Real-IP: 1.2.3.4 request is checked on the old worker, then Worker::upgrade seeds a new worker from the harness's mirror ConfigState (the same ConfigState::update_http_listener the main process runs) and the same request is checked again.

RED (fix reverted)

$ cargo test -p sozu-command-lib --locked x_real_ip
test state::tests::update_https_listener_x_real_ip_patch_applied ... FAILED
test state::tests::update_https_listener_x_real_ip_absent_preserves_existing ... FAILED
test state::tests::update_http_listener_x_real_ip_absent_preserves_existing ... FAILED
test state::tests::update_http_listener_x_real_ip_patch_applied ... FAILED
test state::tests::update_listener_x_real_ip_survives_generate_requests_replay ... FAILED
  left: None
 right: Some(true)
test result: FAILED. 0 passed; 5 failed; 0 ignored; 0 measured; 199 filtered out

$ cargo test -p sozu-e2e --locked test_x_real_ip_hot_update_survives_upgrade
test tests::tests::test_x_real_ip_hot_update_survives_upgrade ... FAILED
after upgrade: spoofed X-Real-IP was not elided:
  left: Fail
 right: Success
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 506 filtered out

(The e2e "before upgrade" check passes on main: the worker applies the patch; only the upgraded worker reverts it.)

GREEN

$ cargo test -p sozu-command-lib --locked x_real_ip
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 199 filtered out
$ cargo test -p sozu-e2e --locked test_x_real_ip_hot_update_survives_upgrade
test tests::tests::test_x_real_ip_hot_update_survives_upgrade ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 506 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-command-lib (204 passed), cargo test -p sozu (121 passed), cargo test -p sozu-lib (1358 passed)
  • e2e: x_real_ip (10 passed), listener_update (14 passed), upgrade (16 passed)
  • check_doc_citations.py --self-test, --root ., --base origin/main, --audit --root .

Docs

  • doc/configure.md: the X-Real-IP section now states that the main state records the patch, so listener list, SaveState and upgrade replay carry it.
  • CHANGELOG.md: Unreleased / Fixed entry.

Out of scope

sozu listener http|https update exposes no CLI flag for either knob (only the protobuf patch does); unchanged here.

Closes #1688

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/state-x-real-ip-patch branch 2 times, most recently from 1f5342c to 67d1f36 Compare September 29, 2026 20:02
UpdateHttpListenerConfig and UpdateHttpsListenerConfig carry
elide_x_real_ip and send_x_real_ip, and the workers apply them, but
ConfigState::update_http_listener and ConfigState::update_https_listener
never wrote either field. The main process therefore kept the value the
listener was created with: `sozu listener list` and SaveState showed it,
and the state replayed to a new worker on upgrade or restart restored
it, silently reverting the hot change.

Patch both flags in both functions like the other optional listener
knobs; an absent field preserves the recorded value.

Tests: state unit tests for patch applied and absent-field preservation
on HTTP and HTTPS listeners, a generate_requests replay test, and an e2e
test that a hot patch survives a worker upgrade (the harness seeds the
new worker from the same ConfigState the main process runs).

Closes #1688

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/state-x-real-ip-patch branch from 67d1f36 to bd694f5 Compare September 29, 2026 21:30
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 9e55ad6 into main Sep 29, 2026
27 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/state-x-real-ip-patch branch September 29, 2026 22:28
FlorentinDUBOIS added a commit that referenced this pull request Sep 30, 2026
…#1708)

Closes #1703

## Problem

`ConfigState::update_http_listener` / `update_https_listener` (`command/src/state.rs`) wrote a patch field by field and validated `alpn_protocols`, `sozu_id_header` and `forwarded_headers` in between. A patch refused on a late field kept every field written before it, so `sozu listener list`, `SaveState` and upgrade replay showed a configuration nobody asked for.

The workers had the same problem. `HttpListener::update_config` and `HttpsListener::update_config` wrote every field into `self.config` before parsing the answer templates. `HttpsListener` also checked `hsts.enabled` last, after swapping in the new rustls context and templates.

Main and workers also disagreed on two patches that main accepted and every worker refused: an unparseable answer template, and an `hsts` block without `enabled`.

## Design

- **Main state:** every check runs before the first write. The lookup order (flood knobs → listener lookup → field checks) and the error variants are unchanged. A new `validate_hsts_patch` refuses `hsts` without `enabled` with the existing `StateError::InvalidValue`.
- **Workers:** validate first, including the hoisted `HstsEnabledRequired` check (same variant) and the patch templates. The patch is then applied to a clone of the listener config, and the rustls context and `HttpAnswers` are built from that clone. Config, context and answers are committed only once everything has succeeded, and the HSTS refresh runs after the commit. The cost is one config clone per patch, on the command path only; nothing touches the hot path.
- **Same verdict on both sides:** a new `HttpAnswers::validate_patch_templates` (`lib/src/protocol/kawa_h1/answers.rs`) is run by the worker and by `validate_listener_request` (`bin/src/command/requests.rs`) before dispatch, since `command` cannot parse templates itself. The main process now refuses exactly the patches the workers refuse, with one exception listed under residual risks.
- **TCP:** nothing can fail after the listener lookup, so it was already all-or-nothing; a test pins it.

## Tests (red → green)

- `update_http_listener_rejected_patch_leaves_the_state_unchanged` and `update_https_listener_rejected_patch_leaves_the_state_unchanged`: red on unmodified code. The hsts case was also shown red on its own by removing the check. `update_tcp_listener_rejected_patch_leaves_the_state_unchanged` pins current behaviour.
- `a_rejected_patch_leaves_the_listener_unchanged` (HTTP and HTTPS workers): red, then green. Checks the config, `Rc`/`Arc` identity of the answers and rustls context, and the HSTS the frontends serve.
- `listener_patches_are_refused_by_the_main_process_exactly_when_a_worker_refuses_them` (bin): compares the main verdict with the worker verdict, and checks that `list_listeners` is unchanged on refusal. Red with the new `validate_listener_request` arms removed.
- e2e `test_rejected_patch_changes_nothing`: red with `lib/src/http.rs` from main (the worker served `rfc7239` after answering `Failure`), green with the fix.

## Validation (after rebasing on 080351b)

- `cargo build --all-features`
- `cargo +stable clippy` and `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings`
- `cargo +nightly fmt --all -- --check`
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked`
- `cargo test -p sozu-command-lib`, `-p sozu`, `-p sozu-lib`
- e2e `listener_update` (16/16) and `listener` (29/29); before the rebase, also `x_real_ip` (10/10) and `forward` (23/23)
- `cd fuzz && cargo +nightly fuzz build`
- `check_doc_citations.py` in its four modes

All exited 0.

Docs are updated in `doc/configure.md` (hot-update section and HSTS partial update), `bin/src/command/LIFECYCLE.md` and `CHANGELOG.md`.

## Residual risks

- `patch.answers` (the map) and `patch.hsts` are validated but **not stored** in the main state, so `SaveState` and replay lose them. This is the same class as #1694 and is left for a follow-up.
- A worker that fails to rebuild its rustls context on an ALPN change keeps its listener unchanged, but the main process has already recorded the patch. Main cannot predict this refusal; it is documented.
- Tightening: a legacy `http_answers` template shadowed only by an entry the listener already holds is now parsed, and refused if it does not parse. Before, it was stored unused.
- When a patch has several invalid fields, the worker may now report a different one first. The error variants are unchanged.



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: hot updates of send_x_real_ip and elide_x_real_ip are not recorded in the main state

1 participant