Skip to content

Commit 54656b6

Browse files
author
Michael Smith
committed
1 parent 31c4773 commit 54656b6

8 files changed

Lines changed: 129 additions & 37 deletions

File tree

‎node_modules/undici/docs/docs/api/Client.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Returns: `Client`
2727
* **maxHeaderSize** `number | null` (optional) - Default: `--max-http-header-size` or `16384` - The maximum length of request headers in bytes. Defaults to Node.js' --max-http-header-size or 16KiB.
2828
* **maxResponseSize** `number | null` (optional) - Default: `-1` - The maximum length of response body in bytes. Set to `-1` to disable.
2929
* **webSocket** `WebSocketOptions` (optional) - WebSocket-specific configuration options.
30+
* **maxFragments** `number` (optional) - Default: `131072` - Maximum number of fragments in a message. Set to 0 to disable the limit.
3031
* **maxPayloadSize** `number` (optional) - Default: `134217728` (128 MB) - Maximum allowed payload size in bytes for WebSocket messages. Applied to uncompressed messages, compressed frame payloads, and decompressed (permessage-deflate) messages. Set to 0 to disable the limit.
3132
* **pipelining** `number | null` (optional) - Default: `1` - The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Carefully consider your workload and environment before enabling concurrent requests as pipelining may reduce performance if used incorrectly. Pipelining is sensitive to network stack settings as well as head of line blocking caused by e.g. long running requests. Set to `0` to disable keep-alive connections.
3233
* **connect** `ConnectOptions | Function | null` (optional) - Default: `null`.

‎node_modules/undici/lib/dispatcher/client-h1.js‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const EMPTY_BUF = Buffer.alloc(0)
5757
const FastBuffer = Buffer[Symbol.species]
5858
const addListener = util.addListener
5959
const removeAllListeners = util.removeAllListeners
60+
const kIdleSocketValidation = Symbol('kIdleSocketValidation')
61+
const kIdleSocketValidationTimeout = Symbol('kIdleSocketValidationTimeout')
62+
const kSocketUsed = Symbol('kSocketUsed')
6063

6164
let extractBody
6265

@@ -371,6 +374,11 @@ class Parser {
371374
return -1
372375
}
373376

