From 2e1e0c81a19830cbf2055a6d15c5d7424c2d0087 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Fri, 18 Feb 2022 09:23:58 +0100 Subject: [PATCH] Add check for admin account Enable server notices for CI Return same values as Synapse --- clientapi/routing/leaveroom.go | 6 ++++++ clientapi/routing/routing.go | 3 +-- clientapi/routing/server_notices.go | 8 +++++--- roomserver/api/perform.go | 2 ++ roomserver/internal/perform/perform_leave.go | 5 ++++- setup/config/config_global.go | 2 ++ 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/clientapi/routing/leaveroom.go b/clientapi/routing/leaveroom.go index 38cef118e..f40b4d85a 100644 --- a/clientapi/routing/leaveroom.go +++ b/clientapi/routing/leaveroom.go @@ -38,6 +38,12 @@ func LeaveRoomByID( // Ask the roomserver to perform the leave. if err := rsAPI.PerformLeave(req.Context(), &leaveReq, &leaveRes); err != nil { + if leaveRes.Code != 0 { + return util.JSONResponse{ + Code: leaveRes.Code, + JSON: jsonerror.Forbidden(leaveRes.Message), + } + } return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.Unknown(err.Error()), diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index db73304c5..6e62a7b8b 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -129,6 +129,7 @@ func Setup( // server notifications if cfg.Matrix.ServerNotices.Enabled { + logrus.Info("Enabling server notices at /_synapse/admin/v1/send_server_notice") serverNotificationSender, err := getSenderDevice(context.Background(), userAPI, accountDB, cfg) if err != nil { logrus.WithError(err).Fatal("unable to get account for sending sending server notices") @@ -170,8 +171,6 @@ func Setup( ).Methods(http.MethodPost, http.MethodOptions) } - - v3mux.Handle("/createRoom", httputil.MakeAuthAPI("createRoom", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse { if r := rateLimits.Limit(req); r != nil { diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index b485a0048..66c898d79 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -65,9 +65,11 @@ func SendServerNotice( txnID *string, txnCache *transactions.Cache, ) util.JSONResponse { - // TODO: Only allow admins to send notices - if !cfgNotices.Enabled { - return util.MessageResponse(http.StatusBadRequest, "Server notices are not enabled on this server.") + if device.AccountType != userapi.AccountTypeAdmin { + return util.JSONResponse{ + Code: http.StatusForbidden, + JSON: jsonerror.Forbidden("This API can only be used by admin users."), + } } if txnID != nil { diff --git a/roomserver/api/perform.go b/roomserver/api/perform.go index 51cbcb1ad..b6e3623a4 100644 --- a/roomserver/api/perform.go +++ b/roomserver/api/perform.go @@ -95,6 +95,8 @@ type PerformLeaveRequest struct { } type PerformLeaveResponse struct { + Code int `json:"code,omitempty"` + Message string `json:"message,omitempty"` } type PerformInviteRequest struct { diff --git a/roomserver/internal/perform/perform_leave.go b/roomserver/internal/perform/perform_leave.go index 9fbd4a4ce..49ddd4810 100644 --- a/roomserver/internal/perform/perform_leave.go +++ b/roomserver/internal/perform/perform_leave.go @@ -107,7 +107,10 @@ func (r *Leaver) performLeaveRoomByID( return nil, fmt.Errorf("unable to unmarshal tag content") } if _, ok = tags.Tags["m.server_notice"]; ok { - return nil, fmt.Errorf("Unable to reject server notice invite") + // mimic the returned values from Synapse + res.Message = "You cannot reject this invite" + res.Code = 403 + return nil, fmt.Errorf("You cannot reject this invite") } } } diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 1440eba02..b947f2076 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -75,6 +75,7 @@ func (c *Global) Defaults(generate bool) { c.Metrics.Defaults(generate) c.DNSCache.Defaults() c.Sentry.Defaults() + c.ServerNotices.Defaults(generate) } func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) { @@ -142,6 +143,7 @@ type ServerNotices struct { func (c *ServerNotices) Defaults(generate bool) { if generate { + c.Enabled = true c.LocalPart = "_server" c.DisplayName = "Server Alert" c.RoomName = "Server Alert"