fix(middleware): log rejected CSRF requests with the trusted client address (#6692)

A panel or API request rejected for a missing or wrong CSRF token got a bare
403 and left nothing in the log, so an operator chasing failed logins from a
stale tab or a script had no trace. CSRFMiddleware now logs a warning with the
method, path and client address. The address comes from getRemoteIp, which
honours the trusted-proxy list, instead of gin's ClientIP, which trusts any
peer's X-Forwarded-For.
This commit is contained in:
ericanannn-blip
2026-10-24 10:51:21 +08:00
committed by GitHub
parent 864cbd234a
commit 3051aebb28
6 changed files with 53 additions and 12 deletions
+2 -2
View File
@@ -16,7 +16,7 @@ import (
func TestCSRFMiddlewareAllowsSafeMethods(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(CSRFMiddleware())
router.Use(CSRFMiddleware(func(*gin.Context) string { return "" }))
router.GET("/safe", func(c *gin.Context) {
c.String(http.StatusOK, "ok")
})
@@ -42,7 +42,7 @@ func TestCSRFMiddlewareRejectsMissingTokenAndAcceptsValidToken(t *testing.T) {
}
c.String(http.StatusOK, token)
})
router.POST("/submit", CSRFMiddleware(), func(c *gin.Context) {
router.POST("/submit", CSRFMiddleware(func(*gin.Context) string { return "" }), func(c *gin.Context) {
c.String(http.StatusOK, "ok")
})