From c554bbd9773dbb9e57711d926afcf1dfd1f43f3d Mon Sep 17 00:00:00 2001 From: Shota Gvinepadze Date: Mon, 31 Jan 2022 17:34:18 +0400 Subject: [PATCH] [MM-41263] Fix stackoverflow error in saml login (#19423) Automatic Merge --- i18n/en.json | 4 ++++ web/saml.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/i18n/en.json b/i18n/en.json index 88c04e4e59..196af13fc3 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3823,6 +3823,10 @@ "id": "api.user.authorize_oauth_user.response.app_error", "translation": "Received invalid response from OAuth service provider." }, + { + "id": "api.user.authorize_oauth_user.saml_response_too_long.app_error", + "translation": "SAML response is too long" + }, { "id": "api.user.authorize_oauth_user.service.app_error", "translation": "Token request to {{.Service}} failed." diff --git a/web/saml.go b/web/saml.go index 635923d470..ba4d11634b 100644 --- a/web/saml.go +++ b/web/saml.go @@ -16,6 +16,8 @@ import ( "github.com/mattermost/mattermost-server/v6/utils" ) +const maxSAMLResponseSize = 2 * 1024 * 1024 // 2MB + func (w *Web) InitSaml() { w.MainRouter.Handle("/login/sso/saml", w.APIHandler(loginWithSaml)).Methods("GET") w.MainRouter.Handle("/login/sso/saml", w.APIHandlerTrustRequester(completeSaml)).Methods("POST") @@ -122,6 +124,13 @@ func completeSaml(c *Context, w http.ResponseWriter, r *http.Request) { } } + if len(encodedXML) > maxSAMLResponseSize { + err := model.NewAppError("completeSaml", "api.user.authorize_oauth_user.saml_response_too_long.app_error", nil, "SAML response is too long", http.StatusBadRequest) + mlog.Error(err.Error()) + handleError(err) + return + } + user, err := samlInterface.DoLogin(c.AppContext, encodedXML, relayProps) if err != nil { c.LogAudit("fail")