From eb22f6355aae2a577ed415034c39f5504efcdd83 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Wed, 14 Oct 2026 04:39:24 +0800 Subject: [PATCH] davsync: a computer joining a sync folder keeps the gateway keys the folder's store has never seen (#1737, #1714, ggbdpq) The gateway-key store rides the settings part, and a first sync brings a part changed only on the server in whole. So a computer with its own gateway keys that joined a folder another computer had set up first had every key replaced by the folder's store (8ugMak1r on #1714 got them back only from the -this-computer copy in the sync folder). On the first sync, before the settings are brought in, the keys this computer holds that the store has neither by id nor by value are added to the store brought in; that merged store is restored here and pushed for the others. A key the folder holds wins; the LAN default key follows the folder's settings as before. After the join a key missing from a store this computer has seen is a removal, as before. ggbdpq's patch (a9605c8a), cherry-picked unchanged. Run (reviewer): TestGatewayKeysJoinKeepsUnseenKeys fails on the old davsync.go (the joining computer's keys are gone, the pushed store holds only the folder's default key, computer B never gets A's key) and passes with it, -race -count=3; TestGatewayKeysJoinWithoutStoreKeepsKeys passes on both. go test ./internal/davsync/... ok. Not tried against a real WebDAV server. --- internal/davsync/davsync.go | 33 +++- internal/davsync/gateway_keys_join_test.go | 199 +++++++++++++++++++++ 2 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 internal/davsync/gateway_keys_join_test.go diff --git a/internal/davsync/davsync.go b/internal/davsync/davsync.go index acf98a72f..6b319d3f7 100644 --- a/internal/davsync/davsync.go +++ b/internal/davsync/davsync.go @@ -11,7 +11,9 @@ // both since the last sync keeps the newer, and the one it replaced is // saved in the sync folder beside magpie's files and named in a notice. // What the server holds is mirrored: a provider removed on one computer -// goes from the others. +// goes from the others. A computer joining a folder keeps the gateway keys +// the folder's store has never seen; after the join, a key removed on the +// folder goes from every computer. package davsync import ( @@ -1003,6 +1005,32 @@ func bring(b backup.Bundle, part string) error { return nil } +// keepUnseenGatewayKeys puts the gateway keys this computer holds that the +// folder's store has never seen into the store a first sync is about to +// bring in (#1714, 8ugMak1r). The store rides the settings part, so a +// computer joining a folder whose store has none of its keys would have +// them replaced — credentials the folder never held a chance to revoke. A +// key the folder holds by id or by value is the folder's, and its copy +// wins; the default key (LAN) follows the folder's settings either way. +// This runs on the first sync alone: afterwards absence from a store this +// computer has seen means a removal, which must go from every computer. +func keepUnseenGatewayKeys(remote *backup.Bundle, local backup.Bundle) { + if local.GatewayKeys == nil || len(*local.GatewayKeys) == 0 || remote.GatewayKeys == nil { + return + } + seen := map[string]bool{} + for _, k := range *remote.GatewayKeys { + seen[k.ID], seen[k.Secret] = true, true + } + for _, k := range *local.GatewayKeys { + if k.LAN || k.Secret == "" || seen[k.ID] || seen[k.Secret] { + continue + } + *remote.GatewayKeys = append(*remote.GatewayKeys, k) + seen[k.ID], seen[k.Secret] = true, true + } +} + func syncOnce(ctx context.Context, c Config, st *state) error { d, err := newRemote(c) if err != nil { @@ -1149,6 +1177,9 @@ func syncOnce(ctx context.Context, c Config, st *state) error { case lc && !rc: take(&merged, local, p) case rc && !lc: + if first && p == "settings" { + keepUnseenGatewayKeys(&remote, local) + } bringIn = append(bringIn, p) if first { here = append(here, p) diff --git a/internal/davsync/gateway_keys_join_test.go b/internal/davsync/gateway_keys_join_test.go new file mode 100644 index 000000000..155b3b436 --- /dev/null +++ b/internal/davsync/gateway_keys_join_test.go @@ -0,0 +1,199 @@ +package davsync + +import ( + "context" + "net/http/httptest" + "slices" + "testing" + + "github.com/yetone/magpie/internal/access" + "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/backup" + "github.com/yetone/magpie/internal/provider" +) + +// A computer joining a folder keeps the gateway keys it holds that the +// folder's store has never seen (#1714, 8ugMak1r): the store rides the +// settings part, and a first sync brings a part changed only on the server +// in whole. A key the folder holds by id or by value is the folder's; the +// default key follows the folder's settings; a key removed on the folder +// after the join still goes from every computer. +func TestGatewayKeysJoinKeepsUnseenKeys(t *testing.T) { + fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}, cond: true, putETag: true} + srv := httptest.NewServer(fake) + defer srv.Close() + cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "gateway join fixture passphrase", Keys: true} + a, b := newComputer(t), newComputer(t) + use := func(c computer) { + t.Helper() + c.use(t) + appdir.UseExecutable("") + provider.ForgetAccounts() + } + t.Cleanup(provider.ForgetAccounts) + now := func() { + t.Helper() + if err := Now(context.Background()); err != nil { + t.Fatal(err) + } + } + remote := func() backup.Bundle { + t.Helper() + fake.mu.Lock() + data := append([]byte(nil), fake.files["/dav/magpie/magpie.magpie-backup"]...) + fake.mu.Unlock() + got, err := backup.Open(data, cfg.Passphrase) + if err != nil { + t.Fatal(err) + } + return got + } + storeIDs := func(b backup.Bundle) []string { + t.Helper() + if b.GatewayKeys == nil { + return nil + } + var ids []string + for _, k := range *b.GatewayKeys { + ids = append(ids, k.ID) + } + return ids + } + + // Computer B configures sync first, holding sharing's default key alone. + use(b) + if err := access.ConfigureLAN(true, false); err != nil { + t.Fatal(err) + } + keysB, err := access.Export() + if err != nil { + t.Fatal(err) + } + if len(keysB) != 1 { + t.Fatalf("fixture: computer B holds %d keys, want the default alone", len(keysB)) + } + defaultB := keysB[0] + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + now() + + // Computer A holds several gateway keys of its own and joins after. + use(a) + if err := access.ConfigureLAN(true, false); err != nil { + t.Fatal(err) + } + laptop, err := access.Update("add-key", access.Change{Name: "Laptop"}) + if err != nil { + t.Fatal(err) + } + who, ok := access.Authenticate(laptop) + if !ok { + t.Fatal("fixture: the added gateway key does not authenticate") + } + own, err := access.Update("add-key", access.Change{Name: "Own", Secret: "fixture-own-client-key"}) + if err != nil { + t.Fatal(err) + } + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + now() + if _, ok := access.Authenticate(laptop); !ok { + t.Error("joining the folder destroyed this computer's own gateway key") + } + if _, ok := access.Authenticate(own); !ok { + t.Error("joining the folder destroyed this computer's own named key value") + } + if _, ok := access.Authenticate(defaultB.Secret); !ok { + t.Error("joining did not bring the folder's default key in") + } + got := remote() + ids := storeIDs(got) + for _, want := range []string{defaultB.ID, who.KeyID} { + if !slices.Contains(ids, want) { + t.Errorf("the store pushed after the join lacks key %s; it holds %v", want, ids) + } + } + + // Computer B takes the joined keys in. + use(b) + now() + if _, ok := access.Authenticate(own); !ok { + t.Error("computer B did not receive computer A's key") + } + + // A key removed after the join still goes from every computer. + use(a) + if _, err := access.Update("remove-key", access.Change{Key: who.KeyID}); err != nil { + t.Fatal(err) + } + now() + use(b) + now() + if _, ok := access.Authenticate(laptop); ok { + t.Error("a key removed after the join came back on computer B") + } + if _, ok := access.Authenticate(own); !ok { + t.Error("an unrelated key was lost on computer B") + } +} + +// A folder with no gateway-key store at all leaves a joining computer's +// keys as they are: nothing there replaces them. +func TestGatewayKeysJoinWithoutStoreKeepsKeys(t *testing.T) { + fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}, cond: true, putETag: true} + srv := httptest.NewServer(fake) + defer srv.Close() + cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "gateway join fixture passphrase", Keys: true} + a, b := newComputer(t), newComputer(t) + use := func(c computer) { + t.Helper() + c.use(t) + appdir.UseExecutable("") + provider.ForgetAccounts() + } + t.Cleanup(provider.ForgetAccounts) + now := func() { + t.Helper() + if err := Now(context.Background()); err != nil { + t.Fatal(err) + } + } + remote := func() backup.Bundle { + t.Helper() + fake.mu.Lock() + data := append([]byte(nil), fake.files["/dav/magpie/magpie.magpie-backup"]...) + fake.mu.Unlock() + got, err := backup.Open(data, cfg.Passphrase) + if err != nil { + t.Fatal(err) + } + return got + } + + use(b) + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + now() + if got := remote(); got.GatewayKeys != nil { + t.Fatal("fixture: the folder holds a gateway-key store") + } + + use(a) + if err := access.ConfigureLAN(true, false); err != nil { + t.Fatal(err) + } + laptop, err := access.Update("add-key", access.Change{Name: "Laptop"}) + if err != nil { + t.Fatal(err) + } + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + now() + if _, ok := access.Authenticate(laptop); !ok { + t.Error("joining a folder with no key store destroyed this computer's key") + } +}