Skip to content

Commit 2698e49

Browse files
committed
fix: validate coerced header values for CRLF (#5579)
Functions bypassed the object guard in processHeader() and were coerced via template literals without isValidHeaderValue(), allowing CRLF injection when toString/Symbol.toPrimitive returned embedded newlines. Validate after coercion on both scalar and array paths. Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 354a151)
1 parent 4fd5a0c commit 2698e49

2 files changed

Lines changed: 128 additions & 1 deletion

File tree

‎lib/core/request.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ function processHeader (request, key, val) {
350350
} else if (typeof val[i] === 'object') {
351351
throw new InvalidArgumentError(`invalid ${key} header`)
352352
} else {
353-
arr.push(`${val[i]}`)
353+
// Coerce primitives (and reject unsafe coercions such as functions
354+
// with a crafted toString/Symbol.toPrimitive).
355+
const str = `${val[i]}`
356+
if (!isValidHeaderValue(str)) {
357+
throw new InvalidArgumentError(`invalid ${key} header`)
358+
}
359+
arr.push(str)
354360
}
355361
}
356362
val = arr
@@ -361,7 +367,12 @@ function processHeader (request, key, val) {
361367
} else if (val === null) {
362368
val = ''
363369
} else {
370+
// Coerce primitives (and reject unsafe coercions such as functions
371+
// with a crafted toString/Symbol.toPrimitive).
364372
val = `${val}`
373+
if (!isValidHeaderValue(val)) {
374+
throw new InvalidArgumentError(`invalid ${key} header`)
375+
}
365376
}
366377

367378
if (headerName === 'host') {

‎test/header-function-crlf.js‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict'
2+
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
5+
const { createServer } = require('node:http')
6+
const { once } = require('node:events')
7+
const { Client, errors } = require('..')
8+
9+
function makeInjectingFn (payload) {
10+
const fn = function () {}
11+
fn.toString = () => payload
12+
return fn
13+
}
14+
15+
function makeToPrimitiveFn (payload) {
16+
const fn = function () {}
17+
fn[Symbol.toPrimitive] = () => payload
18+
return fn
19+
}
20+
21+
test('rejects function header value with CRLF via toString', async (t) => {
22+
t = tspl(t, { plan: 1 })
23+
24+
const client = new Client('http://localhost:8080')
25+
after(() => client.destroy())
26+
27+
t.rejects(client.request({
28+
path: '/',
29+
method: 'GET',
30+
headers: {
31+
'x-safe': makeInjectingFn('legit\r\ntransfer-encoding: chunked')
32+
}
33+
}), new errors.InvalidArgumentError('invalid x-safe header'))
34+
35+
await t.completed
36+
})
37+
38+
test('rejects function header value with CRLF via Symbol.toPrimitive', async (t) => {
39+
t = tspl(t, { plan: 1 })
40+
41+
const client = new Client('http://localhost:8080')
42+
after(() => client.destroy())
43+
44+
t.rejects(client.request({
45+
path: '/',
46+
method: 'GET',
47+
headers: {
48+
'x-safe': makeToPrimitiveFn('legit\r\nx-injected: yes')
49+
}
50+
}), new errors.InvalidArgumentError('invalid x-safe header'))
51+
52+
await t.completed
53+
})
54+
55+
test('rejects function header value in array element with CRLF via toString', async (t) => {
56+
t = tspl(t, { plan: 1 })
57+
58+
const client = new Client('http://localhost:8080')
59+
after(() => client.destroy())
60+
61+
t.rejects(client.request({
62+
path: '/',
63+
method: 'GET',
64+
headers: {
65+
'x-safe': [makeInjectingFn('legit\r\nx-injected: via-array')]
66+
}
67+
}), new errors.InvalidArgumentError('invalid x-safe header'))
68+
69+
await t.completed
70+
})
71+
72+
test('rejects function header value in array element with CRLF via Symbol.toPrimitive', async (t) => {
73+
t = tspl(t, { plan: 1 })
74+
75+
const client = new Client('http://localhost:8080')
76+
after(() => client.destroy())
77+
78+
t.rejects(client.request({
79+
path: '/',
80+
method: 'GET',
81+
headers: {
82+
'x-safe': [makeToPrimitiveFn('legit\r\nx-injected: via-array-primitive')]
83+
}
84+
}), new errors.InvalidArgumentError('invalid x-safe header'))
85+
86+
await t.completed
87+
})
88+
89+
test('still accepts number and boolean header values', async (t) => {
90+
t = tspl(t, { plan: 2 })
91+
92+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
93+
t.strictEqual(req.headers['x-num'], '123')
94+
t.strictEqual(req.headers['x-bool'], 'true')
95+
res.end('ok')
96+
})
97+
after(() => server.close())
98+
99+
server.listen(0)
100+
await once(server, 'listening')
101+
102+
const client = new Client(`http://localhost:${server.address().port}`)
103+
after(() => client.close())
104+
105+
const { body } = await client.request({
106+
path: '/',
107+
method: 'GET',
108+
headers: {
109+
'x-num': 123,
110+
'x-bool': true
111+
}
112+
})
113+
await body.dump()
114+
115+
await t.completed
116+
})

0 commit comments

Comments
 (0)