Skip to content

Commit 79fcff2

Browse files
authored
PseudoID fixes for complement tests (#417)
1 parent 162387a commit 79fcff2

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

eventcontent_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,13 @@ func TestMXIDMapping_SignValidate(t *testing.T) {
217217
assert.NoError(t, err)
218218

219219
// this should pass
220-
err = validateMXIDMappingSignature(context.Background(), ev, &StubVerifier{}, verImpl)
220+
evMapping, err := getMXIDMapping(ev)
221+
assert.NoError(t, err)
222+
err = validateMXIDMappingSignatures(context.Background(), ev, *evMapping, &StubVerifier{}, verImpl)
221223
assert.NoError(t, err)
222224

223225
// this fails, for some random reason
224-
err = validateMXIDMappingSignature(context.Background(), ev, &StubVerifier{
226+
err = validateMXIDMappingSignatures(context.Background(), ev, *evMapping, &StubVerifier{
225227
results: []VerifyJSONResult{{Error: fmt.Errorf("err")}},
226228
}, verImpl)
227229
assert.Error(t, err)
@@ -231,7 +233,6 @@ func TestMXIDMapping_SignValidate(t *testing.T) {
231233
ev, err = eb.Build(time.Now(), serverName, keyID, priv)
232234
assert.NoError(t, err)
233235

234-
err = validateMXIDMappingSignature(context.Background(), ev, &StubVerifier{}, verImpl)
236+
_, err = getMXIDMapping(ev)
235237
assert.Error(t, err)
236-
237238
}

eventcrypto.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ func VerifyEventSignatures(ctx context.Context, e PDU, verifier JSONVerifier, us
8585

8686
// Validate the MXIDMapping is signed correctly
8787
if verImpl.Version() == RoomVersionPseudoIDs && membership == spec.Join {
88-
err = validateMXIDMappingSignature(ctx, e, verifier, verImpl)
88+
mapping, err := getMXIDMapping(e)
89+
if err != nil {
90+
return err
91+
}
92+
err = validateMXIDMappingSignatures(ctx, e, *mapping, verifier, verImpl)
8993
if err != nil {
9094
return err
9195
}
@@ -154,28 +158,32 @@ func VerifyEventSignatures(ctx context.Context, e PDU, verifier JSONVerifier, us
154158
return nil
155159
}
156160

157-
// validateMXIDMappingSignature validates that the MXIDMapping is correctly signed
158-
func validateMXIDMappingSignature(ctx context.Context, e PDU, verifier JSONVerifier, verImpl IRoomVersion) error {
161+
func getMXIDMapping(e PDU) (*MXIDMapping, error) {
159162
var content MemberContent
160163
err := json.Unmarshal(e.Content(), &content)
161164
if err != nil {
162-
return err
165+
return nil, err
163166
}
164167

165168
// if there is no mapping, we can't check the signature
166169
if content.MXIDMapping == nil {
167-
return fmt.Errorf("missing mxid_mapping, unable to validate event")
170+
return nil, fmt.Errorf("missing mxid_mapping")
168171
}
169172

170-
var toVerify []VerifyJSONRequest
173+
return content.MXIDMapping, nil
174+
}
171175

172-
mapping, err := json.Marshal(content.MXIDMapping)
176+
// validateMXIDMappingSignatures validates that the MXIDMapping is correctly signed
177+
func validateMXIDMappingSignatures(ctx context.Context, e PDU, mapping MXIDMapping, verifier JSONVerifier, verImpl IRoomVersion) error {
178+
mappingBytes, err := json.Marshal(mapping)
173179
if err != nil {
174180
return err
175181
}
176-
for s := range content.MXIDMapping.Signatures {
182+
183+
var toVerify []VerifyJSONRequest
184+
for s := range mapping.Signatures {
177185
v := VerifyJSONRequest{
178-
Message: mapping,
186+
Message: mappingBytes,
179187
AtTS: e.OriginServerTS(),
180188
ServerName: s,
181189
ValidityCheckingFunc: verImpl.SignatureValidityCheck,

handlejoin.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/matrix-org/gomatrixserverlib/spec"
2424
"github.com/matrix-org/util"
25-
"github.com/tidwall/gjson"
2625
)
2726

2827
type HandleMakeJoinInput struct {
@@ -351,15 +350,14 @@ func HandleSendJoin(input HandleSendJoinInput) (*HandleSendJoinResponse, error)
351350
// validate the mxid_mapping of the event
352351
if input.RoomVersion == RoomVersionPseudoIDs {
353352
// validate the signature first
354-
if err = validateMXIDMappingSignature(input.Context, event, input.Verifier, verImpl); err != nil {
353+
mapping, err := getMXIDMapping(event)
354+
if err != nil {
355+
return nil, spec.BadJSON(err.Error())
356+
}
357+
if err = validateMXIDMappingSignatures(input.Context, event, *mapping, input.Verifier, verImpl); err != nil {
355358
return nil, spec.Forbidden(err.Error())
356359
}
357360

358-
mapping := MXIDMapping{}
359-
err = json.Unmarshal([]byte(gjson.GetBytes(input.JoinEvent, "content.mxid_mapping").Raw), &mapping)
360-
if err != nil {
361-
return nil, err
362-
}
363361
// store the user room public key -> userID mapping
364362
if err = input.StoreSenderIDFromPublicID(input.Context, mapping.UserRoomKey, mapping.UserID, input.RoomID); err != nil {
365363
return nil, err

performjoin.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,17 @@ func storeMXIDMappings(
304304
if ev.Type() != spec.MRoomMember {
305305
continue
306306
}
307-
mapping := MemberContent{}
308-
if err := json.Unmarshal(ev.Content(), &mapping); err != nil {
307+
mapping, err := getMXIDMapping(ev)
308+
if err != nil {
309309
return err
310310
}
311-
if mapping.MXIDMapping == nil {
312-
continue
313-
}
314311
// we already validated it is a valid roomversion, so this should be safe to use.
315312
verImpl := MustGetRoomVersion(ev.Version())
316-
if err := validateMXIDMappingSignature(ctx, ev, keyRing, verImpl); err != nil {
313+
if err := validateMXIDMappingSignatures(ctx, ev, *mapping, keyRing, verImpl); err != nil {
317314
logrus.WithError(err).Error("invalid signature for mxid_mapping")
318315
continue
319316
}
320-
if err := storeSenderID(ctx, ev.SenderID(), mapping.MXIDMapping.UserID, roomID); err != nil {
317+
if err := storeSenderID(ctx, ev.SenderID(), mapping.UserID, roomID); err != nil {
321318
return err
322319
}
323320
}

0 commit comments

Comments
 (0)