Middleware

middleware/cors

github.com/gofiber/fiber/v3/middleware/cors

import "github.com/gofiber/fiber/v3/middleware/cors"

Variables

ConfigDefault is the default config

Source: middleware/cors/config.go:93

var ConfigDefault = Config{
	Next:                  nil,
	AllowOriginsFunc:      nil,
	AllowOrigins:          []string{"*"},
	DisableValueRedaction: false,
	AllowMethods: []string{
		fiber.MethodGet,
		fiber.MethodPost,
		fiber.MethodHead,
		fiber.MethodPut,
		fiber.MethodDelete,
		fiber.MethodPatch,
		fiber.MethodQuery,
	},
	AllowHeaders:        []string{},
	AllowCredentials:    false,
	ExposeHeaders:       []string{},
	MaxAge:              0,
	AllowPrivateNetwork: false,
}

Functions

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

Source: middleware/cors/cors.go:89

New creates a new middleware handler

Types

type Config

Source: middleware/cors/config.go:8

Config defines the config for middleware.

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c fiber.Ctx) bool

	// AllowOriginsFunc defines a function that will set the 'Access-Control-Allow-Origin'
	// response header to the 'origin' request header when returned true. This allows for
	// dynamic evaluation of allowed origins. Note if AllowCredentials is true, wildcard origins
	// will not have the 'Access-Control-Allow-Credentials' header set to 'true'.
	//
	// The function is called with the Origin request header exactly as the client sent it, and only
	// when that origin did not already match AllowOrigins. A true result takes effect only when the
	// value is a serialized origin (scheme + host) or the literal "null" string.
	// According to the CORS specification (https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS#origin),
	// browsers send "null" for privacy-sensitive contexts like sandboxed iframes or file:// URLs.
	//
	// Origins with userinfo, paths, queries, fragments, or wildcards are still passed to this
	// function, but a true result for them is discarded and no 'Access-Control-Allow-Origin'
	// header is set.
	//
	// Optional. Default: nil
	AllowOriginsFunc func(origin string) bool

	// AllowOrigins defines a list of origins that may access the resource.
	//
	// This supports wildcard matching for subdomains by prefixing the domain with a `*.`
	// e.g. "http://*.domain.com". This will allow all level of subdomains of domain.com to access the resource.
	//
	// If the special wildcard `"*"` is present in the list, all origins will be allowed.
	// An empty list also allows all origins, unless AllowOriginsFunc is set.
	//
	// Optional. Default value []string{"*"}
	AllowOrigins []string

	// AllowMethods defines a list methods allowed when accessing the resource.
	// This is used in response to a preflight request.
	//
	// Optional. Default value []string{"GET", "POST", "HEAD", "PUT", "DELETE", "PATCH", "QUERY"}
	AllowMethods []string

	// AllowHeaders defines a list of request headers that can be used when
	// making the actual request. This is in response to a preflight request.
	//
	// Optional. Default value []string{}
	AllowHeaders []string

	// ExposeHeaders defines an allowlist of headers that clients are allowed to
	// access.
	//
	// Optional. Default value []string{}.
	ExposeHeaders []string

	// MaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached.
	// If you pass MaxAge 0, Access-Control-Max-Age header will not be added and
	// browser will use 5 seconds by default.
	// To disable caching completely, pass MaxAge value negative. It will set the Access-Control-Max-Age header 0.
	//
	// Optional. Default value 0.
	MaxAge int

	// DisableValueRedaction turns off redaction of configuration values and origins in logs and panics.
	//
	// Optional. Default: false
	DisableValueRedaction bool

	// AllowCredentials indicates whether or not the response to the request
	// can be exposed when the credentials flag is true. When used as part of
	// a response to a preflight request, this indicates whether or not the
	// actual request can be made using credentials. Note: if true, the
	// AllowOrigins setting cannot contain the wildcard "*" to prevent
	// security vulnerabilities.
	//
	// Optional. Default value false.
	AllowCredentials bool

	// AllowPrivateNetwork indicates whether the Access-Control-Allow-Private-Network
	// response header should be set to true, allowing requests from private networks.
	//
	// Optional. Default value false.
	AllowPrivateNetwork bool
}
Fields
  • Next func(c fiber.Ctx) bool

    Next defines a function to skip this middleware when returned true.

    Optional. Default: nil

  • AllowOriginsFunc func(origin string) bool

    AllowOriginsFunc defines a function that will set the 'Access-Control-Allow-Origin' response header to the 'origin' request header when returned true. This allows for dynamic evaluation of allowed origins. Note if AllowCredentials is true, wildcard origins will not have the 'Access-Control-Allow-Credentials' header set to 'true'.

    The function is called with the Origin request header exactly as the client sent it, and only when that origin did not already match AllowOrigins. A true result takes effect only when the value is a serialized origin (scheme + host) or the literal "null" string. According to the CORS specification (https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS#origin), browsers send "null" for privacy-sensitive contexts like sandboxed iframes or file:// URLs.

    Origins with userinfo, paths, queries, fragments, or wildcards are still passed to this function, but a true result for them is discarded and no 'Access-Control-Allow-Origin' header is set.

    Optional. Default: nil

  • AllowOrigins []string

    AllowOrigins defines a list of origins that may access the resource.

    This supports wildcard matching for subdomains by prefixing the domain with a *. e.g. "http://*.domain.com". This will allow all level of subdomains of domain.com to access the resource.

    If the special wildcard "*" is present in the list, all origins will be allowed. An empty list also allows all origins, unless AllowOriginsFunc is set.

    Optional. Default value []string{"*"}

  • AllowMethods []string

    AllowMethods defines a list methods allowed when accessing the resource. This is used in response to a preflight request.

    Optional. Default value []string{"GET", "POST", "HEAD", "PUT", "DELETE", "PATCH", "QUERY"}

  • AllowHeaders []string

    AllowHeaders defines a list of request headers that can be used when making the actual request. This is in response to a preflight request.

    Optional. Default value []string{}

  • ExposeHeaders []string

    ExposeHeaders defines an allowlist of headers that clients are allowed to access.

    Optional. Default value []string{}.

  • MaxAge int

    MaxAge indicates how long (in seconds) the results of a preflight request can be cached. If you pass MaxAge 0, Access-Control-Max-Age header will not be added and browser will use 5 seconds by default. To disable caching completely, pass MaxAge value negative. It will set the Access-Control-Max-Age header 0.

    Optional. Default value 0.

  • DisableValueRedaction bool

    DisableValueRedaction turns off redaction of configuration values and origins in logs and panics.

    Optional. Default: false

  • AllowCredentials bool

    AllowCredentials indicates whether or not the response to the request can be exposed when the credentials flag is true. When used as part of a response to a preflight request, this indicates whether or not the actual request can be made using credentials. Note: if true, the AllowOrigins setting cannot contain the wildcard "*" to prevent security vulnerabilities.

    Optional. Default value false.

  • AllowPrivateNetwork bool

    AllowPrivateNetwork indicates whether the Access-Control-Allow-Private-Network response header should be set to true, allowing requests from private networks.

    Optional. Default value false.