2020-06-15 17:57:59 +02:00
|
|
|
package federationapi_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
2022-05-17 14:23:35 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-01-08 19:14:29 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-06-15 17:57:59 +02:00
|
|
|
"strings"
|
2022-08-05 10:19:33 +02:00
|
|
|
"sync"
|
2020-06-15 17:57:59 +02:00
|
|
|
"testing"
|
2022-05-17 14:23:35 +02:00
|
|
|
"time"
|
2020-06-15 17:57:59 +02:00
|
|
|
|
2024-01-08 19:14:29 +01:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/routing"
|
2023-03-17 12:09:45 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2023-03-22 09:21:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-09-22 15:53:15 +02:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 10:55:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2024-01-08 19:14:29 +01:00
|
|
|
"github.com/matrix-org/util"
|
2022-09-22 15:53:15 +02:00
|
|
|
"github.com/nats-io/nats.go"
|
2024-01-08 19:14:29 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/tidwall/gjson"
|
2022-09-22 15:53:15 +02:00
|
|
|
|
2020-06-15 17:57:59 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationapi"
|
2022-05-17 14:23:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2022-05-06 13:39:26 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/internal"
|
2022-05-17 14:23:35 +02:00
|
|
|
rsapi "github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 13:54:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2022-05-17 14:23:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
"github.com/matrix-org/dendrite/test/testrig"
|
2023-02-20 14:58:03 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-06-15 17:57:59 +02:00
|
|
|
)
|
|
|
|
|
2022-05-17 14:23:35 +02:00
|
|
|
type fedRoomserverAPI struct {
|
|
|
|
rsapi.FederationRoomserverAPI
|
|
|
|
inputRoomEvents func(ctx context.Context, req *rsapi.InputRoomEventsRequest, res *rsapi.InputRoomEventsResponse)
|
2023-08-15 13:37:04 +02:00
|
|
|
queryRoomsForUser func(ctx context.Context, userID spec.UserID, desiredMembership string) ([]spec.RoomID, error)
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
func (f *fedRoomserverAPI) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-07 19:14:35 +02:00
|
|
|
return spec.NewUserID(string(senderID), true)
|
|
|
|
}
|
|
|
|
|
2023-08-02 12:12:14 +02:00
|
|
|
func (f *fedRoomserverAPI) QuerySenderIDForUser(ctx context.Context, roomID spec.RoomID, userID spec.UserID) (*spec.SenderID, error) {
|
|
|
|
senderID := spec.SenderID(userID.String())
|
|
|
|
return &senderID, nil
|
2023-06-06 22:55:18 +02:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:23:35 +02:00
|
|
|
// PerformJoin will call this function
|
2023-05-10 00:46:49 +02:00
|
|
|
func (f *fedRoomserverAPI) InputRoomEvents(ctx context.Context, req *rsapi.InputRoomEventsRequest, res *rsapi.InputRoomEventsResponse) {
|
2022-05-17 14:23:35 +02:00
|
|
|
if f.inputRoomEvents == nil {
|
2023-05-10 00:46:49 +02:00
|
|
|
return
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
|
|
|
f.inputRoomEvents(ctx, req, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// keychange consumer calls this
|
2023-08-15 13:37:04 +02:00
|
|
|
func (f *fedRoomserverAPI) QueryRoomsForUser(ctx context.Context, userID spec.UserID, desiredMembership string) ([]spec.RoomID, error) {
|
2022-05-17 14:23:35 +02:00
|
|
|
if f.queryRoomsForUser == nil {
|
2023-08-15 13:37:04 +02:00
|
|
|
return nil, nil
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
2023-08-15 13:37:04 +02:00
|
|
|
return f.queryRoomsForUser(ctx, userID, desiredMembership)
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This struct isn't generic, only works for TestFederationAPIJoinThenKeyUpdate
|
|
|
|
type fedClient struct {
|
2022-08-05 10:19:33 +02:00
|
|
|
fedClientMutex sync.Mutex
|
2023-04-24 18:23:25 +02:00
|
|
|
fclient.FederationClient
|
2022-05-17 14:23:35 +02:00
|
|
|
allowJoins []*test.Room
|
2023-04-19 16:50:33 +02:00
|
|
|
keys map[spec.ServerName]struct {
|
2022-05-17 14:23:35 +02:00
|
|
|
key ed25519.PrivateKey
|
|
|
|
keyID gomatrixserverlib.KeyID
|
|
|
|
}
|
|
|
|
t *testing.T
|
|
|
|
sentTxn bool
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
func (f *fedClient) GetServerKeys(ctx context.Context, matrixServer spec.ServerName) (gomatrixserverlib.ServerKeys, error) {
|
2022-08-05 10:19:33 +02:00
|
|
|
f.fedClientMutex.Lock()
|
|
|
|
defer f.fedClientMutex.Unlock()
|
2022-05-17 14:23:35 +02:00
|
|
|
fmt.Println("GetServerKeys:", matrixServer)
|
|
|
|
var keys gomatrixserverlib.ServerKeys
|
|
|
|
var keyID gomatrixserverlib.KeyID
|
|
|
|
var pkey ed25519.PrivateKey
|
|
|
|
for srv, data := range f.keys {
|
|
|
|
if srv == matrixServer {
|
|
|
|
pkey = data.key
|
|
|
|
keyID = data.keyID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pkey == nil {
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.ServerName = matrixServer
|
2023-04-19 16:50:33 +02:00
|
|
|
keys.ValidUntilTS = spec.AsTimestamp(time.Now().Add(10 * time.Hour))
|
2022-05-17 14:23:35 +02:00
|
|
|
publicKey := pkey.Public().(ed25519.PublicKey)
|
|
|
|
keys.VerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.VerifyKey{
|
|
|
|
keyID: {
|
2023-04-19 16:50:33 +02:00
|
|
|
Key: spec.Base64Bytes(publicKey),
|
2022-05-17 14:23:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
toSign, err := json.Marshal(keys.ServerKeyFields)
|
|
|
|
if err != nil {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.Raw, err = gomatrixserverlib.SignJSON(
|
|
|
|
string(matrixServer), keyID, pkey, toSign,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 02:43:46 +02:00
|
|
|
func (f *fedClient) MakeJoin(ctx context.Context, origin, s spec.ServerName, roomID, userID string) (res fclient.RespMakeJoin, err error) {
|
|
|
|
f.fedClientMutex.Lock()
|
|
|
|
defer f.fedClientMutex.Unlock()
|
2022-05-17 14:23:35 +02:00
|
|
|
for _, r := range f.allowJoins {
|
|
|
|
if r.ID == roomID {
|
2023-06-07 19:14:35 +02:00
|
|
|
senderIDString := userID
|
2022-05-17 14:23:35 +02:00
|
|
|
res.RoomVersion = r.Version
|
2023-05-04 12:17:42 +02:00
|
|
|
res.JoinEvent = gomatrixserverlib.ProtoEvent{
|
2023-06-07 19:14:35 +02:00
|
|
|
SenderID: senderIDString,
|
2022-05-17 14:23:35 +02:00
|
|
|
RoomID: roomID,
|
|
|
|
Type: "m.room.member",
|
2023-06-07 19:14:35 +02:00
|
|
|
StateKey: &senderIDString,
|
2023-04-19 16:50:33 +02:00
|
|
|
Content: spec.RawJSON([]byte(`{"membership":"join"}`)),
|
2022-05-17 14:23:35 +02:00
|
|
|
PrevEvents: r.ForwardExtremities(),
|
|
|
|
}
|
|
|
|
var needed gomatrixserverlib.StateNeeded
|
2023-05-04 12:17:42 +02:00
|
|
|
needed, err = gomatrixserverlib.StateNeededForProtoEvent(&res.JoinEvent)
|
2022-05-17 14:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
f.t.Errorf("StateNeededForEventBuilder: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.JoinEvent.AuthEvents = r.MustGetAuthEventRefsForEvent(f.t, needed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
func (f *fedClient) SendJoin(ctx context.Context, origin, s spec.ServerName, event gomatrixserverlib.PDU) (res fclient.RespSendJoin, err error) {
|
2022-08-05 10:19:33 +02:00
|
|
|
f.fedClientMutex.Lock()
|
|
|
|
defer f.fedClientMutex.Unlock()
|
2022-05-17 14:23:35 +02:00
|
|
|
for _, r := range f.allowJoins {
|
2023-09-15 16:39:06 +02:00
|
|
|
if r.ID == event.RoomID().String() {
|
2023-05-02 16:03:16 +02:00
|
|
|
r.InsertEvent(f.t, &types.HeaderedEvent{PDU: event})
|
2022-05-17 14:23:35 +02:00
|
|
|
f.t.Logf("Join event: %v", event.EventID())
|
2023-04-27 13:54:20 +02:00
|
|
|
res.StateEvents = types.NewEventJSONsFromHeaderedEvents(r.CurrentState())
|
|
|
|
res.AuthEvents = types.NewEventJSONsFromHeaderedEvents(r.Events())
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
func (f *fedClient) SendTransaction(ctx context.Context, t gomatrixserverlib.Transaction) (res fclient.RespSend, err error) {
|
2022-08-05 10:19:33 +02:00
|
|
|
f.fedClientMutex.Lock()
|
|
|
|
defer f.fedClientMutex.Unlock()
|
2022-05-17 14:23:35 +02:00
|
|
|
for _, edu := range t.EDUs {
|
2023-04-19 16:50:33 +02:00
|
|
|
if edu.Type == spec.MDeviceListUpdate {
|
2022-05-17 14:23:35 +02:00
|
|
|
f.sentTxn = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.t.Logf("got /send")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regression test to make sure that /send_join is updating the destination hosts synchronously and
|
|
|
|
// isn't relying on the roomserver.
|
|
|
|
func TestFederationAPIJoinThenKeyUpdate(t *testing.T) {
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
testFederationAPIJoinThenKeyUpdate(t, dbType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFederationAPIJoinThenKeyUpdate(t *testing.T, dbType test.DBType) {
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg, processCtx, close := testrig.CreateConfig(t, dbType)
|
|
|
|
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
|
|
|
caches := caching.NewRistrettoCache(128*1024*1024, time.Hour, caching.DisableMetrics)
|
|
|
|
natsInstance := jetstream.NATSInstance{}
|
|
|
|
cfg.FederationAPI.PreferDirectFetch = true
|
|
|
|
cfg.FederationAPI.KeyPerspectives = nil
|
2022-05-17 14:23:35 +02:00
|
|
|
defer close()
|
2023-03-22 09:21:32 +01:00
|
|
|
jsctx, _ := natsInstance.Prepare(processCtx, &cfg.Global.JetStream)
|
|
|
|
defer jetstream.DeleteAllStreams(jsctx, &cfg.Global.JetStream)
|
2022-05-17 14:23:35 +02:00
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
serverA := spec.ServerName("server.a")
|
2022-05-17 14:23:35 +02:00
|
|
|
serverAKeyID := gomatrixserverlib.KeyID("ed25519:servera")
|
|
|
|
serverAPrivKey := test.PrivateKeyA
|
|
|
|
creator := test.NewUser(t, test.WithSigningServer(serverA, serverAKeyID, serverAPrivKey))
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
myServer := cfg.Global.ServerName
|
|
|
|
myServerKeyID := cfg.Global.KeyID
|
|
|
|
myServerPrivKey := cfg.Global.PrivateKey
|
2022-05-17 14:23:35 +02:00
|
|
|
joiningUser := test.NewUser(t, test.WithSigningServer(myServer, myServerKeyID, myServerPrivKey))
|
|
|
|
fmt.Printf("creator: %v joining user: %v\n", creator.ID, joiningUser.ID)
|
|
|
|
room := test.NewRoom(t, creator)
|
|
|
|
|
2023-08-15 13:37:04 +02:00
|
|
|
roomID, err := spec.NewRoomID(room.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Invalid room ID: %q", roomID)
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:23:35 +02:00
|
|
|
rsapi := &fedRoomserverAPI{
|
|
|
|
inputRoomEvents: func(ctx context.Context, req *rsapi.InputRoomEventsRequest, res *rsapi.InputRoomEventsResponse) {
|
|
|
|
if req.Asynchronous {
|
|
|
|
t.Errorf("InputRoomEvents from PerformJoin MUST be synchronous")
|
|
|
|
}
|
|
|
|
},
|
2023-08-15 13:37:04 +02:00
|
|
|
queryRoomsForUser: func(ctx context.Context, userID spec.UserID, desiredMembership string) ([]spec.RoomID, error) {
|
|
|
|
if userID.String() == joiningUser.ID && desiredMembership == "join" {
|
|
|
|
return []spec.RoomID{*roomID}, nil
|
2022-05-17 14:23:35 +02:00
|
|
|
}
|
2023-08-15 13:37:04 +02:00
|
|
|
return nil, fmt.Errorf("unexpected queryRoomsForUser: %v, %v", userID, desiredMembership)
|
2022-05-17 14:23:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
fc := &fedClient{
|
|
|
|
allowJoins: []*test.Room{room},
|
|
|
|
t: t,
|
2023-04-19 16:50:33 +02:00
|
|
|
keys: map[spec.ServerName]struct {
|
2022-05-17 14:23:35 +02:00
|
|
|
key ed25519.PrivateKey
|
|
|
|
keyID gomatrixserverlib.KeyID
|
|
|
|
}{
|
|
|
|
serverA: {
|
|
|
|
key: serverAPrivKey,
|
|
|
|
keyID: serverAKeyID,
|
|
|
|
},
|
|
|
|
myServer: {
|
|
|
|
key: myServerPrivKey,
|
|
|
|
keyID: myServerKeyID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-03-22 09:21:32 +01:00
|
|
|
fsapi := federationapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, fc, rsapi, caches, nil, false)
|
2022-05-17 14:23:35 +02:00
|
|
|
|
|
|
|
var resp api.PerformJoinResponse
|
|
|
|
fsapi.PerformJoin(context.Background(), &api.PerformJoinRequest{
|
|
|
|
RoomID: room.ID,
|
|
|
|
UserID: joiningUser.ID,
|
2023-04-19 16:50:33 +02:00
|
|
|
ServerNames: []spec.ServerName{serverA},
|
2022-05-17 14:23:35 +02:00
|
|
|
}, &resp)
|
|
|
|
if resp.JoinedVia != serverA {
|
|
|
|
t.Errorf("PerformJoin: joined via %v want %v", resp.JoinedVia, serverA)
|
|
|
|
}
|
|
|
|
if resp.LastError != nil {
|
|
|
|
t.Fatalf("PerformJoin: returned error: %+v", *resp.LastError)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a keyserver key change event and ensure we try to send it out. If we don't, then the
|
|
|
|
// federationapi is incorrectly waiting for an output room event to arrive to update the joined
|
|
|
|
// hosts table.
|
2023-02-20 14:58:03 +01:00
|
|
|
key := userapi.DeviceMessage{
|
|
|
|
Type: userapi.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &userapi.DeviceKeys{
|
2022-05-17 14:23:35 +02:00
|
|
|
UserID: joiningUser.ID,
|
|
|
|
DeviceID: "MY_DEVICE",
|
|
|
|
DisplayName: "BLARGLE",
|
|
|
|
KeyJSON: []byte(`{}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to marshal device message: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &nats.Msg{
|
2023-03-22 09:21:32 +01:00
|
|
|
Subject: cfg.Global.JetStream.Prefixed(jetstream.OutputKeyChangeEvent),
|
2022-05-17 14:23:35 +02:00
|
|
|
Header: nats.Header{},
|
|
|
|
Data: b,
|
|
|
|
}
|
|
|
|
msg.Header.Set(jetstream.UserID, key.UserID)
|
|
|
|
|
|
|
|
testrig.MustPublishMsgs(t, jsctx, msg)
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2022-08-05 10:19:33 +02:00
|
|
|
fc.fedClientMutex.Lock()
|
|
|
|
defer fc.fedClientMutex.Unlock()
|
2022-05-17 14:23:35 +02:00
|
|
|
if !fc.sentTxn {
|
|
|
|
t.Fatalf("did not send device list update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:57:59 +02:00
|
|
|
// Tests that event IDs with '/' in them (escaped as %2F) are correctly passed to the right handler and don't 404.
|
|
|
|
// Relevant for v3 rooms and a cause of flakey sytests as the IDs are randomly generated.
|
|
|
|
func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
roomVer gomatrixserverlib.RoomVersion
|
|
|
|
eventJSON string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
eventJSON: `{"auth_events":[["$Nzfbrhc3oaYVKzGM:localhost",{"sha256":"BCBHOgB4qxLPQkBd6th8ydFSyqjth/LF99VNjYffOQ0"}],["$EZzkD2BH1Gtm5v1D:localhost",{"sha256":"3dLUnDBs8/iC5DMw/ydKtmAqVZtzqqtHpsjsQPk7GJA"}]],"content":{"body":"Test Message"},"depth":11,"event_id":"$mGiPO3oGjQfCkIUw:localhost","hashes":{"sha256":"h+t+4DwIBC9UNyJ3jzyAQAAl4H3yQHVuHrm2S1JZizU"},"origin":"localhost","origin_server_ts":0,"prev_events":[["$tFr64vpiSHdLU0Qr:localhost",{"sha256":"+R07ZrIs4c4tjPFE+tmcYIGUfeLGFI/4e0OITb9uEcM"}]],"room_id":"!roomid:localhost","sender":"@userid:localhost","signatures":{"localhost":{"ed25519:auto":"LYFr/rW9m5/7UKBQMF5qWnG82He4VGsRESUgDmvkn5DrJRyS4TLL/7zl0Lymn3pa3q2yaTO74LQX/CRotqG1BA"}},"type":"m.room.message"}`,
|
|
|
|
roomVer: gomatrixserverlib.RoomVersionV1,
|
|
|
|
},
|
|
|
|
// single / (handlers which do not UseEncodedPath will fail this test)
|
|
|
|
// EventID: $0SFh2WJbjBs3OT+E0yl95giDKo/3Zp52HsHUUk4uPyg
|
|
|
|
{
|
|
|
|
eventJSON: `{"auth_events":["$x4MKEPRSF6OGlo0qpnsP3BfSmYX5HhVlykOsQH3ECyg","$BcEcbZnlFLB5rxSNSZNBn6fO3jU/TKAJ79wfKyCQLiU"],"content":{"body":"Test Message"},"depth":8,"hashes":{"sha256":"dfK0MBn1RZZqCVJqWsn/MGY7QJHjQcwqF0unOonLCTU"},"origin":"localhost","origin_server_ts":0,"prev_events":["$1SwcZ1XY/Y8yKLjP4DzAOHN5WFBcDAZxb5vFDnW2ubA"],"room_id":"!roomid:localhost","sender":"@userid:localhost","signatures":{"localhost":{"ed25519:auto":"INOjuWMg+GmFkUpmzhMB0bqLNs73mSvwldY1ftYIQ/B3lD9soD2OMG3AF+wgZW/I8xqzY4DOHfbnbUeYPf67BA"}},"type":"m.room.message"}`,
|
|
|
|
roomVer: gomatrixserverlib.RoomVersionV3,
|
|
|
|
},
|
|
|
|
// multiple /
|
|
|
|
// EventID: $OzENBCuVv/fnRAYCeQudIon/84/V5pxtEjQMTgi3emk
|
|
|
|
{
|
|
|
|
eventJSON: `{"auth_events":["$x4MKEPRSF6OGlo0qpnsP3BfSmYX5HhVlykOsQH3ECyg","$BcEcbZnlFLB5rxSNSZNBn6fO3jU/TKAJ79wfKyCQLiU"],"content":{"body":"Test Message"},"depth":2,"hashes":{"sha256":"U5+WsiJAhiEM88J8HTjuUjPImVGVzDFD3v/WS+jb2f0"},"origin":"localhost","origin_server_ts":0,"prev_events":["$BcEcbZnlFLB5rxSNSZNBn6fO3jU/TKAJ79wfKyCQLiU"],"room_id":"!roomid:localhost","sender":"@userid:localhost","signatures":{"localhost":{"ed25519:auto":"tKS469e9+wdWPEKB/LbBJWQ8vfOOdKgTWER5IwbSAH1CxmLvkCziUsgVu85zfzDSLoUi5mU5FHLiMTC6P/qICw"}},"type":"m.room.message"}`,
|
|
|
|
roomVer: gomatrixserverlib.RoomVersionV3,
|
|
|
|
},
|
|
|
|
// two slashes (handlers which clean paths before UseEncodedPath will fail this test)
|
|
|
|
// EventID: $EmwNBlHoSOVmCZ1cM//yv/OvxB6r4OFEIGSJea7+Amk
|
|
|
|
{
|
|
|
|
eventJSON: `{"auth_events":["$x4MKEPRSF6OGlo0qpnsP3BfSmYX5HhVlykOsQH3ECyg","$BcEcbZnlFLB5rxSNSZNBn6fO3jU/TKAJ79wfKyCQLiU"],"content":{"body":"Test Message"},"depth":3917,"hashes":{"sha256":"cNAWtlHIegrji0mMA6x1rhpYCccY8W1NsWZqSpJFhjs"},"origin":"localhost","origin_server_ts":0,"prev_events":["$4GDB0bVjkWwS3G4noUZCq5oLWzpBYpwzdMcf7gj24CI"],"room_id":"!roomid:localhost","sender":"@userid:localhost","signatures":{"localhost":{"ed25519:auto":"NKym6Kcy3u9mGUr21Hjfe3h7DfDilDhN5PqztT0QZ4NTZ+8Y7owseLolQVXp+TvNjecvzdDywsXXVvGiuQiWAQ"}},"type":"m.room.message"}`,
|
|
|
|
roomVer: gomatrixserverlib.RoomVersionV3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg, processCtx, close := testrig.CreateConfig(t, test.DBTypeSQLite)
|
|
|
|
defer close()
|
|
|
|
routers := httputil.NewRouters()
|
|
|
|
|
|
|
|
_, privKey, _ := ed25519.GenerateKey(nil)
|
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID("ed25519:auto")
|
2023-04-19 16:50:33 +02:00
|
|
|
cfg.Global.ServerName = spec.ServerName("localhost")
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg.Global.PrivateKey = privKey
|
|
|
|
cfg.Global.JetStream.InMemory = true
|
|
|
|
keyRing := &test.NopJSONVerifier{}
|
|
|
|
natsInstance := jetstream.NATSInstance{}
|
|
|
|
// TODO: This is pretty fragile, as if anything calls anything on these nils this test will break.
|
|
|
|
// Unfortunately, it makes little sense to instantiate these dependencies when we just want to test routing.
|
2023-05-03 11:21:27 +02:00
|
|
|
federationapi.AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, nil, keyRing, nil, &internal.FederationInternalAPI{}, caching.DisableMetrics)
|
2023-03-22 09:21:32 +01:00
|
|
|
baseURL, cancel := test.ListenAndServe(t, routers.Federation, true)
|
|
|
|
defer cancel()
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName := spec.ServerName(strings.TrimPrefix(baseURL, "https://"))
|
2023-03-22 09:21:32 +01:00
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
fedCli := fclient.NewFederationClient(
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg.Global.SigningIdentities(),
|
2023-04-06 10:55:01 +02:00
|
|
|
fclient.WithSkipVerify(true),
|
2023-03-22 09:21:32 +01:00
|
|
|
)
|
|
|
|
|
2020-06-15 17:57:59 +02:00
|
|
|
for _, tc := range testCases {
|
2023-04-21 18:06:29 +02:00
|
|
|
ev, err := gomatrixserverlib.MustGetRoomVersion(tc.roomVer).NewEventFromTrustedJSON([]byte(tc.eventJSON), false)
|
2020-06-15 17:57:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to parse event: %s", err)
|
|
|
|
}
|
2023-04-27 13:54:20 +02:00
|
|
|
invReq, err := fclient.NewInviteV2Request(ev, nil)
|
2020-06-15 17:57:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create invite v2 request: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2022-11-15 16:05:23 +01:00
|
|
|
_, err = fedCli.SendInviteV2(context.Background(), cfg.Global.ServerName, serverName, invReq)
|
2020-06-15 17:57:59 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected an error, got none")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gerr, ok := err.(gomatrix.HTTPError)
|
|
|
|
if !ok {
|
2022-05-17 14:23:35 +02:00
|
|
|
t.Errorf("failed to cast response error as gomatrix.HTTPError: %s", err)
|
2020-06-15 17:57:59 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Logf("Error: %+v", gerr)
|
|
|
|
if gerr.Code == 404 {
|
|
|
|
t.Errorf("invite event resulted in a 404")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 19:14:29 +01:00
|
|
|
|
|
|
|
func TestNotaryServer(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
httpBody string
|
|
|
|
pubKeyRequest *gomatrixserverlib.PublicKeyNotaryLookupRequest
|
|
|
|
validateFunc func(t *testing.T, response util.JSONResponse)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty httpBody",
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.Code)
|
|
|
|
nk, ok := resp.JSON.(spec.MatrixError)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, spec.ErrorBadJSON, nk.ErrCode)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid but empty httpBody",
|
|
|
|
httpBody: "{}",
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
want := util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: routing.NotaryKeysResponse{ServerKeys: []json.RawMessage{}},
|
|
|
|
}
|
|
|
|
assert.Equal(t, want, resp)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "request all keys using an empty criteria",
|
|
|
|
httpBody: `{"server_keys":{"servera":{}}}`,
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
nk, ok := resp.JSON.(routing.NotaryKeysResponse)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "servera", gjson.GetBytes(nk.ServerKeys[0], "server_name").Str)
|
|
|
|
assert.True(t, gjson.GetBytes(nk.ServerKeys[0], "verify_keys.ed25519:someID").Exists())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "request all keys using null as the criteria",
|
|
|
|
httpBody: `{"server_keys":{"servera":null}}`,
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
nk, ok := resp.JSON.(routing.NotaryKeysResponse)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "servera", gjson.GetBytes(nk.ServerKeys[0], "server_name").Str)
|
|
|
|
assert.True(t, gjson.GetBytes(nk.ServerKeys[0], "verify_keys.ed25519:someID").Exists())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "request specific key",
|
|
|
|
httpBody: `{"server_keys":{"servera":{"ed25519:someID":{}}}}`,
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
nk, ok := resp.JSON.(routing.NotaryKeysResponse)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "servera", gjson.GetBytes(nk.ServerKeys[0], "server_name").Str)
|
|
|
|
assert.True(t, gjson.GetBytes(nk.ServerKeys[0], "verify_keys.ed25519:someID").Exists())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "request multiple servers",
|
|
|
|
httpBody: `{"server_keys":{"servera":{"ed25519:someID":{}},"serverb":{"ed25519:someID":{}}}}`,
|
|
|
|
validateFunc: func(t *testing.T, resp util.JSONResponse) {
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
nk, ok := resp.JSON.(routing.NotaryKeysResponse)
|
|
|
|
assert.True(t, ok)
|
|
|
|
wantServers := map[string]struct{}{
|
|
|
|
"servera": {},
|
|
|
|
"serverb": {},
|
|
|
|
}
|
|
|
|
for _, js := range nk.ServerKeys {
|
|
|
|
serverName := gjson.GetBytes(js, "server_name").Str
|
|
|
|
_, ok = wantServers[serverName]
|
|
|
|
assert.True(t, ok, "unexpected servername: %s", serverName)
|
|
|
|
delete(wantServers, serverName)
|
|
|
|
assert.True(t, gjson.GetBytes(js, "verify_keys.ed25519:someID").Exists())
|
|
|
|
}
|
|
|
|
if len(wantServers) > 0 {
|
|
|
|
t.Fatalf("expected response to also contain: %#v", wantServers)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
cfg, processCtx, close := testrig.CreateConfig(t, dbType)
|
|
|
|
defer close()
|
|
|
|
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
|
|
|
caches := caching.NewRistrettoCache(128*1024*1024, time.Hour, caching.DisableMetrics)
|
|
|
|
natsInstance := jetstream.NATSInstance{}
|
|
|
|
fc := &fedClient{
|
|
|
|
keys: map[spec.ServerName]struct {
|
|
|
|
key ed25519.PrivateKey
|
|
|
|
keyID gomatrixserverlib.KeyID
|
|
|
|
}{
|
|
|
|
"servera": {
|
|
|
|
key: test.PrivateKeyA,
|
|
|
|
keyID: "ed25519:someID",
|
|
|
|
},
|
|
|
|
"serverb": {
|
|
|
|
key: test.PrivateKeyB,
|
|
|
|
keyID: "ed25519:someID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fedAPI := federationapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, fc, nil, caches, nil, true)
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tc.httpBody))
|
|
|
|
req.Host = string(cfg.Global.ServerName)
|
|
|
|
|
|
|
|
resp := routing.NotaryKeys(req, &cfg.FederationAPI, fedAPI, tc.pubKeyRequest)
|
|
|
|
// assert that we received the expected response
|
|
|
|
tc.validateFunc(t, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|