diff --git a/crypto/sql_store.go b/crypto/sql_store.go index 1b14f2d..ba14bba 100644 --- a/crypto/sql_store.go +++ b/crypto/sql_store.go @@ -799,9 +799,13 @@ func (store *SQLCryptoStore) FindDeviceByKey(ctx context.Context, userID id.User } +// Xessenger (QA-289): a device ID listed again after it was removed takes the signing key it is listed +// with now, as it takes the identity key. Kept, the removed device's signing key, which its owner +// cross-signed, would vouch for the identity key of whoever listed the device ID again: the trust +// checks look at the signing key, the Olm sessions at the identity key. const deviceInsertQuery = ` INSERT INTO crypto_device (user_id, device_id, identity_key, signing_key, trust, deleted, name) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (user_id, device_id) DO UPDATE - SET identity_key=excluded.identity_key, deleted=excluded.deleted, trust=excluded.trust, name=excluded.name + SET identity_key=excluded.identity_key, signing_key=excluded.signing_key, deleted=excluded.deleted, trust=excluded.trust, name=excluded.name ` diff --git a/crypto/xessengertest/device_keys_test.go b/crypto/xessengertest/device_keys_test.go new file mode 100644 index 0000000..89756c2 --- /dev/null +++ b/crypto/xessengertest/device_keys_test.go @@ -0,0 +1,100 @@ +// Copyright (c) 2026 Xessenger +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// Package xessengertest checks Xessenger's mautrix-go patches to the crypto package from outside it, so +// that its own tests and their dependencies do not build with the bridges. +package xessengertest + +import ( + "context" + "database/sql" + "os" + "path/filepath" + "strings" + "testing" + + _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" + "go.mau.fi/util/dbutil" + + "maunium.net/go/mautrix/crypto" + "maunium.net/go/mautrix/id" +) + +// xsCryptoStore opens a crypto store: SQLite, or the PostgreSQL database in +// XESSENGER_SEAL_TEST_POSTGRES (emptied first) for the postgres subtests. +func xsCryptoStore(t *testing.T) *crypto.SQLCryptoStore { + t.Helper() + dialect := "sqlite3" + uri := "file:" + filepath.Join(t.TempDir(), "crypto.db") + "?_foreign_keys=on" + if strings.HasSuffix(t.Name(), "/postgres") { + dialect, uri = "postgres", os.Getenv("XESSENGER_SEAL_TEST_POSTGRES") + if uri == "" { + t.Skip("XESSENGER_SEAL_TEST_POSTGRES is not set") + } + } + raw, err := sql.Open(dialect, uri) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = raw.Close() }) + if dialect == "postgres" { + if _, err = raw.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public`); err != nil { + t.Fatal(err) + } + } + db, err := dbutil.NewWithDB(raw, dialect) + if err != nil { + t.Fatal(err) + } + store := crypto.NewSQLCryptoStore(db, dbutil.NoopLogger, "bot", "BOTDEVICE", []byte("test pickle key")) + if err = store.DB.Upgrade(context.Background()); err != nil { + t.Fatal(err) + } + return store +} + +// A device ID the homeserver lists again after removing it takes the keys it is listed with now, the +// signing key as well as the identity key, whether the device list is stored whole (FetchKeys) or one +// device at a time. +func TestXessengerDeviceListedAgainTakesItsSigningKey(t *testing.T) { + for _, dialect := range []string{"sqlite", "postgres"} { + t.Run(dialect, func(t *testing.T) { + ctx := context.Background() + store := xsCryptoStore(t) + alice := id.UserID("@alice:example.com") + // Stand-ins for real keys are 43 characters, the width of the key columns (CHAR(43), which + // PostgreSQL pads). + key := func(name string) string { return name + strings.Repeat("=", 43-len(name)) } + phone := func(identity, signing string) *id.Device { + return &id.Device{UserID: alice, DeviceID: "PHONE", IdentityKey: id.Curve25519(key(identity)), SigningKey: id.Ed25519(key(signing))} + } + must := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + check := func(when, identity, signing string) { + t.Helper() + device, err := store.GetDevice(ctx, alice, "PHONE") + must(err) + if device == nil || device.IdentityKey != id.Curve25519(key(identity)) || device.SigningKey != id.Ed25519(key(signing)) { + t.Errorf("%s: PHONE is %+v, want identity key %s and signing key %s", when, device, identity, signing) + } + } + // As FetchKeys stores a device list: the devices listed, and every other one marked deleted. + must(store.PutDevices(ctx, alice, map[id.DeviceID]*id.Device{"PHONE": phone("identity-1", "signing-1")})) + check("listed", "identity-1", "signing-1") + must(store.PutDevices(ctx, alice, nil)) + must(store.PutDevices(ctx, alice, map[id.DeviceID]*id.Device{"PHONE": phone("identity-2", "signing-2")})) + check("listed again", "identity-2", "signing-2") + must(store.PutDevices(ctx, alice, nil)) + must(store.PutDevice(ctx, alice, phone("identity-3", "signing-3"))) + check("stored again on its own", "identity-3", "signing-3") + }) + } +}