Skip to content

command/state: ReplaceCertificate removes the old certificate before validating the new one, so a malformed PEM leaves the address with none #1404

Closed

Description

@FlorentinDUBOIS

ConfigState::replace_certificate (command/src/state.rs, around :1245) removes the old
fingerprint from the address's certificate map before computing the new one, and computing the
new one is fallible:

self.certificates
    .get_mut(&replace_address)
    .ok_or(StateError::NotFound { .. })?
    .remove(&old_fingerprint);                       // (1) the old certificate is gone

let new_fingerprint = Fingerprint(
    calculate_fingerprint(replace.new_certificate.certificate.as_bytes())
        .map_err(|e| StateError::ReplaceCertificate(e.to_string()))?,   // (2) can return Err
);

calculate_fingerprint (command/src/certificate.rs, around :289) is
parse_pem(certificate)? followed by a digest, so it genuinely fails on malformed PEM. When it does,
? propagates after step (1) has already mutated the map, and the address is left with zero
certificates
in the state.

The two debug_assert!s at the end of the function do not catch this: they only run in debug builds,
and only on the Ok path — the early return skips them entirely.

Nothing rolls it back

compute_rollback (bin/src/command/requests.rs, around :2308) covers four listener-add verbs and
four frontend-add verbs. ReplaceCertificate is not among them, so there is no compensating request
for this failure at the orchestration layer either.

This exact bug was already fixed — in the sibling implementation

CHANGELOG.md:

TLS certificate hot-rotation no longer drops the working certificate on failure
(#1202)
: CertificateResolver::replace_certificate
in lib/src/tls.rs now adds the new certificate before removing the old one.

Closes #774.

lib/src/tls.rs's version now builds CertifiedKeyWrapper::try_from(&add)? and derives the new
fingerprint first, precisely so a parse or signing-key failure surfaces before anything is removed.
That fix landed in the runtime resolver and was never mirrored into ConfigState's own
replace_certificate, which is a second, independent implementation of the same verb.

So this is not a new bug class — it is #774 surviving in the copy the #1202 sweep did not reach.

Suggested fix

Mirror the sibling: compute new_fingerprint first, insert, and only then remove old_fingerprint.
That also makes the idempotent case (old fingerprint equals new) naturally safe, which is the other
hazard lib/src/tls.rs documents at length in its own comment — an operator or ACME retry loop
resubmitting the same PEM must not end up deleting the entry it meant to retain.

Worth adding ReplaceCertificate to compute_rollback in the same changeset, or recording
explicitly why it needs no compensating request once the mutation is atomic.

Scope I verified, and what I did not

Verified by reading origin/main at 7be659544163f0d780f8eb0666a25d992f5f8e23: the ordering in
ConfigState::replace_certificate, the fallibility of calculate_fingerprint through parse_pem,
the absence of ReplaceCertificate from compute_rollback, and the sibling fix in lib/src/tls.rs
with its CHANGELOG entry.

Not verified: whether a malformed-PEM ReplaceCertificate can actually reach
ConfigState::dispatch in a deployed topology — i.e. whether some earlier validation rejects it
first. If an earlier gate always catches it, this is a latent defect rather than a reachable one, and
the ordering is still worth fixing because the gate is not visible from this function and the sibling
implementation does not rely on one. No runtime reproduction was attempted; this is a code-reading
finding and is stated as such.

Why it was found now

It surfaced during a survey of which Sōzu subsystems are candidates for the sans-io + deterministic
simulation treatment (following #1339). ConfigState ranked first: a pure BTreeMap/HashMap
mutator with a check_invariants() postcondition and a documented history of partial-failure and
rollback defects (#1301, #1314, #1313). A shadow-model simulation over dispatch + the fan-out
rollback layer, driven by adversarial inputs including malformed PEM on replace, is precisely the
harness that would have caught this on an early seed.

Note also that check_invariants() would not have caught it either: it validates key consistency,
not "at least one certificate survives for an address that had one".

Activity

  1. added a commit that references this issue on Sep 22, 2026
    6dd569e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions