CORS (Cross-Origin Resource Sharing) middleware for Fiber lets servers control who can access resources and how. It isn’t a security feature; it merely relaxes the browser’s same-origin policy so cross-origin requests can succeed. Learn more on MDN. It adds CORS headers to responses, listing allowed origins, methods, and headers, and handles preflight checks.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/gofiber/fiber/llms.txt
Use this file to discover all available pages before exploring further.
Signatures
Usage
Basic Usage
Dynamic Origin Validation
Subdomain Matching
Configuration
Defines a function to skip this middleware when it returns true.
Defines a list of origins that may access the resource. Supports subdomain matching (e.g.,
"https://*.example.com"). If the special wildcard "*" is present, all origins will be allowed.Dynamically determines whether to allow a request based on its origin. If this function returns
true, the ‘Access-Control-Allow-Origin’ response header will be set to the request’s origin. Only used if the request’s origin doesn’t match any origin in AllowOrigins.Defines a list of methods allowed when accessing the resource. Used in response to a preflight request.
Defines a list of request headers that can be used when making the actual request. This is in response to a preflight request.
Indicates whether the response can be exposed when the credentials flag is true. When used as part of a response to a preflight request, this indicates whether the actual request can be made using credentials. Note: If true, AllowOrigins cannot be set to a wildcard (
"*").Defines an allowlist of headers that clients are allowed to access.
Indicates how long (in seconds) the results of a preflight request can be cached. If you pass MaxAge 0, the Access-Control-Max-Age header will not be added and the browser will use 5 seconds by default. To disable caching completely, pass a negative value.
Indicates whether the
Access-Control-Allow-Private-Network response header should be set to true, allowing requests from private networks.Disables redaction of misconfigured origins and settings in panics and logs.
Default Config
Common Use Cases
Production API with Specific Origins
Development Environment
Microservices Communication
Public API
Security Considerations
Best Practices
- Specify Allowed Origins: Instead of using a wildcard, specify exact domains
- Use Credentials Carefully: Only enable
AllowCredentialswhen necessary - Limit Exposed Headers: Only allowlist necessary headers
- Validate with
AllowOriginsFunc: Ensure robust checks when using dynamic validation