Skip to content

Commit a3d5655

Browse files
authored
Merge pull request #65 from ash0ne/master
Implementing configurable CORS settings
2 parents 46601de + 6e7f7f3 commit a3d5655

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This image is executed as non root by default and is fully compliant with Kubern
3030
- [Add a delay before response](#add-a-delay-before-response)
3131
- [Only return body in the response](#only-return-body-in-the-response)
3232
- [Include environment variables in the response](#include-environment-variables-in-the-response)
33+
- [Configuring CORS policy](#setting-corscross-origin-resource-sharing-headers-in-the-response)
3334
- [Client certificate details (mTLS) in the response](#client-certificate-details-mtls-in-the-response)
3435
- [Prometheus Metrics](#prometheus-metrics)
3536
- [Screenshots](#screenshots)
@@ -247,6 +248,17 @@ docker run -d --rm -e ECHO_INCLUDE_ENV_VARS=1 --name http-echo-tests -p 8080:808
247248

248249
Then do a normal request via curl or browser, and you will see the `env` property in the response body.
249250

251+
## Setting CORS(Cross-Origin Resource Sharing) headers in the response
252+
253+
Enable CORS headers in response by setting the environment variable `CORS_ALLOW_ORIGIN` to the list of allowed origins.
254+
CORS configuration can be further fine-tuned by using the following environment variables:
255+
256+
- **`CORS_ALLOW_METHODS`**: List of Http methods allowed.
257+
- **`CORS_ALLOW_HEADERS`**: List of headers allowed.
258+
- **`CORS_ALLOW_CREDENTIALS`**: Comma-separated list of origin URLs from which the policy allows credentials to be sent.
259+
260+
None of these CORS settings can be set without setting the `CORS_ALLOW_ORIGIN` first. By default, they will all be missing when only the `CORS_ALLOW_ORIGIN` is set and need to be explicitly specified alongside `CORS_ALLOW_ORIGIN`.
261+
250262

251263
## Client certificate details (mTLS) in the response
252264

‎index.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ app.all('*', (req, res) => {
114114
res.contentType(setResponseContentType);
115115
}
116116

117+
//Set the CORS policy
118+
if (process.env.CORS_ALLOW_ORIGIN){
119+
res.header('Access-Control-Allow-Origin', process.env.CORS_ALLOW_ORIGIN);
120+
if (process.env.CORS_ALLOW_METHODS) {
121+
res.header('Access-Control-Allow-Methods', process.env.CORS_ALLOW_METHODS);
122+
}
123+
if (process.env.CORS_ALLOW_HEADERS) {
124+
res.header('Access-Control-Allow-Headers', process.env.CORS_ALLOW_HEADERS);
125+
}
126+
if (process.env.CORS_ALLOW_CREDENTIALS) {
127+
res.header('Access-Control-Allow-Credentials', process.env.CORS_ALLOW_CREDENTIALS);
128+
}
129+
}
130+
117131
//Ability to send an empty response back
118132
if (process.env.ECHO_BACK_TO_CLIENT != undefined && process.env.ECHO_BACK_TO_CLIENT == "false"){
119133
res.end();

‎tests.sh‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ else
271271
exit 1
272272
fi
273273

274+
message " Stop containers "
275+
docker stop http-echo-tests
276+
sleep 5
277+
278+
message " Start container with CORS_CONFIG"
279+
docker run -d --rm \
280+
-e CORS_ALLOW_ORIGIN="http://example.com" -e CORS_ALLOW_HEADERS="x-custom-test-header" \
281+
--name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
282+
sleep 5
283+
# Check if the expected CORS headers are present in the response
284+
if curl -s -i http://localhost:8080/ 2>&1 | grep -q -E \
285+
"Access-Control-Allow-Headers: x-custom-test-header" &&
286+
curl -s -i http://localhost:8080/ 2>&1 | grep -q -E \
287+
"Access-Control-Allow-Origin: http://example.com"; then
288+
passed "CORS_CONFIG expected CORS headers found in response"
289+
else
290+
failed "CORS_CONFIG failed."
291+
docker logs http-echo-tests
292+
exit 1
293+
fi
274294

275295
message " Stop containers "
276296
docker stop http-echo-tests

0 commit comments

Comments
 (0)