Skip to content

Commit 1392af7

Browse files
author
ROUSSE Kevin
committed
fix: add removeSecurityFromCookie on ProxyRes to remove secure attribute from cookies
1 parent bbf7251 commit 1392af7

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/proxy.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express';
22
import { createProxyServer } from 'http-proxy';
33

44
import { fixRequestBody } from './fixRequestBody';
5+
import { removeSecurityFromCookie } from './removeSecurityFromCookie';
56
import { ProxyParams } from './stubServer';
67

78
const send = (
@@ -30,6 +31,7 @@ const send = (
3031
});
3132

3233
proxy.on('proxyReq', fixRequestBody);
34+
proxy.on('proxyRes', removeSecurityFromCookie);
3335
proxy.web(req, res, { target }, next);
3436
};
3537

src/removeSecurityFromCookie.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as http from 'http';
2+
import { createProxyServer } from 'http-proxy';
3+
4+
import { removeSecurityFromCookie } from './removeSecurityFromCookie';
5+
6+
let sourceServer: http.Server;
7+
let proxyServer: http.Server;
8+
9+
const proxyPort = 2370;
10+
const sourcePort = 2371;
11+
const proxyUrl = `http://127.0.0.1:${proxyPort}`;
12+
const sourceUrl = `http://127.0.0.1:${sourcePort}`;
13+
14+
afterEach(() => {
15+
sourceServer.close();
16+
proxyServer.close();
17+
});
18+
19+
test('removeSecurityFromCookie', () => {
20+
return new Promise(done => {
21+
const proxy = createProxyServer({ target: sourceUrl });
22+
23+
proxy.on('proxyRes', (proxyRes, req, res) => {
24+
removeSecurityFromCookie(proxyRes, req, res);
25+
expect(proxyRes.headers['set-cookie']).toEqual(['test']);
26+
res.end('Response');
27+
});
28+
29+
function proxyRequestHandler(_req: http.IncomingMessage, _res: http.ServerResponse) {
30+
proxy.web(_req, _res);
31+
}
32+
function sourceRequestHandler(_req: http.IncomingMessage, _res: http.ServerResponse) {
33+
_res.writeHead(200, { 'Content-Type': 'text/plain', 'set-cookie': 'test; secure' });
34+
_res.end('Response');
35+
}
36+
37+
proxyServer = http.createServer(proxyRequestHandler).listen(proxyPort);
38+
sourceServer = http.createServer(sourceRequestHandler).listen(sourcePort);
39+
40+
http
41+
.request(proxyUrl, () => {
42+
done(1);
43+
})
44+
.end();
45+
});
46+
});

src/removeSecurityFromCookie.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ProxyResCallback } from 'http-proxy';
2+
3+
/**
4+
* http-proxy does not support secure removal attribute from cookie.
5+
* @see see https://github.com/http-party/node-http-proxy/issues/1165
6+
* @param proxyReq
7+
*/
8+
const removeSecurityFromCookie: ProxyResCallback = proxyRes => {
9+
if (proxyRes.headers['set-cookie']) {
10+
const cookies = proxyRes.headers['set-cookie'].map(cookie => cookie.replace(/; secure/gi, ''));
11+
/* eslint-disable no-param-reassign */
12+
proxyRes.headers['set-cookie'] = cookies;
13+
/* eslint-enable no-param-reassign */
14+
}
15+
};
16+
17+
export { removeSecurityFromCookie };

0 commit comments

Comments
 (0)