Pass join errors through internal API boundaries (#1157)

* Pass join errors through internal API boundaries

Required for certain invite sytests. We will need to think of a
better way of handling this going forwards.

* Include m.room.avatar in stripped state; handle trailing slashes when GETing state events

* Update whitelist

* Update whitelist
This commit is contained in:
Kegsay 2020-06-24 09:59:59 +01:00 committed by GitHub
parent 1f93427ed9
commit 0577bfca55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 6 deletions

View file

@ -15,8 +15,10 @@
package routing
import (
"errors"
"net/http"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
@ -52,7 +54,8 @@ func JoinRoomByIDOrAlias(
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
} else {
// Request our profile content to populate the request content with.
profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
var profile *authtypes.Profile
profile, err = accountDB.GetProfileByLocalpart(req.Context(), localpart)
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("accountDB.GetProfileByLocalpart failed")
} else {
@ -62,11 +65,32 @@ func JoinRoomByIDOrAlias(
}
// Ask the roomserver to perform the join.
if err := rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes); err != nil {
err = rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes)
// Handle known errors first, if this is 0 then there will be no matches (eg on success)
switch joinRes.Error {
case roomserverAPI.JoinErrorBadRequest:
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown(err.Error()),
JSON: jsonerror.Unknown(joinRes.ErrMsg),
}
case roomserverAPI.JoinErrorNoRoom:
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(joinRes.ErrMsg),
}
case roomserverAPI.JoinErrorNotAllowed:
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(joinRes.ErrMsg),
}
}
// this is always populated on generic errors
if joinRes.ErrMsg != "" {
return util.ErrorResponse(errors.New(joinRes.ErrMsg))
}
// this is set on network errors in polylith mode
if err != nil {
return util.ErrorResponse(err)
}
return util.JSONResponse{

View file

@ -159,13 +159,18 @@ func Setup(
return OnIncomingStateRequest(req.Context(), rsAPI, vars["roomID"])
})).Methods(http.MethodGet, http.MethodOptions)
r0mux.Handle("/rooms/{roomID}/state/{type}", httputil.MakeAuthAPI("room_state", userAPI, func(req *http.Request, device *api.Device) util.JSONResponse {
r0mux.Handle("/rooms/{roomID}/state/{type:[^/]+/?}", httputil.MakeAuthAPI("room_state", userAPI, func(req *http.Request, device *api.Device) util.JSONResponse {
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil {
return util.ErrorResponse(err)
}
// If there's a trailing slash, remove it
eventType := vars["type"]
if strings.HasSuffix(eventType, "/") {
eventType = eventType[:len(eventType)-1]
}
eventFormat := req.URL.Query().Get("format") == "event"
return OnIncomingStateTypeRequest(req.Context(), rsAPI, vars["roomID"], vars["type"], "", eventFormat)
return OnIncomingStateTypeRequest(req.Context(), rsAPI, vars["roomID"], eventType, "", eventFormat)
})).Methods(http.MethodGet, http.MethodOptions)
r0mux.Handle("/rooms/{roomID}/state/{type}/{stateKey}", httputil.MakeAuthAPI("room_state", userAPI, func(req *http.Request, device *api.Device) util.JSONResponse {