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

Remove origin field from PDUs #341

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
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
71 changes: 15 additions & 56 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ type Event struct {
}

type eventFields struct {
RoomID string `json:"room_id"`
Sender string `json:"sender"`
Type string `json:"type"`
StateKey *string `json:"state_key"`
Content RawJSON `json:"content"`
Redacts string `json:"redacts"`
Depth int64 `json:"depth"`
Unsigned RawJSON `json:"unsigned"`
OriginServerTS Timestamp `json:"origin_server_ts"`
Origin ServerName `json:"origin"`
RoomID string `json:"room_id"`
Sender string `json:"sender"`
Type string `json:"type"`
StateKey *string `json:"state_key"`
Content RawJSON `json:"content"`
Redacts string `json:"redacts"`
Depth int64 `json:"depth"`
Unsigned RawJSON `json:"unsigned"`
OriginServerTS Timestamp `json:"origin_server_ts"`
//Origin ServerName `json:"origin"`
}

// Fields for room versions 1, 2.
Expand Down Expand Up @@ -171,8 +171,7 @@ func (e *eventFields) CacheCost() int {
len(e.Redacts) +
4 + // depth int64
cap(e.Unsigned) +
4 + // originserverts timestamp as uint64
len(e.Origin)
4 // originserverts timestamp as uint64
if e.StateKey != nil {
cost += len(*e.StateKey)
}
Expand Down Expand Up @@ -744,47 +743,19 @@ func (e *Event) CheckFields() error { // nolint: gocyclo
}
}

if _, err := checkID(fields.RoomID, "room", '!'); err != nil {
if err := checkID(fields.RoomID, "room", '!'); err != nil {
return err
}

origin := fields.Origin

senderDomain, err := checkID(fields.Sender, "user", '@')
if err != nil {
if err := checkID(fields.Sender, "user", '@'); err != nil {
return err
}

if origin != ServerName(senderDomain) {
// For the most part all events should be sent by a user on the
// originating server.
//
// However "m.room.member" events created from third-party invites
// are allowed to have a different sender because they have the same
// sender as the "m.room.third_party_invite" event they derived from.
// https://github.com/matrix-org/synapse/blob/v0.21.0/synapse/event_auth.py#L58-L64
//
// Also, some old versions of synapse had a bug wherein some
// joins/leaves used the origin and event id supplied by the helping
// server instead of the joining/leaving server.
//
// So in general we allow the sender to be different from the
// origin for m.room.member events. In any case, we check it was
// signed by both servers later.
if fields.Type != MRoomMember {
return fmt.Errorf(
"gomatrixserverlib: sender domain doesn't match origin: %q != %q",
senderDomain, origin,
)
}
}

return nil
}

func checkID(id, kind string, sigil byte) (domain string, err error) {
domain, err = domainFromID(id)
if err != nil {
func checkID(id, kind string, sigil byte) (err error) {
if _, err = domainFromID(id); err != nil {
return
}
if id[0] != sigil {
Expand All @@ -804,18 +775,6 @@ func checkID(id, kind string, sigil byte) (domain string, err error) {
return
}

// Origin returns the name of the server that sent the event
func (e *Event) Origin() ServerName {
switch fields := e.fields.(type) {
case eventFormatV1Fields:
return fields.Origin
case eventFormatV2Fields:
return fields.Origin
default:
panic(e.invalidFieldType())
}
}

func (e *Event) generateEventID() (eventID string, err error) {
var eventFormat EventFormat
eventFormat, err = e.roomVersion.EventFormat()
Expand Down
7 changes: 0 additions & 7 deletions eventcrypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ func (e *Event) VerifyEventSignatures(ctx context.Context, verifier JSONVerifier
}
needed[serverName] = struct{}{}

// TODO: This enables deprecation of the "origin" field as per MSC1664
// (https://github.com/matrix-org/matrix-doc/issues/1664) but really
// this has been done so that we can join rooms touched by Conduit again.
if serverName != e.Origin() && e.Origin() != "" {
needed[e.Origin()] = struct{}{}
}

// In room versions 1 and 2, we should also check that the server
// that created the event is included too. This is probably the
// same as the sender.
Expand Down
7 changes: 2 additions & 5 deletions eventcrypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ func TestVerifyAllEventSignatures(t *testing.T) {
}

// There should be two verification requests
if len(verifier.requests) != 2 {
t.Fatalf("Number of requests: got %d, want 2", len(verifier.requests))
if len(verifier.requests) != 1 {
t.Fatalf("Number of requests: got %d, want 1", len(verifier.requests))
}
wantContent, err := RedactEventJSON(eventJSON, RoomVersionV1)
if err != nil {
Expand All @@ -411,9 +411,6 @@ func TestVerifyAllEventSignatures(t *testing.T) {
if servers[0] != "localhost" {
t.Errorf("Verify server 0: got %s, want %s", servers[0], "localhost")
}
if servers[1] != "originserver" {
t.Errorf("Verify server 1: got %s, want %s", servers[1], "originserver")
}
}

func TestVerifyAllEventSignaturesForInvite(t *testing.T) {
Expand Down