Skip to content

Commit 68c03ea

Browse files
authored
Merge pull request #94 from mendhak/cookieparser
Add cookie parser to echo the value of cookies in the response. Also works for signed cookies.
2 parents 082b9d5 + 4f118c0 commit 68c03ea

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
# The type of runner that the job will run on
2020
runs-on: ubuntu-latest
2121

22+
permissions:
23+
contents: read
24+
security-events: write
25+
2226
# Steps represent a sequence of tasks that will be executed as part of the job
2327
steps:
2428
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
@@ -76,14 +80,14 @@ jobs:
7680

7781
- name: Scan the image
7882
id: scan
79-
uses: anchore/scan-action@v3
83+
uses: anchore/scan-action@v7
8084
with:
8185
image: "mendhak/http-https-echo:testing"
8286
output-format: sarif
8387
# severity-cutoff: critical
8488
fail-build: false
8589

8690
- name: upload Anchore scan SARIF report
87-
uses: github/codeql-action/upload-sarif@v3
91+
uses: github/codeql-action/upload-sarif@v4
8892
with:
8993
sarif_file: ${{ steps.scan.outputs.sarif }}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Version `40` - 2026-03-20
2+
* Echo back cookies and signed cookies
3+
14
## Version `39` - 2026-01-09
25
* Renamed privkey.pem to testpk.pem so Trivy doesn't flag a false positive by [willyguggenheim](https://github.com/mendhak/docker-http-https-echo/pull/89)
36
* Updated dependencies in package.json

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,47 @@ You can use the `MAX_HEADER_SIZE` environment variable to set a maximum header s
301301
docker run -d --rm -e MAX_HEADER_SIZE=1000 -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:39
302302
```
303303

304+
## Cookies and Signed Cookies
305+
306+
Make a request with a `Cookie` header and the response will include the cookies:
307+
308+
```bash
309+
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:39
310+
```
311+
312+
Then make a request with a cookie header:
313+
314+
```bash
315+
curl -s http://localhost:8080/ -H "Cookie: foo=bar; baz=qux"
316+
```
317+
318+
To enable signed cookie support, set the `COOKIE_SECRET` environment variable. Signed cookies appear in the `signedCookies` section of the response:
319+
320+
```bash
321+
docker run -d --rm -e COOKIE_SECRET=mysecretkey123 --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:39
322+
```
323+
324+
Now you need to generate a signed cookie, and send it in the header. Here's a convenience node snippet:
325+
326+
```bash
327+
328+
SIGNED_COOKIE=$(node -e "var crypto = require('crypto');
329+
330+
function sign(val, secret){
331+
return val + '.' + crypto
332+
.createHmac('sha256', secret)
333+
.update(val)
334+
.digest('base64')
335+
.replace(/=+$/, '');
336+
};
337+
338+
console.log(sign('my-value','mysecretkey123'));")
339+
340+
curl -s http://localhost:8080/ -H "Cookie: mysigned=s:$SIGNED_COOKIE" | jq '.signedCookies'
341+
```
342+
343+
Notice the `s:` prefix in the cookie value, that is important.
344+
304345

305346
## Prometheus Metrics
306347

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const http = require('http')
44
const https = require('https')
55
const morgan = require('morgan');
66
const express = require('express')
7+
const cookieParser = require('cookie-parser');
78
const concat = require('concat-stream');
89
const { promisify } = require('util');
910
const promBundle = require("express-prom-bundle");
@@ -39,6 +40,8 @@ if(PROMETHEUS_ENABLED === 'true') {
3940
app.use(metricsMiddleware);
4041
}
4142

43+
app.use(cookieParser(process.env.COOKIE_SECRET || 'examplekey'));
44+
4245
if(process.env.DISABLE_REQUEST_LOGS !== 'true'){
4346
app.use(morgan('combined'));
4447
}
@@ -76,6 +79,7 @@ app.all('*', (req, res) => {
7679
ips: req.ips,
7780
protocol: req.protocol,
7881
query: req.query,
82+
signedCookies: req.signedCookies,
7983
subdomains: req.subdomains,
8084
xhr: req.xhr,
8185
os: {

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"dependencies": {
2020
"concat-stream": "^2.0.0",
21+
"cookie-parser": "^1.4.6",
2122
"express": "^4.22.0",
2223
"express-prom-bundle": "^8.0.0",
2324
"jsonwebtoken": "^9.0.0",

tests.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,60 @@ message " Stop containers "
653653
docker stop http-echo-tests
654654
sleep 5
655655

656+
message " Start container with signed cookies support "
657+
# Set cookie secret for signing/verifying cookies
658+
docker run -d --rm -e COOKIE_SECRET=mysecretkey123 \
659+
--name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:testing
660+
sleep 5
661+
662+
SIGNED_COOKIE=$(node -e "var crypto = require('crypto');
663+
664+
function sign(val, secret){
665+
return val + '.' + crypto
666+
.createHmac('sha256', secret)
667+
.update(val)
668+
.digest('base64')
669+
.replace(/=+$/, '');
670+
};
671+
672+
console.log(sign('my-value','mysecretkey123'));")
673+
674+
675+
RESPONSE=$(curl -s http://localhost:8080/ -H "Cookie: mysigned=s:${SIGNED_COOKIE}")
676+
if [ $(echo $RESPONSE | jq -r '.signedCookies.mysigned') == 'my-value' ]
677+
then
678+
passed "Signed cookie test passed."
679+
else
680+
failed "Signed cookie test failed."
681+
echo $RESPONSE | jq
682+
exit 1
683+
fi
684+
685+
message " Stop containers "
686+
docker stop http-echo-tests
687+
sleep 5
688+
689+
690+
message " Check that regular cookies are returned in response "
691+
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:testing
692+
sleep 5
693+
694+
695+
RESPONSE=$(curl -s http://localhost:8080/ -H "Cookie: foo=bar; baz=qux")
696+
if [ $(echo $RESPONSE | jq -r '.cookies.foo') == 'bar' ] && \
697+
[ $(echo $RESPONSE | jq -r '.cookies.baz') == 'qux' ]
698+
then
699+
passed "Cookies returned in response test passed."
700+
else
701+
failed "Cookies returned in response test failed."
702+
echo $RESPONSE | jq
703+
exit 1
704+
fi
705+
706+
message " Stop containers "
707+
docker stop http-echo-tests
708+
sleep 5
709+
656710
popd
657711
rm -rf testarea
658712
message "DONE"

0 commit comments

Comments
 (0)