Csrf Middleware
📖 Official Go Fiber Documentation: https://docs.gofiber.io/api/middleware/csrf
⚠️ Web Security Note: This middleware will be ignored if used on a server that has an
HttpSecurityconfigured. See Web Security Integration for details.
⚠️ Execution Order: Because this middleware can depend on the
Sessionmiddleware to store its tokens, you must ensure thatSessionis included beforeCsrfin your struct embedding order so the session is initialized first.
Programmatic Configuration Interfaces
If your Controller implements any of the following methods, the Csrf middleware will automatically bind them:
CsrfNext() func(fiber.Ctx) boolCsrfExtractor() extractors.ExtractorCsrfKeyGenerator() func() stringCsrfErrorHandler() func(fiber.Ctx, error) errorCsrfConfig(config *csrf.Config)
Example: Configuring Csrf
Here is an example of a controller embedding the middleware and implementing the CsrfConfig interface to mutate the underlying Fiber configuration:
package controllers
import (
"github.com/gofiber/fiber/v3/middleware/csrf"
. "github.com/BeamFoundry/rays-fiber/pkg/core"
. "github.com/BeamFoundry/rays/pkg/core"
. "github.com/BeamFoundry/rays/pkg/web"
. "github.com/BeamFoundry/rays-fiber/pkg/middleware"
)
type MyCsrfController struct {
FiberController
RequestMapping `Path:"/example"`
// 1. Embed the middleware
Csrf
}
// 2. Implement the configuration interface
func (this *MyCsrfController) CsrfConfig(config *csrf.Config) {
// Mutate the config directly for this controller
// config.SomeProperty = SomeValue
}
var _, _ = any(&MyCsrfController{}).(Stereotype)Default YAML Configuration
The framework applies these default settings to the "primary" server. To override them, copy the properties you want to change into your application.yaml under rays.fiber.servers.primary.csrf.
rays:
fiber:
servers:
primary:
csrf:
cookieName: "csrf_"
cookieDomain: ""
cookiePath: ""
cookieSameSite: "Lax"
trustedOrigins: []
idleTimeout: 30m
cookieSecure: false
cookieHTTPOnly: false
cookieSessionOnly: false
disableValueRedaction: false
singleUseToken: false