Configuring CORS policies for multi-tenant APIs
Multi-tenant API architectures require dynamic cross-origin resource sharing (CORS) policies that adapt to tenant-specific domains at the edge. Static Access-Control-Allow-Origin configurations fail in SaaS environments where each customer operates under a distinct subdomain or custom domain. This guide details the exact routing syntax and middleware execution order required to safely inject tenant-scoped CORS headers without compromising credential isolation or preflight caching.
Dynamic Tenant Resolution in the Request Pipeline
Before evaluating cross-origin permissions, the gateway must resolve the tenant context. This is typically achieved by inspecting the Host header, X-Tenant-ID, or JWT claims. The resolved identifier drives a lookup table that maps tenant IDs to allowed origin patterns. Implementing this logic within Middleware Chains & Request Transformation ensures that origin validation occurs prior to downstream routing, preventing unauthorized cross-tenant header leakage and maintaining strict isolation boundaries.
Exact Gateway Configuration Syntax
The following configuration demonstrates a declarative, tenant-aware CORS policy using a standard API gateway routing engine. It leverages conditional header injection based on a resolved tenant mapping.
route:
match:
prefix: "/api/v1"
cors:
allow_credentials: true
allow_methods: ["GET", "POST", "OPTIONS"]
allow_headers: ["Authorization", "Content-Type", "X-Tenant-ID"]
expose_headers: ["X-Request-ID"]
max_age: "3600"
allow_origin_string_match:
- prefix: "https://app-"
suffix: ".tenant-platform.io"
- exact: "https://dashboard.enterprise-client.com"
response_headers_to_add:
- header:
key: "Vary"
value: "Origin"
This syntax enforces strict origin matching while preserving credential support. The Vary: Origin directive is mandatory to prevent cache poisoning across tenants sharing the same CDN edge node.
Preflight Caching and Failure Modes
Misconfigured multi-tenant CORS policies frequently trigger 403 Forbidden responses during OPTIONS preflight requests. Common failure modes include wildcard origins (*) paired with Access-Control-Allow-Credentials: true, missing Vary headers causing stale cache hits, and regex-based origin matching that inadvertently permits subdomain spoofing. For a comprehensive breakdown of browser enforcement rules and secure header composition, consult the foundational CORS & Cross-Origin Security documentation. Always validate preflight responses using automated integration tests that simulate cross-origin requests from isolated tenant domains.
Integration with Authentication and Rate Limiting
CORS evaluation must execute before authentication proxying to ensure preflight requests bypass token validation. Rate limiting should be applied post-CORS to avoid penalizing legitimate OPTIONS traffic. When combining these layers, ensure the middleware execution order prioritizes origin validation, followed by tenant routing, then credential verification. This sequencing guarantees that tenant-specific security boundaries remain intact while optimizing edge performance.