Overview
Thecors middleware handles Cross-Origin Resource Sharing (CORS), allowing your API to be safely accessed from different domains. It automatically handles preflight requests and sets the appropriate headers.
Use it when you need:
- API access from browser applications on different domains
- Single-page applications calling your backend
- Mobile apps using web views
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
AllowOrigins | []string | ["*"] | Allowed origins |
AllowMethods | []string | ["GET", "POST", "HEAD"] | Allowed HTTP methods |
AllowHeaders | []string | ["Origin", "Content-Type", "Accept"] | Allowed request headers |
ExposeHeaders | []string | [] | Headers exposed to browser |
AllowCredentials | bool | false | Allow credentials (cookies) |
MaxAge | time.Duration | 0 | Preflight cache duration |
AllowOriginFunc | func(string) bool | nil | Custom origin validator |
AllowPrivateNetwork | bool | false | Enable Private Network Access |
Examples
Allow All Origins (Development)
Specific Origins
With Credentials
Custom Origin Validation
Preflight Caching
API Reference
Functions
Technical Details
Implementation Overview
The CORS middleware implements the W3C Cross-Origin Resource Sharing specification by:-
Origin Validation: Checks incoming requests against allowed origins using either:
- Static origin list (
AllowOrigins) - Custom validation function (
AllowOriginFunc) - Wildcard matching for development scenarios
- Static origin list (
-
Header Management: Sets appropriate CORS headers based on configuration:
Access-Control-Allow-Origin: The allowed origin (specific or wildcard)Access-Control-Allow-Methods: Permitted HTTP methodsAccess-Control-Allow-Headers: Permitted request headersAccess-Control-Expose-Headers: Headers exposed to the clientAccess-Control-Allow-Credentials: Credential support flagAccess-Control-Max-Age: Preflight cache duration in secondsVary: Origin: Added when using specific origins to ensure proper caching
-
Preflight Handling: Automatically responds to OPTIONS requests with:
- No content (204 status)
- All necessary CORS headers
- Optional Private Network Access support
Default Values
When options are not specified, the middleware uses these defaults:- AllowOrigins:
["*"](all origins) - AllowMethods:
["GET", "POST", "HEAD"] - AllowHeaders:
["Origin", "Content-Type", "Accept"] - ExposeHeaders:
[](empty) - AllowCredentials:
false - MaxAge:
0(no caching)
Request Flow
- Extract
Originheader from request - If no origin header, skip CORS processing
- Validate origin against allowed list or function
- If origin not allowed, continue without CORS headers
- Set
Access-Control-Allow-Originheader (wildcard or specific) - Add
Vary: Originheader when using specific origins - Set credentials header if enabled
- Set expose headers if configured
- For OPTIONS requests (preflight):
- Set methods and headers
- Set max-age if configured
- Handle Private Network Access if enabled
- Return 204 No Content
- For regular requests, continue to next handler
Security Considerations
- Never use
AllowAll()in production - It exposes your API to any origin - Be careful with credentials - When
AllowCredentialsis true, you cannot use wildcard origins - Validate origins - Use
AllowOriginFuncfor dynamic origin validation - Limit exposed headers - Only expose headers that clients actually need
Best Practices
- Use specific origins in production
- Cache preflight requests with
MaxAgeto reduce OPTIONS requests - Combine with authentication middleware for protected endpoints
- Use
AllowPrivateNetworkonly when needed for local network access
Testing
The CORS middleware includes comprehensive test coverage for all features and edge cases:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew/allows matching origin | Request with allowed origin | Returns 200 OK with Access-Control-Allow-Origin header set to the matching origin |
TestNew/ignores non-matching origin | Request with non-allowed origin | Processes request normally but does not set CORS headers |
TestNew/handles preflight | OPTIONS request with origin | Returns 204 No Content with Access-Control-Allow-Methods and other preflight headers |
TestNew/ignores requests without origin | Request without Origin header | Processes request normally without setting any CORS headers |
TestAllowAll | AllowAll() with any origin | Sets Access-Control-Allow-Origin: * for any origin |
TestWithOrigins/http://a.com | WithOrigins() with allowed origin | Sets Access-Control-Allow-Origin to the specific allowed origin |
TestWithOrigins/http://b.com | WithOrigins() with another allowed origin | Sets Access-Control-Allow-Origin to the specific allowed origin |
TestWithOrigins/http://c.com | WithOrigins() with non-allowed origin | Does not set Access-Control-Allow-Origin header |
TestNew_AllowCredentials | Credentials enabled with specific origin | Sets Access-Control-Allow-Credentials: true and specific origin (not wildcard) |
TestNew_ExposeHeaders | Custom expose headers configured | Sets Access-Control-Expose-Headers with comma-separated header list |
TestNew_MaxAge | MaxAge set to 12 hours | Sets Access-Control-Max-Age: 43200 on preflight requests |
TestNew_AllowOriginFunc/http://example.com | Custom origin validator (exact match) | Allows the origin when validation function returns true |
TestNew_AllowOriginFunc/http://api.example.com | Custom origin validator (subdomain) | Allows subdomain when validation function returns true |
TestNew_AllowOriginFunc/http://other.com | Custom origin validator (non-matching) | Denies origin when validation function returns false |
TestNew_PrivateNetwork | Private Network Access enabled | Sets Access-Control-Allow-Private-Network: true when requested |
TestNew_VaryHeader | Specific origin configured | Adds Vary: Origin header to ensure proper caching |