377+
if (client[kRunning] === 0) {
378+
util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
379+
return -1
380+
}
381+
374382
const request = client[kQueue][client[kRunningIdx]]
375383
if (!request) {
376384
return -1
@@ -474,6 +482,11 @@ class Parser {
474482
return -1
475483
}
476484

485+
if (client[kRunning] === 0) {
486+
util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
487+
return -1
488+
}
489+
477490
const request = client[kQueue][client[kRunningIdx]]
478491

479492
/* istanbul ignore next: difficult to make a test case for */
@@ -647,6 +660,7 @@ class Parser {
647660
request.onComplete(headers)
648661

649662
client[kQueue][client[kRunningIdx]++] = null
663+
socket[kSocketUsed] = true
650664

651665
if (socket[kWriting]) {
652666
assert(client[kRunning] === 0)
@@ -705,6 +719,9 @@ async function connectH1 (client, socket) {
705719
socket[kWriting] = false
706720
socket[kReset] = false
707721
socket[kBlocking] = false
722+
socket[kIdleSocketValidation] = 0
723+
socket[kIdleSocketValidationTimeout] = null
724+
socket[kSocketUsed] = false
708725
socket[kParser] = new Parser(client, socket, llhttpInstance)
709726

710727
addListener(socket, 'error', function (err) {
@@ -751,6 +768,8 @@ async function connectH1 (client, socket) {
751768
const client = this[kClient]
752769
const parser = this[kParser]
753770

771+
clearIdleSocketValidation(this)
772+
754773
if (parser) {
755774
if (!this[kError] && parser.statusCode && !parser.shouldKeepAlive) {
756775
this[kError] = parser.finish() || this[kError]
@@ -816,7 +835,7 @@ async function connectH1 (client, socket) {
816835
return socket.destroyed
817836
},
818837
busy (request) {
819-
if (socket[kWriting] || socket[kReset] || socket[kBlocking]) {
838+
if (socket[kWriting] || socket[kReset] || socket[kBlocking] || socket[kIdleSocketValidation] === 1) {
820839
return true
821840
}
822841

@@ -854,6 +873,31 @@ async function connectH1 (client, socket) {
854873
}
855874
}
856875

876+
function clearIdleSocketValidation (socket) {
877+
if (socket[kIdleSocketValidationTimeout]) {
878+
clearTimeout(socket[kIdleSocketValidationTimeout])
879+
socket[kIdleSocketValidationTimeout] = null
880+
}
881+
882+
socket[kIdleSocketValidation] = 0
883+
}
884+
885+
function scheduleIdleSocketValidation (client, socket) {
886+
socket[kIdleSocketValidation] = 1
887+
socket[kIdleSocketValidationTimeout] = setTimeout(() => {
888+
socket[kIdleSocketValidationTimeout] = null
889+
socket[kIdleSocketValidation] = 2
890+
891+
if (client[kSocket] === socket && !socket.destroyed) {
892+
client[kResume]()
893+
}
894+
}, 0)
895+
socket[kIdleSocketValidationTimeout].unref?.()
896+
}
897+
898+
/**
899+
* @param {import('./client.js')} client
900+
*/
857901
function resumeH1 (client) {
858902
const socket = client[kSocket]
859903

@@ -868,6 +912,32 @@ function resumeH1 (client) {
868912
socket[kNoRef] = false
869913
}
870914

915+
if (client[kRunning] === 0 && client[kPending] > 0 && socket[kSocketUsed]) {
916+
if (socket[kIdleSocketValidation] === 0) {
917+
scheduleIdleSocketValidation(client, socket)
918+
socket[kParser].readMore()
919+
if (socket.destroyed) {
920+
return
921+
}
922+
return
923+
}
924+
925+
if (socket[kIdleSocketValidation] === 1) {
926+
socket[kParser].readMore()
927+
if (socket.destroyed) {
928+
return
929+
}
930+
return
931+
}
932+
}
933+
934+
if (client[kRunning] === 0) {
935+
socket[kParser].readMore()
936+
if (socket.destroyed) {
937+
return
938+
}
939+
}
940+
871941
if (client[kSize] === 0) {
872942
if (socket[kParser].timeoutType !== TIMEOUT_KEEP_ALIVE) {
873943
socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_KEEP_ALIVE)
@@ -961,6 +1031,7 @@ function writeH1 (client, request) {
9611031
}
9621032

9631033
const socket = client[kSocket]
1034+
clearIdleSocketValidation(socket)
9641035

9651036
const abort = (err) => {
9661037
if (request.aborted || request.completed) {

‎node_modules/undici/lib/dispatcher/dispatcher-base.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class DispatcherBase extends Dispatcher {
2626

2727
get webSocketOptions () {
2828
return {
29+
maxFragments: this[kWebSocketOptions].maxFragments ?? 131072,
2930
maxPayloadSize: this[kWebSocketOptions].maxPayloadSize ?? 128 * 1024 * 1024
3031
}
3132
}

‎node_modules/undici/lib/web/cookies/parse.js‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -275,32 +275,25 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {})
275275
// If the attribute-name case-insensitively matches the string
276276
// "SameSite", the user agent MUST process the cookie-av as follows:
277277

278-
// 1. Let enforcement be "Default".
279-
let enforcement = 'Default'
280-
281278
const attributeValueLowercase = attributeValue.toLowerCase()
282-
// 2. If cookie-av's attribute-value is a case-insensitive match for
283-
// "None", set enforcement to "None".
284-
if (attributeValueLowercase.includes('none')) {
285-
enforcement = 'None'
286-
}
287279

288-
// 3. If cookie-av's attribute-value is a case-insensitive match for
289-
// "Strict", set enforcement to "Strict".
290-
if (attributeValueLowercase.includes('strict')) {
291-
enforcement = 'Strict'
280+
// 1. If cookie-av's attribute-value is a case-insensitive match for
281+
// "None", append an attribute to the cookie-attribute-list with an
282+
// attribute-name of "SameSite" and an attribute-value of "None".
283+
if (attributeValueLowercase === 'none') {
284+
cookieAttributeList.sameSite = 'None'
285+
} else if (attributeValueLowercase === 'strict') {
286+
// 2. If cookie-av's attribute-value is a case-insensitive match for
287+
// "Strict", append an attribute to the cookie-attribute-list with
288+
// an attribute-name of "SameSite" and an attribute-value of
289+
// "Strict".
290+
cookieAttributeList.sameSite = 'Strict'
291+
} else if (attributeValueLowercase === 'lax') {
292+
// 3. If cookie-av's attribute-value is a case-insensitive match for
293+
// "Lax", append an attribute to the cookie-attribute-list with an
294+
// attribute-name of "SameSite" and an attribute-value of "Lax".
295+
cookieAttributeList.sameSite = 'Lax'
292296
}
293-
294-
// 4. If cookie-av's attribute-value is a case-insensitive match for
295-
// "Lax", set enforcement to "Lax".
296-
if (attributeValueLowercase.includes('lax')) {
297-
enforcement = 'Lax'
298-
}
299-
300-
// 5. Append an attribute to the cookie-attribute-list with an
301-
// attribute-name of "SameSite" and an attribute-value of
302-
// enforcement.
303-
cookieAttributeList.sameSite = enforcement
304297
} else {
305298
cookieAttributeList.unparsed ??= []
306299

‎node_modules/undici/lib/web/websocket/receiver.js‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const { closeWebSocketConnection } = require('./connection')
2020
const { PerMessageDeflate } = require('./permessage-deflate')
2121
const { MessageSizeExceededError } = require('../../core/errors')
2222

23+
function failWebsocketConnectionWithCode (ws, code, reason) {
24+
closeWebSocketConnection(ws, code, reason, Buffer.byteLength(reason))
25+
failWebsocketConnection(ws, reason)
26+
}
27+
2328
// This code was influenced by ws released under the MIT license.
2429
// Copyright (c) 2011 Einar Otto Stangvik <[email protected]>
2530
// Copyright (c) 2013 Arnout Kazemier and contributors
@@ -39,19 +44,23 @@ class ByteParser extends Writable {
3944
/** @type {Map<string, PerMessageDeflate>} */
4045
#extensions
4146

47+
/** @type {number} */
48+
#maxFragments
49+
4250
/** @type {number} */
4351
#maxPayloadSize
4452

4553
/**
4654
* @param {import('./websocket').WebSocket} ws
4755
* @param {Map<string, string>|null} extensions
48-
* @param {{ maxPayloadSize?: number }} [options]
56+
* @param {{ maxFragments?: number, maxPayloadSize?: number }} [options]
4957
*/
5058
constructor (ws, extensions, options = {}) {
5159
super()
5260

5361
this.ws = ws
5462
this.#extensions = extensions == null ? new Map() : extensions
63+
this.#maxFragments = options.maxFragments ?? 0
5564
this.#maxPayloadSize = options.maxPayloadSize ?? 0
5665

5766
if (this.#extensions.has('permessage-deflate')) {
@@ -75,9 +84,9 @@ class ByteParser extends Writable {
7584
if (
7685
this.#maxPayloadSize > 0 &&
7786
!isControlFrame(this.#info.opcode) &&
78-
this.#info.payloadLength > this.#maxPayloadSize
87+
this.#info.payloadLength + this.#fragmentsBytes > this.#maxPayloadSize
7988
) {
80-
failWebsocketConnection(this.ws, 'Payload size exceeds maximum allowed size')
89+
failWebsocketConnectionWithCode(this.ws, 1009, 'Payload size exceeds maximum allowed size')
8190
return false
8291
}
8392

@@ -242,10 +251,12 @@ class ByteParser extends Writable {
242251
this.#state = parserStates.INFO
243252
} else {
244253
if (!this.#info.compressed) {
245-
this.writeFragments(body)
254+
if (!this.writeFragments(body)) {
255+
return
256+
}
246257

247258
if (this.#maxPayloadSize > 0 && this.#fragmentsBytes > this.#maxPayloadSize) {
248-
failWebsocketConnection(this.ws, new MessageSizeExceededError().message)
259+
failWebsocketConnectionWithCode(this.ws, 1009, new MessageSizeExceededError().message)
249260
return
250261
}
251262

@@ -264,14 +275,17 @@ class ByteParser extends Writable {
264275
this.#info.fin,
265276
(error, data) => {
266277
if (error) {
267-
failWebsocketConnection(this.ws, error.message)
278+
const code = error instanceof MessageSizeExceededError ? 1009 : 1007
279+
failWebsocketConnectionWithCode(this.ws, code, error.message)
268280
return
269281
}
270282

271-
this.writeFragments(data)
283+
if (!this.writeFragments(data)) {
284+
return
285+
}
272286

273287
if (this.#maxPayloadSize > 0 && this.#fragmentsBytes > this.#maxPayloadSize) {
274-
failWebsocketConnection(this.ws, new MessageSizeExceededError().message)
288+
failWebsocketConnectionWithCode(this.ws, 1009, new MessageSizeExceededError().message)
275289
return
276290
}
277291

@@ -341,8 +355,17 @@ class ByteParser extends Writable {
341355
}
342356

343357
writeFragments (fragment) {
358+
if (
359+
this.#maxFragments > 0 &&
360+
this.#fragments.length === this.#maxFragments
361+
) {
362+
failWebsocketConnectionWithCode(this.ws, 1008, 'Too many message fragments')
363+
return false
364+
}
365+
344366
this.#fragmentsBytes += fragment.length
345367
this.#fragments.push(fragment)
368+
return true
346369
}
347370

348371
consumeFragments () {

‎node_modules/undici/lib/web/websocket/websocket.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,12 @@ class WebSocket extends EventTarget {
435435
// once this happens, the connection is open
436436
this[kResponse] = response
437437

438-
const maxPayloadSize = this[kController]?.dispatcher?.webSocketOptions?.maxPayloadSize
438+
const webSocketOptions = this[kController]?.dispatcher?.webSocketOptions
439+
const maxFragments = webSocketOptions?.maxFragments
440+
const maxPayloadSize = webSocketOptions?.maxPayloadSize
439441

440442
const parser = new ByteParser(this, parsedExtensions, {
443+
maxFragments,
441444
maxPayloadSize
442445
})
443446
parser.on('drain', onParserDrain)

‎node_modules/undici/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "undici",
3-
"version": "6.26.0",
3+
"version": "6.27.0",
44
"description": "An HTTP/1.1 client, written from scratch for Node.js",
55
"homepage": "https://undici.nodejs.org",
66
"bugs": {

‎package-lock.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13707,9 +13707,9 @@
1370713707
}
1370813708
},
1370913709
"node_modules/undici": {
13710-
"version": "6.26.0",
13711-
"resolved": "https://registry.npmjs.org/undici/-/undici-6.26.0.tgz",
13712-
"integrity": "sha512-4yqz8a3n5HmGTlsbADNtr/dJlhkh/55Rq798G6ibiULcXbDtaLpTl1pvdqcbFfeoj3iSi52lePFM7h9H21cw/A==",
13710+
"version": "6.27.0",
13711+
"resolved": "https://registry.npmjs.org/undici/-/undici-6.27.0.tgz",
13712+
"integrity": "sha512-YmfV3YnEDzXRC5lZ2jWtWWHKGUm1zIt8AhesR1tens+HTNv+YZlN/dp6G727LOvMJ8xjP9Be7Y2Sdr96LDm+pg==",
1371313713
"inBundle": true,
1371413714
"license": "MIT",
1371513715
"engines": {

0 commit comments

Comments
 (0)