Skip to content

Commit 354a151

Browse files
authored
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]>
1 parent d228fab commit 354a151

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
@@ -472,7 +472,13 @@ function processHeader (request, key, val) {
472472
} else if (typeof val[i] === 'object') {
473473
throw new InvalidArgumentError(`invalid ${key} header`)
474474
} else {
475-
arr.push(`${val[i]}`)
475+
// Coerce primitives (and reject unsafe coercions such as functions
476+
// with a crafted toString/Symbol.toPrimitive).
477+
const str = `${val[i]}`
478+
if (!isValidHeaderValue(str)) {
479+
throw new InvalidArgumentError(`invalid ${key} header`)
480+
}
481+
arr.push(str)
476482
}
477483
}
478484
val = arr
@@ -483,7 +489,12 @@ function processHeader (request, key, val) {
483489
} else if (val === null) {
484490
val = ''
485491
} else {
492+
// Coerce primitives (and reject unsafe coercions such as functions
493+
// with a crafted toString/Symbol.toPrimitive).
486494
val = `${val}`
495+
if (!isValidHeaderValue(val)) {
496+
throw new InvalidArgumentError(`invalid ${key} header`)
497+
}
487498
}
488499

489500
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)