Skip to content

Commit

Permalink
Remove origin field from PDUs
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Sep 26, 2022
1 parent 2217f6c commit 9434e2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 68 deletions.
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

0 comments on commit 9434e2a

Please sign in to comment.