ipn/store: make WriteState(id, nil) delete key instead of adding nil entry (#19920)

All StateStore implementations store a nil value in the cache map when WriteState is called with a nil byte slice instead of deleting the key. This causes ReadState to return (nil, nil) instead of (nil, ErrStateNotExist), since the key is still present in the map.

This breaks reset-auth in Windows, Linux, and Android, and the node can't log back in without manually editing the state file. (macOS uses a different state store)
DeleteProfile, DeleteAllProfilesForUser, setUnattendedModeAsConfigured are impacted but don't seem to break because the deleted keys are not reread.

This deletes the key from the cache instead.

Fixes tailscale/corp#42477

Signed-off-by: kari-ts <kari@tailscale.com>
This commit is contained in:
kari-ts
2026-05-29 11:22:14 -07:00
committed by GitHub
parent 3d5102090f
commit 7355116c05
9 changed files with 194 additions and 10 deletions
+36
View File
@@ -135,11 +135,41 @@ func testStoreSemantics(t *testing.T, store ipn.StateStore) {
}
}
func testStoreDeleteSemantics(t *testing.T, store ipn.StateStore) {
t.Helper()
// Write a key, verify it exists.
if err := store.WriteState("delme", []byte("val")); err != nil {
t.Fatalf("WriteState: %v", err)
}
if bs, err := store.ReadState("delme"); err != nil {
t.Fatalf("ReadState after write: %v", err)
} else if string(bs) != "val" {
t.Fatalf("ReadState after write: got %q, want %q", bs, "val")
}
// Delete by writing nil.
if err := store.WriteState("delme", nil); err != nil {
t.Fatalf("WriteState(nil): %v", err)
}
// Read should return ErrStateNotExist.
if _, err := store.ReadState("delme"); err != ipn.ErrStateNotExist {
t.Fatalf("ReadState after delete: got err %v, want ErrStateNotExist", err)
}
// Delete of a non-existent key should not error.
if err := store.WriteState("never-existed", nil); err != nil {
t.Fatalf("WriteState(nil) on non-existent key: %v", err)
}
}
func TestMemoryStore(t *testing.T) {
tstest.PanicOnLog()
store := new(mem.Store)
testStoreSemantics(t, store)
testStoreDeleteSemantics(t, store)
}
func TestFileStore(t *testing.T) {
@@ -154,6 +184,7 @@ func TestFileStore(t *testing.T) {
}
testStoreSemantics(t, store)
testStoreDeleteSemantics(t, store)
// Build a brand new file store and check that both IDs written
// above are still there.
@@ -176,4 +207,9 @@ func TestFileStore(t *testing.T) {
t.Errorf("reading %q (2nd store): got %q, want %q", key, bs, want)
}
}
// Verify deleted key is still gone after reload.
if _, err := store.ReadState("delme"); err != ipn.ErrStateNotExist {
t.Fatalf("reading deleted key from reloaded store: got err %v, want ErrStateNotExist", err)
}
}