Cross-Origin Resource Sharing (CORS)
If you are facing issues while requesting API from the browser and errors in the console looks similar to the one below, you need to setup CORS policy for your API.
Warning
XMLHttpRequest at 'https://example.com' from origin 'https://example.org' has been blocked by CORS policy
We are not going here to explain what the CORS is. There is plenty of websites explaining it and official specification which you should definitely check before configuring CORS for your website. In documentation below you will find only explanation how to configure CORS in t3api.
CORS configuration in t3api is based and (almost) fully compatible with well known Symfony bundle nelmio/cors-bundle.
In code below there is a list of all supported configuration options and their default values. If you would like to change these values to custom ones, you should use ext_ file of your extension (EXT:).
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['allowCredentials'] = false;
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['allowOrigin'] = [];
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['allowHeaders'] = [];
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['allowMethods'] = [];
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['exposeHeaders'] = [];
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['maxAge'] = 0;
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['cors']['originRegex'] = false;
allow(Credentials boolean) - when set totrueadds response headerAccess-with valueControl- Allow- Credentials true.allow(Origin arrayorstring) - can be set to*to accept any value. Can be an array with string values of allowed origins (e.g.['http://) or regular expressions whenwww. example. com', 'https:// www. example. com'] originis set toRegex true(e.g.['https://). Patterns must match the complete origin, including its scheme and any non-default port: t3api wraps each pattern in[a- z0- 9-]+\. example\. com'] \A. Existing patterns with(?:...)\ z ^and$remain supported.allow(Headers arrayorstring) - can be set to*to accept any value or an array of strings with allowed headers (e.g.['Content-).Type'] allow(Methods array) - array of strings with HTTP methods (e.g.['GET', 'POST', 'PUT']).expose(Headers array) - controls the value ofAccess-.Control- Expose- Headers max(Age intornull) - controls the value ofAccess-. The defaultControl- Max- Age 0disables preflight caching. Set a positive number of seconds to allow caching (e.g.600), ornullto omit the header and let the browser apply its default.origin(Regex boolean) - indicates if values fromallowshould be treated as regular expression.Origin
Request and response handling
An allowed cross-origin request receives Access- containing its request origin.
This also applies when allow is *; t3api reflects the concrete origin so that
allow can work with the same configuration. Combining * with allow
allows every origin to read credentialed responses. Use an explicit list of trusted origins for private APIs.
An origin outside the configured policy receives no CORS permission headers.
A preflight is an OPTIONS request containing both Origin and Access-.
It succeeds only when the origin, requested method and requested headers are allowed.
A disallowed origin returns 403; a disallowed method or header returns 405. Rejected preflights have
an empty body and no CORS permission headers. Ordinary OPTIONS without preflight headers returns an empty
successful response for an existing endpoint. Cross-origin OPTIONS without Access-
uses the policy for an actual request.
Header names are compared without regard to case, and surrounding whitespace in header lists is ignored.
With allow set to *, t3api returns the concrete requested header names, including
Authorization, instead of a literal wildcard. The configured simple list is merged into
allow; by default it includes Accept, Accept-, Content-, Origin
and the configured language header. Add Content- to allow for JSON requests.
Method, allowed-header and exposed-header lists are trimmed and deduplicated; null and empty entries are ignored.
Methods are normalized to uppercase and header names to lowercase.
Responses include Vary: Origin, including requests without an origin and requests from disallowed origins.
Preflight responses also vary by Access- and Access-.
Existing Vary values are preserved. These headers tell HTTP caches which request headers affect the response,
as described in the Fetch specification.
t3api's response cache stores the serialized content; CORS headers are evaluated for each request.
Write requests from other origins
CORS controls whether a browser can read a response. Additionally t3api validates requests changing data
(all methods except GET, HEAD, OPTIONS and TRACE) and rejects requests which:
- are sent from another origin (
Originheader differs from the API origin) not allowed byallow- responseOrigin 403 Forbidden. Requests withoutOriginheader (e.g. server-to-server requests) are not affected, browsers send this header with every such request, - contain a JSON body without JSON content type (
application/,json application/or otherld+json application/*+json) - response415 Unsupported Media Type.
To allow write requests from another origin (e.g. frontend application on a different domain), add it to
allow. Setting allow to * allows write requests from every origin.
The origin check is done by Source. If you need to accept
write requests from any origin without allowing them to read responses, remove the processor in ext_
of your extension:
unset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['processors'][\SourceBroker\T3api\Processor\OriginValidationProcessor::class]);
CORS does not authorize an operation - configure operation access rules separately, see Security.
Customizing CORS handling
Cors provides the shared policy for Cors and Options.
The protected Cors and Cors hooks control only
whether that processor applies headers. They do not control the subsequent operation handler:
Options independently uses Cors.
To customize preflight detection consistently, supply the same custom Cors implementation to both
components through dependency injection, and leave the processor hook delegating to that service.
For release changes, see Changelog.