Skip to content

Commit 335fb23

Browse files
committed
Add cookie parser to echo the value of cookies in the response. Also works for signed cookies.
To sign a cookie, generate a value using SIGNED_COOKIE=$(node -e "var crypto = require('crypto'); function sign(val, secret){ return val + '.' + crypto .createHmac('sha256', secret) .update(val) .digest('base64') .replace(/=+$/, ''); }; console.log(sign('my-value','mysecretkey123'));") Then send it in the header like so curl -s http://localhost:8080/ -H "Cookie: mysigned=s:${SIGNED_COOKIE}" Issue #93
1 parent 082b9d5 commit 335fb23

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

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)