CSRF

CSRF middleware for Fiber that provides Cross-site request forgery protection by passing a csrf token via cookies. This cookie value will be used to compare against the client csrf token on requests, other than those defined as “safe” by RFC7231 (GET, HEAD, OPTIONS, or TRACE). When the csrf token is invalid, this middleware will return the fiber.ErrForbidden error. When no _csrf cookie is set, or the token has expired, a new token will be generated and _csrf cookie set.

Signatures

  1. func New(config ...Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/csrf"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Initialize default config
  2. app.Use(csrf.New())
  3. // Or extend your config for customization
  4. app.Use(csrf.New(csrf.Config{
  5. KeyLookup: "header:X-Csrf-Token",
  6. CookieName: "csrf_",
  7. CookieSameSite: "Strict",
  8. Expiration: 1 * time.Hour,
  9. KeyGenerator: utils.UUID,
  10. }))

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // KeyLookup is a string in the form of "<source>:<key>" that is used
  8. // to extract token from the request.
  9. // Possible values:
  10. // - "header:<name>"
  11. // - "query:<name>"
  12. // - "param:<name>"
  13. // - "form:<name>"
  14. // - "cookie:<name>"
  15. //
  16. // Optional. Default: "header:X-CSRF-Token"
  17. KeyLookup string
  18. // Name of the session cookie. This cookie will store session key.
  19. // Optional. Default value "_csrf".
  20. CookieName string
  21. // Domain of the CSRF cookie.
  22. // Optional. Default value "".
  23. CookieDomain string
  24. // Path of the CSRF cookie.
  25. // Optional. Default value "".
  26. CookiePath string
  27. // Indicates if CSRF cookie is secure.
  28. // Optional. Default value false.
  29. CookieSecure bool
  30. // Indicates if CSRF cookie is HTTP only.
  31. // Optional. Default value false.
  32. CookieHTTPOnly bool
  33. // Indicates if CSRF cookie is HTTP only.
  34. // Optional. Default value "Strict".
  35. CookieSameSite string
  36. // Expiration is the duration before csrf token will expire
  37. //
  38. // Optional. Default: 1 * time.Hour
  39. Expiration time.Duration
  40. // Store is used to store the state of the middleware
  41. //
  42. // Optional. Default: memory.New()
  43. Storage fiber.Storage
  44. // Context key to store generated CSRF token into context.
  45. // If left empty, token will not be stored in context.
  46. //
  47. // Optional. Default: ""
  48. ContextKey string
  49. // KeyGenerator creates a new CSRF token
  50. //
  51. // Optional. Default: utils.UUID
  52. KeyGenerator func() string
  53. }

Default Config

  1. var ConfigDefault = Config{
  2. KeyLookup: "header:X-Csrf-Token",
  3. CookieName: "csrf_",
  4. CookieSameSite: "Strict",
  5. Expiration: 1 * time.Hour,
  6. KeyGenerator: utils.UUID,
  7. }