h2lean 0.19.13 → 0.19.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -163,11 +163,15 @@ const {
|
|
|
163
163
|
WINDOW_UPDATE, CONTINUATION, ALTSVC, ORIGIN,
|
|
164
164
|
F_END_STREAM, F_ACK, F_END_HEADERS, F_PADDED, F_PRIORITY,
|
|
165
165
|
NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, FRAME_SIZE_ERROR, COMPRESSION_ERROR, CANCEL, REFUSED_STREAM,
|
|
166
|
+
ENHANCE_YOUR_CALM,
|
|
166
167
|
HEADER_LEN, PREFACE, PREFACE_LEN, readHeader, writeHeader, trimPadding
|
|
167
168
|
} = f;
|
|
168
169
|
|
|
169
170
|
const MAX_SETTINGS_ENTRIES = 32; // CVE-2020-11080; same bound as the server
|
|
170
171
|
const MAX_CONTINUATIONS = 8; // CVE-2024-28182; same bound as the server
|
|
172
|
+
// node's default outstanding-ping cap (initializeOptions, internal/http2/util.js),
|
|
173
|
+
// mirrored from the server (lib/server/Http2ServerSession.js).
|
|
174
|
+
const DEFAULT_MAX_OUTSTANDING_PINGS = 10;
|
|
171
175
|
// Cap on the pool of completed staging buffers, so what one client retains for reuse is
|
|
172
176
|
// bounded independently of how many batches it sends. Same number as the server's.
|
|
173
177
|
const MAX_STAGE_POOL = 32;
|
|
@@ -539,6 +543,19 @@ class Http2ClientSession extends EventEmitter {
|
|
|
539
543
|
// octets as its pong). Matching by payload would hand those octets to the wrong callback
|
|
540
544
|
// and, for two identical payloads, lose one of them outright.
|
|
541
545
|
this._pingQ = [];
|
|
546
|
+
// Unbounded-work-per-peer guard (SECURITY §8B): node caps the unanswered-ping
|
|
547
|
+
// queue at maxOutstandingPings (default 10). A full queue is not an error: node
|
|
548
|
+
// calls the callback synchronously with ERR_HTTP2_PING_CANCEL and ping() returns
|
|
549
|
+
// false, and the refused ping never reaches the wire (measured on v26.12.1).
|
|
550
|
+
this._maxPingQ = typeof opts.maxOutstandingPings === 'number'
|
|
551
|
+
? opts.maxOutstandingPings : DEFAULT_MAX_OUTSTANDING_PINGS;
|
|
552
|
+
// node's inbound header-pair cap (initializeOptions, internal/http2/util.js):
|
|
553
|
+
// default 128, counting every decoded field (pseudo-headers included) per
|
|
554
|
+
// block. A block over the cap is refused per stream with
|
|
555
|
+
// RST_STREAM(ENHANCE_YOUR_CALM) and ERR_HTTP2_STREAM_ERROR (measured on
|
|
556
|
+
// v26.12.1: default boundary at 128 fields, 5 fields refused with cap 4).
|
|
557
|
+
this._maxHeaderListPairs = typeof opts.maxHeaderListPairs === 'number'
|
|
558
|
+
? opts.maxHeaderListPairs : 128;
|
|
542
559
|
// Response header shapes seen before: the cached object for a block whose bytes
|
|
543
560
|
// we have already decoded under this dynamic-table state (the server's cache).
|
|
544
561
|
this._shapes = opts.shapeCache === false ? null : new Map();
|
|
@@ -1082,7 +1099,12 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1082
1099
|
endStream = payloadMeaningless(method);
|
|
1083
1100
|
}
|
|
1084
1101
|
|
|
1085
|
-
|
|
1102
|
+
// node's refused stream has no id at all: nghttp2 never reserved one, so the
|
|
1103
|
+
// binding leaves stream.id undefined even though the session's nextStreamID has
|
|
1104
|
+
// advanced past the ceiling (measured on v26.12.1). The id is only assigned to
|
|
1105
|
+
// the returned object, never used for wire bookkeeping on a refused request --
|
|
1106
|
+
// rstStream and _trackClose find nothing under undefined and are no-ops.
|
|
1107
|
+
const stream = new Http2ClientStream(this, requestError === undefined ? this._nextSid : undefined, {});
|
|
1086
1108
|
// node records what the application passed at this point (kSentHeaders / kRawHeaders,
|
|
1087
1109
|
// internal/http2/core.js:1902-1904); the getter folds it into the request map on read.
|
|
1088
1110
|
stream._sentSrc = headersParam;
|
|
@@ -1286,7 +1308,7 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1286
1308
|
// at, which is why the check stands above the pseudo branch; an empty array is
|
|
1287
1309
|
// skipped before any bookkeeping (node's `case 0: return`), so it neither pushes
|
|
1288
1310
|
// a value nor spends a slot in _singlesSeen.
|
|
1289
|
-
_addField(list, neverList, rawKey, rawValue) {
|
|
1311
|
+
_addField(list, neverList, rawKey, rawValue, isTrailer) {
|
|
1290
1312
|
if (rawValue === undefined) {
|
|
1291
1313
|
return;
|
|
1292
1314
|
}
|
|
@@ -1313,6 +1335,15 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1313
1335
|
// one-char string, and charCodeAt is the cheaper spelling on every input --
|
|
1314
1336
|
// the fall-through case and the match case alike (OPTIMIZATION.md item 33).
|
|
1315
1337
|
if (key.charCodeAt(0) === 58 /* : */) {
|
|
1338
|
+
// assertValidPseudoHeaderTrailer (internal/http2/util.js) throws for EVERY
|
|
1339
|
+
// colon key, including the request pseudo-headers. A trailer block carries
|
|
1340
|
+
// no pseudo-header at all (measured on v26.12.1 with all five request
|
|
1341
|
+
// pseudo-keys and with an unknown one: TypeError ERR_HTTP2_INVALID_PSEUDOHEADER
|
|
1342
|
+
// and the same message shape, lowercased from the key the caller passed).
|
|
1343
|
+
if (isTrailer) {
|
|
1344
|
+
throw httpErr('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
1345
|
+
`"${key}" is an invalid pseudoheader or is used incorrectly`, TypeError);
|
|
1346
|
+
}
|
|
1316
1347
|
if (key !== ':method' && key !== ':path' && key !== ':scheme' && key !== ':authority'
|
|
1317
1348
|
&& key !== ':protocol') {
|
|
1318
1349
|
throw httpErr('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
@@ -1754,12 +1785,12 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1754
1785
|
}
|
|
1755
1786
|
if (Array.isArray(src)) {
|
|
1756
1787
|
for (let i = 0; i + 1 < src.length; i += 2) {
|
|
1757
|
-
this._addField(list, neverList, src[i], src[i + 1]);
|
|
1788
|
+
this._addField(list, neverList, src[i], src[i + 1], true);
|
|
1758
1789
|
}
|
|
1759
1790
|
} else {
|
|
1760
1791
|
const keys = Object.keys(src);
|
|
1761
1792
|
for (let i = 0; i < keys.length; i++) {
|
|
1762
|
-
this._addField(list, neverList, keys[i], src[keys[i]]);
|
|
1793
|
+
this._addField(list, neverList, keys[i], src[keys[i]], true);
|
|
1763
1794
|
}
|
|
1764
1795
|
}
|
|
1765
1796
|
// RFC 9113 8.1: a trailer block carries no pseudo-header. _addField throws
|
|
@@ -2783,6 +2814,15 @@ class Http2ClientSession extends EventEmitter {
|
|
|
2783
2814
|
// the block was refused, so the caller does not build or hand over the
|
|
2784
2815
|
// header object.
|
|
2785
2816
|
_refuseHeaders(sid, stream, fields, count) {
|
|
2817
|
+
// maxHeaderListPairs counts every field in this block, :status among them.
|
|
2818
|
+
// Each block is checked on its own -- a 1xx block and the later response do
|
|
2819
|
+
// not accumulate -- and a refusal is per stream: RST_STREAM(ENHANCE_YOUR_CALM),
|
|
2820
|
+
// ERR_HTTP2_STREAM_ERROR on the stream, session still up (measured v26.12.1).
|
|
2821
|
+
if (count / 2 > this._maxHeaderListPairs) {
|
|
2822
|
+
this.rstStream(sid, ENHANCE_YOUR_CALM);
|
|
2823
|
+
stream._onRst(ENHANCE_YOUR_CALM);
|
|
2824
|
+
return true;
|
|
2825
|
+
}
|
|
2786
2826
|
let status = 0;
|
|
2787
2827
|
let bad = null;
|
|
2788
2828
|
for (let i = 0; i < count; i += 2) {
|
|
@@ -3141,6 +3181,13 @@ class Http2ClientSession extends EventEmitter {
|
|
|
3141
3181
|
: Buffer.from(payload.buffer, payload.byteOffset, 8);
|
|
3142
3182
|
f.copyInto(body, 0, src, 0, 8);
|
|
3143
3183
|
}
|
|
3184
|
+
// maxOutstandingPings: a full queue refuses the submit. node invokes the callback
|
|
3185
|
+
// SYNCHRONOUSLY with ERR_HTTP2_PING_CANCEL before ping() returns false, and the
|
|
3186
|
+
// callback is never registered in the queue (measured on v26.12.1, both halves).
|
|
3187
|
+
if (this._pingQ.length >= this._maxPingQ) {
|
|
3188
|
+
cb(pingCancelled());
|
|
3189
|
+
return false;
|
|
3190
|
+
}
|
|
3144
3191
|
this._pingQ.push({ cb, at: process.hrtime.bigint() });
|
|
3145
3192
|
this._frame(PING, 0, 0, body, true);
|
|
3146
3193
|
// node hands back what nghttp2's submit returned (measured: a live node session answers
|
package/lib/headers.js
CHANGED
|
@@ -379,7 +379,10 @@ function parse(headers,
|
|
|
379
379
|
|
|
380
380
|
if (key[0] === ':') {
|
|
381
381
|
if (trailer) {
|
|
382
|
-
|
|
382
|
+
// node's assertValidPseudoHeaderTrailer (internal/http2/util.js) rejects
|
|
383
|
+
// every ':' key, including the request and response pseudo-headers.
|
|
384
|
+
throw httpError('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
385
|
+
`"${key}" is an invalid pseudoheader or is used incorrectly`, TypeError);
|
|
383
386
|
}
|
|
384
387
|
if (request) {
|
|
385
388
|
// Request pseudo-headers for PUSH_PROMISE (node: assertValidPseudoHeader,
|
|
@@ -1072,22 +1072,15 @@ const DEFAULT_MAX_OUTSTANDING_PINGS = 10;
|
|
|
1072
1072
|
// (util.js:279, MathMax(1, ...)) and multiplied by 1 000 000 by the C++ layer
|
|
1073
1073
|
// (node_http2.cc:210), whose default is 10 000 000 (node_http2.h:40).
|
|
1074
1074
|
const DEFAULT_MAX_SESSION_MEMORY = 10 * 1000 * 1000;
|
|
1075
|
-
// node
|
|
1076
|
-
//
|
|
1077
|
-
//
|
|
1078
|
-
//
|
|
1079
|
-
//
|
|
1080
|
-
//
|
|
1081
|
-
//
|
|
1082
|
-
//
|
|
1083
|
-
//
|
|
1084
|
-
const STREAM_MEM_CHARGE = 360 + 131;
|
|
1085
|
-
// The session's own persistent buffers count against node's budget too: node
|
|
1086
|
-
// includes sizeof(Http2Session) (760 octets in the same probe) in
|
|
1087
|
-
// current_session_memory(). h2lean's JS counterpart is the pair of scratch
|
|
1088
|
-
// buffers plus the first staging buffer; the header assembly buffer is charged
|
|
1089
|
-
// when it is actually allocated, in _appendHdr.
|
|
1090
|
-
const SESSION_MEM_CHARGE = HEADER_LEN + 6 + STAGE_MIN;
|
|
1075
|
+
// node charges sizeof(Http2Stream) (360 octets compiled against v26.12.1), its
|
|
1076
|
+
// own per-stream nghttp2 allocation, name.length + value.length + 32 per field
|
|
1077
|
+
// and the full length of every queued output buffer (node_http2.cc:915/925,
|
|
1078
|
+
// :2745, current_session_memory()). h2lean has none of those C++ objects, so it
|
|
1079
|
+
// does not invent equivalents for them: it charges the JS bytes it actually
|
|
1080
|
+
// holds -- the session's own buffers (HPACK scratch, split frame header, header
|
|
1081
|
+
// block assembly and staging) plus the bodies streams park while the peer
|
|
1082
|
+
// withholds credit. The trip point is therefore h2lean's real memory rather
|
|
1083
|
+
// than node's; DIVERGENCES.md records the difference.
|
|
1091
1084
|
// nghttp2 answers a failed stream-open callback with this session error
|
|
1092
1085
|
// (node_http2.cc:1073-1085, NGHTTP2_ERR_CALLBACK_FAILURE). Measured on a live
|
|
1093
1086
|
// node 26 server: ERR_HTTP2_ERROR with this message on both 'sessionError' and
|
|
@@ -1262,6 +1255,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1262
1255
|
this._goawayCode = null;
|
|
1263
1256
|
this._goawayLastStreamID = null;
|
|
1264
1257
|
this.pendingSettingsAck = true;
|
|
1258
|
+
// node seeds a new stream's receive window from the initialWindowSize in effect at the
|
|
1259
|
+
// moment the stream opens: 65535 until our own SETTINGS is acked (the C++ default), then the
|
|
1260
|
+
// value we advertised. Measured on v26.12.1 with { initialWindowSize: 18 }: a HEADERS before
|
|
1261
|
+
// the ACK reports 65535 while one after it reports 18; the upshot is that a client which wrote
|
|
1262
|
+
// a default-window burst before our SETTINGS arrived is not punished for it. h2lean applies
|
|
1263
|
+
// opts.settings to localSettings at construction (the callback-side divergence documented at
|
|
1264
|
+
// settings()), so the pre-ACK default has to be tracked explicitly for this one window.
|
|
1265
|
+
this._settingsAcked = false;
|
|
1265
1266
|
// node keeps the origin set in private state and exposes `session.originSet` through a getter
|
|
1266
1267
|
// that returns a COPY of it -- or undefined unless the session is encrypted (measured on a live
|
|
1267
1268
|
// node h2c server: `session.originSet` reads undefined there, whatever a peer sends). It is a
|
|
@@ -1361,10 +1362,19 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1361
1362
|
// reaches MaxListeners after eleven unanswered pings (measured: node warns for none). The
|
|
1362
1363
|
// queue order is node's answer rule too (lib/client/Http2ClientSession.js, _pingQ).
|
|
1363
1364
|
this._pingQ = [];
|
|
1365
|
+
// Streams granted a WINDOW_UPDATE in the current read batch; only these need
|
|
1366
|
+
// their queued flag cleared at _endBatch(), not every live stream.
|
|
1367
|
+
this._wuStreams = [];
|
|
1364
1368
|
// Unbounded-work-per-peer guards (SECURITY §8B): node bounds these, h2lean must too.
|
|
1365
1369
|
// maxOutstandingPings defaults to 10 (initializeOptions, internal/http2/util.js).
|
|
1366
1370
|
this._maxPingQ = typeof opts.maxOutstandingPings === 'number'
|
|
1367
1371
|
? opts.maxOutstandingPings : DEFAULT_MAX_OUTSTANDING_PINGS;
|
|
1372
|
+
// maxSettings: node reads the option without creation-time validation and clamps a
|
|
1373
|
+
// finite number to at least 1; anything else keeps the 32-entry default. Measured on
|
|
1374
|
+
// a live node server: 0/1/1.5 refuse a two-entry SETTINGS frame, while NaN, Infinity
|
|
1375
|
+
// and a string fall back to 32 and still refuse the 33rd entry.
|
|
1376
|
+
this._maxSettings = typeof opts.maxSettings === 'number' && Number.isFinite(opts.maxSettings)
|
|
1377
|
+
? Math.max(1, opts.maxSettings) : MAX_SETTINGS_ENTRIES;
|
|
1368
1378
|
// maxSessionInvalidFrames: count the HTTP-messaging refusals (a bad request field
|
|
1369
1379
|
// list, a trailer block without END_STREAM) instead of letting them be free; the
|
|
1370
1380
|
// first limit+1 are refused per stream, the next closes the connection
|
|
@@ -1378,14 +1388,12 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1378
1388
|
this._rejectedStreams = 0;
|
|
1379
1389
|
this._maxRejectedStreams = typeof opts.maxSessionRejectedStreams === 'number'
|
|
1380
1390
|
? opts.maxSessionRejectedStreams : DEFAULT_MAX_REJECTED_STREAMS;
|
|
1381
|
-
// maxSessionMemory:
|
|
1382
|
-
//
|
|
1383
|
-
//
|
|
1384
|
-
//
|
|
1385
|
-
//
|
|
1386
|
-
//
|
|
1387
|
-
// scratch and staging buffers open the budget, as sizeof(Http2Session) does
|
|
1388
|
-
// on node; _appendHdr adds the header assembly buffer when it exists.
|
|
1391
|
+
// maxSessionMemory: refuse a new stream with ENHANCE_YOUR_CALM when the
|
|
1392
|
+
// bytes this session actually holds cross the budget. Unlike node there is
|
|
1393
|
+
// no per-stream sizeof() charge: h2lean's accounting is the live byteLength
|
|
1394
|
+
// of its own buffers plus the bodies streams park without credit (the
|
|
1395
|
+
// _memUsed getter). A drained connection releases the parked bytes and can
|
|
1396
|
+
// open streams again.
|
|
1389
1397
|
this._memLimit = typeof opts.maxSessionMemory === 'number'
|
|
1390
1398
|
? Math.max(1, opts.maxSessionMemory) * 1000000 : DEFAULT_MAX_SESSION_MEMORY;
|
|
1391
1399
|
// nghttp2 charges queued outbound bytes against the SAME session budget that
|
|
@@ -1395,7 +1403,6 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1395
1403
|
// all go out on node, while the fixed 8 MiB ceiling refused 39 of them; with
|
|
1396
1404
|
// this ceiling the counts agree at m=1/10/100/1000 and the DoS guard stays.
|
|
1397
1405
|
this._holdLimit = this._memLimit;
|
|
1398
|
-
this._memUsed = SESSION_MEM_CHARGE;
|
|
1399
1406
|
// Outbound padding: node's C++ switch only acts on 0/1/2; every other
|
|
1400
1407
|
// number is an unpadded session (measured on a live node 26 server).
|
|
1401
1408
|
this._padding = opts.paddingStrategy === f.PADDING_ALIGNED ? f.PADDING_ALIGNED
|
|
@@ -2606,6 +2613,7 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2606
2613
|
return;
|
|
2607
2614
|
}
|
|
2608
2615
|
this.pendingSettingsAck = false;
|
|
2616
|
+
this._settingsAcked = true;
|
|
2609
2617
|
// Process the oldest outstanding submit: node's settingsCallback fires the
|
|
2610
2618
|
// callback with (null, localSettings, duration) where duration is a monotonic
|
|
2611
2619
|
// clock reading in milliseconds, and emits 'localSettings' on EVERY ack --
|
|
@@ -2627,6 +2635,12 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2627
2635
|
return;
|
|
2628
2636
|
}
|
|
2629
2637
|
this._applySettings(buf, start, len);
|
|
2638
|
+
// A refused block produces no remoteSettings event and no SETTINGS ACK:
|
|
2639
|
+
// node's protocol error ends the session here (measured with maxSettings 1:
|
|
2640
|
+
// one server session, no stream, no remoteSettings, GOAWAY(11, 0)).
|
|
2641
|
+
if (!this._open) {
|
|
2642
|
+
return;
|
|
2643
|
+
}
|
|
2630
2644
|
this.emit('remoteSettings', this.remoteSettings);
|
|
2631
2645
|
this._frame(SETTINGS, F_ACK, 0, null);
|
|
2632
2646
|
return;
|
|
@@ -2665,6 +2679,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2665
2679
|
const entry = this._pingQ.shift();
|
|
2666
2680
|
if (entry !== undefined) {
|
|
2667
2681
|
entry.cb(null, Number(process.hrtime.bigint() - entry.at) / 1e6, payload);
|
|
2682
|
+
} else {
|
|
2683
|
+
// An ACK with no PING of ours outstanding is a protocol error. Measured on
|
|
2684
|
+
// a live node server: ERR_HTTP2_ERROR "Protocol error" on the session, a
|
|
2685
|
+
// close and GOAWAY(error 2, last 0) with an empty debug field and no 'goaway'
|
|
2686
|
+
// event on the session. nghttp2 reports this protocol error with INTERNAL_ERROR
|
|
2687
|
+
// on the wire, not PROTOCOL_ERROR, and with node's own error shape rather
|
|
2688
|
+
// than the default ERR_HTTP2_SESSION_ERROR a fatal would otherwise report.
|
|
2689
|
+
this._fatal(INTERNAL_ERROR, 'Protocol error', 'ERR_HTTP2_ERROR', '');
|
|
2668
2690
|
}
|
|
2669
2691
|
return;
|
|
2670
2692
|
}
|
|
@@ -2796,9 +2818,13 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2796
2818
|
}
|
|
2797
2819
|
// CVE-2020-11080: the frame size limit does not cover SETTINGS (_allowFrame exempts
|
|
2798
2820
|
// it, as RFC 9113 6.2 requires), so without an entry bound a peer can hand us any
|
|
2799
|
-
// number of entries to parse. 32 is
|
|
2800
|
-
|
|
2801
|
-
|
|
2821
|
+
// number of entries to parse. 32 is node's default; the maxSettings option moves the
|
|
2822
|
+
// bound per session, as measured on a live node server (see _maxSettings above).
|
|
2823
|
+
// node's own error is ERR_HTTP2_ERROR "Protocol error" with the entry-count reason
|
|
2824
|
+
// as GOAWAY debug data, not a session error named after the reason.
|
|
2825
|
+
if (len / 6 > this._maxSettings) {
|
|
2826
|
+
this._fatal(ENHANCE_YOUR_CALM, 'Protocol error', 'ERR_HTTP2_ERROR',
|
|
2827
|
+
'SETTINGS: too many setting entries');
|
|
2802
2828
|
return;
|
|
2803
2829
|
}
|
|
2804
2830
|
const seen = this.remoteSettings;
|
|
@@ -3017,17 +3043,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3017
3043
|
cap *= 2;
|
|
3018
3044
|
}
|
|
3019
3045
|
this._hdr = Buffer.allocUnsafe(cap);
|
|
3020
|
-
this._memUsed += cap;
|
|
3021
3046
|
} else if (this._hdrLen + len > this._hdr.length) {
|
|
3022
|
-
|
|
3023
|
-
let cap = old * 2;
|
|
3047
|
+
let cap = this._hdr.length * 2;
|
|
3024
3048
|
while (cap < this._hdrLen + len) {
|
|
3025
3049
|
cap *= 2;
|
|
3026
3050
|
}
|
|
3027
3051
|
const bigger = Buffer.allocUnsafe(cap);
|
|
3028
3052
|
f.copyInto(bigger, 0, this._hdr, 0, this._hdrLen);
|
|
3029
3053
|
this._hdr = bigger;
|
|
3030
|
-
this._memUsed += cap - old;
|
|
3031
3054
|
}
|
|
3032
3055
|
f.copyInto(this._hdr, this._hdrLen, buf, start, start + len);
|
|
3033
3056
|
this._hdrLen += len;
|
|
@@ -3667,12 +3690,12 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3667
3690
|
|
|
3668
3691
|
// maxSessionMemory: node refuses a stream it cannot account for with the same
|
|
3669
3692
|
// code and the same rejected-stream escalation as a concurrency refusal
|
|
3670
|
-
// (node_http2.cc:1073-1085).
|
|
3671
|
-
//
|
|
3672
|
-
//
|
|
3673
|
-
//
|
|
3674
|
-
|
|
3675
|
-
if (this._memUsed
|
|
3693
|
+
// (node_http2.cc:1073-1085). h2lean checks the bytes it actually holds here
|
|
3694
|
+
// -- after the block is decoded, before the stream is created, the only
|
|
3695
|
+
// place the peer is still refused without a handler holding its object.
|
|
3696
|
+
// A flood of tiny header-only streams does not cross the cap: node charges
|
|
3697
|
+
// sizeof(Http2Stream) for each, h2lean has no such object to charge.
|
|
3698
|
+
if (this._memUsed > this._memLimit) {
|
|
3676
3699
|
// node compares the PRE-increment count (rejected_stream_count_++ > max)
|
|
3677
3700
|
// and submits no RST on the frame that trips it: max+1 refusals go out,
|
|
3678
3701
|
// the next opens nothing and ends the session.
|
|
@@ -3767,8 +3790,6 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3767
3790
|
}
|
|
3768
3791
|
this._streams.set(sid, stream);
|
|
3769
3792
|
this._liveStreams++;
|
|
3770
|
-
stream._memCharge = memCharge;
|
|
3771
|
-
this._memUsed += memCharge;
|
|
3772
3793
|
if (promised !== undefined) {
|
|
3773
3794
|
stream._expLen = +promised;
|
|
3774
3795
|
}
|
|
@@ -3906,6 +3927,21 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3906
3927
|
this._fatal(FLOW_CONTROL_ERROR, 'connection flow control window exceeded');
|
|
3907
3928
|
return undefined;
|
|
3908
3929
|
}
|
|
3930
|
+
// The per-stream half of the same rule. node's nghttp2 counts the bytes RECEIVED
|
|
3931
|
+
// against the stream window as frames arrive, and consumption only grants credit
|
|
3932
|
+
// back through WINDOW_UPDATE (node sets NO_AUTO_WINDOW_UPDATE in its own C++).
|
|
3933
|
+
// A stream that overruns its window therefore kills the connection with an empty
|
|
3934
|
+
// GOAWAY(FLOW_CONTROL_ERROR) and ERR_HTTP2_ERROR "Protocol error" on both the
|
|
3935
|
+
// session and the stream, whether or not the application is reading -- measured on
|
|
3936
|
+
// a live node server with a flowing window of 18 (two 18-byte chunks delivered,
|
|
3937
|
+
// GOAWAY last stream 13 on the third) and a paused window of 36 (no data delivered,
|
|
3938
|
+
// GOAWAY on the third). The stream error is reported without an RST_STREAM: the
|
|
3939
|
+
// connection error's GOAWAY is the only frame on the wire.
|
|
3940
|
+
if (l > stream._state.localWindowSize) {
|
|
3941
|
+
this._fatal(FLOW_CONTROL_ERROR, 'Protocol error', 'ERR_HTTP2_ERROR', '');
|
|
3942
|
+
stream._tick(() => stream.emit('error', errorOf('ERR_HTTP2_ERROR', 'Protocol error')));
|
|
3943
|
+
return undefined;
|
|
3944
|
+
}
|
|
3909
3945
|
// RFC 9113 8.2 makes content-length a promise, and the body may not run past it. Measured
|
|
3910
3946
|
// on node (h2spec http2/8.1.2.6): the frame that crosses the line ends the CONNECTION --
|
|
3911
3947
|
// GOAWAY(INTERNAL_ERROR) -- rather than the stream, because nghttp2 reads an overflow as a
|
|
@@ -4041,6 +4077,21 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4041
4077
|
+ (this._blocked === null ? 0 : this._blocked.size * STREAM_HOLD_CHARGE);
|
|
4042
4078
|
}
|
|
4043
4079
|
|
|
4080
|
+
// The bytes this session actually holds, read on demand instead of maintained
|
|
4081
|
+
// as a counter: there is no calibration to keep in sync when one of the
|
|
4082
|
+
// buffers grows or is handed to the socket, and each term is the buffer's own
|
|
4083
|
+
// byteLength. Views of a socket read are not counted -- the session does not
|
|
4084
|
+
// own that memory (AGENTS.md §5) -- and a stage already queued belongs to the
|
|
4085
|
+
// socket queue, bounded separately by MAX_OUT_BATCH.
|
|
4086
|
+
get _memUsed() {
|
|
4087
|
+
return this._decoder.scratch.byteLength
|
|
4088
|
+
+ this._fragHdr.byteLength
|
|
4089
|
+
+ this._hdrPrefix.byteLength
|
|
4090
|
+
+ (this._stage === null ? 0 : this._stage.byteLength)
|
|
4091
|
+
+ (this._hdr === null ? 0 : this._hdr.byteLength)
|
|
4092
|
+
+ this._heldBytes;
|
|
4093
|
+
}
|
|
4094
|
+
|
|
4044
4095
|
// Only streams that actually stalled are worth revisiting. Scanning every
|
|
4045
4096
|
// stream on each connection-level WINDOW_UPDATE looks harmless at low load
|
|
4046
4097
|
// and is quadratic under a large response body (OPTIMIZATION.md item 19).
|
|
@@ -4250,14 +4301,32 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4250
4301
|
_consume(stream, n) {
|
|
4251
4302
|
stream._recvUsed -= n; // connection share, released below
|
|
4252
4303
|
this._connRecvUsed += n;
|
|
4253
|
-
// The peer's stream window only grows when we
|
|
4254
|
-
//
|
|
4304
|
+
// The peer's stream window only grows when we grant it. node's nghttp2 grants
|
|
4305
|
+
// consumed bytes in half-window steps and lets only ONE window update per stream
|
|
4306
|
+
// be queued at a time: the queued flag clears when the frame is sent, which here
|
|
4307
|
+
// is _endBatch()/_flush(). That is why a burst of DATA overruns the stream window
|
|
4308
|
+
// after the first grant rather than after every frame (measured on a live node
|
|
4309
|
+
// server: two 18-byte chunks with a window of 18, three with a window of 36).
|
|
4255
4310
|
stream._unannounced += n;
|
|
4256
|
-
if (stream.
|
|
4257
|
-
const
|
|
4258
|
-
stream._unannounced
|
|
4259
|
-
stream.
|
|
4260
|
-
|
|
4311
|
+
if (!stream._wuQueued) {
|
|
4312
|
+
const owed = stream._streamInitialWindow - stream._state.localWindowSize;
|
|
4313
|
+
const inc = Math.min(stream._unannounced, owed);
|
|
4314
|
+
if (inc >= (stream._streamInitialWindow >> 1)) {
|
|
4315
|
+
stream._unannounced -= inc;
|
|
4316
|
+
stream._state.localWindowSize += inc;
|
|
4317
|
+
stream._wuQueued = true;
|
|
4318
|
+
// In a read batch _endBatch clears the flag once the batch is flushed.
|
|
4319
|
+
// Between batches _windowUpdate schedules its own flush and there is no
|
|
4320
|
+
// _endBatch coming, so it has to be cleared here or this stream could
|
|
4321
|
+
// never be granted credit again (a reader arriving on a timer stalls
|
|
4322
|
+
// the sender permanently).
|
|
4323
|
+
if (this._inBatch) {
|
|
4324
|
+
this._wuStreams.push(stream);
|
|
4325
|
+
} else {
|
|
4326
|
+
stream._wuQueued = false;
|
|
4327
|
+
}
|
|
4328
|
+
this._windowUpdate(stream.id, inc);
|
|
4329
|
+
}
|
|
4261
4330
|
}
|
|
4262
4331
|
// A reader that shows up between batches (a timer, an await) has to be able
|
|
4263
4332
|
// to unblock the sender on its own: _endBatch() is not coming this turn.
|
|
@@ -4306,6 +4375,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4306
4375
|
_endBatch() {
|
|
4307
4376
|
this._creditConnection();
|
|
4308
4377
|
this._flush();
|
|
4378
|
+
// The batch's WINDOW_UPDATE frames are on the wire now: node clears a stream's
|
|
4379
|
+
// queued-update flag when the frame is sent, so the next consumption may grant
|
|
4380
|
+
// that stream more credit. Leftover consumed bytes stay in _unannounced until
|
|
4381
|
+
// the application takes more data -- nothing is granted to a reader that stopped.
|
|
4382
|
+
for (const stream of this._wuStreams) {
|
|
4383
|
+
stream._wuQueued = false;
|
|
4384
|
+
}
|
|
4385
|
+
this._wuStreams.length = 0;
|
|
4309
4386
|
if (this._closing && this._liveStreams === 0) {
|
|
4310
4387
|
// The same finish the stream teardown uses, not a raw socket.end(): the
|
|
4311
4388
|
// flush above only HANDS the batch to the socket (_frozenLen covers it
|
|
@@ -4432,12 +4509,9 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4432
4509
|
return;
|
|
4433
4510
|
}
|
|
4434
4511
|
this._liveStreams--;
|
|
4435
|
-
//
|
|
4436
|
-
//
|
|
4437
|
-
|
|
4438
|
-
this._memUsed -= stream._memCharge;
|
|
4439
|
-
stream._memCharge = undefined;
|
|
4440
|
-
}
|
|
4512
|
+
// _memUsed is read from the buffers themselves: the parked bytes this
|
|
4513
|
+
// stream may have held leave the charge with the stream object, so a
|
|
4514
|
+
// drained connection can open streams again.
|
|
4441
4515
|
// inbound bytes the application never got still owe the connection window
|
|
4442
4516
|
this._abandon(stream);
|
|
4443
4517
|
if (killed) {
|
|
@@ -4863,7 +4937,11 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4863
4937
|
// maxOutstandingPings: node caps the unanswered-ping queue (initializeOptions, util.js).
|
|
4864
4938
|
// A full queue is not an error: nghttp2's submit fails, node's ping() answers false and
|
|
4865
4939
|
// the callback is never registered (core.js: `if (duration !== false) this[kQueue].push`).
|
|
4940
|
+
// The refused submit still calls its callback SYNCHRONOUSLY with ERR_HTTP2_PING_CANCEL
|
|
4941
|
+
// ("HTTP2 ping cancelled", duration and pong undefined) before ping() returns false,
|
|
4942
|
+
// exactly as on the client half (measured on a live node server with cap 1 and 3).
|
|
4866
4943
|
if (this._pingQ.length >= this._maxPingQ) {
|
|
4944
|
+
callback(pingCancelled());
|
|
4867
4945
|
return false;
|
|
4868
4946
|
}
|
|
4869
4947
|
this._pingQ.push({ cb: callback, at: process.hrtime.bigint() });
|
|
@@ -135,13 +135,20 @@ class Http2ServerStream extends Http2Stream {
|
|
|
135
135
|
this.aborted = false;
|
|
136
136
|
this.endAfterHeaders = false;
|
|
137
137
|
this.allowHalfOpen = false;
|
|
138
|
+
// node seeds this from the initialWindowSize in effect when the stream opens: the C++
|
|
139
|
+
// default of 65535 until our SETTINGS is acked, the advertised value afterwards
|
|
140
|
+
// (measured on v26.12.1 with initialWindowSize 18: HEADERS before the ACK reports
|
|
141
|
+
// 65535, one after reports 18). A stream therefore keeps the window it was born with
|
|
142
|
+
// even if SETTINGS changes later, which is why the credit math uses this same value.
|
|
143
|
+
this._streamInitialWindow = session._settingsAcked
|
|
144
|
+
? session.localSettings.initialWindowSize : 65535;
|
|
138
145
|
this._state = {
|
|
139
146
|
state: ST_OPEN,
|
|
140
147
|
weight: 16,
|
|
141
148
|
sumDependencyWeight: 0,
|
|
142
149
|
localClose: 0,
|
|
143
150
|
remoteClose: 0,
|
|
144
|
-
localWindowSize:
|
|
151
|
+
localWindowSize: this._streamInitialWindow
|
|
145
152
|
};
|
|
146
153
|
this._sendWin = session.remoteSettings.initialWindowSize;
|
|
147
154
|
this._recvUsed = 0; // inbound bytes not yet released as credit
|
package/lib/server/compat.js
CHANGED
|
@@ -1341,7 +1341,13 @@ class Http2ServerResponse extends Stream {
|
|
|
1341
1341
|
callback(e);
|
|
1342
1342
|
return;
|
|
1343
1343
|
}
|
|
1344
|
-
|
|
1344
|
+
// node's compat creates a pushed response with the same constructor the server
|
|
1345
|
+
// was given via Http2ServerResponse (internal/http2/compat, createPushResponse).
|
|
1346
|
+
const server = this._stream.session !== undefined
|
|
1347
|
+
? this._stream.session.server : undefined;
|
|
1348
|
+
const Response = server !== undefined && server._http2ServerResponse !== undefined
|
|
1349
|
+
? server._http2ServerResponse : Http2ServerResponse;
|
|
1350
|
+
const push = new Response(stream);
|
|
1345
1351
|
// A pushed stream has no request, so nothing else installs the compat
|
|
1346
1352
|
// dispatch on it; the response's handlers need it the same way the
|
|
1347
1353
|
// request's constructor installs it for a normal exchange.
|
|
@@ -1357,8 +1363,16 @@ class Http2ServerResponse extends Stream {
|
|
|
1357
1363
|
// the server as `this`.
|
|
1358
1364
|
function onServerStream(stream) {
|
|
1359
1365
|
const server = this;
|
|
1360
|
-
|
|
1361
|
-
|
|
1366
|
+
// node's setupCompat hands the server's Http2ServerRequest/Http2ServerResponse
|
|
1367
|
+
// options to the compat layer, so the handler receives the caller's subclasses.
|
|
1368
|
+
// The objects are still built here, at the first compat delivery, with the same
|
|
1369
|
+
// lazy-instantiation and dispatch-before-event order as before.
|
|
1370
|
+
const Request = server._http2ServerRequest !== undefined
|
|
1371
|
+
? server._http2ServerRequest : Http2ServerRequest;
|
|
1372
|
+
const Response = server._http2ServerResponse !== undefined
|
|
1373
|
+
? server._http2ServerResponse : Http2ServerResponse;
|
|
1374
|
+
const request = new Request(stream);
|
|
1375
|
+
const response = new Response(stream);
|
|
1362
1376
|
|
|
1363
1377
|
const method = stream._reqMethod;
|
|
1364
1378
|
if (method === 'CONNECT') {
|
package/lib/server/server.js
CHANGED
|
@@ -207,6 +207,11 @@ function initHttp2Server(server, options) {
|
|
|
207
207
|
maxOutstandingPings: opts.maxOutstandingPings,
|
|
208
208
|
maxSessionInvalidFrames: opts.maxSessionInvalidFrames,
|
|
209
209
|
maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
|
|
210
|
+
// maxSettings bounds the entries in one inbound SETTINGS frame. node reads it without
|
|
211
|
+
// creation-time validation: a finite number is clamped to at least 1, anything else
|
|
212
|
+
// keeps the 32-entry default (measured on a live node server with values 0, 1, 1.5,
|
|
213
|
+
// NaN, Infinity and a string). The session applies the bound per connection.
|
|
214
|
+
maxSettings: opts.maxSettings,
|
|
210
215
|
// Connection-level receive window (separate from the per-stream SETTINGS value):
|
|
211
216
|
// validated above at createServer time, applied to the session's state and
|
|
212
217
|
// announced with one WINDOW_UPDATE(stream 0) when it exceeds the default.
|
|
@@ -225,6 +230,12 @@ function initHttp2Server(server, options) {
|
|
|
225
230
|
remoteCustomSettings: opts.remoteCustomSettings
|
|
226
231
|
};
|
|
227
232
|
server._sessions = new Set();
|
|
233
|
+
// node's setupCompat passes the caller's Http2ServerRequest/Http2ServerResponse
|
|
234
|
+
// subclasses into the compat layer, so the handler receives those objects rather
|
|
235
|
+
// than the built-in classes. Keep them on the server and let onServerStream pick
|
|
236
|
+
// them up at construction, without changing when the objects are created.
|
|
237
|
+
server._http2ServerRequest = opts.Http2ServerRequest;
|
|
238
|
+
server._http2ServerResponse = opts.Http2ServerResponse;
|
|
228
239
|
server[kCompat] = null;
|
|
229
240
|
// node arms its compat layer through a newListener hook (setupCompat,
|
|
230
241
|
// internal/http2/core.js) rather than by wrapping the registration methods, so
|
|
@@ -674,8 +685,8 @@ function createSecureServer(options, onRequestHandler) {
|
|
|
674
685
|
// setupHandle throw escapes the call. The rest (origins) concerns a feature
|
|
675
686
|
// h2lean does implement but has no server-level ORIGIN frame to send, so it rides
|
|
676
687
|
// through raw and is applied per-session. Unbounded-work-per-peer guards
|
|
677
|
-
// (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings)
|
|
678
|
-
// ride through raw and are resolved in the session constructor; connectionWindowSize
|
|
688
|
+
// (maxSessionInvalidFrames, maxSessionRejectedStreams, maxOutstandingPings, maxSettings)
|
|
689
|
+
// also ride through raw and are resolved in the session constructor; connectionWindowSize
|
|
679
690
|
// is a session option validated here, like node's initializeOptions does.
|
|
680
691
|
// The one surface difference
|
|
681
692
|
// (node's JSStreamSocket wrap of a generic Duplex, so 'connect's second argument
|
package/package.json
CHANGED