From 71a21450b0e28346cf5be21faca9dec424c46f3f Mon Sep 17 00:00:00 2001 From: Perma Alesheikh Date: Thu, 29 Oct 2026 10:52:21 +0100 Subject: [PATCH] fix(csapi): sign keys when uploading them. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since Matrix version 1.17 uploaded keys must be signed. > A device uploads the public parts of identity keys to their homeserver > as a signed JSON object, using the /keys/upload API. The JSON object > must include the public part of the device’s Ed25519 key, and must be > signed by that key, as described in Signing JSON. Certain tests in the tests/csapi/device_list_test.go do not do that. To fix that the signature was added to the request. Signed-off-by: Perma Alesheikh --- tests/csapi/device_lists_test.go | 34 ++++++-------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/tests/csapi/device_lists_test.go b/tests/csapi/device_lists_test.go index 278fb4a15..93371f734 100644 --- a/tests/csapi/device_lists_test.go +++ b/tests/csapi/device_lists_test.go @@ -1,12 +1,9 @@ package csapi_tests import ( - "encoding/base64" "fmt" "testing" - "math/rand" - "github.com/matrix-org/complement" "github.com/matrix-org/complement/client" "github.com/matrix-org/complement/helpers" @@ -22,37 +19,18 @@ import ( // 1. `/sync`'s `device_lists.changed/left` contain the correct user IDs. // 2. `/keys/query` returns the correct information after device list updates. func TestDeviceListUpdates(t *testing.T) { - prng := rand.New(rand.NewSource(42)) // uploadNewKeys uploads a new set of keys for a given client. // Returns a check function that can be passed to mustQueryKeys. uploadNewKeys := func(t *testing.T, user *client.CSAPI) []match.JSON { t.Helper() ed25519KeyID := fmt.Sprintf("ed25519:%s", user.DeviceID) curve25519KeyID := fmt.Sprintf("curve25519:%s", user.DeviceID) - // generate key-like looking values - ed25519KeyBytes := make([]byte, 32) - _, err := prng.Read(ed25519KeyBytes) - must.NotError(t, "failed to read from prng", err) - ed25519Key := base64.RawStdEncoding.EncodeToString(ed25519KeyBytes) - curve25519KeyBytes := make([]byte, 32) - _, err = prng.Read(curve25519KeyBytes) - must.NotError(t, "failed to read from prng", err) - curve25519Key := base64.RawStdEncoding.EncodeToString(curve25519KeyBytes) - - user.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, - client.WithJSONBody(t, map[string]interface{}{ - "device_keys": map[string]interface{}{ - "user_id": user.UserID, - "device_id": user.DeviceID, - "algorithms": []interface{}{"m.olm.v1.curve25519-aes-sha2", "m.megolm.v1.aes-sha2"}, - "keys": map[string]interface{}{ - ed25519KeyID: ed25519Key, - curve25519KeyID: curve25519Key, - }, - "signatures": map[string]interface{}{}, - }, - }), - ) + + deviceKeys, _ := user.MustGenerateOneTimeKeys(t, 0) + user.MustUploadKeys(t, deviceKeys, nil) + keys := deviceKeys["keys"].(map[string]any) + ed25519Key := keys[ed25519KeyID] + curve25519Key := keys[curve25519KeyID] algorithmsPath := fmt.Sprintf("device_keys.%s.%s.algorithms", user.UserID, user.DeviceID) ed25519Path := fmt.Sprintf("device_keys.%s.%s.keys.%s", user.UserID, user.DeviceID, ed25519KeyID)