aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkegsay <kegan@matrix.org>2022-05-11 11:29:23 +0100
committerGitHub <noreply@github.com>2022-05-11 11:29:23 +0100
commitc15bfefd0dbbd9619c2606b59b784f2a7926ca20 (patch)
tree528579b85f7c504430d4e2d05485d02a9fbc4b4d
parent6db08b2874307c516b10ef9c9e996807fbfdb1ff (diff)
Add RoomExists flag to QueryMembershipForUser (#2450)
Fixes https://github.com/matrix-org/complement/pull/369
-rw-r--r--clientapi/routing/membership.go12
-rw-r--r--clientapi/routing/state.go6
-rw-r--r--roomserver/api/query.go1
-rw-r--r--roomserver/internal/query/query.go4
-rw-r--r--syncapi/routing/context.go6
-rw-r--r--syncapi/routing/messages.go14
6 files changed, 38 insertions, 5 deletions
diff --git a/clientapi/routing/membership.go b/clientapi/routing/membership.go
index cfdf6f2d..77f627eb 100644
--- a/clientapi/routing/membership.go
+++ b/clientapi/routing/membership.go
@@ -188,6 +188,12 @@ func SendUnban(
if err != nil {
return util.ErrorResponse(err)
}
+ if !queryRes.RoomExists {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: jsonerror.Forbidden("room does not exist"),
+ }
+ }
// unban is only valid if the user is currently banned
if queryRes.Membership != "ban" {
return util.JSONResponse{
@@ -471,6 +477,12 @@ func SendForget(
logger.WithError(err).Error("QueryMembershipForUser: could not query membership for user")
return jsonerror.InternalServerError()
}
+ if !membershipRes.RoomExists {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: jsonerror.Forbidden("room does not exist"),
+ }
+ }
if membershipRes.IsInRoom {
return util.JSONResponse{
Code: http.StatusBadRequest,
diff --git a/clientapi/routing/state.go b/clientapi/routing/state.go
index c6e9e91d..12984c39 100644
--- a/clientapi/routing/state.go
+++ b/clientapi/routing/state.go
@@ -56,6 +56,12 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
return jsonerror.InternalServerError()
}
+ if !stateRes.RoomExists {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: jsonerror.Forbidden("room does not exist"),
+ }
+ }
// Look at the room state and see if we have a history visibility event
// that marks the room as world-readable. If we don't then we assume that
diff --git a/roomserver/api/query.go b/roomserver/api/query.go
index ef2e6bb5..afafb87c 100644
--- a/roomserver/api/query.go
+++ b/roomserver/api/query.go
@@ -122,6 +122,7 @@ type QueryMembershipForUserResponse struct {
Membership string `json:"membership"`
// True if the user asked to forget this room.
IsRoomForgotten bool `json:"is_room_forgotten"`
+ RoomExists bool `json:"room_exists"`
}
// QueryMembershipsForRoomRequest is a request to QueryMembershipsForRoom
diff --git a/roomserver/internal/query/query.go b/roomserver/internal/query/query.go
index 5b33ec3c..d25bdc37 100644
--- a/roomserver/internal/query/query.go
+++ b/roomserver/internal/query/query.go
@@ -169,8 +169,10 @@ func (r *Queryer) QueryMembershipForUser(
return err
}
if info == nil {
- return fmt.Errorf("QueryMembershipForUser: unknown room %s", request.RoomID)
+ response.RoomExists = false
+ return nil
}
+ response.RoomExists = true
membershipEventNID, stillInRoom, isRoomforgotten, err := r.DB.GetMembership(ctx, info.RoomNID, request.UserID)
if err != nil {
diff --git a/syncapi/routing/context.go b/syncapi/routing/context.go
index 87cc2aae..96438e18 100644
--- a/syncapi/routing/context.go
+++ b/syncapi/routing/context.go
@@ -73,6 +73,12 @@ func Context(
logrus.WithError(err).Error("unable to query membership")
return jsonerror.InternalServerError()
}
+ if !membershipRes.RoomExists {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: jsonerror.Forbidden("room does not exist"),
+ }
+ }
stateFilter := gomatrixserverlib.StateFilter{
Limit: 100,
diff --git a/syncapi/routing/messages.go b/syncapi/routing/messages.go
index b0c990ec..e55c661d 100644
--- a/syncapi/routing/messages.go
+++ b/syncapi/routing/messages.go
@@ -68,10 +68,16 @@ func OnIncomingMessagesRequest(
var err error
// check if the user has already forgotten about this room
- isForgotten, err := checkIsRoomForgotten(req.Context(), roomID, device.UserID, rsAPI)
+ isForgotten, roomExists, err := checkIsRoomForgotten(req.Context(), roomID, device.UserID, rsAPI)
if err != nil {
return jsonerror.InternalServerError()
}
+ if !roomExists {
+ return util.JSONResponse{
+ Code: http.StatusForbidden,
+ JSON: jsonerror.Forbidden("room does not exist"),
+ }
+ }
if isForgotten {
return util.JSONResponse{
@@ -244,17 +250,17 @@ func OnIncomingMessagesRequest(
}
}
-func checkIsRoomForgotten(ctx context.Context, roomID, userID string, rsAPI api.SyncRoomserverAPI) (bool, error) {
+func checkIsRoomForgotten(ctx context.Context, roomID, userID string, rsAPI api.SyncRoomserverAPI) (forgotten bool, exists bool, err error) {
req := api.QueryMembershipForUserRequest{
RoomID: roomID,
UserID: userID,
}
resp := api.QueryMembershipForUserResponse{}
if err := rsAPI.QueryMembershipForUser(ctx, &req, &resp); err != nil {
- return false, err
+ return false, false, err
}
- return resp.IsRoomForgotten, nil
+ return resp.IsRoomForgotten, resp.RoomExists, nil
}
// retrieveEvents retrieves events from the local database for a request on