Skip to content

Commit e7aa758

Browse files
committed
test(rsc-mf): expand hop-by-hop header sanitization coverage
1 parent 42cffd9 commit e7aa758

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

tests/integration/rsc-mf/shared/proxyResponse.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
const HOP_BY_HOP_RESPONSE_HEADERS = [
1+
const PROXY_UNSAFE_RESPONSE_HEADERS = [
22
'content-length',
33
'content-encoding',
4+
'connection',
5+
'keep-alive',
6+
'proxy-authenticate',
7+
'proxy-authorization',
8+
'te',
9+
'trailer',
410
'transfer-encoding',
11+
'upgrade',
512
];
613

714
export const createSafeProxyResponse = (upstream: Response) => {
815
const headers = new Headers(upstream.headers);
9-
for (const headerName of HOP_BY_HOP_RESPONSE_HEADERS) {
16+
for (const headerName of PROXY_UNSAFE_RESPONSE_HEADERS) {
1017
headers.delete(headerName);
1118
}
19+
const connectionHeaderTokens = (upstream.headers.get('connection') || '')
20+
.split(',')
21+
.map(token => token.trim().toLowerCase())
22+
.filter(Boolean);
23+
for (const token of connectionHeaderTokens) {
24+
headers.delete(token);
25+
}
1226
return new Response(upstream.body, {
1327
status: upstream.status,
1428
headers,

tests/integration/rsc-mf/tests/modernServerConfig.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ describe('rsc-mf host modern.server middleware contracts', () => {
144144
'content-type': 'application/javascript',
145145
'content-length': '999',
146146
'content-encoding': 'gzip',
147+
connection: 'keep-alive, x-proxy-hop-header',
148+
'keep-alive': 'timeout=5',
149+
'x-proxy-hop-header': 'remove-me',
150+
te: 'trailers',
151+
trailer: 'x-trailer-a',
152+
upgrade: 'websocket',
147153
'transfer-encoding': 'chunked',
148154
},
149155
});
@@ -162,6 +168,12 @@ describe('rsc-mf host modern.server middleware contracts', () => {
162168
);
163169
expect(context.res?.headers.get('content-length')).toBeNull();
164170
expect(context.res?.headers.get('content-encoding')).toBeNull();
171+
expect(context.res?.headers.get('connection')).toBeNull();
172+
expect(context.res?.headers.get('keep-alive')).toBeNull();
173+
expect(context.res?.headers.get('x-proxy-hop-header')).toBeNull();
174+
expect(context.res?.headers.get('te')).toBeNull();
175+
expect(context.res?.headers.get('trailer')).toBeNull();
176+
expect(context.res?.headers.get('upgrade')).toBeNull();
165177
expect(context.res?.headers.get('transfer-encoding')).toBeNull();
166178
await expect(context.res?.text()).resolves.toBe(
167179
'proxied-with-transport-headers',

tests/integration/rsc-mf/tests/proxyResponse.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe('rsc-mf proxy response helper', () => {
88
'content-type': 'application/javascript',
99
'content-length': '999',
1010
'content-encoding': 'gzip',
11+
connection: 'keep-alive, x-custom-hop-header',
12+
'keep-alive': 'timeout=5',
13+
'x-custom-hop-header': 'remove-me',
14+
te: 'trailers',
15+
trailer: 'x-trailer-a',
16+
upgrade: 'websocket',
1117
'transfer-encoding': 'chunked',
1218
},
1319
});
@@ -18,6 +24,12 @@ describe('rsc-mf proxy response helper', () => {
1824
expect(proxied.headers.get('content-type')).toBe('application/javascript');
1925
expect(proxied.headers.get('content-length')).toBeNull();
2026
expect(proxied.headers.get('content-encoding')).toBeNull();
27+
expect(proxied.headers.get('connection')).toBeNull();
28+
expect(proxied.headers.get('keep-alive')).toBeNull();
29+
expect(proxied.headers.get('x-custom-hop-header')).toBeNull();
30+
expect(proxied.headers.get('te')).toBeNull();
31+
expect(proxied.headers.get('trailer')).toBeNull();
32+
expect(proxied.headers.get('upgrade')).toBeNull();
2133
expect(proxied.headers.get('transfer-encoding')).toBeNull();
2234
await expect(proxied.text()).resolves.toBe('proxied-body');
2335
});

tests/integration/rsc-mf/tests/remoteModernServerConfig.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ describe('rsc-mf remote modern.server middleware contracts', () => {
187187
'content-type': 'application/javascript',
188188
'content-length': '999',
189189
'content-encoding': 'gzip',
190+
connection: 'keep-alive, x-proxy-hop-header',
191+
'keep-alive': 'timeout=5',
192+
'x-proxy-hop-header': 'remove-me',
193+
te: 'trailers',
194+
trailer: 'x-trailer-a',
195+
upgrade: 'websocket',
190196
'transfer-encoding': 'chunked',
191197
},
192198
}),
@@ -209,6 +215,12 @@ describe('rsc-mf remote modern.server middleware contracts', () => {
209215
);
210216
expect(context.res?.headers.get('content-length')).toBeNull();
211217
expect(context.res?.headers.get('content-encoding')).toBeNull();
218+
expect(context.res?.headers.get('connection')).toBeNull();
219+
expect(context.res?.headers.get('keep-alive')).toBeNull();
220+
expect(context.res?.headers.get('x-proxy-hop-header')).toBeNull();
221+
expect(context.res?.headers.get('te')).toBeNull();
222+
expect(context.res?.headers.get('trailer')).toBeNull();
223+
expect(context.res?.headers.get('upgrade')).toBeNull();
212224
expect(context.res?.headers.get('transfer-encoding')).toBeNull();
213225
await expect(context.res?.text()).resolves.toBe(
214226
'fallback-with-transport-headers',

0 commit comments

Comments
 (0)