Skip to content

fix: keep a pending sign key across reconnects until the server accepts it - #52

Merged
maxidragon merged 1 commit into
masterfrom
fix/keep-pending-sign-key-across-reconnects
Nov 25, 2026
Merged

maxidragon merged 1 commit into
masterfrom
fix/keep-pending-sign-key-across-reconnects

Conversation

@maxidragon

Copy link
Copy Markdown
Member

Problem

A station with an unreliable link can't be added. Seen on a v4 station (v3.5.5) behind micro-connector (ids and keys invented):

[station]   submit → new sign key 0x2f1a9c40 (pending), Add sent
[backend]   requestToConnect espId=0xA1B2C3D4 → "Request sent", request shown in the panel
[station]   ws error: Socket                      (~1 s later)
[station]   reconnect; RandomSigned is signed with the key the backend still holds (the old one)
[station]   [trust] the server never accepted the sign key, dropping it to be added again
[panel]     admin accepts → backend stores 0x2f1a9c40
[station]   [trust] no sign key yet, waiting for the station to be added again

Every later submit goes the same way whenever the link drops between the submit and the approval.

Cause

#48 treated any mismatch while the key was pending as "the server never accepted it" and dropped the key. But before approval the server always signs with its old key, so a reconnect in that window is enough to throw the key away. The admin then accepts a key that no longer exists on the station.

Fix

A pending key survives reconnects until a handshake proves the server signs with it. The trust decision now lives in a pure function, src/utils/trust.rs, with host tests.

Handshake Key state Before Now
signed with our key pending trusted, pending cleared same
signed with our key accepted trusted same
no RandomSigned (server has no key for us) any untrusted, key kept same
signed with another key pending key dropped untrusted, key kept, waits for approval
signed with another key accepted "Server Not Trusted!" same, and trust_server is cleared explicitly
anything no key (0) trusted if the server signed with the all-zero key never trusted (NoKey)

While a key waits for approval:

  • Screen. The station counts as not added even though the server knows its old key, so it shows "device not added", as it did after the drop.
  • Submit. Submit re-sends Add with the same pending key; it does not make a new one. fix: let a station without a sign key be added again #45/fix: drop a sign key the server never accepted, so the station can be added again #48 and sim/scenarios/readd-sign-key.json show why: a second key races the first Add. The backend dedupes an identical request and keeps only the latest request per device. If the admin rejected the request, it reappears in the panel.
  • No optimistic trust. add_device no longer sets trust_server on that resend, because this connection's handshake just saw another key.
  • Approval. Trust is decided only in the handshake. micro-connector pushes device settings again when the backend's device list changes, which is how an approval arrives. A settings push that is not the first one of a connection makes a station still waiting for approval reconnect (WsError::Recheck). The new handshake is signed with the accepted key, so the station becomes trusted and pending is cleared. The first settings of a connection never trigger this, so it can't loop. Any other device-list change costs a waiting station one reconnect.

Power cycles. The pending flag was already in NVS (SIGN_PENDING) and still is. After a reboot the request in the panel still waits for that same key, and a new key would just repeat the bug. After a reboot the session starts untrusted, and the approval push makes the station re-check, as above.

No timeout. A timer that drops the key brings the bug back for any admin slower than the timer. Dropping also doesn't help any case it was meant for:

  • a rejected request: submit re-sends the same key;
  • a backend that never accepts re-keys: a new key wouldn't be accepted either.

There is also no trustworthy clock at handshake time after a reboot, because EpochTime arrives after the handshake. The way out stays Reset settings, which clears both NVS entries, so the next submit makes a fresh key (unchanged).

auto_add. It follows added. A station waiting for approval re-sends its pending key, and on a first connect (no key) it still makes a fresh key with optimistic trust, as before.

Security. A station still never trusts a server that signs with a key it doesn't hold. The only optimistic trust left is the one that already existed right after a fresh Add on the same connection.

Testing

  • Host tests: 8 new cases in host-tests (trust_tests) cover every row of the table, the "not added while waiting" rule, the recheck rule (first settings vs later push), and the reported sequence end to end. All 51 tests pass.
  • cargo fmt --check, clippy -D warnings for v3, v4, v4+auto_add and the sim feature (SIM_CLIPPY=1 sim/run.sh), and release builds for v3 and v4.
  • No sim scenario. micro-connector's e2e fake backend approves RequestToConnectDevice the moment it arrives, so there is no window in which to reconnect before approval. A scenario would need a hold/approve command in the e2e control socket (micro-connector repo) first. esp-emu also isn't installed on the machine this was built on, so the existing scenarios weren't re-run either. readd-sign-key.json and a monkey run are worth running before merging.

@maxidragon
maxidragon merged commit a5ad0bb into master Nov 25, 2026
10 checks passed
@maxidragon
maxidragon deleted the fix/keep-pending-sign-key-across-reconnects branch November 25, 2026 21:35
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