CORS¶
CORS is enabled by default and configured with the CORS_* options:
| Option | Default | Description |
|---|---|---|
CORS_ENABLED |
True |
Global CORS handling. |
CORS_ALLOW_ORIGINS |
["*"] |
Allowed origins. |
CORS_ALLOW_METHODS |
["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] |
Allowed methods. |
CORS_ALLOW_HEADERS |
["Authorization", "Content-Type"] |
Allowed request headers. |
CORS_EXPOSE_HEADERS |
[] |
Response headers exposed to the browser. |
CORS_ALLOW_CREDENTIALS |
False |
Allow cookies and Authorization on cross-origin requests (the request origin is echoed instead of *). |
CORS_MAX_AGE |
None |
Preflight cache time in seconds. |
Behaviour¶
- The middleware answers preflight requests (
OPTIONSwithAccess-Control-Request-Method) and adds CORS headers to all responses of allowed origins, including error responses. - Requests from origins that are not allowed are processed normally but without CORS headers, so
the browser blocks cross-origin reads while same-origin requests keep working. Preflights from
such origins get
403. - Every response of a route with CORS enabled carries
Vary: Origin, so shared caches keep the variants apart.
Credentials
Turn on CORS_ALLOW_CREDENTIALS only together with an explicit CORS_ALLOW_ORIGINS list,
and declare specific methods and headers. CORS is not authentication or CSRF protection.