Commit d15ad82c29b for nodejs
commit d15ad82c29bdd69f610b455adc56300ff12b7cfc
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date: Wed Nov 18 18:16:54 2026 -0300
quic: validate maxPayloadSize range
The maxPayloadSize option was copied into ngtcp2's
max_tx_udp_payload_size unchecked. ngtcp2 requires 1200 to 65527 and
only asserts it, so in a release build 0 gave the congestion controller
an empty initial window and the process died with SIGFPE in
ngtcp2_cc_cubic_init() when the connection was created. Values from 1
to 1199 were accepted and the session was destroyed before it opened.
Reject values outside that range when the session options are parsed,
for both connect() and listen().
Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67531
Refs: https://github.com/nodejs/node/pull/65827
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/doc/api/quic.md b/doc/api/quic.md
index b91a8d803b1..a8f0132e2ec 100644
--- a/doc/api/quic.md
+++ b/doc/api/quic.md
@@ -3294,8 +3294,10 @@ added: v23.8.0
-->
* Type: {bigint|number}
+* **Default:** `1200`
-Specifies the maximum UDP packet payload size.
+Specifies the maximum UDP packet payload size. The valid range is `1200` to
+`65527`.
#### `sessionOptions.maxStreamWindow`
diff --git a/src/quic/session.cc b/src/quic/session.cc
index 71c531e42b7..387938df977 100644
--- a/src/quic/session.cc
+++ b/src/quic/session.cc
@@ -629,6 +629,20 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
#undef SET
+ // ngtcp2 only asserts this range. With assertions compiled out, a value
+ // below the minimum gives the congestion controller an initial window it
+ // cannot work with: zero makes it divide by zero as soon as the connection
+ // is created.
+ if (options.max_payload_size < NGTCP2_MAX_UDP_PAYLOAD_SIZE ||
+ options.max_payload_size > NGTCP2_MAX_TX_UDP_PAYLOAD_SIZE) {
+ THROW_ERR_INVALID_ARG_VALUE(env,
+ "The maxPayloadSize option must be between "
+ "%d and %d",
+ NGTCP2_MAX_UDP_PAYLOAD_SIZE,
+ NGTCP2_MAX_TX_UDP_PAYLOAD_SIZE);
+ return Nothing<Options>();
+ }
+
// RFC 9000 Section 10.2 requires the draining period to be at least 3x PTO.
static const uint8_t kMinDrainingPeriodMultiplier = 3;
options.draining_period_multiplier = std::max(
diff --git a/test/parallel/test-quic-max-payload-size.mjs b/test/parallel/test-quic-max-payload-size.mjs
index 9923f120407..fde5e4e12b8 100644
--- a/test/parallel/test-quic-max-payload-size.mjs
+++ b/test/parallel/test-quic-max-payload-size.mjs
@@ -5,7 +5,7 @@
// We verify by checking that more packets are needed to transfer
// the same amount of data compared to the default.
-import { hasQuic, skip, mustCall } from '../common/index.mjs';
+import { hasQuic, skip, mustCall, mustNotCall } from '../common/index.mjs';
import assert from 'node:assert';
import { dump } from 'node:stream/iter';
@@ -55,3 +55,18 @@ const smallPkts = await transferAndGetPacketCount(1200);
// The key assertion: the option is accepted and data transfers correctly.
assert.ok(defaultPkts > 0n);
assert.ok(smallPkts > 0n);
+
+// Both ends of the supported range work, as a number and as a bigint.
+for (const maxPayloadSize of [1200n, 65527, 65527n]) {
+ assert.ok(await transferAndGetPacketCount(maxPayloadSize) > 0n);
+}
+
+// Anything outside of the range is rejected before a session is created.
+for (const maxPayloadSize of [0, 0n, 1199, 1199n, 65528, 65528n]) {
+ const expected = {
+ code: 'ERR_INVALID_ARG_VALUE',
+ message: 'The maxPayloadSize option must be between 1200 and 65527',
+ };
+ await assert.rejects(listen(mustNotCall(), { maxPayloadSize }), expected);
+ await assert.rejects(connect('127.0.0.1:1', { maxPayloadSize }), expected);
+}