Skip to content

Commit 6cb425c

Browse files
authored
Merge pull request #1725 from hashtopolis/1265-properly-implement-cors
Added an improved CORS implementation #1265
2 parents f3b4eb7 + 38464bc commit 6cb425c

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

doc/installation_guidelines/tls.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ http {
9797
}
9898
```
9999

100-
3. Update the value of `HASHTOPOLIS_BACKEND_URL` in the `.env` file to reflect the changes done above.
100+
3. Update the value of `HASHTOPOLIS_BACKEND_URL` in the `.env` file to reflect the changes done above. If your server name isn't localhost, be sure to also update the comma-separated list of `HASHTOPOLIS_FRONTEND_URLS` to include new https frontend.
101101

102102
4. Start the containers
103103
```
104104
105105
docker compose up
106106
107107
```
108-
5. Visit hashtopolis at https://localhost/
108+
5. Visit hashtopolis at https://localhost/

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ services:
1414
HASHTOPOLIS_DB_DATABASE: $MYSQL_DATABASE
1515
HASHTOPOLIS_ADMIN_USER: $HASHTOPOLIS_ADMIN_USER
1616
HASHTOPOLIS_ADMIN_PASSWORD: $HASHTOPOLIS_ADMIN_PASSWORD
17-
HASHTOPOLIS_APIV2_ENABLE: $HASHTOPOLIS_APIV2_ENABLE
17+
HASHTOPOLIS_APIV2_ENABLE: $HASHTOPOLIS_APIV2_ENABLE
18+
HASHTOPOLIS_FRONTEND_URLS: $HASHTOPOLIS_FRONTEND_URLS
1819
depends_on:
1920
- db
2021
ports:

env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ HASHTOPOLIS_DB_HOST=db
99

1010
HASHTOPOLIS_APIV2_ENABLE=0
1111
HASHTOPOLIS_BACKEND_URL=http://localhost:8080/api/v2
12+
HASHTOPOLIS_FRONTEND_URLS=http://127.0.0.1:4200,http://localhost:4200,http://127.0.0.1:8080,http://localhost:8080,https://127.0.0.1,https://localhost

src/api/v2/index.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ public function process(Request $request, RequestHandler $handler): Response {
192192
}
193193

194194

195-
/* FIXME: CORS wildcard hack should require proper implementation and validation */
196-
197195
/* This middleware will append the response header Access-Control-Allow-Methods with all allowed methods */
198196

199197
class CorsHackMiddleware implements MiddlewareInterface {
@@ -209,7 +207,19 @@ public static function addCORSheaders(Request $request, $response) {
209207
$methods = $routingResults->getAllowedMethods();
210208
$requestHeaders = $request->getHeaderLine('Access-Control-Request-Headers');
211209

212-
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
210+
if (getenv('HASHTOPOLIS_FRONTEND_URLS') !== false) {
211+
if(in_array($request->getHeaderLine('HTTP_ORIGIN'), explode(',', getenv('HASHTOPOLIS_FRONTEND_URLS')), true)) {
212+
$response = $response->withHeader('Access-Control-Allow-Origin', $request->getHeaderLine('HTTP_ORIGIN'));
213+
}
214+
else {
215+
error_log("CORS error: Allow-Origin doesn't match. Please make sure to include the used frontend in the .env file.");
216+
}
217+
}
218+
else {
219+
//No frontend URLs given in .env file, switch to default allow all
220+
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
221+
}
222+
213223
$response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods));
214224
$response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders);
215225

0 commit comments

Comments
 (0)