Skip to content

Commit b17d3d1

Browse files
authored
fix(server): ignore invalid headers in RequestHeadersHandlerPlugin (#2490)
`RequestHeadersHandlerPlugin` now leaves out header entries that `Headers` rejects instead of throwing. Batch subrequest headers come from the request body, so they can contain names and values an HTTP parser would not accept. ## Fixes - Invalid header names and values are left out of `context.reqHeaders`, so the subrequest still reaches its procedure. - Valid headers on the same request are kept. ## Testing - Added a batch request test with invalid header names and values. - `packages/server` tests pass.
1 parent 2e3c4d8 commit b17d3d1

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎apps/content/docs/plugins/request-headers.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const example = base
3232
This allows procedures to run safely even without `RequestHeadersHandlerPlugin`, such as in direct calls.
3333
:::
3434

35+
:::info[Why can a header be missing from `reqHeaders`?]
36+
Header names and values that `Headers` rejects are left out. Batch subrequests can carry them because their headers come from the request body.
37+
:::
38+
3539
:::tip
3640
Combine with [Cookie Helpers](/docs/helpers/cookie) for streamlined cookie management.
3741
:::

‎packages/server/src/plugins/request-headers.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RPCHandler } from '../adapters/fetch/rpc-handler'
22
import { os } from '../builder'
3+
import { BatchHandlerPlugin } from './batch'
34
import type { RequestHeadersHandlerPluginContext } from './request-headers'
45
import { RequestHeadersHandlerPlugin } from './request-headers'
56

@@ -62,4 +63,48 @@ describe('requestHeadersHandlerPlugin', () => {
6263
expect(interceptor).toHaveBeenCalledOnce()
6364
expect(interceptor.mock.calls[0]![0].context.reqHeaders).toBe(existingHeaders)
6465
})
66+
67+
it('leaves out invalid batch subrequest headers instead of failing the subrequest', async () => {
68+
const procedureHandler = vi.fn(() => 'pong')
69+
const procedure = os.$context<RequestHeadersHandlerPluginContext>().handler(procedureHandler)
70+
71+
const handler = new RPCHandler(procedure, {
72+
plugins: [new RequestHeadersHandlerPlugin(), new BatchHandlerPlugin()],
73+
})
74+
75+
const { response } = await handler.handle(
76+
new Request('https://example.com', {
77+
method: 'POST',
78+
headers: { 'content-type': 'application/json', 'orpc-batch': '1' },
79+
body: JSON.stringify([
80+
{
81+
id: '1',
82+
kind: 'request',
83+
json: {
84+
url: '/',
85+
method: 'POST',
86+
headers: {
87+
'a b': '1',
88+
'x-newline': 'a\nb',
89+
'x-mixed': ['valid', 'a\nb'],
90+
'x-valid': 'value',
91+
},
92+
},
93+
},
94+
]),
95+
}),
96+
)
97+
98+
expect(response?.status).toBe(207)
99+
await expect(response?.json()).resolves.toEqual([
100+
{ id: '1', kind: 'response', json: { body: { json: 'pong' } } },
101+
])
102+
103+
const capturedHeaders: Headers = (procedureHandler as any).mock.calls[0]![0].context.reqHeaders
104+
expect(Object.fromEntries(capturedHeaders)).toEqual({
105+
'content-type': 'application/json',
106+
'x-mixed': 'valid',
107+
'x-valid': 'value',
108+
})
109+
})
65110
})

‎packages/server/src/plugins/request-headers.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
import { toArray } from '@orpc/shared'
2+
import type { StandardHeaders } from '@standard-server/core'
23
import { toFetchHeaders } from '@standard-server/fetch'
34

45
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index'
56

7+
function appendValidHeader(headers: Headers, name: string, value: string): void {
8+
try {
9+
headers.append(name, value)
10+
} catch {}
11+
}
12+
13+
/**
14+
* Not every request passes through an HTTP parser: batch subrequests carry their headers
15+
* in the request body, so a client can send any header name or value.
16+
*/
17+
function toSafeFetchHeaders(standardHeaders: StandardHeaders): Headers {
18+
try {
19+
return toFetchHeaders(standardHeaders)
20+
} catch {
21+
const headers = new Headers()
22+
23+
for (const name of Object.keys(standardHeaders)) {
24+
const value = standardHeaders[name]
25+
26+
if (Array.isArray(value)) {
27+
for (const item of value) {
28+
appendValidHeader(headers, name, item)
29+
}
30+
} else if (value !== undefined) {
31+
appendValidHeader(headers, name, value)
32+
}
33+
}
34+
35+
return headers
36+
}
37+
}
38+
639
/**
740
* The context shape into which the Request Headers Plugin injects `reqHeaders`.
841
*
@@ -33,7 +66,7 @@ export class RequestHeadersHandlerPlugin<
3366
(interceptorOptions) => {
3467
const reqHeaders =
3568
interceptorOptions.context.reqHeaders ??
36-
toFetchHeaders(interceptorOptions.request.headers)
69+
toSafeFetchHeaders(interceptorOptions.request.headers)
3770

3871
return interceptorOptions.next({
3972
...interceptorOptions,

0 commit comments

Comments
 (0)