Skip to main content

Overview

The cors2 middleware provides enhanced Cross-Origin Resource Sharing handling with additional features like pattern-based origin matching, preflight response caching, and more granular control. Use it when you need:
  • Wildcard subdomain matching
  • Advanced preflight caching
  • More control over CORS behavior

Installation

Quick Start

Configuration

Options

Examples

Wildcard Subdomains

Combined Exact and Pattern

Full Configuration

API Reference

Functions

Pattern Syntax

  • * matches any sequence of characters
  • https://*.example.com matches https://app.example.com, https://api.example.com
  • Patterns are matched against the full origin including protocol

Technical Details

Implementation Overview

The cors2 middleware implements a simplified CORS handling mechanism with the following key components: Origin Matching:
  • Supports wildcard (*) for all origins
  • Implements exact origin matching with case-insensitive comparison
  • The matchOrigin function handles both wildcard and exact match scenarios
Header Management:
  • Sets Access-Control-Allow-Origin based on the request origin and configured options
  • Conditionally sets Access-Control-Allow-Credentials when credentials are enabled
  • Applies Access-Control-Expose-Headers for custom headers that should be exposed to the browser
Preflight Request Handling:
  • Detects OPTIONS requests for preflight handling
  • Responds with Access-Control-Allow-Methods and Access-Control-Allow-Headers
  • Sets Access-Control-Max-Age to cache preflight responses (when configured)
  • Returns HTTP 204 (No Content) for preflight requests
Default Values:
  • Origin: * (all origins)
  • Methods: GET, POST, PUT, DELETE, OPTIONS
  • Headers: Content-Type, Authorization
  • Credentials: false
  • MaxAge: 0 (no caching)

Helper Functions

The middleware provides convenience functions:
  • New(): Creates middleware with default settings
  • WithOptions(opts): Creates middleware with custom configuration
  • AllowOrigin(origin): Quick setup for a specific origin
  • AllowAll(): Permissive setup with extended methods and headers
  • AllowCredentials(origin): Enables credentials for a specific origin

Best Practices

  • Use exact origins when possible for security
  • Use patterns only for known subdomain structures
  • Set appropriate MaxAge to reduce preflight requests
  • Be cautious with credentials and wildcards

Testing

The cors2 middleware includes comprehensive test coverage for all functionality:
  • cors - Basic CORS handling
  • secure - Security settings
  • helmet - Security headers