Skip to main content

Overview

The cors 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

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:
  1. Origin Validation: Checks incoming requests against allowed origins using either:
    • Static origin list (AllowOrigins)
    • Custom validation function (AllowOriginFunc)
    • Wildcard matching for development scenarios
  2. 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 methods
    • Access-Control-Allow-Headers: Permitted request headers
    • Access-Control-Expose-Headers: Headers exposed to the client
    • Access-Control-Allow-Credentials: Credential support flag
    • Access-Control-Max-Age: Preflight cache duration in seconds
    • Vary: Origin: Added when using specific origins to ensure proper caching
  3. 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

  1. Extract Origin header from request
  2. If no origin header, skip CORS processing
  3. Validate origin against allowed list or function
  4. If origin not allowed, continue without CORS headers
  5. Set Access-Control-Allow-Origin header (wildcard or specific)
  6. Add Vary: Origin header when using specific origins
  7. Set credentials header if enabled
  8. Set expose headers if configured
  9. For OPTIONS requests (preflight):
    • Set methods and headers
    • Set max-age if configured
    • Handle Private Network Access if enabled
    • Return 204 No Content
  10. For regular requests, continue to next handler

Security Considerations

  1. Never use AllowAll() in production - It exposes your API to any origin
  2. Be careful with credentials - When AllowCredentials is true, you cannot use wildcard origins
  3. Validate origins - Use AllowOriginFunc for dynamic origin validation
  4. Limit exposed headers - Only expose headers that clients actually need

Best Practices

  • Use specific origins in production
  • Cache preflight requests with MaxAge to reduce OPTIONS requests
  • Combine with authentication middleware for protected endpoints
  • Use AllowPrivateNetwork only when needed for local network access

Testing

The CORS middleware includes comprehensive test coverage for all features and edge cases:
  • cors2 - Enhanced CORS with additional features
  • secure - HTTPS enforcement
  • helmet - Security headers