Skip to content

fix(router): report removal failures instead of silently succeeding - #1330

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/router-equality
Sep 18, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/router-equality

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Impact

Two defects let a frontend removal answer Ok while leaving the route in place and still routing traffic.

PathRule equality was not reflexive

The hand-written PartialEq had arms for Prefix and Regex only, so (Equals, Equals) fell into _ => false and two identical PathRule::Equals compared unequal. remove_tree_rule's retain then matched nothing and returned true unconditionally, so remove_http_front reported success for a path-equals frontend it had not removed. The same asymmetry meant such a frontend could never be deduplicated on add.

Pre/Post removal returns false → RouterError::RemoveRoute, so those failed loudly; only the Tree path was silent.

Regex domain removal took out siblings

TrieNode::remove_recursive diverged between its two regex-segment arms: the pos > 0 arm never pruned an emptied subtree, and the pos == 0 arm retained on the regex alone, discarding every sibling domain under the same segment. Both arms now recurse into the matching subtree and drop the entry only once that subtree is empty.

Removal uses Vec::remove rather than swap_remove deliberately: lookup and lookup_with_path both return on the first matching regex, which makes regexps order part of routing behaviour.

Observable API change

A duplicate --path-equals AddHttpFrontend now returns RouterError::AddRoute instead of silently storing a second, unreachable copy. This is the answer Prefix and Regex frontends have always given, so state replay becomes consistent rather than newly strict. Reviewed for dependants: no existing test, e2e or doc relied on the old silent success, and the command path cannot reach it anyway because ConfigState::add_http_frontend already refuses a duplicate route key with Exists.

Why CI never caught this

add_tree_rule carries a pre-existing assertion that a freshly inserted tree domain resolves to its inserted rule. On main that fires on the first Equals add in a debug build — so main being green is itself proof that no existing test ever added a path-equals frontend through the router.

Tests

Three red-then-green observations, each with a To SEE THIS RED: block naming its mutation; an independent reviewer re-executed two of them and reproduced both documented panics verbatim.

Checked and confirmed while making the change: PathRule carries neither an Eq nor a Hash impl and is used as no map or set key, so restoring reflexivity introduces no eq/hash inconsistency.

The nine existing assertions these defects tripped are unchanged — only their causes are fixed. Unlike the wire-derived assertions elsewhere in this batch, these are correct and each has a release-mode consequence.

Commands run

cargo +1.93.1 build --locked --all-features
cargo +1.93.1 clippy --all-targets --locked -- -D warnings
cargo +nightly fmt --all -- --check
cargo +1.93.1 test --workspace --locked      # 1544 passed / 0 failed / 12 ignored
RUSTFLAGS="--cfg tokio_unstable" cargo +1.93.1 test -p sozu-sim --locked
cargo +1.93.1 bench --no-run --locked

plus the msrv-full cell including the full e2e suite.

Docs

doc/configure.md and doc/configure_cli.md describe path matching semantics, which are unchanged; neither documents add/remove equality or trie pruning. CHANGELOG.md updated.

Adjacent pre-existing bug, not fixed here

While reviewing this branch, insert_recursive's pos == 0 arm was found to return InsertResult::Existing when the regex entry already exists without checking that the subtree holds a value — so inserting the deeper domain first makes the leftmost-regex host permanently un-insertable. insert_recursive is untouched by this diff, and the new sibling test picks the working insertion order, so it is not covered here. Flagged for a separate change.

Two defects let a frontend removal answer `Ok` while leaving the route in
place and still routing traffic.

`PathRule`'s hand-written `PartialEq` had arms for `Prefix` and `Regex`
only, so `(Equals, Equals)` fell into `_ => false` and two identical
`PathRule::Equals` compared unequal. `remove_tree_rule`'s `retain` then
matched nothing and returned `true` unconditionally, so `remove_http_front`
reported success for a path-equals frontend it had not removed. The same
asymmetry meant such a frontend could never be deduplicated on add. A
duplicate `--path-equals` `AddHttpFrontend` now returns
`RouterError::AddRoute`, which is the answer `Prefix` and `Regex`
frontends have always given.

