Skip to content

Commit 740a0b7

Browse files
committed
fix: validate blob body content type
1 parent 2698e49 commit 740a0b7

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,
@@ -993,8 +994,16 @@ function writeH1 (client, request) {
993994
}
994995
body = bodyStream.stream
995996
contentLength = bodyStream.length
996-
} else if (util.isBlobLike(body) && request.contentType == null && body.type) {
997-
headers.push('content-type', body.type)
997+
} else if (util.isBlobLike(body) && request.contentType == null) {
998+
const contentType = body.type
999+
if (contentType) {
1000+
const contentTypeValue = `${contentType}`
1001+
if (!util.isValidHeaderValue(contentTypeValue)) {
1002+
util.errorRequest(client, request, new InvalidArgumentError('invalid content-type header'))
1003+
return false
1004+
}
1005+
headers.push('content-type', contentTypeValue)
1006+
}
9981007
}
9991008

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