h2lean 0.19.3 โ 0.19.5
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.
- package/README.md +1 -1
- package/lib/Http2Stream.js +4 -4
- package/lib/client/Http2ClientSession.js +74 -22
- package/lib/frames.js +40 -0
- package/lib/headers.js +21 -23
- package/lib/hpack.js +3 -15
- package/lib/server/Http2ServerSession.js +127 -5
- package/lib/server/Http2ServerStream.js +21 -6
- package/lib/server/server.js +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -292,7 +292,7 @@ None silently returns a wrong answer: a missing method is `undefined`, so callin
|
|
|
292
292
|
|
|
293
293
|
**Behavioural differences** (full reasoning in the header comment of the affected file and [AGENTS.md](./AGENTS.md) ยง10):
|
|
294
294
|
|
|
295
|
-
- ๐๏ธ **Options accepted but ignored:** `
|
|
295
|
+
- ๐๏ธ **Options accepted but ignored:** `headerHintTimeout`/`headerHintSize` (no 1xx informational-flood guard). `paddingStrategy` (0/1/2, with node's 8-octet-aligned and 255-octet-pad wire shapes) and `maxSessionMemory` (per-stream and retained-header budget with node's ENHANCE_YOUR_CALM refusal and callback-failure teardown) are now implemented; see [SECURITY.md](./SECURITY.md).
|
|
296
296
|
- ๐ช The **graceful-close predicate**, the **spurious `200` on `end()` with no body**, a **response queued in the same batch as a refusal**, the **`COMPRESSION_ERROR` vs `FRAME_SIZE_ERROR`** verdict, the **teardown event order**, and **`req.pause()/read()` not slowing the peer** (credit is window-bounded, not per-`read()`) differ from node โ each pinned as a KNOWN DIVERGENCE in the test suite.
|
|
297
297
|
|
|
298
298
|
## ๐ Documentation
|
package/lib/Http2Stream.js
CHANGED
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
// ERR_HTTP2_STREAM_ERROR(NGHTTP2_INTERNAL_ERROR) rather than throwing out of the call,
|
|
83
83
|
// matching node's buildNgHeaderString/processRespondWithFD teardown: a bad field name
|
|
84
84
|
// leaves headersSent true (node sets HEADERS_SENT before validating fields) and takes
|
|
85
|
-
// the destroy path. An array-form headers argument passes through hdr.
|
|
85
|
+
// the destroy path. An array-form headers argument passes through hdr.prepare() as-is
|
|
86
86
|
// instead of node's for..in over indices (which sends field names '0', '1', ...);
|
|
87
87
|
// the content-length statCheck injects is only applied to object-form headers.
|
|
88
88
|
// * 'finish' and writableFinished exist on the stream: 'finish' is queued on a
|
|
@@ -345,11 +345,11 @@ function fdOptions(options) {
|
|
|
345
345
|
// node's prepareResponseHeadersObject (internal/http2/core.js): a null-prototype copy
|
|
346
346
|
// of the caller's object with ':status' coerced in, so a statCheck that rewrites
|
|
347
347
|
// 'content-length' lands on the copy -- which is also what sentHeaders reports,
|
|
348
|
-
// exactly where node's kSentHeaders has it. The generated date is left to hdr.
|
|
349
|
-
// whose sendDate rule
|
|
348
|
+
// exactly where node's kSentHeaders has it. The generated date is left to hdr.prepare(),
|
|
349
|
+
// whose sendDate rule governs the encode path too. Status validation happens here,
|
|
350
350
|
// before the payload-forbidden check -- measured: ':status': 700 throws
|
|
351
351
|
// ERR_HTTP2_STATUS_INVALID out of the call, and ':status': '700' is coerced to 700
|
|
352
|
-
// first. The array form passes through untouched (hdr.
|
|
352
|
+
// first. The array form passes through untouched (hdr.prepare() handles pairs natively);
|
|
353
353
|
// node's for..in over an array copies the index keys into the copy and sends field
|
|
354
354
|
// names '0', '1', ... instead -- the junk shape the header comment documents.
|
|
355
355
|
function prepareResponseHeadersObject(headersParam) {
|
|
@@ -250,6 +250,15 @@ class Http2ClientSession extends EventEmitter {
|
|
|
250
250
|
// maxOriginSetSize is client-only and validated by connect(); the server
|
|
251
251
|
// session leaves it undefined so ORIGIN frames over h2s accumulate unbounded.
|
|
252
252
|
this._maxOriginSetSize = opts.maxOriginSetSize;
|
|
253
|
+
// paddingStrategy is a per-session node option (internal/http2/util.js:259)
|
|
254
|
+
// and applies to every outbound HEADERS/DATA frame. maxSessionMemory is
|
|
255
|
+
// accepted by connect() and forwarded to the C++ session, but a client's
|
|
256
|
+
// streams are created locally by request(): measured on a live node 26
|
|
257
|
+
// client with a 1 MB budget and 3000 unanswered requests, none is refused
|
|
258
|
+
// and no session error fires. The server-side budget is the one that bounds
|
|
259
|
+
// untrusted inbound work; there is nothing for the client to enforce here.
|
|
260
|
+
this._padding = opts.paddingStrategy === f.PADDING_ALIGNED ? f.PADDING_ALIGNED
|
|
261
|
+
: opts.paddingStrategy === f.PADDING_MAX ? f.PADDING_MAX : f.PADDING_NONE;
|
|
253
262
|
// enablePush defaults to true on the client (node's initializeOptions sets
|
|
254
263
|
// NGHTTP2_SETTINGS_ENABLE_PUSH = 1 for clients; the server's OUR_DEFAULTS has
|
|
255
264
|
// false because a server does not receive pushes). The application can override
|
|
@@ -653,19 +662,46 @@ class Http2ClientSession extends EventEmitter {
|
|
|
653
662
|
// staging buffer, because it is a view into memory the next call rewrites -- the HPACK
|
|
654
663
|
// scratch block -- and those payloads are tens of bytes.
|
|
655
664
|
_frame(type, flags, sid, payload, owned) {
|
|
656
|
-
const
|
|
657
|
-
|
|
665
|
+
const bodyLen = payload === null || payload === undefined ? 0 : payload.length;
|
|
666
|
+
let len = bodyLen;
|
|
667
|
+
let pad = 0;
|
|
668
|
+
let padded = false;
|
|
669
|
+
if (this._padding !== f.PADDING_NONE && (type === f.DATA || type === f.HEADERS)) {
|
|
670
|
+
const total = f.padTotal(len, this._padding, this.remoteSettings.maxFrameSize);
|
|
671
|
+
if (total > len) {
|
|
672
|
+
pad = total - len - 1;
|
|
673
|
+
padded = true;
|
|
674
|
+
flags |= f.F_PADDED;
|
|
675
|
+
len = total;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
if (owned && bodyLen >= BODY_MIN) {
|
|
658
679
|
this._commit(); // what is staged was produced earlier, so it
|
|
659
680
|
// has to go out first
|
|
660
681
|
const head = Buffer.allocUnsafe(HEADER_LEN);
|
|
661
682
|
writeHeader(head, 0, len, type, flags, sid);
|
|
662
|
-
|
|
683
|
+
let body = payload;
|
|
684
|
+
if (padded) {
|
|
685
|
+
body = Buffer.allocUnsafe(len);
|
|
686
|
+
body[0] = pad;
|
|
687
|
+
if (bodyLen) {
|
|
688
|
+
f.copyInto(body, 1, payload, 0, bodyLen);
|
|
689
|
+
}
|
|
690
|
+
body.fill(0, 1 + bodyLen, len);
|
|
691
|
+
}
|
|
692
|
+
this._outQ.push(head, body);
|
|
663
693
|
} else {
|
|
664
694
|
this._need(HEADER_LEN + len);
|
|
665
695
|
const off = this._stageLen;
|
|
666
696
|
writeHeader(this._stage, off, len, type, flags, sid);
|
|
667
|
-
if (
|
|
668
|
-
|
|
697
|
+
if (padded) {
|
|
698
|
+
this._stage[off + HEADER_LEN] = pad;
|
|
699
|
+
if (bodyLen) {
|
|
700
|
+
f.copyInto(this._stage, off + HEADER_LEN + 1, payload, 0, bodyLen);
|
|
701
|
+
}
|
|
702
|
+
this._stage.fill(0, off + HEADER_LEN + 1 + bodyLen, off + HEADER_LEN + len);
|
|
703
|
+
} else if (bodyLen) {
|
|
704
|
+
f.copyInto(this._stage, off + HEADER_LEN, payload, 0, bodyLen);
|
|
669
705
|
}
|
|
670
706
|
this._stageLen = off + HEADER_LEN + len;
|
|
671
707
|
}
|
|
@@ -1230,10 +1266,10 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1230
1266
|
const enc = this._encoder;
|
|
1231
1267
|
// The dynamic-table-size announcement has to precede the block it applies to, and its
|
|
1232
1268
|
// length is not known before the block is written: the field lines are what change the
|
|
1233
|
-
// table, and the announcement reports where the table ended up.
|
|
1234
|
-
//
|
|
1235
|
-
//
|
|
1236
|
-
|
|
1269
|
+
// table, and the announcement reports where the table ended up. Five octets are
|
|
1270
|
+
// reserved at the front of the one buffer this block is encoded into, and
|
|
1271
|
+
// takeSizeUpdateInto() writes the announcement there and returns the exact offset
|
|
1272
|
+
// the field lines start at -- no intermediate Buffer and no copy.
|
|
1237
1273
|
let cap = 0;
|
|
1238
1274
|
// nghttp2's outbound estimate, accumulated in the pass that already sizes the
|
|
1239
1275
|
// scratch -- the same formula and the same string-length-not-octets rule
|
|
@@ -1246,26 +1282,19 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1246
1282
|
bound += 12 + list[i].length + list[i + 1].length;
|
|
1247
1283
|
}
|
|
1248
1284
|
sentBound = bound;
|
|
1249
|
-
// Without a size update the block IS the
|
|
1285
|
+
// Without a size update the block IS the buffer: _sendHeaders runs before any later
|
|
1250
1286
|
// encode can rewrite it, and _frame(owned=false) puts a stable copy in the batch.
|
|
1251
1287
|
// With one, the old form was Buffer.concat([update, block]) -- a second allocation,
|
|
1252
|
-
// a second copy of the whole block and a temporary array per request.
|
|
1253
|
-
//
|
|
1254
|
-
//
|
|
1255
|
-
// The headroom exists on this branch only, and the returned view then starts past
|
|
1256
|
-
// the front of its buffer -- which every consumer here reads as
|
|
1288
|
+
// a second copy of the whole block and a temporary array per request. The announcement
|
|
1289
|
+
// now goes straight into the reserved headroom (OPTIMIZATION.md item 14). The returned
|
|
1290
|
+
// view then starts past the front of its buffer -- every consumer here reads
|
|
1257
1291
|
// .length/.copy/write(), all of which honour byteOffset.
|
|
1258
|
-
const
|
|
1259
|
-
const buf = Buffer.allocUnsafe(ul + cap);
|
|
1292
|
+
const buf = Buffer.allocUnsafe(5 + cap);
|
|
1260
1293
|
enc.unsettled = false;
|
|
1261
|
-
let off =
|
|
1294
|
+
let off = enc.takeSizeUpdateInto(buf, 0);
|
|
1262
1295
|
for (let i = 0; i < list.length; i += 3) {
|
|
1263
1296
|
off = enc.write(buf, off, list[i], list[i + 1], list[i + 2]);
|
|
1264
1297
|
}
|
|
1265
|
-
if (ul === 0) {
|
|
1266
|
-
return buf.subarray(0, off);
|
|
1267
|
-
}
|
|
1268
|
-
f.copyInto(buf, 0, update, 0, update.length);
|
|
1269
1298
|
return buf.subarray(0, off);
|
|
1270
1299
|
}
|
|
1271
1300
|
|
|
@@ -1284,6 +1313,23 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1284
1313
|
}
|
|
1285
1314
|
sentBound = bound;
|
|
1286
1315
|
const max = this.remoteSettings.maxFrameSize;
|
|
1316
|
+
if (this._padding !== f.PADDING_NONE) {
|
|
1317
|
+
// Padding is opt-in, so keep the single-stage layout of the common path.
|
|
1318
|
+
// Encode into one owned buffer and let _sendHeaders/_frame pad each frame;
|
|
1319
|
+
// the table-size update has to be written before the fields either way.
|
|
1320
|
+
const raw = Buffer.allocUnsafe(cap);
|
|
1321
|
+
let endOff = enc.takeSizeUpdateInto(raw, 0);
|
|
1322
|
+
enc.unsettled = false;
|
|
1323
|
+
for (let i = 0; i < list.length; i += 3) {
|
|
1324
|
+
endOff = enc.write(raw, endOff, list[i], list[i + 1], list[i + 2]);
|
|
1325
|
+
}
|
|
1326
|
+
if (hp !== undefined && hp !== null && typeof hp === 'object') {
|
|
1327
|
+
this._blocks.set(hp, { epoch: enc.epoch,
|
|
1328
|
+
block: Buffer.from(raw.subarray(0, endOff)), bound });
|
|
1329
|
+
}
|
|
1330
|
+
this._sendHeaders(sid, raw.subarray(0, endOff), end, bound, true);
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1287
1333
|
const frames = Math.max(1, Math.ceil(cap / max));
|
|
1288
1334
|
this._need(HEADER_LEN + cap + HEADER_LEN * (frames - 1));
|
|
1289
1335
|
const dst = this._stage;
|
|
@@ -1525,6 +1571,12 @@ class Http2ClientSession extends EventEmitter {
|
|
|
1525
1571
|
// withheld from the body rides on a zero-length DATA instead (the same rule the
|
|
1526
1572
|
// server path applies in lib/Http2Stream.js sendTrailers, measured against node). A
|
|
1527
1573
|
// pending table-size update makes the block non-empty, so it is never dropped here.
|
|
1574
|
+
// With padding enabled the empty DATA end() submits alongside the
|
|
1575
|
+
// trailers carries a pad-length octet and becomes visible on the wire,
|
|
1576
|
+
// ahead of the END_STREAM frame (measured on node 26).
|
|
1577
|
+
if (this._padding !== f.PADDING_NONE) {
|
|
1578
|
+
this._frame(DATA, 0, stream.id, null, false);
|
|
1579
|
+
}
|
|
1528
1580
|
this._frame(DATA, F_END_STREAM, stream.id, null, false);
|
|
1529
1581
|
} else {
|
|
1530
1582
|
if (this._sendHeaders(stream.id, block, true, bound, false)) {
|
package/lib/frames.js
CHANGED
|
@@ -70,6 +70,45 @@ function writeHeader(dst, off, len, type, flags, streamId) {
|
|
|
70
70
|
dst.writeUInt32BE(streamId >>> 0, off + 5); // reserved bit stays 0
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
// Outbound padding strategies. node passes the raw option through to C++
|
|
74
|
+
// (internal/http2/util.js:259) and its switch (node_http2.cc OnSelectPadding)
|
|
75
|
+
// only has cases for 0, 1 and 2; every other number falls through to "no
|
|
76
|
+
// padding" (measured: 16/32/.../1024 produce unpadded frames).
|
|
77
|
+
const PADDING_NONE = 0, PADDING_ALIGNED = 1, PADDING_MAX = 2;
|
|
78
|
+
|
|
79
|
+
// Total payload length of a padded frame, given its unpadded length and the
|
|
80
|
+
// peer's SETTINGS_MAX_FRAME_SIZE. Mirrors node's OnSelectPadding and the two
|
|
81
|
+
// helpers it calls (node_http2.cc:954/971): ALIGNED rounds the whole frame
|
|
82
|
+
// (length + 9-octet header) up to a multiple of eight, MAX asks for the full
|
|
83
|
+
// frame size, and nghttp2 caps the result at one pad-length octet plus 255
|
|
84
|
+
// pad octets. A result that leaves no room for the pad-length octet means no
|
|
85
|
+
// PADDED flag. Returns the unpadded length when no padding applies.
|
|
86
|
+
function padTotal(len, strategy, maxPayload) {
|
|
87
|
+
if (strategy === PADDING_NONE) {
|
|
88
|
+
return len;
|
|
89
|
+
}
|
|
90
|
+
let total;
|
|
91
|
+
if (strategy === PADDING_MAX) {
|
|
92
|
+
total = maxPayload;
|
|
93
|
+
} else {
|
|
94
|
+
const r = (len + HEADER_LEN) & 7;
|
|
95
|
+
if (r === 0) {
|
|
96
|
+
return len;
|
|
97
|
+
}
|
|
98
|
+
total = len + (8 - r);
|
|
99
|
+
}
|
|
100
|
+
if (total > len + 256) {
|
|
101
|
+
total = len + 256;
|
|
102
|
+
}
|
|
103
|
+
if (total > maxPayload) {
|
|
104
|
+
total = maxPayload;
|
|
105
|
+
}
|
|
106
|
+
// A zero pad length is still a PADDED frame: the pad-length octet itself is
|
|
107
|
+
// the padding (measured: a 6-octet body under ALIGNED goes out as len=7,
|
|
108
|
+
// padlen=0 on node 26).
|
|
109
|
+
return total >= len + 1 ? total : len;
|
|
110
|
+
}
|
|
111
|
+
|
|
73
112
|
// RFC 9113 6.1: a padded DATA frame's payload is one octet of pad length, then
|
|
74
113
|
// the data, then that many octets of padding. The views a payload arrived in are
|
|
75
114
|
// therefore not the payload: the pad length has to come off the front and the
|
|
@@ -147,6 +186,7 @@ module.exports = {
|
|
|
147
186
|
DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
148
187
|
WINDOW_UPDATE, CONTINUATION, ALTSVC, ORIGIN,
|
|
149
188
|
F_END_STREAM, F_ACK, F_END_HEADERS, F_PADDED, F_PRIORITY,
|
|
189
|
+
PADDING_NONE, PADDING_ALIGNED, PADDING_MAX, padTotal,
|
|
150
190
|
NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, SETTINGS_TIMEOUT,
|
|
151
191
|
STREAM_CLOSED, FRAME_SIZE_ERROR, REFUSED_STREAM, CANCEL, COMPRESSION_ERROR,
|
|
152
192
|
CONNECT_ERROR, ENHANCE_YOUR_CALM, INADEQUATE_SECURITY, HTTP_1_1_REQUIRED,
|
package/lib/headers.js
CHANGED
|
@@ -44,11 +44,13 @@ function nullProto() {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
// Encoded bytes for one response, reused between calls. The encoder that fills it
|
|
47
|
-
// belongs to the session (lib/
|
|
48
|
-
// state, so nothing on this path is shared between sessions.
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
47
|
+
// belongs to the session (lib/server/Http2ServerSession.js), because a dynamic table
|
|
48
|
+
// is per-connection state, so nothing on this path is shared between sessions.
|
|
49
|
+
// The server path does not touch this buffer: prepare()/writePrepared() write straight
|
|
50
|
+
// into the session's staging buffer (OPTIMIZATION.md item 68), including the refused
|
|
51
|
+
// PUSH_PROMISE response, which is parsed but never encoded. block() survives for the
|
|
52
|
+
// tests and microbenches that need a standalone block and exercise the block cache. A
|
|
53
|
+
// caller must finish with that view before the next block() call rewrites the scratch.
|
|
52
54
|
let scratch = Buffer.allocUnsafe(4096);
|
|
53
55
|
|
|
54
56
|
const isToken = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
@@ -220,13 +222,6 @@ let singlesSeen = [];
|
|
|
220
222
|
const CONNECTION_SPECIFIC = new Set(['connection', 'keep-alive', 'proxy-connection',
|
|
221
223
|
'transfer-encoding', 'upgrade']);
|
|
222
224
|
|
|
223
|
-
// Whether a field value carries a character that may not appear in one. The server's
|
|
224
|
-
// inbound path asks the same question (lib/server/Http2ServerSession.js) and answers it the way nghttp2
|
|
225
|
-
// does: the field is ignored, the stream lives.
|
|
226
|
-
function hasBadValue(value) {
|
|
227
|
-
return badValue.test(value);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
225
|
// The names of the field lines that arrived as never-indexed HPACK literals,
|
|
231
226
|
// one entry per delivered line, duplicates and arrival order kept. node hangs
|
|
232
227
|
// this under its `sensitiveHeaders` symbol on every inbound header object --
|
|
@@ -548,7 +543,6 @@ function block(headers, opts, session) {
|
|
|
548
543
|
}
|
|
549
544
|
}
|
|
550
545
|
|
|
551
|
-
const update = enc.takeSizeUpdate();
|
|
552
546
|
const parsed = parse(headers,
|
|
553
547
|
{ trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
|
|
554
548
|
strict: session._strictSingle });
|
|
@@ -566,13 +560,17 @@ function block(headers, opts, session) {
|
|
|
566
560
|
const epoch = enc.epoch;
|
|
567
561
|
// The announcement goes in front of the block, in the same buffer: the old form was
|
|
568
562
|
// Buffer.concat([update, out]) -- a second allocation, a copy of the whole block and a
|
|
569
|
-
// temporary array, on the one path where the peer changed the table size.
|
|
570
|
-
//
|
|
571
|
-
|
|
563
|
+
// temporary array, on the one path where the peer changed the table size. Reserve the
|
|
564
|
+
// largest 5-bit-prefix announcement before writing so encodeAt() cannot move the
|
|
565
|
+
// scratch under it, then let takeSizeUpdateInto() consume the pending change and use
|
|
566
|
+
// its exact length as the block's start offset. The client does the same in its own
|
|
567
|
+
// _encodeRequest (lib/client/Http2ClientSession.js).
|
|
568
|
+
const need = capOf(parsed, 5);
|
|
569
|
+
if (scratch.length < need) {
|
|
570
|
+
scratch = Buffer.allocUnsafe(need);
|
|
571
|
+
}
|
|
572
|
+
const at = enc.takeSizeUpdateInto(scratch, 0);
|
|
572
573
|
const end = encodeAt(parsed, enc, at);
|
|
573
|
-
if (at !== 0) {
|
|
574
|
-
copyInto(scratch, 0, update, 0, update.length);
|
|
575
|
-
}
|
|
576
574
|
if (cacheable && enc.epoch === epoch && !enc.unsettled) {
|
|
577
575
|
// a cached block has to outlive the scratch, so only the cached path copies --
|
|
578
576
|
// allocUnsafe, not Buffer.from(subarray): at the sizes a response block has the
|
|
@@ -697,8 +695,8 @@ function prepare(headers, opts, session) {
|
|
|
697
695
|
sent.status = parsed.status;
|
|
698
696
|
sent.date = parsed.genDate;
|
|
699
697
|
// The size update is NOT consumed here: writePrepared() writes its octets into the
|
|
700
|
-
// destination itself. Reserve the maximum the 5-bit prefix can need (the
|
|
701
|
-
//
|
|
698
|
+
// destination itself. Reserve the maximum the 5-bit prefix can need (the room
|
|
699
|
+
// takeSizeUpdateInto() checks) so the encoder has room before a byte is written.
|
|
702
700
|
const at = enc.pendingSize === null ? 0 : 5;
|
|
703
701
|
let bound = 12;
|
|
704
702
|
let cap = 16 + at;
|
|
@@ -719,7 +717,7 @@ function prepare(headers, opts, session) {
|
|
|
719
717
|
|
|
720
718
|
function writePrepared(prep, session, dst, off) {
|
|
721
719
|
const enc = session._encoder;
|
|
722
|
-
//
|
|
720
|
+
// The announcement goes straight into the caller's buffer: no standalone 5-octet
|
|
723
721
|
// Buffer and no subarray. The encoder consumes the pending update here, so the
|
|
724
722
|
// announcement cannot be repeated by a later block.
|
|
725
723
|
const at = enc.takeSizeUpdateInto(dst, off) - off;
|
|
@@ -875,5 +873,5 @@ function requestView(src, scheme, authority) {
|
|
|
875
873
|
|
|
876
874
|
module.exports = { block, encode, parse, prepare, writePrepared, capOf, boundOf,
|
|
877
875
|
nullProto, isFieldName, trimName, lowerName,
|
|
878
|
-
nameCacheSize,
|
|
876
|
+
nameCacheSize, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
|
|
879
877
|
sent, responseView, requestView };
|
package/lib/hpack.js
CHANGED
|
@@ -1226,21 +1226,9 @@ class Encoder {
|
|
|
1226
1226
|
} // the indices the caches name just shifted
|
|
1227
1227
|
}
|
|
1228
1228
|
|
|
1229
|
-
// The size update
|
|
1230
|
-
// never announces the same change twice.
|
|
1231
|
-
|
|
1232
|
-
const n = this.pendingSize;
|
|
1233
|
-
if (n === null) {
|
|
1234
|
-
return null;
|
|
1235
|
-
}
|
|
1236
|
-
this.pendingSize = null;
|
|
1237
|
-
const b = Buffer.allocUnsafe(5);
|
|
1238
|
-
return b.subarray(0, writeInt(b, 0, n, 5, 0x20));
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
// The size update written straight into the caller's destination. Same octets as
|
|
1242
|
-
// takeSizeUpdate(), but the direct-write path has no intermediate 5-octet Buffer
|
|
1243
|
-
// and no subarray to hand to the staging buffer.
|
|
1229
|
+
// The size update written straight into the caller's destination, consumed here so
|
|
1230
|
+
// a block never announces the same change twice. No intermediate 5-octet Buffer and
|
|
1231
|
+
// no subarray to hand to the staging buffer. Returns the offset after the octets.
|
|
1244
1232
|
takeSizeUpdateInto(dst, off) {
|
|
1245
1233
|
const n = this.pendingSize;
|
|
1246
1234
|
if (n === null) {
|
|
@@ -1039,6 +1039,24 @@ const DEFAULT_MAX_INVALID_FRAMES = 1000;
|
|
|
1039
1039
|
const DEFAULT_MAX_REJECTED_STREAMS = 100;
|
|
1040
1040
|
const DEFAULT_MAX_OUTSTANDING_PINGS = 10;
|
|
1041
1041
|
|
|
1042
|
+
// maxSessionMemory is expressed in decimal megabytes by node's JS layer
|
|
1043
|
+
// (util.js:279, MathMax(1, ...)) and multiplied by 1 000 000 by the C++ layer
|
|
1044
|
+
// (node_http2.cc:210), whose default is 10 000 000 (node_http2.h:40).
|
|
1045
|
+
const DEFAULT_MAX_SESSION_MEMORY = 10 * 1000 * 1000;
|
|
1046
|
+
// node charges sizeof(Http2Stream) for each live stream (node_http2.cc:915/925)
|
|
1047
|
+
// plus name.length + value.length + 32 for each header (:2745). sizeof is a C++
|
|
1048
|
+
// compile-time constant not present in the JS source; it was derived from a
|
|
1049
|
+
// live node 26 server at a 1 MB budget with a minimal request block: 1028
|
|
1050
|
+
// streams were accepted, and the block costs ~180 bytes, leaving ~794 for the
|
|
1051
|
+
// stream record. 800 is the rounded charge; the budget is a bound, not a byte
|
|
1052
|
+
// counter, and the test pins the refusal shape rather than the exact stream.
|
|
1053
|
+
const STREAM_MEM_CHARGE = 800;
|
|
1054
|
+
// nghttp2 answers a failed stream-open callback with this session error
|
|
1055
|
+
// (node_http2.cc:1073-1085, NGHTTP2_ERR_CALLBACK_FAILURE). Measured on a live
|
|
1056
|
+
// node 26 server: ERR_HTTP2_ERROR with this message on both 'sessionError' and
|
|
1057
|
+
// the session's 'error', and GOAWAY(INTERNAL_ERROR) with empty debug data.
|
|
1058
|
+
const CALLBACK_FAILURE = 'The user callback function failed';
|
|
1059
|
+
|
|
1042
1060
|
// The error the invalid-frame budget dies of: node's OnInvalidFrame sets
|
|
1043
1061
|
// custom_recv_error_code_ = "ERR_HTTP2_TOO_MANY_INVALID_FRAMES" (node_http2.cc),
|
|
1044
1062
|
// whose message internal/errors defines as this string. Measured on a live
|
|
@@ -1312,6 +1330,19 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1312
1330
|
this._rejectedStreams = 0;
|
|
1313
1331
|
this._maxRejectedStreams = typeof opts.maxSessionRejectedStreams === 'number'
|
|
1314
1332
|
? opts.maxSessionRejectedStreams : DEFAULT_MAX_REJECTED_STREAMS;
|
|
1333
|
+
// maxSessionMemory: charge each live stream its record and its retained
|
|
1334
|
+
// header block, and refuse a new one with ENHANCE_YOUR_CALM when the charge
|
|
1335
|
+
// would cross the budget (node_http2.cc CanAddStream / OnHeader). The charge
|
|
1336
|
+
// leaves with the stream in _dropStream, so a connection that drains can open
|
|
1337
|
+
// new streams again. node counts queued outbound bytes too; those are already
|
|
1338
|
+
// bounded separately by _heldCost (MAX_SESSION_HOLD).
|
|
1339
|
+
this._memLimit = typeof opts.maxSessionMemory === 'number'
|
|
1340
|
+
? Math.max(1, opts.maxSessionMemory) * 1000000 : DEFAULT_MAX_SESSION_MEMORY;
|
|
1341
|
+
this._memUsed = 0;
|
|
1342
|
+
// Outbound padding: node's C++ switch only acts on 0/1/2; every other
|
|
1343
|
+
// number is an unpadded session (measured on a live node 26 server).
|
|
1344
|
+
this._padding = opts.paddingStrategy === f.PADDING_ALIGNED ? f.PADDING_ALIGNED
|
|
1345
|
+
: opts.paddingStrategy === f.PADDING_MAX ? f.PADDING_MAX : f.PADDING_NONE;
|
|
1315
1346
|
// The settings submissions this side sent and has not had answered, oldest first: one entry
|
|
1316
1347
|
// per SETTINGS frame awaiting its ACK. Queue order is node's answer rule too (client-side
|
|
1317
1348
|
// _settingsAcks). Each entry holds { cb, values } where values are the ackValues from
|
|
@@ -1629,13 +1660,40 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1629
1660
|
// because it is a view into memory the next call rewrites -- the HPACK scratch block
|
|
1630
1661
|
// in lib/headers.js -- and those payloads are tens of bytes.
|
|
1631
1662
|
_frame(type, flags, sid, payload, owned) {
|
|
1632
|
-
const
|
|
1633
|
-
|
|
1663
|
+
const bodyLen = payload === null || payload === undefined ? 0 : payload.length;
|
|
1664
|
+
let len = bodyLen;
|
|
1665
|
+
// Only DATA and HEADERS carry padding (RFC 9113 6.1/6.2). node's nghttp2
|
|
1666
|
+
// callback selects the total padded payload length; the frame gets PADDED
|
|
1667
|
+
// and a leading pad-length octet only when the selection actually grew it.
|
|
1668
|
+
let pad = 0;
|
|
1669
|
+
let padded = false;
|
|
1670
|
+
if (this._padding !== f.PADDING_NONE && (type === DATA || type === HEADERS)) {
|
|
1671
|
+
const total = f.padTotal(len, this._padding, this.remoteSettings.maxFrameSize);
|
|
1672
|
+
if (total > len) {
|
|
1673
|
+
pad = total - len - 1;
|
|
1674
|
+
padded = true;
|
|
1675
|
+
flags |= f.F_PADDED;
|
|
1676
|
+
len = total;
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
if (owned && bodyLen >= BODY_MIN) {
|
|
1634
1680
|
this._commit(); // what is staged was produced earlier, so it
|
|
1635
1681
|
// has to go out first
|
|
1636
1682
|
const head = Buffer.allocUnsafe(HEADER_LEN);
|
|
1637
1683
|
writeHeader(head, 0, len, type, flags, sid);
|
|
1638
|
-
|
|
1684
|
+
let body = payload;
|
|
1685
|
+
if (padded) {
|
|
1686
|
+
// A padded payload cannot travel by reference: the pad-length octet
|
|
1687
|
+
// goes in front and the zero tail has to be written. This buffer is
|
|
1688
|
+
// ours, so it may still go into _outQ as an owned view.
|
|
1689
|
+
body = Buffer.allocUnsafe(len);
|
|
1690
|
+
body[0] = pad;
|
|
1691
|
+
if (bodyLen) {
|
|
1692
|
+
f.copyInto(body, 1, payload, 0, bodyLen);
|
|
1693
|
+
}
|
|
1694
|
+
body.fill(0, 1 + bodyLen, len);
|
|
1695
|
+
}
|
|
1696
|
+
this._outQ.push(head, body);
|
|
1639
1697
|
if (type === DATA && (flags & F_END_STREAM)) {
|
|
1640
1698
|
this._esMarkers.set(sid, { q0: this._outQ.length - 2, head });
|
|
1641
1699
|
}
|
|
@@ -1643,8 +1701,14 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1643
1701
|
this._need(HEADER_LEN + len);
|
|
1644
1702
|
const off = this._stageLen;
|
|
1645
1703
|
writeHeader(this._stage, off, len, type, flags, sid);
|
|
1646
|
-
if (
|
|
1647
|
-
|
|
1704
|
+
if (padded) {
|
|
1705
|
+
this._stage[off + HEADER_LEN] = pad;
|
|
1706
|
+
if (bodyLen) {
|
|
1707
|
+
f.copyInto(this._stage, off + HEADER_LEN + 1, payload, 0, bodyLen);
|
|
1708
|
+
}
|
|
1709
|
+
this._stage.fill(0, off + HEADER_LEN + 1 + bodyLen, off + HEADER_LEN + len);
|
|
1710
|
+
} else if (bodyLen) {
|
|
1711
|
+
f.copyInto(this._stage, off + HEADER_LEN, payload, 0, bodyLen);
|
|
1648
1712
|
}
|
|
1649
1713
|
this._stageLen = off + HEADER_LEN + len;
|
|
1650
1714
|
if (type === DATA && (flags & F_END_STREAM)) {
|
|
@@ -1882,6 +1946,34 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1882
1946
|
const prefix = prefixSid === undefined ? 0 : 4;
|
|
1883
1947
|
const cap = prep.hit === undefined ? prep.cap : prep.hit.bytes.length;
|
|
1884
1948
|
const frames = Math.max(1, Math.ceil((prefix + cap) / max));
|
|
1949
|
+
if (this._padding !== f.PADDING_NONE) {
|
|
1950
|
+
// Padding is opt-in, so the in-place single-stage layout is left to the
|
|
1951
|
+
// common path: a padded frame needs a leading pad-length octet and a
|
|
1952
|
+
// zero tail inserted into every frame of the block, which cannot be done
|
|
1953
|
+
// by shifting the already-written CONTINUATION headers. Encode into one
|
|
1954
|
+
// owned buffer and let _frame() pad each HEADERS/CONTINUATION on the way
|
|
1955
|
+
// out (CONTINUATION itself is not padding-eligible in RFC 9113 6.3).
|
|
1956
|
+
const raw = Buffer.allocUnsafe(prefix + cap);
|
|
1957
|
+
if (prefix !== 0) {
|
|
1958
|
+
raw.writeUInt32BE(prefixSid, 0);
|
|
1959
|
+
}
|
|
1960
|
+
const rawLen = hdr.writePrepared(prep, this, raw, prefix);
|
|
1961
|
+
const count = Math.max(1, Math.ceil(rawLen / max));
|
|
1962
|
+
for (let i = 0; i < count; i++) {
|
|
1963
|
+
const take = Math.min(rawLen - i * max, max);
|
|
1964
|
+
let bits = 0;
|
|
1965
|
+
if (count === 1) {
|
|
1966
|
+
bits = F_END_HEADERS | (end ? F_END_STREAM : 0);
|
|
1967
|
+
} else if (i === 0) {
|
|
1968
|
+
bits = end ? F_END_STREAM : 0;
|
|
1969
|
+
} else if (i === count - 1) {
|
|
1970
|
+
bits = F_END_HEADERS;
|
|
1971
|
+
}
|
|
1972
|
+
this._frame(i === 0 ? type : f.CONTINUATION, bits, sid,
|
|
1973
|
+
raw.subarray(i * max, i * max + take), true);
|
|
1974
|
+
}
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1885
1977
|
this._need(HEADER_LEN + prefix + cap + HEADER_LEN * (frames - 1));
|
|
1886
1978
|
const dst = this._stage;
|
|
1887
1979
|
const off = this._stageLen;
|
|
@@ -3468,6 +3560,28 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3468
3560
|
return;
|
|
3469
3561
|
}
|
|
3470
3562
|
|
|
3563
|
+
// maxSessionMemory: node refuses a stream it cannot account for with the same
|
|
3564
|
+
// code and the same rejected-stream escalation as a concurrency refusal
|
|
3565
|
+
// (node_http2.cc:1073-1085). The charge is one stream record plus the header
|
|
3566
|
+
// block the application may keep alive for the stream's lifetime. Charging
|
|
3567
|
+
// here -- after the block is decoded, before the stream is created -- is the
|
|
3568
|
+
// only place the peer is still refused without a handler holding its object.
|
|
3569
|
+
const memCharge = STREAM_MEM_CHARGE + bytes;
|
|
3570
|
+
if (this._memUsed + memCharge > this._memLimit) {
|
|
3571
|
+
// node compares the PRE-increment count (rejected_stream_count_++ > max)
|
|
3572
|
+
// and submits no RST on the frame that trips it: max+1 refusals go out,
|
|
3573
|
+
// the next opens nothing and ends the session.
|
|
3574
|
+
if (this._rejectedStreams++ > this._maxRejectedStreams) {
|
|
3575
|
+
return this._fatal(INTERNAL_ERROR, CALLBACK_FAILURE, 'ERR_HTTP2_ERROR', '');
|
|
3576
|
+
}
|
|
3577
|
+
this.sendRst(sid, ENHANCE_YOUR_CALM);
|
|
3578
|
+
this._refused.push(sid);
|
|
3579
|
+
if (this._refused.length > MAX_REFUSED_HISTORY) {
|
|
3580
|
+
this._refused.shift();
|
|
3581
|
+
}
|
|
3582
|
+
return;
|
|
3583
|
+
}
|
|
3584
|
+
|
|
3471
3585
|
// The request object is built here only when a reader is already known: a
|
|
3472
3586
|
// session 'stream' listener takes it as the event argument, and the compat
|
|
3473
3587
|
// request materializes it at the first req.headers access (lib/server/compat.js),
|
|
@@ -3544,6 +3658,8 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3544
3658
|
}
|
|
3545
3659
|
this._streams.set(sid, stream);
|
|
3546
3660
|
this._liveStreams++;
|
|
3661
|
+
stream._memCharge = memCharge;
|
|
3662
|
+
this._memUsed += memCharge;
|
|
3547
3663
|
if (promised !== undefined) {
|
|
3548
3664
|
stream._expLen = +promised;
|
|
3549
3665
|
}
|
|
@@ -4193,6 +4309,12 @@ class Http2ServerSession extends EventEmitter {
|
|
|
4193
4309
|
return;
|
|
4194
4310
|
}
|
|
4195
4311
|
this._liveStreams--;
|
|
4312
|
+
// the stream record and its retained header block no longer count against
|
|
4313
|
+
// maxSessionMemory, so a drained connection can open streams again
|
|
4314
|
+
if (stream._memCharge !== undefined) {
|
|
4315
|
+
this._memUsed -= stream._memCharge;
|
|
4316
|
+
stream._memCharge = undefined;
|
|
4317
|
+
}
|
|
4196
4318
|
// inbound bytes the application never got still owe the connection window
|
|
4197
4319
|
this._abandon(stream);
|
|
4198
4320
|
if (killed) {
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
// ERR_HTTP2_STREAM_ERROR(NGHTTP2_INTERNAL_ERROR) rather than throwing out of the call,
|
|
75
75
|
// matching node's buildNgHeaderString/processRespondWithFD teardown: a bad field name
|
|
76
76
|
// leaves headersSent true (node sets HEADERS_SENT before validating fields) and takes
|
|
77
|
-
// the destroy path. An array-form headers argument passes through hdr.
|
|
77
|
+
// the destroy path. An array-form headers argument passes through hdr.prepare() as-is
|
|
78
78
|
// instead of node's for..in over indices (which sends field names '0', '1', ...);
|
|
79
79
|
// the content-length statCheck injects is only applied to object-form headers.
|
|
80
80
|
// * 'finish' and writableFinished exist on the stream: 'finish' is queued on a
|
|
@@ -354,13 +354,15 @@ class Http2ServerStream extends Http2Stream {
|
|
|
354
354
|
// internal/http2/util.js) -- there is no options.sensitiveHeaders for node to ignore.
|
|
355
355
|
// A refused PUSH_PROMISE closed the promised stream at submit time: node never
|
|
356
356
|
// submits its response block, so sending it here would put HEADERS on a stream
|
|
357
|
-
// the peer never opened (GOAWAY(PROTOCOL_ERROR) there). Keep
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
//
|
|
357
|
+
// the peer never opened (GOAWAY(PROTOCOL_ERROR) there). Keep parsing the block so
|
|
358
|
+
// sentHeaders still reports this response, but do not encode or write it: a live
|
|
359
|
+
// node probe measured the parent's next HEADERS byte-identical to a session that
|
|
360
|
+
// never built the discarded block, so our encoder must not advance either, and
|
|
361
|
+
// sendHeadersAt() writes straight into the session's stage where it cannot be
|
|
362
|
+
// called and then discarded.
|
|
361
363
|
let refused = this._blockRefused;
|
|
362
364
|
if (refused) {
|
|
363
|
-
hdr.
|
|
365
|
+
hdr.prepare(headers, { sendDate }, this.session);
|
|
364
366
|
} else {
|
|
365
367
|
refused = this.session.sendHeadersAt(this.id, headers, { sendDate },
|
|
366
368
|
opts.endStream === true || head || this._endQueued);
|
|
@@ -761,6 +763,12 @@ class Http2ServerStream extends Http2Stream {
|
|
|
761
763
|
// end() sets _ending before anything is sent, and the peer-reset 'aborted' rule
|
|
762
764
|
// reads it -- the fast path must carry the same state (test/compat.test.js).
|
|
763
765
|
this._ending = true;
|
|
766
|
+
// node's compat end() also submits an empty DATA before the trailer END.
|
|
767
|
+
// Without padding that zero-payload frame is invisible; with padding its
|
|
768
|
+
// pad-length octet reaches the peer ahead of the END_STREAM frame.
|
|
769
|
+
if (this.session._padding !== frames.PADDING_NONE) {
|
|
770
|
+
this.session._frame(frames.DATA, 0, this.id, null);
|
|
771
|
+
}
|
|
764
772
|
this.session._frame(frames.DATA, frames.F_END_STREAM, this.id, null);
|
|
765
773
|
this._esStaged = true;
|
|
766
774
|
this._endLocalSide();
|
|
@@ -844,6 +852,13 @@ class Http2ServerStream extends Http2Stream {
|
|
|
844
852
|
return this;
|
|
845
853
|
}
|
|
846
854
|
if (empty) {
|
|
855
|
+
// node's compat end() submits an empty DATA before the trailers arrive.
|
|
856
|
+
// Without padding that zero-payload frame is invisible on the wire; with
|
|
857
|
+
// padding it carries a pad-length octet, so it goes out here ahead of the
|
|
858
|
+
// END_STREAM frame (measured: f=8 then f=9 on a live node 26 server).
|
|
859
|
+
if (this.session._padding !== frames.PADDING_NONE) {
|
|
860
|
+
this.session._frame(frames.DATA, 0, this.id, null);
|
|
861
|
+
}
|
|
847
862
|
// sendData() frames the zero-length DATA and ends the local half with it.
|
|
848
863
|
this.session.sendData(this, EMPTY, true);
|
|
849
864
|
return this;
|
package/lib/server/server.js
CHANGED
|
@@ -145,6 +145,13 @@ function initHttp2Server(server, options) {
|
|
|
145
145
|
maxSessionRejectedStreams: opts.maxSessionRejectedStreams,
|
|
146
146
|
// Connection-level send window override (separate from per-stream initialWindowSize).
|
|
147
147
|
connectionWindowSize: opts.connectionWindowSize,
|
|
148
|
+
// Per-session memory budget in decimal MB (node multiplies by 1e6 in C++) and
|
|
149
|
+
// the outbound frame-padding strategy. Both ride through raw: util.js only
|
|
150
|
+
// tests `typeof === 'number'` for each, so the session constructor applies
|
|
151
|
+
// the same test, Math.max(1, ...) for the budget, and the C++ switch's
|
|
152
|
+
// "anything but 0/1/2 is unpadded" rule for the strategy.
|
|
153
|
+
maxSessionMemory: opts.maxSessionMemory,
|
|
154
|
+
paddingStrategy: opts.paddingStrategy,
|
|
148
155
|
// Validated and copied above, where node's initializeOptions runs
|
|
149
156
|
// validateSessionOptions -- synchronously, at createServer. The per-entry checks
|
|
150
157
|
// wait for the session constructor (setupHandle), where node runs them per
|
package/package.json
CHANGED