`PathRule` carries neither an `Eq` nor a `Hash` impl and is used as no map
or set key, so restoring reflexivity introduces no `eq`/`hash`
inconsistency.

`TrieNode::remove_recursive` diverged between its two regex-segment arms:
the `pos > 0` arm never pruned an emptied subtree, and the `pos == 0` arm
retained on the regex alone, discarding every sibling domain under the
same segment. Both arms now recurse into the matching subtree and drop the
entry only once that subtree is empty. Removal uses `Vec::remove` rather
than `swap_remove` because `lookup` and `lookup_with_path` both return on
the first matching regex, which makes `regexps` order part of routing
behaviour.

The nine existing assertions these defects tripped are unchanged; only
their causes are fixed.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 3fcf4ec into main Sep 18, 2026
18 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/router-equality branch September 18, 2026 17:40
pull Bot pushed a commit to jrcribb/sozu that referenced this pull request Sep 19, 2026
…ling

In the pattern trie a domain whose leftmost label is a regex
(`/test[0-9]/.example.com`) and a deeper domain sharing that segment
(`foo./test[0-9]/.example.com`) live in the same `(regex, subtree)` entry:
the first as the subtree's own value, the second as a child of it.

`insert_recursive`'s `pos == 0` arm returned `InsertResult::Existing`
unconditionally, justified by a comment claiming the subtree was already a
value-bearing leaf built by `TrieNode::new`. That premise is false when the
entry was created by the `pos > 0` branch, which builds a valueless
`TrieNode::root()`. Inserting the deeper domain first therefore made the
leftmost-regex host permanently un-insertable: the second insert answered
`Existing` and stored nothing.

Debug builds panicked the worker from a control-plane `AddHttpFrontend`, on
`add_tree_rule`'s post-condition. Release builds reported success, listed the
frontend in `sozu query frontends`, and never routed it — the main process
recorded a frontend the data plane did not have. Pre-existing; PR sozu-proxy#1330 fixed
`remove_recursive` and left this path untouched.

The arm now asks the slot through a new `insert_own_value`: free answers
`Ok`, occupied answers `Existing`. The empty-key arm is deliberately not
reused, because on the remove side it is a query and on the insert side it is
validation — routing an insert through it would make every leading-dot
hostname insertable, including as a TLS certificate name, which
`CertificateResolver::add_certificate` rejects by dry-running the same trie.

Three comments resting on the same false premise are corrected, and the
no-dot arm's soundness is documented: it is safe only because the two
`children.insert` sites use disjoint key spaces, one keyed by a dotless
segment and one by a key that always starts with a dot.

`doc/configure.md` claimed configuration order does not affect routing. It
does, in ways the manual had wrong in six separate dimensions, so the
precedence section is rewritten and pinned by fourteen tests:

  - `PREFIX` rules: longest match wins among rules whose method matches, with
    an equal-length tie going to the last declared.
  - `REGEX` and `EQUALS`: the scan returns on the first match, so the first
    declared wins, and neither type outranks the other.
  - a matching `EQUALS` or `REGEX` beats a longer `PREFIX` in either order,
    but only when it carries a method the request matches.
  - without a method the scan does not short-circuit, so the last declared
    wins and only a prefix spanning the whole request path displaces a regex.
  - a rule whose method is set but does not match is skipped entirely,
    whatever its path type.
  - the request path is the request-target including any query string, which
    inverts the whole-path case and stops `EQUALS` matching at all.

Path regexes are documented as unanchored, which they are: the `\A...\z`
work of 2.0.0 applies to hostname segments only. Whether they should be
anchored is sozu#1350.

Hostname overlap is stated as not resolved by declaration order, without
describing what does resolve it: that behaviour attaches an exact-host rule
to a matching regex segment's leaf and is sozu#1351.

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.

1 participant