Avoid panic from reading CSRF of nil session pointer (#10554)
* Avoid panic from reading CSRF of nil session pointer * Reorganize CSRF Handling * Remove autoimport added by IDE * Remove unnecessary nil check * gofmt
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
24c8fdb1fa
Коммит
41fe33bbb1
@@ -102,34 +102,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if len(token) != 0 {
|
||||
session, err := c.App.GetSession(token)
|
||||
csrfCheckPassed := false
|
||||
|
||||
// CSRF Check
|
||||
if tokenLocation == app.TokenLocationCookie && h.RequireSession && !h.TrustRequester && r.Method != "GET" {
|
||||
csrfHeader := r.Header.Get(model.HEADER_CSRF_TOKEN)
|
||||
if csrfHeader == session.GetCSRF() {
|
||||
csrfCheckPassed = true
|
||||
} else if r.Header.Get(model.HEADER_REQUESTED_WITH) == model.HEADER_REQUESTED_WITH_XML {
|
||||
// ToDo(DSchalla) 2019/01/04: Remove after deprecation period and only allow CSRF Header (MM-13657)
|
||||
csrfErrorMessage := "CSRF Header check failed for request - Please upgrade your web application or custom app to set a CSRF Header"
|
||||
if *c.App.Config().ServiceSettings.ExperimentalStrictCSRFEnforcement {
|
||||
c.Log.Warn(csrfErrorMessage)
|
||||
} else {
|
||||
c.Log.Debug(csrfErrorMessage)
|
||||
csrfCheckPassed = true
|
||||
}
|
||||
}
|
||||
|
||||
if !csrfCheckPassed {
|
||||
token = ""
|
||||
session = nil
|
||||
c.Err = model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token+" Appears to be a CSRF attempt", http.StatusUnauthorized)
|
||||
}
|
||||
} else {
|
||||
csrfCheckPassed = true
|
||||
}
|
||||
|
||||
if csrfCheckPassed {
|
||||
if err != nil {
|
||||
c.Log.Info("Invalid session", mlog.Err(err))
|
||||
if err.StatusCode == http.StatusInternalServerError {
|
||||
@@ -148,6 +120,30 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if c.App.Srv.RateLimiter != nil && c.App.Srv.RateLimiter.UserIdRateLimit(c.App.Session.UserId, w) {
|
||||
return
|
||||
}
|
||||
|
||||
csrfCheckPassed := false
|
||||
|
||||
// CSRF Check
|
||||
if c.Err == nil && tokenLocation == app.TokenLocationCookie && h.RequireSession && !h.TrustRequester && r.Method != "GET" {
|
||||
csrfHeader := r.Header.Get(model.HEADER_CSRF_TOKEN)
|
||||
if csrfHeader == session.GetCSRF() {
|
||||
csrfCheckPassed = true
|
||||
} else if r.Header.Get(model.HEADER_REQUESTED_WITH) == model.HEADER_REQUESTED_WITH_XML {
|
||||
// ToDo(DSchalla) 2019/01/04: Remove after deprecation period and only allow CSRF Header (MM-13657)
|
||||
csrfErrorMessage := "CSRF Header check failed for request - Please upgrade your web application or custom app to set a CSRF Header"
|
||||
if *c.App.Config().ServiceSettings.ExperimentalStrictCSRFEnforcement {
|
||||
c.Log.Warn(csrfErrorMessage)
|
||||
} else {
|
||||
c.Log.Debug(csrfErrorMessage)
|
||||
csrfCheckPassed = true
|
||||
}
|
||||
}
|
||||
|
||||
if !csrfCheckPassed {
|
||||
token = ""
|
||||
c.App.Session = model.Session{}
|
||||
c.Err = model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token+" Appears to be a CSRF attempt", http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user