Skip to content

Commit b8a9e80

Browse files
committed
Add cors.allowedOrigins to define a list of allowed CORS origins
1 parent ba3f86c commit b8a9e80

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Classes/Dispatcher.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function __construct(
7777
* @param ServerRequestInterface $request
7878
* @param ResponseInterface $response
7979
* @return ResponseInterface
80+
* @throws \Exception
8081
*/
8182
public function processRequest(ServerRequestInterface $request, ResponseInterface $response)
8283
{
@@ -97,7 +98,10 @@ public function processRequest(ServerRequestInterface $request, ResponseInterfac
9798
*/
9899
public function dispatch(RestRequestInterface $request, ResponseInterface $response)
99100
{
100-
return $this->addAdditionalHeaders($this->dispatchInternal($request, $response));
101+
return $this->addCorsHeaders(
102+
$request,
103+
$this->addAdditionalHeaders($this->dispatchInternal($request, $response))
104+
);
101105
}
102106

103107
/**
@@ -146,7 +150,6 @@ private function getResultConverter()
146150
*
147151
* @param RestRequestInterface $request
148152
* @return ResponseInterface
149-
* @throws \Exception
150153
*/
151154
private function callHandler(RestRequestInterface $request)
152155
{
@@ -243,6 +246,24 @@ private function addAdditionalHeaders(ResponseInterface $response)
243246
return $response;
244247
}
245248

249+
private function addCorsHeaders(RestRequestInterface $request, ResponseInterface $response)
250+
{
251+
$origin = $request->getHeaderLine('origin');
252+
if ($origin) {
253+
$allowedOrigins = $this->objectManager
254+
->getConfigurationProvider()
255+
->getSetting('cors.allowedOrigins', []);
256+
257+
foreach ($allowedOrigins as $allowedOrigin) {
258+
if ($allowedOrigin === $origin) {
259+
return $response->withHeader('Access-Control-Allow-Origin', $allowedOrigin);
260+
}
261+
}
262+
}
263+
264+
return $response;
265+
}
266+
246267
/**
247268
* @param RestRequestInterface $request
248269
* @param ResponseInterface $response

Documentation/Configuration/CORS.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ By controlling the access in your TYPO3 installation, client-side workarounds
1414
Example
1515
-------
1616

17-
An example which will allow the local development site on port `3000` to make `GET`, `POST` and preflight requests.
17+
The following example will allow the local development site on port `3000` and
18+
`https://production.com` to make `GET`, `POST` and preflight requests.
19+
20+
The `Access-Control-Allow-Origin` header will be set to the first
21+
`cors.allowedOrigins` value that matches the sent `origin` header.
1822

1923
plugin.tx_rest.settings {
24+
cors.allowedOrigins {
25+
0 = http://localhost:3000
26+
1 = https://production.com
27+
}
2028
responseHeaders {
21-
Access-Control-Allow-Origin = http://localhost:3000
2229
Access-Control-Allow-Methods = POST, GET, OPTIONS
2330
2431
# Inform the client that credentials may be used

ext_typoscript_setup.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ plugin.tx_rest.settings {
2222
# Access-Control-Allow-Origin = example.com
2323
# Access-Control-Allow-Methods = GET, POST, OPTIONS, DELETE
2424
#}
25+
#cors.allowedOrigins {
26+
# 0 = http://localhost:3000
27+
#}
2528

2629
# This is not defined here to allow easy customization in third party extensions TypoScript setup
2730
# cacheLifeTime = -1

0 commit comments

Comments
 (0)