Repository navigation
fix(state): record hot x_real_ip listener patches in the main state - #1694
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 29, 2026 17:07
FlorentinDUBOIS
force-pushed
the
fix/state-x-real-ip-patch
branch
2 times, most recently
from
September 29, 2026 20:02
1f5342c to
67d1f36
Compare
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
force-pushed
the
fix/state-x-real-ip-patch
branch
from
September 29, 2026 21:30
67d1f36 to
bd694f5
Compare
This was referenced Sep 29, 2026
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]>
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.
Problem
UpdateHttpListenerConfig/UpdateHttpsListenerConfigcarryelide_x_real_ipandsend_x_real_ip, and the workers apply them (HttpListener::update_configinlib/src/http.rs, and its HTTPS counterpart), butConfigState::update_http_listener/ConfigState::update_https_listener(command/src/state.rs) never patched either field. Onorigin/main(3ed6e8a6; still true after #1686 re-keyed the listener maps):So the main state kept the creation-time value:
sozu listener listandSaveStateshowed 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 sameif 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 addsforwarded_headers, to keep rebasing trivial.Tests
Rebased onto
05ac4f9c(#1686 listener-by-interface, #1693). The field writes sit afterlistener_key_for_updateresolves the listener, and the tests look listeners up byListenerKey::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 hotUpdateHttpListenerturns both on, a spoofedX-Real-IP: 1.2.3.4request is checked on the old worker, thenWorker::upgradeseeds a new worker from the harness's mirrorConfigState(the sameConfigState::update_http_listenerthe main process runs) and the same request is checked again.RED (fix reverted)
(The e2e "before upgrade" check passes on
main: the worker applies the patch; only the upgraded worker reverts it.)GREEN
Validation (each by its own exit code, all 0)
cargo build --all-featurescargo clippy --all-features --all-targets -- -D warningscargo +nightly fmt --all -- --checkRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedcargo test -p sozu-command-lib(204 passed),cargo test -p sozu(121 passed),cargo test -p sozu-lib(1358 passed)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, solistener list,SaveStateand upgrade replay carry it.CHANGELOG.md: Unreleased / Fixed entry.Out of scope
sozu listener http|https updateexposes no CLI flag for either knob (only the protobuf patch does); unchanged here.Closes #1688