Skip to content

Commit 1325a87

Browse files
committed
fix(fetch): abort excessive content encoding responses
Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit b5084c7) (cherry picked from commit 07ded49)
1 parent 8c59f6b commit 1325a87

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎lib/web/fetch/index.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,8 +2122,6 @@ async function httpNetworkFetch (
21222122
}
21232123
location = headersList.get('location', true)
21242124

2125-
this.body = new Readable({ read: resume })
2126-
21272125
const decoders = []
21282126

21292127
const willFollow = location && request.redirect === 'follow' &&
@@ -2141,7 +2139,7 @@ async function httpNetworkFetch (
21412139
// CVE fix similar to urllib3 (GHSA-gm62-xv2j-4w53) and curl (CVE-2022-32206).
21422140
const maxContentEncodings = 5
21432141
if (codings.length > maxContentEncodings) {
2144-
reject(new Error(`too many content-encodings in response: ${codings.length}, maximum allowed is ${maxContentEncodings}`))
2142+
this.abort(new Error(`too many content-encodings in response: ${codings.length}, maximum allowed is ${maxContentEncodings}`))
21452143
return true
21462144
}
21472145

@@ -2174,6 +2172,8 @@ async function httpNetworkFetch (
21742172
}
21752173
}
21762174

2175+
this.body = new Readable({ read: resume })
2176+
21772177
const onError = this.onError.bind(this)
21782178

21792179
resolve({

‎test/fetch/encoding.js‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
const { test, describe, before, after } = require('node:test')
44
const assert = require('node:assert')
55
const { createServer } = require('node:http')
6+
const { createServer: createNetServer } = require('node:net')
67
const { once } = require('node:events')
7-
const { fetch } = require('../..')
8+
const { fetch, Client } = require('../..')
89
const { createBrotliCompress, createGzip, createDeflate } = require('node:zlib')
910
const { closeServerAsPromise } = require('../utils/node-http')
1011

@@ -121,6 +122,40 @@ describe('content-encoding chain limit', () => {
121122
}
122123
)
123124
})
125+
126+
test('should handle an early close after rejecting excessive content-encodings', async (t) => {
127+
const response = Buffer.from(
128+
'HTTP/1.1 200 OK\r\n' +
129+
`Content-Encoding: ${Array(MAX_CONTENT_ENCODINGS + 1).fill('gzip').join(', ')}\r\n` +
130+
'Content-Length: 1\r\n' +
131+
'Connection: close\r\n\r\n'
132+
)
133+
let resolveSocketClosed
134+
const socketClosed = new Promise((resolve) => {
135+
resolveSocketClosed = resolve
136+
})
137+
const earlyCloseServer = createNetServer((socket) => {
138+
socket.once('close', resolveSocketClosed)
139+
socket.once('data', () => socket.end(response))
140+
})
141+
t.after(() => earlyCloseServer.close())
142+
143+
await once(earlyCloseServer.listen(0, '127.0.0.1'), 'listening')
144+
145+
const client = new Client(`http://127.0.0.1:${earlyCloseServer.address().port}`)
146+
t.after(() => client.close())
147+
148+
await assert.rejects(
149+
fetch(`http://127.0.0.1:${earlyCloseServer.address().port}`, {
150+
dispatcher: client
151+
}),
152+
(err) => {
153+
assert.ok(err.cause?.message.includes('content-encoding'))
154+
return true
155+
}
156+
)
157+
await socketClosed
158+
})
124159
})
125160

126161
// https://github.com/nodejs/undici/issues/5999

0 commit comments

Comments
 (0)