Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for transactions containing a V1 event into a V10 room #743

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions tests/federation_room_send_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"context"
"encoding/json"
"fmt"
"net/url"
Expand All @@ -9,11 +10,14 @@ import (

"github.com/matrix-org/complement"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"golang.org/x/exp/slices"

"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/ct"
"github.com/matrix-org/complement/federation"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
Expand Down Expand Up @@ -249,3 +253,86 @@ func TestNetworkPartitionOrdering(t *testing.T) {
},
})
}

func TestAllowInvalidTransactionPDU(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)

alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})

srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
federation.HandleMakeSendJoinRequests(),
federation.HandleTransactionRequests(nil, nil),
)

cancel := srv.Listen()
defer cancel()

nexy := srv.UserID("nexy")

room := alice.MustCreateRoom(t, map[string]interface{}{
"preset": "public_chat",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the log below wants room version 10, specify it, as it otherwise defaults to the server implementations default (which might be unknown).

Suggested change
"preset": "public_chat",
"preset": "public_chat",
"room_version": "10",

"room_version": "10",
})
server_room := srv.MustJoinRoom(t, deployment, "hs1", room, nexy)


event := srv.MustCreateEvent(t, server_room, federation.Event{
Sender: nexy,
Type: "m.room.message",
Content: map[string]interface{}{"body": "hello i am nexy and i somehow broke the room into a v1 room"},
})
data, err := json.Marshal(event)
if err != nil {
t.Log("invalid json bwuh", err)
t.FailNow()
}
data, err = sjson.SetBytes(data, "event_id", event.EventID())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error check.

if err != nil {
t.Log("failed to add event id to json dict")
t.FailNow()
}

prev := event.PrevEventIDs()[0]
v1_prev_events := [][]interface{}{
{
prev,
map[string]interface{}{
"sha256": "abase64encodedsha256hashshouldbe43byteslong",
},
},
}

data, err = sjson.SetBytes(data, "prev_events", v1_prev_events)

client := srv.FederationClient(deployment)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, err := client.SendTransaction(ctx, gomatrixserverlib.Transaction{
TransactionID: gomatrixserverlib.TransactionID(fmt.Sprintf("complement-invalid-transaction-%d", time.Now().Nanosecond())),
Origin: spec.ServerName(srv.ServerName()),
Destination: "hs1",
PDUs: []json.RawMessage{data},
EDUs: nil,
})

if err != nil {
ct.Fatalf(t, "Invalid PDU in transaction made the request fail")
}

for eventID, e := range resp.PDUs {
if eventID != event.EventID() && eventID != event.EventID() {
ct.Fatalf(t, "Server responded with bogus event ID %s", eventID)
} else if e.Error == "" {
ct.Fatalf(t, "Server accepted event sent into a V10 room using V1 format")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for Dendrite, this is fine. When it receives a transaction, it extracts the room ID, asks the DB about the version and then executes NewEventFromUntrustedJSON (link), which for a V10 room (not specified in the test) runs newEventFromUntrustedJSONV2 (link), which strips out the event_id.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bogus prev_event too, now it will get the INFO log about not including the event id in the error dict instead

} else {
t.Logf("Correctly returned a 200 response including rejected event %s with error %s", eventID, e)
}
}

if len(resp.PDUs) == 0 {
t.Logf("INFO: Server didn't indicate an error when sending an invalid event id")
}

}