Refine SenderID/UserID usage

This commit is contained in:
Devon Hudson 2023-06-02 14:35:14 -06:00
parent 222b67bfeb
commit d81b63f556
No known key found for this signature in database
GPG key ID: CD06B18E77F6A628
3 changed files with 12 additions and 7 deletions

View file

@ -76,7 +76,7 @@ func SendRedaction(
// "Users may redact their own events, and any user with a power level greater than or equal // "Users may redact their own events, and any user with a power level greater than or equal
// to the redact power level of the room may redact events there" // to the redact power level of the room may redact events there"
// https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid // https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
allowedToRedact := ev.SenderID() == device.UserID allowedToRedact := ev.SenderID() == device.UserID // TODO: Should replace device.UserID with device...PerRoomKey
if !allowedToRedact { if !allowedToRedact {
plEvent := roomserverAPI.GetStateEvent(req.Context(), rsAPI, roomID, gomatrixserverlib.StateKeyTuple{ plEvent := roomserverAPI.GetStateEvent(req.Context(), rsAPI, roomID, gomatrixserverlib.StateKeyTuple{
EventType: spec.MRoomPowerLevels, EventType: spec.MRoomPowerLevels,

View file

@ -509,10 +509,15 @@ func (r *FederationInternalAPI) SendInvite(
event gomatrixserverlib.PDU, event gomatrixserverlib.PDU,
strippedState []gomatrixserverlib.InviteStrippedState, strippedState []gomatrixserverlib.InviteStrippedState,
) (gomatrixserverlib.PDU, error) { ) (gomatrixserverlib.PDU, error) {
_, origin, err := r.cfg.Matrix.SplitLocalID('@', event.SenderID()) inviter, err := event.UserID()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// For portable accounts, we need to verify the inviter domain is still associated with this server.
// The userID of the inviter may have changed to another server in which case we cannot send the invite.
if !r.cfg.Matrix.IsLocalServerName(inviter.Domain()) {
return nil, fmt.Errorf("the invite must be from a local user")
}
if event.StateKey() == nil { if event.StateKey() == nil {
return nil, errors.New("invite must be a state event") return nil, errors.New("invite must be a state event")
@ -542,7 +547,7 @@ func (r *FederationInternalAPI) SendInvite(
return nil, fmt.Errorf("gomatrixserverlib.NewInviteV2Request: %w", err) return nil, fmt.Errorf("gomatrixserverlib.NewInviteV2Request: %w", err)
} }
inviteRes, err := r.federation.SendInviteV2(ctx, origin, destination, inviteReq) inviteRes, err := r.federation.SendInviteV2(ctx, inviter.Domain(), destination, inviteReq)
if err != nil { if err != nil {
return nil, fmt.Errorf("r.federation.SendInviteV2: failed to send invite: %w", err) return nil, fmt.Errorf("r.federation.SendInviteV2: failed to send invite: %w", err)
} }

View file

@ -223,13 +223,13 @@ func SendLeave(
// Check that the sender belongs to the server that is sending us // Check that the sender belongs to the server that is sending us
// the request. By this point we've already asserted that the sender // the request. By this point we've already asserted that the sender
// and the state key are equal so we don't need to check both. // and the state key are equal so we don't need to check both.
var serverName spec.ServerName sender, err := event.UserID()
if _, serverName, err = gomatrixserverlib.SplitID('@', event.SenderID()); err != nil { if err != nil {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusForbidden, Code: http.StatusForbidden,
JSON: spec.Forbidden("The sender of the join is invalid"), JSON: spec.Forbidden("The sender of the join is invalid"),
} }
} else if serverName != request.Origin() { } else if sender.Domain() != request.Origin() {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusForbidden, Code: http.StatusForbidden,
JSON: spec.Forbidden("The sender does not match the server that originated the request"), JSON: spec.Forbidden("The sender does not match the server that originated the request"),
@ -291,7 +291,7 @@ func SendLeave(
} }
} }
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
ServerName: serverName, ServerName: sender.Domain(),
Message: redacted, Message: redacted,
AtTS: event.OriginServerTS(), AtTS: event.OriginServerTS(),
StrictValidityChecking: true, StrictValidityChecking: true,