diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go index 946103462..9713b7229 100644 --- a/federationapi/routing/invite.go +++ b/federationapi/routing/invite.go @@ -70,9 +70,10 @@ func Invite( } // Check that the event is signed by the server sending the request. + redacted := event.Redact() verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ ServerName: event.Origin(), - Message: event.Redact().JSON(), + Message: redacted.JSON(), AtTS: event.OriginServerTS(), }} verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) diff --git a/federationapi/routing/join.go b/federationapi/routing/join.go index 6ab2a164e..684899eaf 100644 --- a/federationapi/routing/join.go +++ b/federationapi/routing/join.go @@ -158,9 +158,10 @@ func SendJoin( } // Check that the event is signed by the server sending the request. + redacted := event.Redact() verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ ServerName: event.Origin(), - Message: event.Redact().JSON(), + Message: redacted.JSON(), AtTS: event.OriginServerTS(), }} verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) diff --git a/federationapi/routing/leave.go b/federationapi/routing/leave.go index f27989ff7..60d1acc6c 100644 --- a/federationapi/routing/leave.go +++ b/federationapi/routing/leave.go @@ -134,9 +134,10 @@ func SendLeave( } // Check that the event is signed by the server sending the request. + redacted := event.Redact() verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ ServerName: event.Origin(), - Message: event.Redact().JSON(), + Message: redacted.JSON(), AtTS: event.OriginServerTS(), }} verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) diff --git a/federationapi/routing/send.go b/federationapi/routing/send.go index 079e121f9..1cba18931 100644 --- a/federationapi/routing/send.go +++ b/federationapi/routing/send.go @@ -26,6 +26,8 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" ) // Send implements /_matrix/federation/v1/send/{txnID} @@ -39,6 +41,7 @@ func Send( keys gomatrixserverlib.KeyRing, federation *gomatrixserverlib.FederationClient, ) util.JSONResponse { + fmt.Println("TRANSACTION CONTENT IS", string(request.Content())) t := txnReq{ context: httpReq.Context(), @@ -47,13 +50,69 @@ func Send( keys: keys, federation: federation, } - if err := json.Unmarshal(request.Content(), &t); err != nil { + + // we need to work out what the room version is for each of these events + // or otherwise we will not be able to unmarshal them from the tx + pdus := gjson.GetBytes(request.Content(), "pdus") + if rest, err := sjson.DeleteBytes(request.Content(), "pdus"); err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.NotJSON("Extracting PDUs from JSON failed. " + err.Error()), + } + } else if err := json.Unmarshal(rest, &t); err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()), } } + fmt.Println("Number of PDUs:", len(t.PDUs)) + fmt.Println("Number of EDUs:", len(t.EDUs)) + + roomVersions := make(map[string]gomatrixserverlib.RoomVersion) + if pdus.Exists() { + for _, pdu := range pdus.Array() { + if rid := pdu.Get("room_id"); rid.Exists() { + // Look up the room version for this room if we don't already + // know it. + if _, ok := roomVersions[rid.String()]; !ok { + // Look up the room version for this room. + vReq := api.QueryRoomVersionForRoomIDRequest{RoomID: rid.String()} + vRes := api.QueryRoomVersionForRoomIDResponse{} + if query.QueryRoomVersionForRoomID(httpReq.Context(), &vReq, &vRes) == nil { + roomVersions[rid.String()] = vRes.RoomVersion + fmt.Println("Room version for", rid.String(), "is", vRes.RoomVersion) + } + } + + // Check if we know the room ID again. It's possible the previous + // step failed. + if roomVersion, ok := roomVersions[rid.String()]; ok { + fmt.Println("We know the room version for", rid.String()) + // Now unmarshal the event. + event, err := gomatrixserverlib.NewEventFromUntrustedJSON([]byte(pdu.Raw), roomVersion) + if err != nil { + fmt.Println("Couldn't create event:", err) + continue + } + + fmt.Println("Event", event.EventID(), "was created successfully") + fmt.Println("Auth events:", event.AuthEventIDs()) + fmt.Println("Prev events:", event.PrevEventIDs()) + + // Assuming nothing has gone wrong at this point, we can now + // append our newly formatted event into the transaction object. + t.PDUs = append(t.PDUs, event) + } else { + fmt.Println("We don't know the room version for", rid.String()) + } + } + } + } + + fmt.Println("Number of PDUs:", len(t.PDUs)) + fmt.Println("Number of EDUs:", len(t.EDUs)) + t.Origin = request.Origin() t.TransactionID = txnID t.Destination = cfg.Matrix.ServerName diff --git a/go.mod b/go.mod index 4e3bec63e..d293084dc 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/matrix-org/go-http-js-libp2p v0.0.0-20200304160008-4ec1129a00c4 github.com/matrix-org/go-sqlite3-js v0.0.0-20200304164012-aa524245b658 github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 - github.com/matrix-org/gomatrixserverlib v0.0.0-20200311120602-b35ce5fecaf9 + github.com/matrix-org/gomatrixserverlib v0.0.0-20200313131013-a22a1f205e65 github.com/matrix-org/naffka v0.0.0-20200312164717-c7dfc13211e2 github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7 github.com/mattn/go-sqlite3 v2.0.2+incompatible @@ -20,6 +20,7 @@ require ( github.com/sirupsen/logrus v1.4.2 github.com/tidwall/gjson v1.6.0 github.com/tidwall/pretty v1.0.1 // indirect + github.com/tidwall/sjson v1.0.3 github.com/uber/jaeger-client-go v2.22.1+incompatible github.com/uber/jaeger-lib v2.2.0+incompatible golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 diff --git a/go.sum b/go.sum index f8f459879..65e5cec3f 100644 --- a/go.sum +++ b/go.sum @@ -109,6 +109,12 @@ github.com/matrix-org/gomatrixserverlib v0.0.0-20200311110021-ac84b78b81be h1:4Z github.com/matrix-org/gomatrixserverlib v0.0.0-20200311110021-ac84b78b81be/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= github.com/matrix-org/gomatrixserverlib v0.0.0-20200311120602-b35ce5fecaf9 h1:/w9EejfY70MXpvuu4EgJ39Pu+Exb6q2zWT8DquhUvxU= github.com/matrix-org/gomatrixserverlib v0.0.0-20200311120602-b35ce5fecaf9/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313111420-d40c49b492f8 h1:ZzsZ/jI02jEYB0j8ymDrCZ3lqaFAVYWHT6z8bvfMsJc= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313111420-d40c49b492f8/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313114715-b0e6ba19500b h1:CNMV0q3/+ZfPu+IR8ZLOlhvin2QBL0CxVuC+4rPn7QU= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313114715-b0e6ba19500b/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313131013-a22a1f205e65 h1:aO0ma9VLj6ONwaSUcF5CkIgC8AbEBjujzJ8A/RGJMkQ= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200313131013-a22a1f205e65/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 h1:osLoFdOy+ChQqVUn2PeTDETFftVkl4w9t/OW18g3lnk= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A= github.com/matrix-org/naffka v0.0.0-20200312163934-bc6535bbe14e h1:TXjtFG6ywBxdWVTWbVw8iuseo0COu8OJxSTkmCTOfqg=