Skip to content

Commit 7d3cf92

Browse files
committed
fix: validate blob body content type
1 parent c601fff commit 7d3cf92

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎lib/dispatcher/client-h1.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
RequestContentLengthMismatchError,
1111
ResponseContentLengthMismatchError,
1212
RequestAbortedError,
13+
InvalidArgumentError,
1314
HeadersTimeoutError,
1415
HeadersOverflowError,
1516
SocketError,
@@ -1200,8 +1201,16 @@ function writeH1 (client, request) {
12001201
}
12011202
body = bodyStream.stream
12021203
contentLength = bodyStream.length
1203-
} else if (util.isBlobLike(body) && request.contentType == null && body.type) {
1204-
headers.push('content-type', body.type)
1204+
} else if (util.isBlobLike(body) && request.contentType == null) {
1205+
const contentType = body.type
1206+
if (contentType) {
1207+
const contentTypeValue = `${contentType}`
1208+
if (!util.isValidHeaderValue(contentTypeValue)) {
1209+
util.errorRequest(client, request, new InvalidArgumentError('invalid content-type header'))
1210+
return false
1211+
}
1212+
headers.push('content-type', contentTypeValue)
1213+
}
12051214
}
12061215

12071216
if (body && typeof body.read === 'function') {

‎test/request-crlf.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const assert = require('node:assert')
34
const { tspl } = require('@matteo.collina/tspl')
45
const { createServer } = require('node:http')
56
const { test, after } = require('node:test')
@@ -34,3 +35,33 @@ test('should validate content-type CRLF Injection', async (t) => {
3435
}
3536
await t.completed
3637
})
38+
39+
test('should validate blob body content-type CRLF Injection', async () => {
40+
let receivedRequest = false
41+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
42+
receivedRequest = true
43+
res.statusCode = 200
44+
res.end('hello')
45+
})
46+
47+
after(() => server.close())
48+
49+
server.listen(0)
50+
51+
await once(server, 'listening')
52+
53+
class MaliciousBlob extends Blob {
54+
get type () {
55+
return 'text/html\r\nX-Injected: true'
56+
}
57+
}
58+
59+
await assert.rejects(
60+
request(`http://localhost:${server.address().port}/endpoint`, {
61+
method: 'POST',
62+
body: new MaliciousBlob(['hello'])
63+
}),
64+
(e) => e instanceof errors.InvalidArgumentError && e.message === 'invalid content-type header'
65+
)
66+
assert.strictEqual(receivedRequest, false)
67+
})

0 commit comments

Comments
 (0)