Refactor auth middleware (#36848)
Principles: let the caller decide what it needs, but not let the framework (middleware) guess what it should do. Then a lot of hacky code can be removed. And some FIXMEs can be fixed. This PR introduces a new kind of middleware: "PreMiddleware", it will be executed before all other middlewares on the same routing level, then a route can declare its options for other middlewares. By the way, allow the workflow badge to be accessed by Basic or OAuth2 auth. Fixes: https://github.com/go-gitea/gitea/pull/36830 Fixes: https://github.com/go-gitea/gitea/issues/36859
This commit is contained in:
@@ -53,18 +53,18 @@ func renderServerErrorPage(w http.ResponseWriter, req *http.Request, respCode in
|
||||
_, _ = io.Copy(w, outBuf)
|
||||
}
|
||||
|
||||
// RenderPanicErrorPage renders a 500 page, and it never panics
|
||||
func RenderPanicErrorPage(w http.ResponseWriter, req *http.Request, err any) {
|
||||
combinedErr := fmt.Sprintf("%v\n%s", err, log.Stack(2))
|
||||
log.Error("PANIC: %s", combinedErr)
|
||||
// renderPanicErrorPage renders a 500 page with the recovered panic value, it handles the stack trace, and it never panics
|
||||
func renderPanicErrorPage(w http.ResponseWriter, req *http.Request, recovered any) {
|
||||
combinedErr := fmt.Errorf("%v\n%s", recovered, log.Stack(2))
|
||||
log.Error("PANIC: %v", combinedErr)
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Error("Panic occurs again when rendering error page: %v. Stack:\n%s", err, log.Stack(2))
|
||||
log.Error("Panic occurs again when rendering error page: %v. Stack:\n%s", combinedErr, log.Stack(2))
|
||||
}
|
||||
}()
|
||||
|
||||
routing.UpdatePanicError(req.Context(), err)
|
||||
routing.UpdatePanicError(req.Context(), combinedErr)
|
||||
|
||||
plainMsg := "Internal Server Error"
|
||||
ctxData := middleware.GetContextData(req.Context())
|
||||
@@ -72,7 +72,7 @@ func RenderPanicErrorPage(w http.ResponseWriter, req *http.Request, err any) {
|
||||
// Otherwise, the 500-page may cause new panics, eg: cache.GetContextWithData, it makes the developer&users couldn't find the original panic.
|
||||
user, _ := ctxData[middleware.ContextDataKeySignedUser].(*user_model.User)
|
||||
if !setting.IsProd || (user != nil && user.IsAdmin) {
|
||||
plainMsg = "PANIC: " + combinedErr
|
||||
plainMsg = "PANIC: " + combinedErr.Error()
|
||||
ctxData["ErrorMsg"] = plainMsg
|
||||
}
|
||||
renderServerErrorPage(w, req, http.StatusInternalServerError, tplStatus500, ctxData, plainMsg)
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestRenderPanicErrorPage(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
req := &http.Request{URL: &url.URL{}, Header: http.Header{"Accept": []string{"text/html"}}}
|
||||
req = req.WithContext(reqctx.NewRequestContextForTest(t.Context()))
|
||||
RenderPanicErrorPage(w, req, errors.New("fake panic error (for test only)"))
|
||||
renderPanicErrorPage(w, req, errors.New("fake panic error (for test only)"))
|
||||
respContent := w.Body.String()
|
||||
assert.Contains(t, respContent, `class="page-content status-page-500"`)
|
||||
assert.Contains(t, respContent, `</html>`)
|
||||
|
||||
@@ -5,13 +5,13 @@ package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/cache"
|
||||
"code.gitea.io/gitea/modules/gtprof"
|
||||
"code.gitea.io/gitea/modules/httplib"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/reqctx"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/web/routing"
|
||||
@@ -63,8 +63,8 @@ func RequestContextHandler() func(h http.Handler) http.Handler {
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
RenderPanicErrorPage(respWriter, req, err) // it should never panic
|
||||
if recovered := recover(); recovered != nil {
|
||||
renderPanicErrorPage(respWriter, req, recovered) // it should never panic, and it handles the stack trace internally
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -130,7 +130,7 @@ func MustInitSessioner() func(next http.Handler) http.Handler {
|
||||
Domain: setting.SessionConfig.Domain,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("common.Sessioner failed: %v", err)
|
||||
log.Fatal("common.Sessioner failed: %v", err)
|
||||
}
|
||||
return middleware
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user