h2lean 0.11.1 → 0.16.0
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/index.js +79 -19
- package/lib/client-session.js +260 -187
- package/lib/client-stream.js +2188 -147
- package/lib/client.js +9 -1
- package/lib/compat.js +68 -22
- package/lib/frames.js +25 -1
- package/lib/headers.js +4 -3
- package/lib/pieces.js +3 -1
- package/lib/server.js +125 -28
- package/lib/session.js +1047 -136
- package/lib/stream.js +213 -39
- package/package.json +1 -1
package/lib/session.js
CHANGED
|
@@ -38,7 +38,7 @@ const fastTick = require('./fast-tick');
|
|
|
38
38
|
// shrink back to the preallocation.
|
|
39
39
|
const FLAT = new FlatList(64);
|
|
40
40
|
const { pieceCopyTo, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
|
|
41
|
-
const { nullProto,
|
|
41
|
+
const { nullProto, neverNames, SINGLE_VALUE_FIELDS, KSensitiveHeaders } = require('./headers');
|
|
42
42
|
|
|
43
43
|
// The socket's ownership mark, shared with the client session (node keeps it as
|
|
44
44
|
// socket[kBoundSession] in the one Http2Session base constructor both kinds run
|
|
@@ -98,6 +98,30 @@ function extError(code, msg) {
|
|
|
98
98
|
return err;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
// ERR_HTTP2_INVALID_SESSION is defined off plain Error in node's internal/errors.js, not off
|
|
102
|
+
// TypeError like the argument validators, so it needs its own constructor rather than extError.
|
|
103
|
+
function invalidSession() {
|
|
104
|
+
const err = new Error('The session has been destroyed');
|
|
105
|
+
err.code = 'ERR_HTTP2_INVALID_SESSION';
|
|
106
|
+
return err;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// node's ERR_HTTP2_PING_CANCEL (internal/errors.js), also off plain Error: a ping whose pong
|
|
110
|
+
// never arrives is cancelled when the session ends, and the callback is still called. Measured
|
|
111
|
+
// against a live node: destroy() with a ping outstanding leaves the callback unfired on the
|
|
112
|
+
// synchronous path and on process.nextTick, and fires it with this error by setImmediate.
|
|
113
|
+
function pingCancelled() {
|
|
114
|
+
const err = new Error('HTTP2 ping cancelled');
|
|
115
|
+
err.code = 'ERR_HTTP2_PING_CANCEL';
|
|
116
|
+
return err;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function settingsCancelled() {
|
|
120
|
+
const err = new Error('HTTP2 settings cancelled');
|
|
121
|
+
err.code = 'ERR_HTTP2_SETTINGS_CANCEL';
|
|
122
|
+
return err;
|
|
123
|
+
}
|
|
124
|
+
|
|
101
125
|
// node's ERR_INVALID_ARG_TYPE messages do not say `Received object` -- they say what the
|
|
102
126
|
// value *is*: `Received an instance of Array`, `Received type bigint (1n)`, `Received null`,
|
|
103
127
|
// `Received function namedFn`. That wording comes from `determineSpecificType` in node's
|
|
@@ -265,6 +289,377 @@ function buildOriginPayload(origins) {
|
|
|
265
289
|
return payload;
|
|
266
290
|
}
|
|
267
291
|
|
|
292
|
+
// node's Http2Session.prototype.ping prologue (internal/http2/core.js), ported in node's
|
|
293
|
+
// order. It returns [payload, callback] rather than the payload alone, because the first of
|
|
294
|
+
// node's three steps moves a caller's argument between the two slots: ping(cb) has to register
|
|
295
|
+
// `cb` as the callback, and a caller that only read the payload back would keep the original
|
|
296
|
+
// (undefined) one and hand the pong to nothing. A function in the payload slot is
|
|
297
|
+
// the callback. Only a *truthy* payload is looked at: 0, '', null, false and NaN are simply
|
|
298
|
+
// skipped -- `if (payload)`, not a `!== undefined` test (measured on both halves of a live
|
|
299
|
+
// node pair) -- and the type check is validateBuffer's isArrayBufferView, which refuses a
|
|
300
|
+
// string and a bare ArrayBuffer even though Buffer.from() would have accepted both. An
|
|
301
|
+
// accepted view must be exactly `byteLength` 8, and that is what keeps the caller's bytes
|
|
302
|
+
// off the wire uninitialised: a short copy into an 8-octet body would send whatever the pool
|
|
303
|
+
// already held. The callback is mandatory here in a way it is not for settings(), where node
|
|
304
|
+
// guards the check with `if (callback)`; ping(buf8) therefore throws for a missing callback.
|
|
305
|
+
// ERR_HTTP2_PING_LENGTH is a RangeError (internal/errors.js) and ERR_INVALID_ARG_TYPE a
|
|
306
|
+
// TypeError, so the two cannot share extError's constructor.
|
|
307
|
+
function checkPing(payload, cb) {
|
|
308
|
+
if (typeof payload === 'function') {
|
|
309
|
+
cb = payload;
|
|
310
|
+
payload = undefined;
|
|
311
|
+
}
|
|
312
|
+
if (payload) {
|
|
313
|
+
if (!ArrayBuffer.isView(payload)) {
|
|
314
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
315
|
+
'The "payload" argument must be an instance of Buffer, TypedArray, or DataView. '
|
|
316
|
+
+ `Received ${received(payload)}`);
|
|
317
|
+
}
|
|
318
|
+
if (payload.byteLength !== 8) {
|
|
319
|
+
const err = new RangeError('HTTP2 ping payload must be 8 bytes');
|
|
320
|
+
err.code = 'ERR_HTTP2_PING_LENGTH';
|
|
321
|
+
throw err;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (typeof cb !== 'function') {
|
|
325
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
326
|
+
`The "callback" argument must be of type function. Received ${received(cb)}`);
|
|
327
|
+
}
|
|
328
|
+
return [payload, cb];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// The ranges validateSettings applies (internal/http2/core.js), whose limits are
|
|
332
|
+
// kMaxInt = 2**32-1, kMaxInitialWindowSize = 2**31-1, kMaxFrameSize = 2**24-1 and
|
|
333
|
+
// kMaxStreams = 2**32-1. maxFrameSize is the only one with a non-zero floor: RFC 9113
|
|
334
|
+
// 6.5.2 defines SETTINGS_MAX_FRAME_SIZE as 16384..16777215.
|
|
335
|
+
const SETTINGS_RANGE = {
|
|
336
|
+
headerTableSize: [0, 0xffffffff],
|
|
337
|
+
initialWindowSize: [0, 0x7fffffff],
|
|
338
|
+
maxFrameSize: [16384, 0xffffff],
|
|
339
|
+
maxConcurrentStreams: [0, 0xffffffff],
|
|
340
|
+
maxHeaderListSize: [0, 0xffffffff],
|
|
341
|
+
maxHeaderSize: [0, 0xffffffff]
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// node caps a customSettings bag and a remoteCustomSettings list at this many entries
|
|
345
|
+
// (internal/http2/util.js MAX_ADDITIONAL_SETTINGS), and the error is a plain Error, not a
|
|
346
|
+
// TypeError -- internal/errors.js defines it without a base-class argument.
|
|
347
|
+
const MAX_ADDITIONAL_SETTINGS = 10;
|
|
348
|
+
|
|
349
|
+
function tooManyCustomSettings() {
|
|
350
|
+
const err = new Error('Number of custom settings exceeds MAX_ADDITIONAL_SETTINGS');
|
|
351
|
+
err.code = 'ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS';
|
|
352
|
+
return err;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// node's settings-slot enum (internal/http2/util.js IDX_SETTINGS, 0..6), which is NOT the
|
|
356
|
+
// wire-id order: a customSettings entry whose numeric id is below the flags slot folds into
|
|
357
|
+
// the NAMED setting at that enum index -- custom id 4 (enum MAX_CONCURRENT_STREAMS) submits
|
|
358
|
+
// as wire id 3, custom id 5 as wire id 6. Measured on a live node server:
|
|
359
|
+
// {customSettings: {5: 20000}} puts 6=20000 on the wire and the session reports
|
|
360
|
+
// maxHeaderListSize 20000 after the ACK, with no customSettings key in localSettings. The
|
|
361
|
+
// map is indexed by the custom id and carries the named setting plus its wire id; ids >= 7
|
|
362
|
+
// travel in the custom section, as-is.
|
|
363
|
+
const CUSTOM_FOLD = [
|
|
364
|
+
['headerTableSize', 1], // 0: enum HEADER_TABLE_SIZE
|
|
365
|
+
['enablePush', 2], // 1: enum ENABLE_PUSH
|
|
366
|
+
['initialWindowSize', 4], // 2: enum INITIAL_WINDOW_SIZE
|
|
367
|
+
['maxFrameSize', 5], // 3: enum MAX_FRAME_SIZE
|
|
368
|
+
['maxConcurrentStreams', 3], // 4: enum MAX_CONCURRENT_STREAMS
|
|
369
|
+
['maxHeaderListSize', 6], // 5: enum MAX_HEADER_LIST_SIZE
|
|
370
|
+
['enableConnectProtocol', 8] // 6: enum ENABLE_CONNECT_PROTOCOL
|
|
371
|
+
];
|
|
372
|
+
|
|
373
|
+
// ERR_HTTP2_INVALID_SETTING_VALUE has two constructors and the two validation paths in
|
|
374
|
+
// core.js use different ones (internal/http2/util.js): assertWithinRange throws the
|
|
375
|
+
// `.RangeError` variant for a number outside a range, while the enablePush /
|
|
376
|
+
// enableConnectProtocol tests throw the plain TypeError one. Measured on a live node 26
|
|
377
|
+
// pair, both halves identical: {maxFrameSize: 1} is a RangeError carrying .min 16384 and
|
|
378
|
+
// .max 16777215, {enablePush: 1} is a TypeError with .actual and no .min. The message is
|
|
379
|
+
// the same either way and names only the setting and the value — it is not
|
|
380
|
+
// ERR_OUT_OF_RANGE's, which quotes the range.
|
|
381
|
+
function invalidSetting(name, value, min, max) {
|
|
382
|
+
const err = min === undefined
|
|
383
|
+
? new TypeError(`Invalid value for setting "${name}": ${value}`)
|
|
384
|
+
: new RangeError(`Invalid value for setting "${name}": ${value}`);
|
|
385
|
+
err.code = 'ERR_HTTP2_INVALID_SETTING_VALUE';
|
|
386
|
+
err.actual = value;
|
|
387
|
+
if (min !== undefined) {
|
|
388
|
+
err.min = min;
|
|
389
|
+
err.max = max;
|
|
390
|
+
}
|
|
391
|
+
return err;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// validateSettings' customSettings block (core.js), which runs BEFORE the named ranges.
|
|
395
|
+
// The object test is assertIsObject(cs, 'customSettings', 'Number'): node's
|
|
396
|
+
// ERR_INVALID_ARG_TYPE prints a capitalised type as "must be an instance of Number"
|
|
397
|
+
// (the lowercase 'Object' form is the "must be of type object" one), and the count is
|
|
398
|
+
// Object.entries, so own keys only. Each entry is assertWithinRange on
|
|
399
|
+
// Number(key)/Number(value): NaN passes both (typeof is 'number', every comparison
|
|
400
|
+
// false) and is refused by the packer instead; a string id or value that is out of range
|
|
401
|
+
// after Number() refuses with the same ERR_HTTP2_INVALID_SETTING_VALUE RangeError the
|
|
402
|
+
// named settings use, under the name 'customSettings:id' / 'customSettings:value'. A raw
|
|
403
|
+
// string value like '5' passes here (Number('5') is in range) and is skipped by the
|
|
404
|
+
// packer, which takes numbers only.
|
|
405
|
+
function checkCustomSettings(customSettings) {
|
|
406
|
+
if (customSettings === undefined) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (customSettings === null || typeof customSettings !== 'object' ||
|
|
410
|
+
Array.isArray(customSettings)) {
|
|
411
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
412
|
+
'The "customSettings" argument must be an instance of Number. ' +
|
|
413
|
+
`Received ${received(customSettings)}`);
|
|
414
|
+
}
|
|
415
|
+
const entries = Object.entries(customSettings);
|
|
416
|
+
if (entries.length > MAX_ADDITIONAL_SETTINGS) {
|
|
417
|
+
throw tooManyCustomSettings();
|
|
418
|
+
}
|
|
419
|
+
for (const [key, value] of entries) {
|
|
420
|
+
const id = Number(key);
|
|
421
|
+
if (id < 0 || id > 0xffff) {
|
|
422
|
+
throw invalidSetting('customSettings:id', id, 0, 0xffff);
|
|
423
|
+
}
|
|
424
|
+
const v = Number(value);
|
|
425
|
+
if (v < 0 || v > 0xffffffff) {
|
|
426
|
+
throw invalidSetting('customSettings:value', v, 0, 0xffffffff);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// assertIsObject + validateSettings, in node's order. undefined is legal -- it submits an
|
|
432
|
+
// empty SETTINGS frame -- while null, a string and an array are ERR_INVALID_ARG_TYPE;
|
|
433
|
+
// a Buffer is an object whose setting properties are all undefined, so it passes and
|
|
434
|
+
// submits nothing (both measured). customSettings is checked before the named ranges
|
|
435
|
+
// (node's order, validateSettings), and an unknown key is ignored, not refused. A
|
|
436
|
+
// non-integer within range passes (the checks are `< min || > max`, so 16384.5 and NaN are
|
|
437
|
+
// legal -- NaN because every comparison with it is false).
|
|
438
|
+
function checkSettings(settings) {
|
|
439
|
+
if (settings === undefined) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (settings === null || typeof settings !== 'object' || Array.isArray(settings)) {
|
|
443
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
444
|
+
`The "settings" argument must be of type object. Received ${received(settings)}`);
|
|
445
|
+
}
|
|
446
|
+
checkCustomSettings(settings.customSettings);
|
|
447
|
+
for (const name of Object.keys(SETTINGS_RANGE)) {
|
|
448
|
+
const value = settings[name];
|
|
449
|
+
if (value === undefined) {
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const [min, max] = SETTINGS_RANGE[name];
|
|
453
|
+
if (typeof value !== 'number' || value < min || value > max) {
|
|
454
|
+
throw invalidSetting(name, value, min, max);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
for (const name of ['enablePush', 'enableConnectProtocol']) {
|
|
458
|
+
if (settings[name] !== undefined && typeof settings[name] !== 'boolean') {
|
|
459
|
+
throw invalidSetting(name, settings[name]);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// The named half of one submission: node's updateSettingsBuffer writes a slot only for
|
|
465
|
+
// `typeof === 'number'` (or boolean, for the two flags -- checked before the pack runs)
|
|
466
|
+
// and resolves the maxHeaderSize alias first, warning when the two differ. The alias is
|
|
467
|
+
// node's exactly: it wins whenever the two differ, and a set the call did not name
|
|
468
|
+
// leaves the alias alone -- node's option bag never merges with the defaults its alias
|
|
469
|
+
// path would read, so a default maxHeaderSize (65535 in node v26 getDefaultSettings)
|
|
470
|
+
// cannot overwrite the caller's maxHeaderListSize at an initial submission (measured:
|
|
471
|
+
// node advertises 6=48 from settings {maxHeaderListSize: 48} and refuses the
|
|
472
|
+
// 174-entry-octet block the limit should refuse).
|
|
473
|
+
// `advertised`, when given, narrows the result to the names the application asked for --
|
|
474
|
+
// the initial submission's rule (an advertised number is a promise to the peer), while a
|
|
475
|
+
// settings() call submits whatever it names.
|
|
476
|
+
function submissionValues(settings, advertised) {
|
|
477
|
+
const values = {};
|
|
478
|
+
if (settings === undefined) {
|
|
479
|
+
return values;
|
|
480
|
+
}
|
|
481
|
+
let listSize = settings.maxHeaderListSize;
|
|
482
|
+
// The alias moves an application-NAMED maxHeaderSize only: a merged defaults bag
|
|
483
|
+
// (server.settings, where getDefaultSettings carries maxHeaderSize 65535 on node v26)
|
|
484
|
+
// would otherwise overwrite the caller's maxHeaderListSize through it.
|
|
485
|
+
if (typeof settings.maxHeaderSize === 'number' &&
|
|
486
|
+
(advertised === undefined || advertised.has('maxHeaderSize'))) {
|
|
487
|
+
if (settings.maxHeaderSize !== listSize) {
|
|
488
|
+
process.emitWarning('settings.maxHeaderSize overwrite settings.maxHeaderListSize');
|
|
489
|
+
}
|
|
490
|
+
listSize = settings.maxHeaderSize;
|
|
491
|
+
}
|
|
492
|
+
for (const name of Object.keys(ID_BY_SETTING)) {
|
|
493
|
+
if (advertised !== undefined && !advertised.has(name)) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const value = name === 'maxHeaderListSize' ? listSize : settings[name];
|
|
497
|
+
if (value !== undefined) {
|
|
498
|
+
values[name] = value;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return values;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// One SETTINGS submission, built the way node's updateSettingsBuffer + the C++
|
|
505
|
+
// Http2Settings::Init do. `values` are the named entries (alias-resolved by the caller),
|
|
506
|
+
// `customSettings` the raw customSettings object of the same source. Entries whose
|
|
507
|
+
// numeric id is below the flags slot fold into the NAMED setting at the enum index
|
|
508
|
+
// (CUSTOM_FOLD), filling the slot the way node's switch does; a named value in the same
|
|
509
|
+
// call overwrites the fold, because node processes customs first and named second. A
|
|
510
|
+
// non-integer id in 0..6 is node's switch `default` -- `{customSettings: {'0.5': 1}}`
|
|
511
|
+
// packs as wire id 0 -- so the fold is taken only when CUSTOM_FOLD[id] exists. Ids >= 7
|
|
512
|
+
// form the custom section in for-in order (integer-like keys iterate ascending),
|
|
513
|
+
// each id once: node's buffer scan overwrites a repeated id's value in place, keeping
|
|
514
|
+
// the first position. The packer checks live here, not in checkCustomSettings: node lets
|
|
515
|
+
// {customSettings: {0: 5}} through validateSettings (0 is within the checked range) and
|
|
516
|
+
// refuses it only when the frame is built -- a server created with one dies at accept
|
|
517
|
+
// with the uncaught RangeError and connect() with one throws out of setupHandle (both
|
|
518
|
+
// measured). A non-number raw value passes validation (Number('5') is in range) and is
|
|
519
|
+
// skipped, the `typeof val === 'number'` gate. Numeric keys past MAX_ADDITIONAL_SETTINGS
|
|
520
|
+
// throw ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS here exactly as node's buffer does --
|
|
521
|
+
// reachable only through prototype keys, because validateSettings counts own keys.
|
|
522
|
+
// Returns { body, ackValues, rest, folded }: body is the frame payload, ackValues the flat
|
|
523
|
+
// [name, value, ...] list the ACK applies to localSettings (named entries as given, a
|
|
524
|
+
// fold converted the way getSettings reports it -- `!!slot`, so raw !== 0), rest the
|
|
525
|
+
// flat [id, value, ...] custom-section list the session accumulates into its view,
|
|
526
|
+
// folded a Map of the slot names whose final value came from a fold (a named value in
|
|
527
|
+
// the same call removes its slot, because node processes customs first and named
|
|
528
|
+
// second). `folded` is what getPackedSettings needs to answer nghttp2's own validity
|
|
529
|
+
// test; a session submission ignores it.
|
|
530
|
+
function buildSubmission(values, customSettings) {
|
|
531
|
+
const slots = new Map(); // name -> [wireId, wireValue, ackValue]
|
|
532
|
+
const customs = new Map(); // section id -> value, insertion order = for-in order
|
|
533
|
+
const folded = new Map(); // slot name -> raw fold value still in effect
|
|
534
|
+
if (customSettings !== undefined && typeof customSettings === 'object' &&
|
|
535
|
+
!Array.isArray(customSettings)) {
|
|
536
|
+
for (const key in customSettings) {
|
|
537
|
+
const raw = customSettings[key];
|
|
538
|
+
if (typeof raw !== 'number') {
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
const id = Number(key);
|
|
542
|
+
if (Number.isNaN(id) || id <= 0 || id > 0xffff) {
|
|
543
|
+
throw invalidSetting('Range Error', id, 0, 0xffff);
|
|
544
|
+
}
|
|
545
|
+
if (Number.isNaN(raw) || raw <= 0 || raw > 0xffffffff) {
|
|
546
|
+
throw invalidSetting('Range Error', raw, 0, 0xffffffff);
|
|
547
|
+
}
|
|
548
|
+
if (CUSTOM_FOLD[id] !== undefined) {
|
|
549
|
+
const [name, wireId] = CUSTOM_FOLD[id];
|
|
550
|
+
slots.set(name, [wireId, raw,
|
|
551
|
+
name === 'enablePush' || name === 'enableConnectProtocol' ? raw !== 0 : raw, name]);
|
|
552
|
+
folded.set(name, raw);
|
|
553
|
+
} else {
|
|
554
|
+
if (!customs.has(id) && customs.size >= MAX_ADDITIONAL_SETTINGS) {
|
|
555
|
+
throw tooManyCustomSettings();
|
|
556
|
+
}
|
|
557
|
+
customs.set(id, raw);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
for (const name of Object.keys(ID_BY_SETTING)) {
|
|
562
|
+
const value = values[name];
|
|
563
|
+
if (value === undefined) {
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
const wire = typeof value === 'boolean' ? (value ? 1 : 0) : value;
|
|
567
|
+
slots.set(name, [ID_BY_SETTING[name], wire, value, name]);
|
|
568
|
+
folded.delete(name); // the named value overwrote the fold
|
|
569
|
+
}
|
|
570
|
+
const named = [...slots.values()].sort((a, b) => a[0] - b[0]);
|
|
571
|
+
const body = Buffer.allocUnsafe((named.length + customs.size) * 6);
|
|
572
|
+
const ackValues = [];
|
|
573
|
+
let at = 0;
|
|
574
|
+
for (const [wireId, wire, ack, name] of named) {
|
|
575
|
+
body.writeUInt16BE(wireId, at);
|
|
576
|
+
body.writeUInt32BE(wire >>> 0, at + 2);
|
|
577
|
+
at += 6;
|
|
578
|
+
ackValues.push(name, ack);
|
|
579
|
+
}
|
|
580
|
+
const rest = [];
|
|
581
|
+
for (const [id, raw] of customs) {
|
|
582
|
+
body.writeUInt16BE(id, at);
|
|
583
|
+
body.writeUInt32BE(raw >>> 0, at + 2);
|
|
584
|
+
at += 6;
|
|
585
|
+
rest.push(id, raw);
|
|
586
|
+
}
|
|
587
|
+
return { body, ackValues, rest, folded };
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// node's accumulated custom view: node_http2.cc UpdateLocalCustomSettings runs at
|
|
591
|
+
// Http2Settings::Send -- entries with a wire id >= 7 join a per-session list, a repeated
|
|
592
|
+
// id overwrites its value in place, and past MAX_ADDITIONAL_SETTINGS a NEW id is dropped
|
|
593
|
+
// silently. getSettings reads that list (addCustomSettingsToObj), so the localSettings
|
|
594
|
+
// view ACCUMULATES across calls while each frame carries only its own call's entries --
|
|
595
|
+
// measured on a live node client: the wire carried [1001=43] then [1002=44], the view
|
|
596
|
+
// showed {1001:43} then {1001:43,1002:44}, and an empty third call changed neither. The
|
|
597
|
+
// list is ordered ascending by id (a std::map on the C++ side).
|
|
598
|
+
function accumulateCustom(rest, map) {
|
|
599
|
+
for (let i = 0; i < rest.length; i += 2) {
|
|
600
|
+
if (!map.has(rest[i]) && map.size >= MAX_ADDITIONAL_SETTINGS) {
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
map.set(rest[i], rest[i + 1]);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// The string-keyed view of one accumulated custom list, ascending by id the way
|
|
608
|
+
// addCustomSettingsToObj reads node's std::map back.
|
|
609
|
+
function customView(map) {
|
|
610
|
+
const view = {};
|
|
611
|
+
for (const id of [...map.keys()].sort((a, b) => a - b)) {
|
|
612
|
+
view[id] = map.get(id);
|
|
613
|
+
}
|
|
614
|
+
return view;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// validateSessionOptions' remoteCustomSettings check (core.js), run where node runs it:
|
|
618
|
+
// synchronously, before any socket exists -- at connect() (core.js calls
|
|
619
|
+
// validateSessionOptions before createConnection), at createServer and at
|
|
620
|
+
// performServerHandshake (both through initializeOptions). undefined passes; null, a
|
|
621
|
+
// non-object and an array-worth of anything else are assertIsArray's TypeError, more
|
|
622
|
+
// than MAX_ADDITIONAL_SETTINGS entries the plain Error. The array is copied, node's
|
|
623
|
+
// [...remoteCustomSettings], so a later mutation of the caller's list does not reach
|
|
624
|
+
// the session. The per-entry checks do NOT run here: node defers them to setupHandle,
|
|
625
|
+
// which runs per connection (see checkRemoteCustomEntries).
|
|
626
|
+
function validateRemoteCustomSettings(options) {
|
|
627
|
+
const list = options.remoteCustomSettings;
|
|
628
|
+
if (list === undefined) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
if (list === null || !Array.isArray(list)) {
|
|
632
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
633
|
+
'The "options.remoteCustomSettings" property must be an instance of Array. ' +
|
|
634
|
+
`Received ${received(list)}`);
|
|
635
|
+
}
|
|
636
|
+
if (list.length > MAX_ADDITIONAL_SETTINGS) {
|
|
637
|
+
throw tooManyCustomSettings();
|
|
638
|
+
}
|
|
639
|
+
options.remoteCustomSettings = [...list];
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// remoteCustomSettingsToBuffer (internal/http2/util.js), run at setupHandle -- per
|
|
643
|
+
// connection on the server, once at socket-connect on the client -- not at option
|
|
644
|
+
// validation: a session created with remoteCustomSettings: ['x'] is built and the error
|
|
645
|
+
// lands in its setup path instead (measured on a live node client: the session reports
|
|
646
|
+
// ERR_HTTP2_INVALID_SETTING_VALUE through 'error' and no 'connect' fires; on a live
|
|
647
|
+
// node server the throw escapes connectionListener and the process dies -- node never
|
|
648
|
+
// catches it there). 0 is legal HERE, unlike a customSettings submission: this list only
|
|
649
|
+
// declares which ids the application wants to SEE. The predicate is node's, so NaN and
|
|
650
|
+
// strings refuse exactly where node's does.
|
|
651
|
+
function checkRemoteCustomEntries(list) {
|
|
652
|
+
if (list === undefined) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
for (let i = 0; i < list.length; i++) {
|
|
656
|
+
const id = list[i];
|
|
657
|
+
if (!(typeof id === 'number' && id <= 0xffff && id >= 0)) {
|
|
658
|
+
throw invalidSetting('Range Error', id, 0, 0xffff);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
268
663
|
// Decode a received ALTSVC payload. Tolerant by design: node answers a malformed ALTSVC by
|
|
269
664
|
// discarding it and keeping the connection (measured: a 1-octet type-10 frame leaves node serving
|
|
270
665
|
// the next request), and the same holds for a length that overruns the frame. Returns null to mean
|
|
@@ -313,10 +708,11 @@ const OUR_DEFAULTS = { ...require('node:http2').getDefaultSettings(), enablePush
|
|
|
313
708
|
const PEER_DEFAULTS = {
|
|
314
709
|
headerTableSize: 4096,
|
|
315
710
|
enablePush: true,
|
|
316
|
-
maxConcurrentStreams: 4294967295,
|
|
317
711
|
initialWindowSize: 65535,
|
|
318
712
|
maxFrameSize: 16384,
|
|
713
|
+
maxConcurrentStreams: 4294967295,
|
|
319
714
|
maxHeaderListSize: 4294967295,
|
|
715
|
+
maxHeaderSize: 4294967295,
|
|
320
716
|
enableConnectProtocol: false
|
|
321
717
|
};
|
|
322
718
|
|
|
@@ -476,8 +872,75 @@ const SOCKET_CLOSE_GRACE = 1000;
|
|
|
476
872
|
const TOKEN_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; // RFC 9110 9: method is a token
|
|
477
873
|
const SCHEME_RE = /^[A-Za-z][A-Za-z0-9+\-.]*$/; // RFC 3986 3.1
|
|
478
874
|
const NUM_RE = /^[0-9]+$/; // RFC 9110 8.6: Content-Length = 1*DIGIT
|
|
479
|
-
//
|
|
480
|
-
|
|
875
|
+
// 2^53 = 9007199254740992: past it a JS number stops distinguishing unit steps, and the
|
|
876
|
+
// content-length promise is consumed as one (stream._expLen = +promised, the octet
|
|
877
|
+
// accounting below compares against it). nghttp2 counts in int64 and serves anything up to
|
|
878
|
+
// INT64_MAX (parse_uint(), nghttp2_http.c), so this bound is a divergence, not a parity:
|
|
879
|
+
// measured on node (tools/probe-validation-parity.js), 9007199254740993 and 18 nines are
|
|
880
|
+
// served there and both are refused here. The hybrid, in nghttp2's own order: a digits-only
|
|
881
|
+
// string shorter than 16 holds at most 999999999999999 and is below the bound with no
|
|
882
|
+
// arithmetic (the common case); the zeros parse_uint() accepts are skipped next, because
|
|
883
|
+
// "000...0009007199254740992" and 16 zeros (value 0) are both served shapes (measured);
|
|
884
|
+
// 17 significant digits are already past the bound with no parse; exactly 16 go through a
|
|
885
|
+
// BigInt compare -- the only case arithmetic decides, and the slice keeps the BigInt off a
|
|
886
|
+
// padded giant. Verified against a BigInt oracle on 200 080 crafted and random digit
|
|
887
|
+
// strings with zero mismatches (tools/cl53-oracle.js); without the zero skip every padded
|
|
888
|
+
// form misclassifies.
|
|
889
|
+
const CL_53 = 9007199254740992n;
|
|
890
|
+
function clPast53(value) {
|
|
891
|
+
const len = value.length;
|
|
892
|
+
if (len < 16) {
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
let start = 0;
|
|
896
|
+
while (value.charCodeAt(start) === 48 /* 0 */) {
|
|
897
|
+
start++;
|
|
898
|
+
}
|
|
899
|
+
const digits = len - start;
|
|
900
|
+
if (digits !== 16) {
|
|
901
|
+
return digits > 16;
|
|
902
|
+
}
|
|
903
|
+
return BigInt(value.slice(start)) > CL_53;
|
|
904
|
+
}
|
|
905
|
+
// RFC 9113 8.3.1 :authority is host/pport, i.e. URI authority syntax (RFC 3986 3.2). The
|
|
906
|
+
// character set is check_authority() in nghttp2_http.c -- "Same as the array in
|
|
907
|
+
// nghttp2_helper.c, but '@' is not allowed": userinfo is an HTTP/1.1 shape, and two stacks
|
|
908
|
+
// that disagree about who a request is for is the desync class. Measured on node
|
|
909
|
+
// (tools/probe-validation-parity.js): :authority with '@' is RST_STREAM(PROTOCOL_ERROR), a
|
|
910
|
+
// host with '@' is dropped -- the handler sees no such field -- while the request itself
|
|
911
|
+
// is served. host shares this predicate, and the drop stays at its call site below, which
|
|
912
|
+
// is what makes those two different actions out of one rule.
|
|
913
|
+
const AUTHORITY_RE = /^[A-Za-z0-9._~%!$&'()*+,;=:[\]-]+$/;
|
|
914
|
+
// check_path() in nghttp2_helper.c takes 0x21-0x7e and 0x80-0xff: no space, no control
|
|
915
|
+
// octet, no DEL. Measured on node (tools/probe-validation-parity.js): a space, 0x01, CR LF or
|
|
916
|
+
// 0x7f in :path is RST_STREAM(PROTOCOL_ERROR) before the handler runs, and the CR LF case
|
|
917
|
+
// is the smuggling shape itself -- a request line and a header line inside one field, the
|
|
918
|
+
// thing a downstream HTTP/1.1 translator breaks on (CVE-2020-8287's class). Every octet
|
|
919
|
+
// 0x80-0xff is in the class, including 0x8d (measured: served).
|
|
920
|
+
const PATH_BAD_RE = /[\u0000-\u0020\u007f]/;
|
|
921
|
+
// The value shape check_header_value_rfc9113() (nghttp2_http.c) applies to :protocol and to
|
|
922
|
+
// every regular field value alike: the character class of nghttp2_check_header_value() --
|
|
923
|
+
// no control octet but HTAB, no DEL -- plus the RFC 9113 edges, no leading or trailing
|
|
924
|
+
// SP/HTAB. On a regular field both halves are the ignore-rule (the field disappears, the
|
|
925
|
+
// stream lives); on :protocol it is the hard error.
|
|
926
|
+
const RFC9113_VALUE_BAD_RE = /^[\t ]|[\t ]$|[\u0000-\u0008\u000a-\u001f\u007f]/;
|
|
927
|
+
// check_scheme() flags http/https with memieq(), i.e. case-insensitively, and the flag is
|
|
928
|
+
// what turns the absolute-path rule on. Measured on node
|
|
929
|
+
// (tools/probe-validation-parity.js): ':scheme "HTTP"' with a relative :path is
|
|
930
|
+
// RST_STREAM(PROTOCOL_ERROR) while the absolute sibling is served, so the letter case of
|
|
931
|
+
// the two schemes decides the rule, not an exact match.
|
|
932
|
+
function schemeHttpLike(s) {
|
|
933
|
+
if (s.length === 4) {
|
|
934
|
+
return (s.charCodeAt(0) | 32) === 104 /* h */ && (s.charCodeAt(1) | 32) === 116 /* t */
|
|
935
|
+
&& (s.charCodeAt(2) | 32) === 116 /* t */ && (s.charCodeAt(3) | 32) === 112 /* p */;
|
|
936
|
+
}
|
|
937
|
+
if (s.length === 5) {
|
|
938
|
+
return (s.charCodeAt(0) | 32) === 104 && (s.charCodeAt(1) | 32) === 116
|
|
939
|
+
&& (s.charCodeAt(2) | 32) === 116 && (s.charCodeAt(3) | 32) === 112
|
|
940
|
+
&& (s.charCodeAt(4) | 32) === 115 /* s */;
|
|
941
|
+
}
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
481
944
|
// Field names on an HTTP/2 connection must be lower case (RFC 9113 8.2.1). Split into
|
|
482
945
|
// two tests because the two halves have different consequences, see above.
|
|
483
946
|
const NAME_LOWER_RE = /^[!#$%&'*+\-.^_`|~0-9a-z]+$/;
|
|
@@ -488,6 +951,35 @@ const HAS_UPPER = /[A-Z]/;
|
|
|
488
951
|
const INBOUND_FORBIDDEN = new Set(['connection', 'keep-alive', 'proxy-connection',
|
|
489
952
|
'transfer-encoding', 'upgrade']);
|
|
490
953
|
|
|
954
|
+
// The same trade OPTIMIZATION.md item 7 measured for VALID_NAMES (lib/headers.js):
|
|
955
|
+
// each validator above is a pure predicate of the string, so a string that passed
|
|
956
|
+
// once passes forever, and a bounded process-wide Set of passing strings beats
|
|
957
|
+
// re-running the regex on every request. Measured on these exact call sites
|
|
958
|
+
// (bench/micro/inbound-validate.js): a 14-line request walk costs ~380 ns of regex
|
|
959
|
+
// against ~70 ns cached, with every step winning -- the name pair ~27 ns per line,
|
|
960
|
+
// :authority ~17 ns per line and ~55 ns on a long authority, method and scheme ~8-11.
|
|
961
|
+
// The bound is the price guard: a peer that never repeats a string pays a Set miss
|
|
962
|
+
// on top of the regex forever and never grows the cache past it (the adversarial
|
|
963
|
+
// shapes in the bench hold the numbers; item 7 accepted the same trade for names).
|
|
964
|
+
// Only passing strings are ever cached, so the sets never hold a refused or dropped
|
|
965
|
+
// line and the refusal/drop decisions stay at the call sites below. The length cap
|
|
966
|
+
// is the memory guard: VALID_NAMES caches application-chosen names, but these sets
|
|
967
|
+
// are fed by the peer, and a header block may carry a legal ~100 KiB authority that
|
|
968
|
+
// would otherwise be retained for the life of the process; 1024 entries of at most
|
|
969
|
+
// 512 octets bound all four sets to ~2 MiB together. A longer string still
|
|
970
|
+
// validates, it just re-runs the regex every time. content-length is deliberately
|
|
971
|
+
// not cached: a body size is workload-shaped, repeats almost never, and the miss path
|
|
972
|
+
// (hash + regex) is then strictly worse than the plain NUM_RE test while the entries
|
|
973
|
+
// still occupy the bound (the rotating arms in bench/micro/inbound-validate.js).
|
|
974
|
+
const VALID_VALUE_MAX = 1024; // VALID_NAMES_MAX's number, same shape and reason
|
|
975
|
+
const VALID_VALUE_MAX_LEN = 512; // only strings this short are retained
|
|
976
|
+
const VALID_METHODS = new Set(); // TOKEN_RE values: :method
|
|
977
|
+
const VALID_SCHEMES = new Set(); // SCHEME_RE values: :scheme
|
|
978
|
+
// Shared by :authority and host, which validate the same predicate but refuse and
|
|
979
|
+
// drop respectively -- both actions stay at the call sites.
|
|
980
|
+
const VALID_AUTHORITIES = new Set(); // AUTHORITY_RE values
|
|
981
|
+
const VALID_INBOUND_NAMES = new Set(); // clean names: no upper case, NAME_LOWER_RE
|
|
982
|
+
|
|
491
983
|
// How many SETTINGS entries one frame may carry. CVE-2020-11080 is the shape: a frame
|
|
492
984
|
// with 2 400 entries (14 400 bytes, and SETTINGS is exempt from the max frame size limit
|
|
493
985
|
// -- see _allowFrame) repeated until the CPU is gone, because every entry is parsed even
|
|
@@ -531,6 +1023,23 @@ const MAX_CONTINUATIONS = 8;
|
|
|
531
1023
|
const RST_BURST = 1000;
|
|
532
1024
|
const RST_RATE = 33; // tokens per second
|
|
533
1025
|
|
|
1026
|
+
// Unbounded-work-per-peer limits (SECURITY §8B): node bounds each of these,
|
|
1027
|
+
// h2lean must do the same so a peer's budget against node is its budget here.
|
|
1028
|
+
// Defaults are read off nghttp2 / node's util.js:
|
|
1029
|
+
// maxSessionInvalidFrames — 1000 (kSessionMaxInvalidFrames, core.js:1155)
|
|
1030
|
+
// maxSessionRejectedStreams — 100 (kSessionMaxRejectedStreams, core.js:1161)
|
|
1031
|
+
// maxOutstandingPings — 10 (initializeOptions, util.js)
|
|
1032
|
+
const DEFAULT_MAX_INVALID_FRAMES = 1000;
|
|
1033
|
+
const DEFAULT_MAX_REJECTED_STREAMS = 100;
|
|
1034
|
+
const DEFAULT_MAX_OUTSTANDING_PINGS = 10;
|
|
1035
|
+
|
|
1036
|
+
// The error the invalid-frame budget dies of: node's OnInvalidFrame sets
|
|
1037
|
+
// custom_recv_error_code_ = "ERR_HTTP2_TOO_MANY_INVALID_FRAMES" (node_http2.cc),
|
|
1038
|
+
// whose message internal/errors defines as this string. Measured on a live
|
|
1039
|
+
// node 26 server, both the sessionError and the killing frame's stream error
|
|
1040
|
+
// carry it.
|
|
1041
|
+
const TOO_MANY_INVALID_FRAMES = 'Too many invalid HTTP/2 frames';
|
|
1042
|
+
|
|
534
1043
|
// FNV-1a over the octets of a header block. Math.imul keeps the accumulator in
|
|
535
1044
|
// 32 bits, which is also what makes the hash cheap: no strings are allocated.
|
|
536
1045
|
function hashBlock(buf) {
|
|
@@ -704,7 +1213,21 @@ class Http2Session extends EventEmitter {
|
|
|
704
1213
|
// budget against node (AGENTS.md 8B). The server default is therefore undefined; the client
|
|
705
1214
|
// constructor overrides it from options.
|
|
706
1215
|
this._maxOriginSetSize = undefined;
|
|
1216
|
+
// node's setupHandle, per session: the declared remote custom ids are imported first
|
|
1217
|
+
// (remoteCustomSettingsToBuffer), then options.settings re-runs through settings() --
|
|
1218
|
+
// validateSettings and the packer. A refused value therefore throws HERE, at accept,
|
|
1219
|
+
// uncaught in connectionListener, where node's throw lands (measured: a server
|
|
1220
|
+
// created with settings.customSettings {0: 5} dies on the first connection with the
|
|
1221
|
+
// uncaught RangeError; performServerHandshake throws out of the call the same way).
|
|
1222
|
+
checkRemoteCustomEntries(opts.remoteCustomSettings);
|
|
1223
|
+
checkSettings(opts.settings);
|
|
707
1224
|
this.localSettings = { ...OUR_DEFAULTS, ...opts.settings };
|
|
1225
|
+
// getSettings' mirror: maxHeaderSize always reports maxHeaderListSize, whether or not
|
|
1226
|
+
// the option bag named either. The alias (a numeric maxHeaderSize) moves the
|
|
1227
|
+
// ADVERTISED value only, in submissionValues: node's local bound for inbound header
|
|
1228
|
+
// lists is the options.maxHeaderListSize field, not the setting, and
|
|
1229
|
+
// localSettings.maxHeaderListSize is what the request loop reads as its guard.
|
|
1230
|
+
this.localSettings.maxHeaderSize = this.localSettings.maxHeaderListSize;
|
|
708
1231
|
// Which of those values the application asked for, in the order it asked for them, and only
|
|
709
1232
|
// those go on the wire. Measured on a live node server: created with no options its first
|
|
710
1233
|
// SETTINGS frame carries an EMPTY payload, and created with
|
|
@@ -718,9 +1241,33 @@ class Http2Session extends EventEmitter {
|
|
|
718
1241
|
// Nothing is lost by keeping silent, because the local values still guard the inbound path, and
|
|
719
1242
|
// the one promise the old comment worried about -- push -- is kept by the frame rule, not by
|
|
720
1243
|
// the advertisement: a peer that sends PUSH_PROMISE gets GOAWAY(PROTOCOL_ERROR) in _onFrame.
|
|
1244
|
+
// The set is the membership of the INITIAL frame only: it is built from opts.settings
|
|
1245
|
+
// (server.updateSettings merges into the same object, so a session created later carries the
|
|
1246
|
+
// merged keys), while settings() submits only the entries the current call names -- node's
|
|
1247
|
+
// shape, measured on a live node server (two calls produced 5=32768 then 3=5) and pinned as
|
|
1248
|
+
// parity in test/settings-validation.test.js.
|
|
721
1249
|
this._advertised = new Set((opts.advertised !== undefined
|
|
722
1250
|
? opts.advertised : Object.keys(opts.settings || {}))
|
|
1251
|
+
.map((k) => k === 'maxHeaderSize' ? 'maxHeaderListSize' : k)
|
|
723
1252
|
.filter((k) => ID_BY_SETTING[k] !== undefined));
|
|
1253
|
+
// The custom entries this side has SUBMITTED (accumulated for the view) and the
|
|
1254
|
+
// declared/received pair the peer's customs surface through (see _applySettings).
|
|
1255
|
+
this._customEntries = new Map();
|
|
1256
|
+
this._remoteCustomIds = new Set(opts.remoteCustomSettings || []);
|
|
1257
|
+
this._remoteCustomValues = new Map();
|
|
1258
|
+
// The initial submission is built once, where node's setupHandle runs it: the
|
|
1259
|
+
// advertised named values (alias-resolved), the option bag's customs folded in, the
|
|
1260
|
+
// section accumulated into the view, the packer checks throwing above at accept.
|
|
1261
|
+
// _sendSettings only writes what was built.
|
|
1262
|
+
const built = buildSubmission(submissionValues(opts.settings, this._advertised),
|
|
1263
|
+
opts.settings?.customSettings);
|
|
1264
|
+
this._initialSettings = built.body;
|
|
1265
|
+
// The server half applies named values immediately (the callback-side divergence
|
|
1266
|
+
// documented at settings()), so the fold conversions land the same way.
|
|
1267
|
+
for (let i = 0; i < built.ackValues.length; i += 2) {
|
|
1268
|
+
this.localSettings[built.ackValues[i]] = built.ackValues[i + 1];
|
|
1269
|
+
}
|
|
1270
|
+
this._addCustomEntries(built.rest);
|
|
724
1271
|
this.remoteSettings = { ...PEER_DEFAULTS };
|
|
725
1272
|
this.state = {
|
|
726
1273
|
effectiveLocalWindowSize: this.localSettings.initialWindowSize,
|
|
@@ -735,6 +1282,36 @@ class Http2Session extends EventEmitter {
|
|
|
735
1282
|
};
|
|
736
1283
|
|
|
737
1284
|
this._streams = new Map();
|
|
1285
|
+
// The pings this side sent and has not had answered, oldest first: one entry per PING
|
|
1286
|
+
// frame awaiting its pong. A queue rather than once('ping') listeners, because node keeps
|
|
1287
|
+
// outstanding pings in the C++ session rather than on the emitter and the event belongs to
|
|
1288
|
+
// the application: a listener per ping both competes with the application's own handler and
|
|
1289
|
+
// reaches MaxListeners after eleven unanswered pings (measured: node warns for none). The
|
|
1290
|
+
// queue order is node's answer rule too (lib/client-session.js, _pingQ).
|
|
1291
|
+
this._pingQ = [];
|
|
1292
|
+
// Unbounded-work-per-peer guards (SECURITY §8B): node bounds these, h2lean must too.
|
|
1293
|
+
// maxOutstandingPings defaults to 10 (initializeOptions, internal/http2/util.js).
|
|
1294
|
+
this._maxPingQ = typeof opts.maxOutstandingPings === 'number'
|
|
1295
|
+
? opts.maxOutstandingPings : DEFAULT_MAX_OUTSTANDING_PINGS;
|
|
1296
|
+
// maxSessionInvalidFrames: count the HTTP-messaging refusals (a bad request field
|
|
1297
|
+
// list, a trailer block without END_STREAM) instead of letting them be free; the
|
|
1298
|
+
// first limit+1 are refused per stream, the next closes the connection
|
|
1299
|
+
// (nghttp2 kSessionMaxInvalidFrames, node OnInvalidFrame).
|
|
1300
|
+
this._invalidFrames = 0;
|
|
1301
|
+
this._maxInvalidFrames = typeof opts.maxSessionInvalidFrames === 'number'
|
|
1302
|
+
? opts.maxSessionInvalidFrames : DEFAULT_MAX_INVALID_FRAMES;
|
|
1303
|
+
// maxSessionRejectedStreams: refuse new streams when concurrency/hold/memory limits hit;
|
|
1304
|
+
// close the connection once the peer has been refused this many times
|
|
1305
|
+
// (nghttp2 kSessionMaxRejectedStreams).
|
|
1306
|
+
this._rejectedStreams = 0;
|
|
1307
|
+
this._maxRejectedStreams = typeof opts.maxSessionRejectedStreams === 'number'
|
|
1308
|
+
? opts.maxSessionRejectedStreams : DEFAULT_MAX_REJECTED_STREAMS;
|
|
1309
|
+
// The settings submissions this side sent and has not had answered, oldest first: one entry
|
|
1310
|
+
// per SETTINGS frame awaiting its ACK. Queue order is node's answer rule too (client-side
|
|
1311
|
+
// _settingsAcks). Each entry holds { cb, values } where values are the ackValues from
|
|
1312
|
+
// buildSubmission -- they are applied to localSettings when THIS submit's ACK arrives.
|
|
1313
|
+
this._settingsAcks = [];
|
|
1314
|
+
this._settingsPending = 0;
|
|
738
1315
|
this._decoder = new Decoder(this.localSettings.headerTableSize);
|
|
739
1316
|
// The two halves of the HPACK state, and neither is shared. Each one is bounded by
|
|
740
1317
|
// the SETTINGS of the endpoint that OWNS it: the decoder holds the table our peer
|
|
@@ -771,6 +1348,11 @@ class Http2Session extends EventEmitter {
|
|
|
771
1348
|
// Copying it is a per-request allocation, so it only happens for servers
|
|
772
1349
|
// that actually listen for 'request'.
|
|
773
1350
|
this._wantRaw = opts.compat === true;
|
|
1351
|
+
// The compat entry point the accept path calls directly with the stable
|
|
1352
|
+
// field list (server.js _setupCompat); null on a session no server owns and
|
|
1353
|
+
// on a server whose application listens for 'stream' only -- the object is
|
|
1354
|
+
// then built for the 'stream' emit and the event chain delivers it.
|
|
1355
|
+
this._onStreamCompat = null;
|
|
774
1356
|
|
|
775
1357
|
// Outbound frames go to the socket as one corked writev batch. A body is queued as
|
|
776
1358
|
// the buffers that already hold it -- a frame header and a view of the
|
|
@@ -801,7 +1383,10 @@ class Http2Session extends EventEmitter {
|
|
|
801
1383
|
// already cut out of the queue wait in _esRequeue for the same reason.
|
|
802
1384
|
this._esMarkers = new Map();
|
|
803
1385
|
this._esRequeue = [];
|
|
804
|
-
|
|
1386
|
+
// Connection-level send window: starts at the peer default (initialWindowSize), but
|
|
1387
|
+
// can be overridden by the `connectionWindowSize` option (node's Http2Session option).
|
|
1388
|
+
this._connSendWin = typeof opts.connectionWindowSize === 'number'
|
|
1389
|
+
? opts.connectionWindowSize : PEER_DEFAULTS.initialWindowSize;
|
|
805
1390
|
this._prevInitialWin = PEER_DEFAULTS.initialWindowSize;
|
|
806
1391
|
this._connRecvUsed = 0; // consumed bytes owed back to the connection
|
|
807
1392
|
this._blocked = null; // streams waiting on credit
|
|
@@ -1033,7 +1618,7 @@ class Http2Session extends EventEmitter {
|
|
|
1033
1618
|
const off = this._stageLen;
|
|
1034
1619
|
writeHeader(this._stage, off, len, type, flags, sid);
|
|
1035
1620
|
if (len) {
|
|
1036
|
-
|
|
1621
|
+
f.copyInto(this._stage, off + HEADER_LEN, payload, 0, len);
|
|
1037
1622
|
}
|
|
1038
1623
|
this._stageLen = off + HEADER_LEN + len;
|
|
1039
1624
|
if (type === DATA && (flags & F_END_STREAM)) {
|
|
@@ -1200,28 +1785,26 @@ class Http2Session extends EventEmitter {
|
|
|
1200
1785
|
}
|
|
1201
1786
|
|
|
1202
1787
|
_sendSettings() {
|
|
1203
|
-
|
|
1204
|
-
//
|
|
1205
|
-
|
|
1206
|
-
const names = [...this._advertised].filter((k) => ID_BY_SETTING[k] !== undefined
|
|
1207
|
-
&& s[k] !== undefined);
|
|
1208
|
-
// Six octets per setting (RFC 9113 6.5), so the size is known before the loop and
|
|
1209
|
-
// every octet of it is written here -- allocUnsafe rather than alloc, which would
|
|
1210
|
-
// zero memory we are about to fill. Once per session, so this is the code saying
|
|
1211
|
-
// what it means; there is no measurable claim attached to it.
|
|
1212
|
-
const body = Buffer.allocUnsafe(names.length * 6);
|
|
1213
|
-
for (let i = 0; i < names.length; i++) {
|
|
1214
|
-
const name = names[i];
|
|
1215
|
-
const value = typeof s[name] === 'boolean' ? (s[name] ? 1 : 0) : s[name];
|
|
1216
|
-
const at = i * 6;
|
|
1217
|
-
body.writeUInt16BE(ID_BY_SETTING[name], at);
|
|
1218
|
-
body.writeUInt32BE(value >>> 0, at + 2);
|
|
1219
|
-
}
|
|
1220
|
-
this._frame(SETTINGS, 0, 0, body, true);
|
|
1788
|
+
// Built in the constructor, where node's setupHandle re-runs settings() -- see the
|
|
1789
|
+
// initial-submission comment there. Only the write happens here.
|
|
1790
|
+
this._frame(SETTINGS, 0, 0, this._initialSettings, true);
|
|
1221
1791
|
this.pendingSettingsAck = true;
|
|
1222
1792
|
this._flush();
|
|
1223
1793
|
}
|
|
1224
1794
|
|
|
1795
|
+
// Joins one submission's custom section into the session's accumulated view: node
|
|
1796
|
+
// accumulates at submit (node_http2.cc UpdateLocalCustomSettings) and getSettings
|
|
1797
|
+
// reads the list back, so localSettings.customSettings appears as soon as a custom
|
|
1798
|
+
// entry was submitted and keeps every entry ever submitted (see accumulateCustom).
|
|
1799
|
+
_addCustomEntries(rest) {
|
|
1800
|
+
accumulateCustom(rest, this._customEntries);
|
|
1801
|
+
if (this._customEntries.size === 0) {
|
|
1802
|
+
delete this.localSettings.customSettings;
|
|
1803
|
+
} else {
|
|
1804
|
+
this.localSettings.customSettings = customView(this._customEntries);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1225
1808
|
// Returns true when the block was refused, so the caller knows the wire state its
|
|
1226
1809
|
// HEADERS would have advanced never happened (see _blockRefused in lib/stream.js).
|
|
1227
1810
|
sendHeaders(sid, block, end, bound) {
|
|
@@ -1284,7 +1867,7 @@ class Http2Session extends EventEmitter {
|
|
|
1284
1867
|
}
|
|
1285
1868
|
setImmediate(() => {
|
|
1286
1869
|
if (stream !== undefined && !stream.closed) {
|
|
1287
|
-
stream._destroy(FRAME_SIZE_ERROR,
|
|
1870
|
+
stream._destroy(FRAME_SIZE_ERROR, true, streamErrorOf(FRAME_SIZE_ERROR));
|
|
1288
1871
|
}
|
|
1289
1872
|
this.close();
|
|
1290
1873
|
});
|
|
@@ -1310,7 +1893,7 @@ class Http2Session extends EventEmitter {
|
|
|
1310
1893
|
.emit('frameError', PUSH_PROMISE, FRAME_SIZE_ERROR, parentSid);
|
|
1311
1894
|
const pushed = this._streams.get(promisedSid);
|
|
1312
1895
|
if (pushed !== undefined && !pushed.closed) {
|
|
1313
|
-
pushed._destroy(INTERNAL_ERROR,
|
|
1896
|
+
pushed._destroy(INTERNAL_ERROR, true, streamErrorOf(INTERNAL_ERROR));
|
|
1314
1897
|
}
|
|
1315
1898
|
this.close();
|
|
1316
1899
|
});
|
|
@@ -1365,7 +1948,7 @@ class Http2Session extends EventEmitter {
|
|
|
1365
1948
|
}
|
|
1366
1949
|
} else if (es.stage) {
|
|
1367
1950
|
esBytes = Buffer.allocUnsafe(es.len);
|
|
1368
|
-
|
|
1951
|
+
f.copyInto(esBytes, 0, this._stage, es.off, es.off + es.len);
|
|
1369
1952
|
this._stage.copyWithin(es.off, es.off + es.len, this._stageLen);
|
|
1370
1953
|
this._stageLen -= es.len;
|
|
1371
1954
|
} else {
|
|
@@ -1400,13 +1983,13 @@ class Http2Session extends EventEmitter {
|
|
|
1400
1983
|
if (block.length <= firstCap) {
|
|
1401
1984
|
const payload = Buffer.allocUnsafe(4 + block.length);
|
|
1402
1985
|
payload.writeUInt32BE(promisedSid, 0);
|
|
1403
|
-
|
|
1986
|
+
f.copyInto(payload, 4, block, 0, block.length);
|
|
1404
1987
|
this._frame(PUSH_PROMISE, F_END_HEADERS, parentSid, payload);
|
|
1405
1988
|
return false;
|
|
1406
1989
|
}
|
|
1407
1990
|
const first = Buffer.allocUnsafe(4 + firstCap);
|
|
1408
1991
|
first.writeUInt32BE(promisedSid, 0);
|
|
1409
|
-
|
|
1992
|
+
f.copyInto(first, 4, block, 0, firstCap);
|
|
1410
1993
|
this._frame(PUSH_PROMISE, 0, parentSid, first);
|
|
1411
1994
|
let off = firstCap;
|
|
1412
1995
|
for (;;) {
|
|
@@ -1481,9 +2064,13 @@ class Http2Session extends EventEmitter {
|
|
|
1481
2064
|
break;
|
|
1482
2065
|
}
|
|
1483
2066
|
const last = end && off + take === total;
|
|
1484
|
-
// owned: a view of the application's buffer, which is not copied any more
|
|
1485
|
-
|
|
1486
|
-
|
|
2067
|
+
// owned: a view of the application's buffer, which is not copied any more. A frame
|
|
2068
|
+
// that takes the whole body needs no view at all -- `take === total` is only
|
|
2069
|
+
// reachable at off === 0, since take is bounded by total - off -- and the view object
|
|
2070
|
+
// is the ~28 ns item 57 prices. The same skip the pump in lib/stream.js and the
|
|
2071
|
+
// client's `_pending` loop already make.
|
|
2072
|
+
this._frame(DATA, last ? F_END_STREAM : 0, stream.id,
|
|
2073
|
+
take === total ? buf : buf.subarray(off, off + take), true);
|
|
1487
2074
|
this._connSendWin -= take;
|
|
1488
2075
|
stream._sendWin -= take;
|
|
1489
2076
|
off += take;
|
|
@@ -1515,13 +2102,13 @@ class Http2Session extends EventEmitter {
|
|
|
1515
2102
|
return done && this._connSendWin > 0 && stream._sendWin > 0;
|
|
1516
2103
|
}
|
|
1517
2104
|
|
|
1518
|
-
sendRst(sid, code
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
2105
|
+
sendRst(sid, code) {
|
|
2106
|
+
// RFC 9113 6.4: the RST_STREAM payload is exactly the 4-octet error code. The
|
|
2107
|
+
// old optional reason tail wrote an illegal frame any strict peer must answer
|
|
2108
|
+
// with FRAME_SIZE_ERROR -- this stack's own client refuses it that way
|
|
2109
|
+
// (test/inbound-limits.test.js pins the rule).
|
|
2110
|
+
const body = Buffer.allocUnsafe(4);
|
|
2111
|
+
body.writeUInt32BE(code >>> 0, 0);
|
|
1525
2112
|
this._frame(RST_STREAM, 0, sid, body, true);
|
|
1526
2113
|
}
|
|
1527
2114
|
|
|
@@ -1599,7 +2186,7 @@ class Http2Session extends EventEmitter {
|
|
|
1599
2186
|
} else {
|
|
1600
2187
|
// the frame's header is still incomplete: its octets wait in the
|
|
1601
2188
|
// scratch until the header is whole
|
|
1602
|
-
|
|
2189
|
+
f.copyInto(this._fragHdr, this._frag, chunk, i, i + take);
|
|
1603
2190
|
}
|
|
1604
2191
|
this._frag += take;
|
|
1605
2192
|
i += take;
|
|
@@ -1635,7 +2222,7 @@ class Http2Session extends EventEmitter {
|
|
|
1635
2222
|
this._fragTarget = null;
|
|
1636
2223
|
this._fragPieces = null;
|
|
1637
2224
|
this._frag = n - i;
|
|
1638
|
-
|
|
2225
|
+
f.copyInto(this._fragHdr, 0, chunk, i, n);
|
|
1639
2226
|
break;
|
|
1640
2227
|
}
|
|
1641
2228
|
|
|
@@ -1798,6 +2385,24 @@ class Http2Session extends EventEmitter {
|
|
|
1798
2385
|
return;
|
|
1799
2386
|
}
|
|
1800
2387
|
this.pendingSettingsAck = false;
|
|
2388
|
+
// Process the oldest outstanding submit: node's settingsCallback fires the
|
|
2389
|
+
// callback with (null, localSettings, duration) where duration is a monotonic
|
|
2390
|
+
// clock reading in milliseconds, and emits 'localSettings' on EVERY ack --
|
|
2391
|
+
// including the one for the connection's own first SETTINGS frame. Measured on
|
|
2392
|
+
// a live node 26 server against a raw peer: two ACKs produced two 'localSettings'
|
|
2393
|
+
// events and one callback, in the order event, callback.
|
|
2394
|
+
const entry = this._settingsAcks.shift();
|
|
2395
|
+
if (entry !== undefined) {
|
|
2396
|
+
this._settingsPending--;
|
|
2397
|
+
for (let i = 0; i < entry.values.length; i += 2) {
|
|
2398
|
+
this.localSettings[entry.values[i]] = entry.values[i + 1];
|
|
2399
|
+
}
|
|
2400
|
+
this.localSettings.maxHeaderSize = this.localSettings.maxHeaderListSize;
|
|
2401
|
+
if (typeof entry.cb === 'function') {
|
|
2402
|
+
entry.cb(null, this.localSettings, Number(process.hrtime.bigint()) / 1e6);
|
|
2403
|
+
}
|
|
2404
|
+
}
|
|
2405
|
+
this.emit('localSettings', this.localSettings);
|
|
1801
2406
|
return;
|
|
1802
2407
|
}
|
|
1803
2408
|
this._applySettings(buf, start, len);
|
|
@@ -1819,22 +2424,34 @@ class Http2Session extends EventEmitter {
|
|
|
1819
2424
|
// The payload goes to the application, which may keep it. A PING that arrived
|
|
1820
2425
|
// in pieces is put in a buffer of its own for the parse -- a fresh one, not a
|
|
1821
2426
|
// reused one -- so the view does not change under the application
|
|
1822
|
-
// (AGENTS.md 5: issued buffers stay valid).
|
|
1823
|
-
//
|
|
1824
|
-
// halves of the exchange. Node's payload comes out of the C++ layer as a
|
|
1825
|
-
// fresh buffer, so it is stable there.
|
|
2427
|
+
// (AGENTS.md 5: issued buffers stay valid). Node's payload comes out of the C++
|
|
2428
|
+
// layer as a fresh buffer, so it is stable there.
|
|
1826
2429
|
// copy through allocUnsafe rather than Buffer.from(subarray): the payload goes
|
|
1827
2430
|
// to the application and the ACK frame, so it must be a fresh buffer either
|
|
1828
2431
|
// way, and the pooled allocation of a small buffer is the cheaper way to get
|
|
1829
2432
|
// one -- see the block cache in lib/headers.js for the same measurement
|
|
2433
|
+
// f.copyInto rather than buf.copy: at eight octets the native call's JS
|
|
2434
|
+
// validation floor is the whole cost (26.0 ns against 12.1, OPTIMIZATION.md
|
|
2435
|
+
// item 56). The length check above is load-bearing for the call -- copy
|
|
2436
|
+
// clamps a range that runs off the source, the stores do not.
|
|
1830
2437
|
const payload = Buffer.allocUnsafe(8);
|
|
1831
|
-
|
|
2438
|
+
f.copyInto(payload, 0, buf, start, start + 8);
|
|
1832
2439
|
if (flags & F_ACK) {
|
|
1833
|
-
this
|
|
2440
|
+
// An answer to a ping this side sent is not an event: node's onPing is reached only
|
|
2441
|
+
// for a PING without the ACK flag (measured: a peer's PING|ACK leaves the session's
|
|
2442
|
+
// 'ping' listener count untouched). It resolves the entry ping() registered instead,
|
|
2443
|
+
// with node's measured callback shape (null, durationInMilliseconds, pong).
|
|
2444
|
+
const entry = this._pingQ.shift();
|
|
2445
|
+
if (entry !== undefined) {
|
|
2446
|
+
entry.cb(null, Number(process.hrtime.bigint() - entry.at) / 1e6, payload);
|
|
2447
|
+
}
|
|
1834
2448
|
return;
|
|
1835
2449
|
}
|
|
2450
|
+
// The frame is answered before the application sees it, and the same fresh buffer
|
|
2451
|
+
// serves both: _frame copies eight octets into the staging buffer rather than keeping
|
|
2452
|
+
// the reference, so what the application holds afterwards is nobody else's to move.
|
|
1836
2453
|
this._frame(PING, F_ACK, 0, payload);
|
|
1837
|
-
this.emit('ping',
|
|
2454
|
+
this.emit('ping', payload);
|
|
1838
2455
|
return;
|
|
1839
2456
|
}
|
|
1840
2457
|
case HEADERS:
|
|
@@ -1967,6 +2584,15 @@ class Http2Session extends EventEmitter {
|
|
|
1967
2584
|
for (let i = 0; i + 6 <= len; i += 6) {
|
|
1968
2585
|
const id = buf.readUInt16BE(start + i);
|
|
1969
2586
|
const value = buf.readUInt32BE(start + i + 2);
|
|
2587
|
+
// A custom id the session DECLARED via remoteCustomSettings is remembered with its
|
|
2588
|
+
// value and reported back through remoteSettings.customSettings -- node's
|
|
2589
|
+
// HandleSettingsFrame stores received entries whose id is in the list
|
|
2590
|
+
// remoteCustomSettingsToBuffer imported at setupHandle. Id 8 double-reports: the
|
|
2591
|
+
// named enableConnectProtocol AND customSettings {"8": value} (measured on a live
|
|
2592
|
+
// node client declared [7, 8, 1001]). An undeclared id surfaces nowhere.
|
|
2593
|
+
if (id >= 7 && this._remoteCustomIds.has(id)) {
|
|
2594
|
+
this._remoteCustomValues.set(id, value);
|
|
2595
|
+
}
|
|
1970
2596
|
const name = SETTINGS_BY_ID[id];
|
|
1971
2597
|
if (name === undefined) {
|
|
1972
2598
|
continue;
|
|
@@ -1991,15 +2617,22 @@ class Http2Session extends EventEmitter {
|
|
|
1991
2617
|
return;
|
|
1992
2618
|
}
|
|
1993
2619
|
// The two boolean settings land in remoteSettings as booleans, the way node's
|
|
1994
|
-
//
|
|
1995
|
-
// internal/http2/
|
|
2620
|
+
// getSettings turns them (`enablePush: !!slot`, `enableConnectProtocol: !!slot`,
|
|
2621
|
+
// internal/http2/util.js). The upstream push-disabled test reads
|
|
1996
2622
|
// `settings.enablePush === false` off the remoteSettings event; the wire is a
|
|
1997
2623
|
// uint32, and handing the raw 0/1 to the application diverged from node.
|
|
1998
|
-
if (id === 0x2 || id ===
|
|
2624
|
+
if (id === 0x2 || id === 0x8) {
|
|
1999
2625
|
seen[name] = value !== 0;
|
|
2000
|
-
|
|
2626
|
+
} else {
|
|
2627
|
+
seen[name] = value;
|
|
2001
2628
|
}
|
|
2002
|
-
|
|
2629
|
+
}
|
|
2630
|
+
// getSettings' mirror: maxHeaderSize always reports maxHeaderListSize, whether or not
|
|
2631
|
+
// the frame carried id 6 (a peer's id 6 moves both keys on a live node server; the
|
|
2632
|
+
// mirror re-syncs to the current value on every SETTINGS either way).
|
|
2633
|
+
seen.maxHeaderSize = seen.maxHeaderListSize;
|
|
2634
|
+
if (this._remoteCustomValues.size !== 0) {
|
|
2635
|
+
this.remoteSettings.customSettings = customView(this._remoteCustomValues);
|
|
2003
2636
|
}
|
|
2004
2637
|
// RFC 9113 6.9.2: a new INITIAL_WINDOW_SIZE shifts every open stream window
|
|
2005
2638
|
// by the same delta, and may legitimately drive it negative
|
|
@@ -2158,10 +2791,10 @@ class Http2Session extends EventEmitter {
|
|
|
2158
2791
|
cap *= 2;
|
|
2159
2792
|
}
|
|
2160
2793
|
const bigger = Buffer.allocUnsafe(cap);
|
|
2161
|
-
|
|
2794
|
+
f.copyInto(bigger, 0, this._hdr, 0, this._hdrLen);
|
|
2162
2795
|
this._hdr = bigger;
|
|
2163
2796
|
}
|
|
2164
|
-
|
|
2797
|
+
f.copyInto(this._hdr, this._hdrLen, buf, start, start + len);
|
|
2165
2798
|
this._hdrLen += len;
|
|
2166
2799
|
}
|
|
2167
2800
|
|
|
@@ -2284,8 +2917,13 @@ class Http2Session extends EventEmitter {
|
|
|
2284
2917
|
if (method !== null) {
|
|
2285
2918
|
return { problem: ':method repeated' };
|
|
2286
2919
|
}
|
|
2287
|
-
if (!
|
|
2288
|
-
|
|
2920
|
+
if (!VALID_METHODS.has(value)) {
|
|
2921
|
+
if (!TOKEN_RE.test(value)) {
|
|
2922
|
+
return { problem: `:method ${value} is not a token` };
|
|
2923
|
+
}
|
|
2924
|
+
if (value.length <= VALID_VALUE_MAX_LEN && VALID_METHODS.size < VALID_VALUE_MAX) {
|
|
2925
|
+
VALID_METHODS.add(value);
|
|
2926
|
+
}
|
|
2289
2927
|
}
|
|
2290
2928
|
method = value;
|
|
2291
2929
|
break;
|
|
@@ -2299,8 +2937,13 @@ class Http2Session extends EventEmitter {
|
|
|
2299
2937
|
if (scheme !== null) {
|
|
2300
2938
|
return { problem: ':scheme repeated' };
|
|
2301
2939
|
}
|
|
2302
|
-
if (!
|
|
2303
|
-
|
|
2940
|
+
if (!VALID_SCHEMES.has(value)) {
|
|
2941
|
+
if (!SCHEME_RE.test(value)) {
|
|
2942
|
+
return { problem: `:scheme ${value} is not a scheme` };
|
|
2943
|
+
}
|
|
2944
|
+
if (value.length <= VALID_VALUE_MAX_LEN && VALID_SCHEMES.size < VALID_VALUE_MAX) {
|
|
2945
|
+
VALID_SCHEMES.add(value);
|
|
2946
|
+
}
|
|
2304
2947
|
}
|
|
2305
2948
|
scheme = value;
|
|
2306
2949
|
break;
|
|
@@ -2308,8 +2951,13 @@ class Http2Session extends EventEmitter {
|
|
|
2308
2951
|
if (authority !== null) {
|
|
2309
2952
|
return { problem: ':authority repeated' };
|
|
2310
2953
|
}
|
|
2311
|
-
if (!
|
|
2312
|
-
|
|
2954
|
+
if (!VALID_AUTHORITIES.has(value)) {
|
|
2955
|
+
if (!AUTHORITY_RE.test(value)) {
|
|
2956
|
+
return { problem: `:authority ${value} has illegal characters` };
|
|
2957
|
+
}
|
|
2958
|
+
if (value.length <= VALID_VALUE_MAX_LEN && VALID_AUTHORITIES.size < VALID_VALUE_MAX) {
|
|
2959
|
+
VALID_AUTHORITIES.add(value);
|
|
2960
|
+
}
|
|
2313
2961
|
}
|
|
2314
2962
|
authority = value;
|
|
2315
2963
|
break;
|
|
@@ -2323,6 +2971,11 @@ class Http2Session extends EventEmitter {
|
|
|
2323
2971
|
if (!this.localSettings.enableConnectProtocol) {
|
|
2324
2972
|
return { problem: ':protocol without enableConnectProtocol' };
|
|
2325
2973
|
}
|
|
2974
|
+
// check_header_value_rfc9113() applies: see RFC9113_VALUE_BAD_RE above for
|
|
2975
|
+
// the class and the edges.
|
|
2976
|
+
if (RFC9113_VALUE_BAD_RE.test(value)) {
|
|
2977
|
+
return { problem: `:protocol ${value} has illegal characters` };
|
|
2978
|
+
}
|
|
2326
2979
|
protocol = value;
|
|
2327
2980
|
break;
|
|
2328
2981
|
default:
|
|
@@ -2334,18 +2987,23 @@ class Http2Session extends EventEmitter {
|
|
|
2334
2987
|
}
|
|
2335
2988
|
|
|
2336
2989
|
pseudoAllowed = false;
|
|
2337
|
-
if (
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
kept
|
|
2344
|
-
|
|
2345
|
-
|
|
2990
|
+
if (!VALID_INBOUND_NAMES.has(name)) {
|
|
2991
|
+
if (HAS_UPPER.test(name)) {
|
|
2992
|
+
return { problem: `field name is not lower-case: ${name}` };
|
|
2993
|
+
}
|
|
2994
|
+
if (!NAME_LOWER_RE.test(name)) {
|
|
2995
|
+
// Illegal character in a regular name: drop the field, keep the stream.
|
|
2996
|
+
if (kept === null) {
|
|
2997
|
+
kept = src.slice(0, i);
|
|
2998
|
+
if (flags !== null) {
|
|
2999
|
+
keptFlags = Array.from(flags.subarray(0, i >> 1));
|
|
3000
|
+
}
|
|
2346
3001
|
}
|
|
3002
|
+
continue;
|
|
3003
|
+
}
|
|
3004
|
+
if (name.length <= VALID_VALUE_MAX_LEN && VALID_INBOUND_NAMES.size < VALID_VALUE_MAX) {
|
|
3005
|
+
VALID_INBOUND_NAMES.add(name);
|
|
2347
3006
|
}
|
|
2348
|
-
continue;
|
|
2349
3007
|
}
|
|
2350
3008
|
if (INBOUND_FORBIDDEN.has(name)) {
|
|
2351
3009
|
return { problem: `${name} is a connection-specific field` };
|
|
@@ -2360,6 +3018,12 @@ class Http2Session extends EventEmitter {
|
|
|
2360
3018
|
if (!NUM_RE.test(value)) {
|
|
2361
3019
|
return { problem: `content-length ${value} is not numeric` };
|
|
2362
3020
|
}
|
|
3021
|
+
// See CL_53 above: past 2^53 the promise cannot be compared exactly any more, so
|
|
3022
|
+
// it is refused here. node serves up to INT64_MAX; the gap is written down with
|
|
3023
|
+
// the constant.
|
|
3024
|
+
if (clPast53(value)) {
|
|
3025
|
+
return { problem: `content-length ${value} exceeds 2^53` };
|
|
3026
|
+
}
|
|
2363
3027
|
// Two copies are the smuggling case itself; nghttp2 refuses a second one even
|
|
2364
3028
|
// when both say the same thing, and node does too (measured).
|
|
2365
3029
|
if (contentLength !== null) {
|
|
@@ -2367,13 +3031,25 @@ class Http2Session extends EventEmitter {
|
|
|
2367
3031
|
}
|
|
2368
3032
|
contentLength = value;
|
|
2369
3033
|
} else if (name === 'host') {
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
3034
|
+
// host is processed by the pseudo-header rules in nghttp2 (the HOST flag of
|
|
3035
|
+
// http_request_on_header), where an empty value is the hard error rather than a
|
|
3036
|
+
// dropped field -- measured on node (tools/probe-validation-parity.js host-empty).
|
|
3037
|
+
// check_authority("") itself passes, so it is the emptiness that refuses.
|
|
3038
|
+
if (value.length === 0) {
|
|
3039
|
+
return { problem: 'host with an empty value' };
|
|
3040
|
+
}
|
|
3041
|
+
if (!VALID_AUTHORITIES.has(value)) {
|
|
3042
|
+
if (!AUTHORITY_RE.test(value)) {
|
|
3043
|
+
if (kept === null) {
|
|
3044
|
+
kept = src.slice(0, i);
|
|
3045
|
+
if (flags !== null) {
|
|
3046
|
+
keptFlags = Array.from(flags.subarray(0, i >> 1));
|
|
3047
|
+
}
|
|
3048
|
+
} continue;
|
|
3049
|
+
}
|
|
3050
|
+
if (value.length <= VALID_VALUE_MAX_LEN && VALID_AUTHORITIES.size < VALID_VALUE_MAX) {
|
|
3051
|
+
VALID_AUTHORITIES.add(value);
|
|
3052
|
+
}
|
|
2377
3053
|
}
|
|
2378
3054
|
if (host !== null) {
|
|
2379
3055
|
return { problem: 'host repeated' };
|
|
@@ -2381,12 +3057,15 @@ class Http2Session extends EventEmitter {
|
|
|
2381
3057
|
host = value;
|
|
2382
3058
|
}
|
|
2383
3059
|
// The last of the per-field rules, and the only one that looks at the value as a
|
|
2384
|
-
// whole: a character that may not appear in a field value
|
|
2385
|
-
// disappear, and the stream lives
|
|
2386
|
-
// bench/micro/probe-value-matrix.js
|
|
2387
|
-
//
|
|
2388
|
-
//
|
|
2389
|
-
|
|
3060
|
+
// whole: a character that may not appear in a field value, or an RFC 9113 edge
|
|
3061
|
+
// (SP/HTAB first or last), makes the field disappear, and the stream lives
|
|
3062
|
+
// (NGHTTP2_ERR_IGN_HTTP_HEADER, measured on node: bench/micro/probe-value-matrix.js
|
|
3063
|
+
// for the class, tools/probe-validation-parity.js value-* for the edges; an empty
|
|
3064
|
+
// value has no edges and is delivered, measured the same way). It sits after the
|
|
3065
|
+
// rules above because those win: a 'te' or a 'content-length' carrying a CR -- or a
|
|
3066
|
+
// leading space -- is a PROTOCOL_ERROR on node, not a dropped field
|
|
3067
|
+
// (bench/micro/probe-value-order.js, te-lead-ws / cl-lead-ws).
|
|
3068
|
+
if (RFC9113_VALUE_BAD_RE.test(value)) {
|
|
2390
3069
|
if (kept === null) {
|
|
2391
3070
|
kept = src.slice(0, i);
|
|
2392
3071
|
if (flags !== null) {
|
|
@@ -2434,10 +3113,16 @@ class Http2Session extends EventEmitter {
|
|
|
2434
3113
|
if (authority === null && host === null) {
|
|
2435
3114
|
return { problem: 'missing :authority and host' };
|
|
2436
3115
|
}
|
|
2437
|
-
//
|
|
2438
|
-
//
|
|
2439
|
-
|
|
2440
|
-
|
|
3116
|
+
// nghttp2_check_path() refuses anything outside 0x21-0x7e and 0x80-0xff; see
|
|
3117
|
+
// PATH_BAD_RE above for the measurement and the smuggling shape it closes.
|
|
3118
|
+
if (PATH_BAD_RE.test(path)) {
|
|
3119
|
+
return { problem: `:path ${path} has illegal characters` };
|
|
3120
|
+
}
|
|
3121
|
+
// check_path() in nghttp2: with an http or https scheme -- flagged case-insensitively,
|
|
3122
|
+
// see schemeHttpLike above -- the path is an absolute path, or "*" for a system-wide
|
|
3123
|
+
// OPTIONS. Anything else is refused -- a relative form is exactly what a downstream
|
|
3124
|
+
// HTTP/1 translator would resolve on its own.
|
|
3125
|
+
if (schemeHttpLike(scheme)
|
|
2441
3126
|
&& path.charCodeAt(0) !== 47 /* / */
|
|
2442
3127
|
&& !(method === 'OPTIONS' && path === '*')) {
|
|
2443
3128
|
return { problem: `:path ${path} does not start with "/"` };
|
|
@@ -2622,6 +3307,23 @@ class Http2Session extends EventEmitter {
|
|
|
2622
3307
|
// and the closed stream is what keeps the unread-request auto-close
|
|
2623
3308
|
// (Http2Stream[kMaybeDestroy], see lib/stream.js _maybeAutoClose) from
|
|
2624
3309
|
// putting a second RST(NO_ERROR) on the wire behind the refusal's.
|
|
3310
|
+
//
|
|
3311
|
+
// The refusal is also a counted invalid frame (node's OnInvalidFrame counts
|
|
3312
|
+
// every NGHTTP2_ERR_HTTP_MESSAGING): the first maxSessionInvalidFrames + 1
|
|
3313
|
+
// are refused per stream, and the next one kills the session. Measured on a
|
|
3314
|
+
// live node 26 server (budget 3, five violations): the killing frame still
|
|
3315
|
+
// refuses a live stream -- RST_STREAM(PROTOCOL_ERROR), with
|
|
3316
|
+
// ERR_HTTP2_TOO_MANY_INVALID_FRAMES as the stream's error -- while a stream
|
|
3317
|
+
// the session never opened takes nothing, and the GOAWAY(INTERNAL_ERROR)
|
|
3318
|
+
// goes out alone.
|
|
3319
|
+
if (this._recordInvalidFrame()) {
|
|
3320
|
+
if (existing !== undefined) {
|
|
3321
|
+
this._streamError(existing, PROTOCOL_ERROR, TOO_MANY_INVALID_FRAMES,
|
|
3322
|
+
'ERR_HTTP2_TOO_MANY_INVALID_FRAMES');
|
|
3323
|
+
}
|
|
3324
|
+
this._fatalTooMany();
|
|
3325
|
+
return;
|
|
3326
|
+
}
|
|
2625
3327
|
if (existing !== undefined) {
|
|
2626
3328
|
this._streamError(existing, PROTOCOL_ERROR, verdict.problem);
|
|
2627
3329
|
} else {
|
|
@@ -2636,6 +3338,16 @@ class Http2Session extends EventEmitter {
|
|
|
2636
3338
|
// trailer closes the message -- it has to arrive with END_STREAM. Without it the body
|
|
2637
3339
|
// would still be open with its tail already sent. Measured on node (h2spec http2/8.1):
|
|
2638
3340
|
// RST_STREAM(PROTOCOL_ERROR), the connection untouched.
|
|
3341
|
+
//
|
|
3342
|
+
// Counted like the refusals above (measured on node, budget 3: four of these refused
|
|
3343
|
+
// per stream, the fifth trips) -- and the killing frame still takes its
|
|
3344
|
+
// RST_STREAM(PROTOCOL_ERROR), with the too-many code as its error.
|
|
3345
|
+
if (this._recordInvalidFrame()) {
|
|
3346
|
+
this._streamError(existing, PROTOCOL_ERROR, TOO_MANY_INVALID_FRAMES,
|
|
3347
|
+
'ERR_HTTP2_TOO_MANY_INVALID_FRAMES');
|
|
3348
|
+
this._fatalTooMany();
|
|
3349
|
+
return;
|
|
3350
|
+
}
|
|
2639
3351
|
this._streamError(existing, PROTOCOL_ERROR, 'trailing HEADERS without END_STREAM');
|
|
2640
3352
|
return;
|
|
2641
3353
|
}
|
|
@@ -2652,6 +3364,10 @@ class Http2Session extends EventEmitter {
|
|
|
2652
3364
|
if (this._liveStreams >= this.localSettings.maxConcurrentStreams) {
|
|
2653
3365
|
this.sendRst(sid, REFUSED_STREAM);
|
|
2654
3366
|
this._refused.push(sid);
|
|
3367
|
+
this._rejectedStreams++;
|
|
3368
|
+
if (this._rejectedStreams >= this._maxRejectedStreams) {
|
|
3369
|
+
return this._fatal(PROTOCOL_ERROR, 'too many rejected streams');
|
|
3370
|
+
}
|
|
2655
3371
|
if (this._refused.length > MAX_REFUSED_HISTORY) {
|
|
2656
3372
|
this._refused.shift();
|
|
2657
3373
|
}
|
|
@@ -2673,6 +3389,10 @@ class Http2Session extends EventEmitter {
|
|
|
2673
3389
|
if (this._heldCost() >= MAX_SESSION_HOLD) {
|
|
2674
3390
|
this.sendRst(sid, REFUSED_STREAM);
|
|
2675
3391
|
this._refused.push(sid);
|
|
3392
|
+
this._rejectedStreams++;
|
|
3393
|
+
if (this._rejectedStreams >= this._maxRejectedStreams) {
|
|
3394
|
+
return this._fatal(PROTOCOL_ERROR, 'too many rejected streams');
|
|
3395
|
+
}
|
|
2676
3396
|
if (this._refused.length > MAX_REFUSED_HISTORY) {
|
|
2677
3397
|
this._refused.shift();
|
|
2678
3398
|
}
|
|
@@ -2692,17 +3412,54 @@ class Http2Session extends EventEmitter {
|
|
|
2692
3412
|
if (bytes > this.localSettings.maxHeaderListSize) {
|
|
2693
3413
|
this.sendRst(sid, ENHANCE_YOUR_CALM);
|
|
2694
3414
|
this._refused.push(sid);
|
|
3415
|
+
this._rejectedStreams++;
|
|
3416
|
+
if (this._rejectedStreams >= this._maxRejectedStreams) {
|
|
3417
|
+
return this._fatal(PROTOCOL_ERROR, 'too many rejected streams');
|
|
3418
|
+
}
|
|
2695
3419
|
if (this._refused.length > MAX_REFUSED_HISTORY) {
|
|
2696
3420
|
this._refused.shift();
|
|
2697
3421
|
}
|
|
2698
3422
|
return;
|
|
2699
3423
|
}
|
|
2700
3424
|
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
3425
|
+
// The request object is built here only when a reader is already known: a
|
|
3426
|
+
// session 'stream' listener takes it as the event argument, and the compat
|
|
3427
|
+
// request materializes it at the first req.headers access (lib/compat.js),
|
|
3428
|
+
// so a handler that only consumes the body and routes on req.url pays no
|
|
3429
|
+
// per-request object -- the paired marginal is OPTIMIZATION.md item 49. The
|
|
3430
|
+
// build still happens eagerly under shareHeaders, whose frozen shared
|
|
3431
|
+
// objects are the point of that option.
|
|
3432
|
+
const sensitive = neverNames(fields, fcount, blockFlags);
|
|
3433
|
+
const headers = shared !== null || this.listenerCount('stream') > 0 || this._share
|
|
3434
|
+
? (shared !== null ? shared
|
|
3435
|
+
: (this._share
|
|
3436
|
+
? Object.freeze(this._toObject(fields, fcount, sensitive))
|
|
3437
|
+
: this._toObject(fields, fcount, sensitive)))
|
|
3438
|
+
: undefined;
|
|
3439
|
+
// What the stack reads without the object: :method (the HEAD checks in
|
|
3440
|
+
// lib/stream.js and the compat CONNECT check), :path (req.url) and
|
|
3441
|
+
// content-length (the promise checked below). When the object was built
|
|
3442
|
+
// these are its own property reads, exactly what the eager code did; the
|
|
3443
|
+
// one-pass scan is the lazy path's only walk, and it takes the first copy
|
|
3444
|
+
// of a repeated field, the way the single-value field lands in the object.
|
|
3445
|
+
let reqMethod;
|
|
3446
|
+
let reqPath;
|
|
3447
|
+
let promised;
|
|
3448
|
+
if (headers !== undefined) {
|
|
3449
|
+
reqMethod = headers[':method'];
|
|
3450
|
+
promised = headers['content-length'];
|
|
3451
|
+
} else {
|
|
3452
|
+
for (let i = 0; i < fcount; i += 2) {
|
|
3453
|
+
const name = fields[i];
|
|
3454
|
+
if (name === ':method') {
|
|
3455
|
+
reqMethod = fields[i + 1];
|
|
3456
|
+
} else if (name === ':path') {
|
|
3457
|
+
reqPath = fields[i + 1];
|
|
3458
|
+
} else if (name === 'content-length' && promised === undefined) {
|
|
3459
|
+
promised = fields[i + 1];
|
|
3460
|
+
}
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
2706
3463
|
// The one shape in which the content-length promise is already broken before a single body
|
|
2707
3464
|
// octet could have arrived: the field promises a body and the HEADERS frame ends the message.
|
|
2708
3465
|
// Refusing at the accept decision -- the same place the parse failures and the size limits are
|
|
@@ -2712,7 +3469,7 @@ class Http2Session extends EventEmitter {
|
|
|
2712
3469
|
// Letting it through to Http2Stream._remoteEnded() instead resets the stream and then hands
|
|
2713
3470
|
// the handler a destroyed one, and a handler that answers it puts a response on the wire for a
|
|
2714
3471
|
// request this side has already refused.
|
|
2715
|
-
if (end &&
|
|
3472
|
+
if (end && promised !== undefined && +promised > 0) {
|
|
2716
3473
|
this.sendRst(sid, PROTOCOL_ERROR);
|
|
2717
3474
|
// The frame that carried the refusal also carried the peer's END_STREAM, so the message is
|
|
2718
3475
|
// over: the DATA that follows it (or a second HEADERS) is the error of 5.1 and not the
|
|
@@ -2726,15 +3483,21 @@ class Http2Session extends EventEmitter {
|
|
|
2726
3483
|
return;
|
|
2727
3484
|
}
|
|
2728
3485
|
const stream = new Http2Stream(this, sid);
|
|
2729
|
-
stream.
|
|
3486
|
+
stream._reqMethod = reqMethod;
|
|
3487
|
+
stream._reqPath = reqPath;
|
|
3488
|
+
stream._reqSensitive = sensitive;
|
|
3489
|
+
// node's argument order; the field list is the session's shared decode array
|
|
3490
|
+
// or a cached one, so the raw list handed out is always a copy. In the compat
|
|
3491
|
+
// path the copy is also the stable list the lazy request object builds from
|
|
3492
|
+
// (the scratch's backing array is cleared in place by the next decode), so it
|
|
3493
|
+
// travels with the stream even when no emit will carry it.
|
|
3494
|
+
const raw = this._wantRaw ? fields.slice(0, fcount) : null;
|
|
3495
|
+
stream._reqFields = raw;
|
|
3496
|
+
if (headers !== undefined) {
|
|
3497
|
+
stream._requestHeaders = headers;
|
|
3498
|
+
}
|
|
2730
3499
|
this._streams.set(sid, stream);
|
|
2731
3500
|
this._liveStreams++;
|
|
2732
|
-
// content-length is a promise about the body, and the two checks that hold the peer to it
|
|
2733
|
-
// (see _onDataFrame and Http2Stream._remoteEnded) need it as a number. The inbound
|
|
2734
|
-
// validator has already proved the value is all digits, so this is a property read and a
|
|
2735
|
-
// coercion -- and -1, the "nothing was promised" marker, costs one comparison per DATA
|
|
2736
|
-
// frame for the requests that never sent the field at all.
|
|
2737
|
-
const promised = headers['content-length'];
|
|
2738
3501
|
if (promised !== undefined) {
|
|
2739
3502
|
stream._expLen = +promised;
|
|
2740
3503
|
}
|
|
@@ -2744,10 +3507,15 @@ class Http2Session extends EventEmitter {
|
|
|
2744
3507
|
if (end) {
|
|
2745
3508
|
stream._remoteEnded();
|
|
2746
3509
|
}
|
|
2747
|
-
//
|
|
2748
|
-
//
|
|
2749
|
-
|
|
2750
|
-
|
|
3510
|
+
// Delivery turns on who is listening, not on whether an object was built:
|
|
3511
|
+
// with shareHeaders on the object exists even when no 'stream' listener
|
|
3512
|
+
// does, and emitting it into a listenerless session would drop the request
|
|
3513
|
+
// the compat callback was meant to receive.
|
|
3514
|
+
if (this.listenerCount('stream') > 0) {
|
|
3515
|
+
this.emit('stream', stream, headers, end ? OPT_END_STREAM : OPT_NO_END_STREAM, raw);
|
|
3516
|
+
} else if (this._onStreamCompat !== null) {
|
|
3517
|
+
this._onStreamCompat(stream);
|
|
3518
|
+
}
|
|
2751
3519
|
}
|
|
2752
3520
|
|
|
2753
3521
|
// node's toHeaderObject semantics: null prototype, :status coerced to a
|
|
@@ -3035,8 +3803,9 @@ class Http2Session extends EventEmitter {
|
|
|
3035
3803
|
}
|
|
3036
3804
|
const take = Math.min(room, buf.length - sent);
|
|
3037
3805
|
const last = q.length === 1 && sent + take === buf.length && stream._endQueued;
|
|
3038
|
-
|
|
3039
|
-
|
|
3806
|
+
// same whole-chunk skip as sendData(): take === buf.length means sent === 0
|
|
3807
|
+
this._frame(DATA, last ? F_END_STREAM : 0, stream.id,
|
|
3808
|
+
take === buf.length ? buf : buf.subarray(sent, sent + take), true);
|
|
3040
3809
|
this._connSendWin -= take;
|
|
3041
3810
|
stream._sendWin -= take;
|
|
3042
3811
|
// the byte is no longer held once the frame is on its way to the socket
|
|
@@ -3132,7 +3901,7 @@ class Http2Session extends EventEmitter {
|
|
|
3132
3901
|
// reset. Whether the application hears 'aborted' is decided there, by
|
|
3133
3902
|
// whether the response was still open (lib/stream.js, _destroy). The error it
|
|
3134
3903
|
// hears is the code's own name, and codes 0 and 8 are not errors at all.
|
|
3135
|
-
stream._destroy(code,
|
|
3904
|
+
stream._destroy(code, false, streamErrorOf(code));
|
|
3136
3905
|
}
|
|
3137
3906
|
|
|
3138
3907
|
get goawayCode() {
|
|
@@ -3162,7 +3931,7 @@ class Http2Session extends EventEmitter {
|
|
|
3162
3931
|
let debug;
|
|
3163
3932
|
if (len > 8) {
|
|
3164
3933
|
debug = Buffer.allocUnsafe(len - 8);
|
|
3165
|
-
|
|
3934
|
+
f.copyInto(debug, 0, buf, start + 8, start + len);
|
|
3166
3935
|
}
|
|
3167
3936
|
this.emit('goaway', this._goawayCode, this._goawayLastStreamID, debug);
|
|
3168
3937
|
// node's rule on receipt (onGoawayData in internal/http2/core): a GOAWAY with NO_ERROR
|
|
@@ -3284,10 +4053,34 @@ class Http2Session extends EventEmitter {
|
|
|
3284
4053
|
return false;
|
|
3285
4054
|
}
|
|
3286
4055
|
|
|
3287
|
-
|
|
4056
|
+
// Count an invalid frame from the peer. node's OnInvalidFrame (node_http2.cc)
|
|
4057
|
+
// increments for every counted refusal and trips only when the PRE-increment
|
|
4058
|
+
// count passes maxSessionInvalidFrames (`invalid_frame_count_++ >
|
|
4059
|
+
// max_invalid_frames`): with the default 1000 the 1002nd violation closes and
|
|
4060
|
+
// the first 1001 are refused per stream -- measured on a live node 26 server
|
|
4061
|
+
// (budget 3: four violations tolerated, the fifth trips). Returns true on the
|
|
4062
|
+
// killing frame; the caller still shapes that frame's own refusal (a live
|
|
4063
|
+
// stream takes its RST_STREAM(PROTOCOL_ERROR) with the too-many code as its
|
|
4064
|
+
// error, a stream the session never opened takes nothing) and then closes the
|
|
4065
|
+
// session with _fatalTooMany().
|
|
4066
|
+
_recordInvalidFrame() {
|
|
4067
|
+
return this._invalidFrames++ > this._maxInvalidFrames;
|
|
4068
|
+
}
|
|
4069
|
+
|
|
4070
|
+
// The budget trip, measured on a live node 26 server (budget 3): the GOAWAY
|
|
4071
|
+
// carries INTERNAL_ERROR with EMPTY debug data and the highest id the peer
|
|
4072
|
+
// reached, and the session error is node's custom_recv_error_code_ --
|
|
4073
|
+
// ERR_HTTP2_TOO_MANY_INVALID_FRAMES -- not the ERR_HTTP2_SESSION_ERROR of an
|
|
4074
|
+
// ordinary fatal.
|
|
4075
|
+
_fatalTooMany() {
|
|
4076
|
+
this._fatal(INTERNAL_ERROR, TOO_MANY_INVALID_FRAMES,
|
|
4077
|
+
'ERR_HTTP2_TOO_MANY_INVALID_FRAMES', '');
|
|
4078
|
+
}
|
|
4079
|
+
|
|
4080
|
+
_streamError(stream, code, msg, errCode) {
|
|
3288
4081
|
this.sendRst(stream.id, code);
|
|
3289
4082
|
this._flush();
|
|
3290
|
-
stream._destroy(code,
|
|
4083
|
+
stream._destroy(code, false);
|
|
3291
4084
|
// No 'frameError' here. node's onFrameError is the SEND-error path -- its own comment
|
|
3292
4085
|
// says "Called by the native layer when an error has occurred sending a frame. This
|
|
3293
4086
|
// should be exceedingly rare." (core.js:675) -- and it emits (type, code, id). An
|
|
@@ -3298,10 +4091,11 @@ class Http2Session extends EventEmitter {
|
|
|
3298
4091
|
// any of the five (out/probe-frameerror.js). This used to emit one on the session with
|
|
3299
4092
|
// the id and the code swapped and DATA hardcoded as the type; the outbound sites
|
|
3300
4093
|
// (_refuseSend, the PUSH_PROMISE bound) are the ones that match node's event.
|
|
3301
|
-
stream._tick(() => stream.emit('error',
|
|
4094
|
+
stream._tick(() => stream.emit('error',
|
|
4095
|
+
errorOf(errCode !== undefined ? errCode : 'ERR_HTTP2_STREAM_ERROR', msg)));
|
|
3302
4096
|
}
|
|
3303
4097
|
|
|
3304
|
-
_fatal(code, msg) {
|
|
4098
|
+
_fatal(code, msg, errCode, debugData) {
|
|
3305
4099
|
if (!this._open) {
|
|
3306
4100
|
return;
|
|
3307
4101
|
}
|
|
@@ -3310,9 +4104,34 @@ class Http2Session extends EventEmitter {
|
|
|
3310
4104
|
// _reachedSid) -- naming only the streams a stream object was made for would tell
|
|
3311
4105
|
// the peer it may retry a request that was already acted on
|
|
3312
4106
|
const reached = this._maxInboundSid > this._reachedSid ? this._maxInboundSid : this._reachedSid;
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
4107
|
+
// debugData: the invalid-frame budget trip carries none on the wire (measured
|
|
4108
|
+
// on node: GOAWAY(INTERNAL_ERROR) with an empty debug field), while the error
|
|
4109
|
+
// the application sees still has node's message.
|
|
4110
|
+
const tail = debugData !== undefined ? debugData : msg;
|
|
4111
|
+
if (this._writing) {
|
|
4112
|
+
// A batch is already on the socket: staging the GOAWAY behind it loses it,
|
|
4113
|
+
// because _writePending() drops whatever is queued once _open goes false and
|
|
4114
|
+
// destroy() below sets _open false before the in-flight write completes --
|
|
4115
|
+
// measured on the invalid-frame budget trip, where the killing frame's own
|
|
4116
|
+
// RST_STREAM is the write in flight and the GOAWAY never reached the peer.
|
|
4117
|
+
// Socket writes keep their order, so the GOAWAY goes straight to the socket
|
|
4118
|
+
// and lands after that batch; anything still staged was produced before the
|
|
4119
|
+
// fatal and is dropped by _writePending(), as before.
|
|
4120
|
+
const tlen = tail ? Buffer.byteLength(tail) : 0;
|
|
4121
|
+
const buf = Buffer.allocUnsafe(HEADER_LEN + 8 + tlen);
|
|
4122
|
+
writeHeader(buf, 0, 8 + tlen, GOAWAY, 0, 0);
|
|
4123
|
+
buf.writeUInt32BE(reached >>> 0, HEADER_LEN);
|
|
4124
|
+
buf.writeUInt32BE(code, HEADER_LEN + 4);
|
|
4125
|
+
if (tlen) {
|
|
4126
|
+
buf.write(tail, HEADER_LEN + 8);
|
|
4127
|
+
}
|
|
4128
|
+
this._socket.write(buf);
|
|
4129
|
+
this._goawaySent = true;
|
|
4130
|
+
} else {
|
|
4131
|
+
this.sendGoaway(reached, code, tail);
|
|
4132
|
+
this._flush();
|
|
4133
|
+
}
|
|
4134
|
+
const err = errorOf(errCode !== undefined ? errCode : 'ERR_HTTP2_SESSION_ERROR', msg);
|
|
3316
4135
|
err.http2Code = code;
|
|
3317
4136
|
this.destroy(code);
|
|
3318
4137
|
if (this.listenerCount('error') > 0) {
|
|
@@ -3468,7 +4287,7 @@ class Http2Session extends EventEmitter {
|
|
|
3468
4287
|
// `session.closed` keeps saying whether close() was asked for. Measured on a session
|
|
3469
4288
|
// destroyed after a peer GOAWAY with a code: node reports closed:false destroyed:true.
|
|
3470
4289
|
for (const s of [...this._streams.values()]) {
|
|
3471
|
-
s._destroy(code,
|
|
4290
|
+
s._destroy(code, false, appError);
|
|
3472
4291
|
}
|
|
3473
4292
|
this._streams.clear();
|
|
3474
4293
|
this._liveStreams = 0;
|
|
@@ -3476,6 +4295,31 @@ class Http2Session extends EventEmitter {
|
|
|
3476
4295
|
idleClearTimeout(this._idleTimer);
|
|
3477
4296
|
this._idleTimer = null;
|
|
3478
4297
|
}
|
|
4298
|
+
// A ping whose pong can no longer arrive still owes its callback: node cancels the
|
|
4299
|
+
// outstanding ones with ERR_HTTP2_PING_CANCEL, and it does so off the synchronous path --
|
|
4300
|
+
// measured unfired both in destroy() and on process.nextTick, fired by setImmediate.
|
|
4301
|
+
if (this._pingQ.length > 0) {
|
|
4302
|
+
const pending = this._pingQ;
|
|
4303
|
+
this._pingQ = [];
|
|
4304
|
+
setImmediate(() => {
|
|
4305
|
+
for (const entry of pending) {
|
|
4306
|
+
entry.cb(pingCancelled());
|
|
4307
|
+
}
|
|
4308
|
+
});
|
|
4309
|
+
}
|
|
4310
|
+
// A settings submit whose ACK can no longer arrive still owes its callback: node cancels
|
|
4311
|
+
// the outstanding ones with ERR_HTTP2_SETTINGS_CANCEL, off the synchronous path -- same
|
|
4312
|
+
// shape as the ping cancellation above.
|
|
4313
|
+
if (this._settingsAcks.length > 0) {
|
|
4314
|
+
const pending = this._settingsAcks;
|
|
4315
|
+
this._settingsAcks = [];
|
|
4316
|
+
this._settingsPending = 0;
|
|
4317
|
+
setImmediate(() => {
|
|
4318
|
+
for (const entry of pending) {
|
|
4319
|
+
entry.cb(settingsCancelled());
|
|
4320
|
+
}
|
|
4321
|
+
});
|
|
4322
|
+
}
|
|
3479
4323
|
this._flush();
|
|
3480
4324
|
// node's teardown shape (internal/http2/core.js, finishSessionClose): the socket is
|
|
3481
4325
|
// ended first and the destroy waits for the write to be flushed, so the GOAWAY queued
|
|
@@ -3617,34 +4461,99 @@ class Http2Session extends EventEmitter {
|
|
|
3617
4461
|
// on a live session of node 26, `session.settings()` answers undefined, so this does not hand
|
|
3618
4462
|
// out the session for chaining either; a caller that wants the current values has
|
|
3619
4463
|
// session.localSettings, which both stacks expose.
|
|
4464
|
+
//
|
|
4465
|
+
// The callback fires on the peer's ACK, not on a tick: core.js settingsCallback calls
|
|
4466
|
+
// `cb(null, localSettings, duration)` -- three arguments, the duration being a monotonic
|
|
4467
|
+
// clock reading, not an elapsed time -- and emits 'localSettings' on the session. An
|
|
4468
|
+
// unanswered one is cancelled with ERR_HTTP2_SETTINGS_CANCEL when the session ends.
|
|
3620
4469
|
settings(settings, cb) {
|
|
4470
|
+
// node's prologue, in its order (internal/http2/core.js settings(), shared with the
|
|
4471
|
+
// client half because ping() and settings() live on Http2Session and both classes
|
|
4472
|
+
// inherit them): the destroyed check comes first, so a bad argument on a dead session
|
|
4473
|
+
// reports ERR_HTTP2_INVALID_SESSION rather than the type error the same call raises
|
|
4474
|
+
// while it lives; then assertIsObject + validateSettings; then the callback, guarded by
|
|
4475
|
+
// `if (callback)` the way ping()'s unconditional validateFunction is not — so
|
|
4476
|
+
// settings({}, null) is legal and settings({}, 'nope') throws. Without the callback
|
|
4477
|
+
// check the bad value reached _tick and process.nextTick refused it with a message that
|
|
4478
|
+
// happens to be node's, but only after a SETTINGS frame had already gone out: node
|
|
4479
|
+
// throws before it submits anything.
|
|
4480
|
+
if (this.destroyed) {
|
|
4481
|
+
throw invalidSession();
|
|
4482
|
+
}
|
|
4483
|
+
checkSettings(settings);
|
|
4484
|
+
if (cb && typeof cb !== 'function') {
|
|
4485
|
+
throw extError('ERR_INVALID_ARG_TYPE',
|
|
4486
|
+
`The "callback" argument must be of type function. Received ${received(cb)}`);
|
|
4487
|
+
}
|
|
4488
|
+
// node submits only the entries the CURRENT call names, ascending by setting id
|
|
4489
|
+
// (measured on a live node server: settings({maxFrameSize: 32768}) then
|
|
4490
|
+
// settings({maxConcurrentStreams: 5}) put 5=32768 then 3=5 on the wire), and
|
|
4491
|
+
// settings(undefined) submits an empty frame. localSettings keeps the accumulated
|
|
4492
|
+
// values the way node's settings buffer slots do -- a submission does not withdraw
|
|
4493
|
+
// an earlier one. maxHeaderSize is the deprecated alias of maxHeaderListSize: it
|
|
4494
|
+
// wins when the two differ, and node warns exactly then (submissionValues, the pack
|
|
4495
|
+
// both halves share). The call's customSettings ride the same pack: below the flags
|
|
4496
|
+
// slot they fold into the named settings, the rest forms the custom section and
|
|
4497
|
+
// accumulates into the view (buildSubmission, accumulateCustom).
|
|
4498
|
+
let body;
|
|
3621
4499
|
if (settings) {
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
//
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
}
|
|
4500
|
+
const built = buildSubmission(submissionValues(settings), settings.customSettings);
|
|
4501
|
+
body = built.body;
|
|
4502
|
+
// the server half applies named values immediately (the callback-side divergence
|
|
4503
|
+
// documented above), so the fold-converted entries land the same way
|
|
4504
|
+
for (let i = 0; i < built.ackValues.length; i += 2) {
|
|
4505
|
+
this.localSettings[built.ackValues[i]] = built.ackValues[i + 1];
|
|
3629
4506
|
}
|
|
4507
|
+
this._addCustomEntries(built.rest);
|
|
4508
|
+
} else {
|
|
4509
|
+
body = buildSubmission({}, undefined).body;
|
|
3630
4510
|
}
|
|
3631
|
-
this.
|
|
4511
|
+
this._frame(SETTINGS, 0, 0, body, true);
|
|
4512
|
+
this.pendingSettingsAck = true;
|
|
4513
|
+
this._settingsPending++;
|
|
4514
|
+
this._flush();
|
|
3632
4515
|
if (cb) {
|
|
3633
|
-
|
|
4516
|
+
// Queue the callback: it fires when THIS submit's ACK arrives, alongside
|
|
4517
|
+
// 'localSettings' emission. Measured on a live node 26 server: two ACKs
|
|
4518
|
+
// produced two 'localSettings' events and one callback, in the order event,
|
|
4519
|
+
// callback. The duration is a monotonic clock reading in milliseconds.
|
|
4520
|
+
this._settingsAcks.push({ cb, values: settings ? buildSubmission(
|
|
4521
|
+
submissionValues(settings), settings.customSettings).ackValues : [] });
|
|
3634
4522
|
}
|
|
3635
4523
|
}
|
|
3636
4524
|
|
|
3637
4525
|
ping(opaque, cb) {
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
4526
|
+
if (this.destroyed) {
|
|
4527
|
+
throw invalidSession();
|
|
4528
|
+
}
|
|
4529
|
+
const [payload, callback] = checkPing(opaque, cb);
|
|
4530
|
+
// A falsy payload sends eight zero octets: node lets nghttp2 choose them, and nothing
|
|
4531
|
+
// depends on the choice, since the pong is matched by position in the queue rather than by
|
|
4532
|
+
// value (see _pingQ in lib/client-session.js for the measurement that fixes the rule).
|
|
4533
|
+
const body = payload ? Buffer.allocUnsafe(8) : Buffer.alloc(8);
|
|
4534
|
+
if (payload) {
|
|
4535
|
+
// The only two shapes are a caller's Buffer, which copyInto indexes directly, and any
|
|
4536
|
+
// other view, which has to be re-based onto a Buffer first: a DataView has no numeric
|
|
4537
|
+
// indexing, and reading a TypedArray's backing store would copy the wrong octets. The
|
|
4538
|
+
// length is exactly 8, checked above, so there is nothing to clamp and no tail left to
|
|
4539
|
+
// fall through to whatever the pool held before this body was handed out.
|
|
4540
|
+
const src = Buffer.isBuffer(payload)
|
|
4541
|
+
? payload
|
|
4542
|
+
: Buffer.from(payload.buffer, payload.byteOffset, 8);
|
|
4543
|
+
f.copyInto(body, 0, src, 0, 8);
|
|
4544
|
+
}
|
|
4545
|
+
// maxOutstandingPings: node caps the unanswered-ping queue (initializeOptions, util.js).
|
|
4546
|
+
// A full queue is not an error: nghttp2's submit fails, node's ping() answers false and
|
|
4547
|
+
// the callback is never registered (core.js: `if (duration !== false) this[kQueue].push`).
|
|
4548
|
+
if (this._pingQ.length >= this._maxPingQ) {
|
|
4549
|
+
return false;
|
|
3641
4550
|
}
|
|
4551
|
+
this._pingQ.push({ cb: callback, at: process.hrtime.bigint() });
|
|
3642
4552
|
this._frame(PING, 0, 0, body, true);
|
|
3643
4553
|
this._flush();
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
return this;
|
|
4554
|
+
// node hands back what nghttp2's submit returned (measured: a live node session answers
|
|
4555
|
+
// true), not the session -- ping() is not a chaining method.
|
|
4556
|
+
return true;
|
|
3648
4557
|
}
|
|
3649
4558
|
|
|
3650
4559
|
goaway(code = NO_ERROR, lastStreamID = this._maxInboundSid, reason) {
|
|
@@ -3692,7 +4601,7 @@ class Http2Session extends EventEmitter {
|
|
|
3692
4601
|
// at all for no arguments. Returns undefined like node (it is not a chaining method).
|
|
3693
4602
|
origin(...origins) {
|
|
3694
4603
|
if (this.destroyed) {
|
|
3695
|
-
throw
|
|
4604
|
+
throw invalidSession();
|
|
3696
4605
|
}
|
|
3697
4606
|
const payload = buildOriginPayload(origins);
|
|
3698
4607
|
if (payload === null) {
|
|
@@ -3709,7 +4618,7 @@ class Http2Session extends EventEmitter {
|
|
|
3709
4618
|
// process, since a pure-JS library has no abort to fall back to.
|
|
3710
4619
|
altsvc(alt, originOrStream) {
|
|
3711
4620
|
if (this.destroyed) {
|
|
3712
|
-
throw
|
|
4621
|
+
throw invalidSession();
|
|
3713
4622
|
}
|
|
3714
4623
|
const { sid, origin, alt: value } = checkAltSvc(alt, originOrStream);
|
|
3715
4624
|
// latin1 truncates every code unit to its low byte and is one octet per code
|
|
@@ -3747,4 +4656,6 @@ module.exports = { Http2Session, OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID,
|
|
|
3747
4656
|
STAGE_MIN, MAX_SHAPES, MAX_SEND_BLOCK, PRIORITY_SPECLEN,
|
|
3748
4657
|
strictSingleValueFields, hashBlock, hashPieces, kBoundSession,
|
|
3749
4658
|
proxySocketHandler, extError, urlOrigin, checkAltSvc, buildOriginPayload,
|
|
3750
|
-
|
|
4659
|
+
checkPing, invalidSession, pingCancelled, checkSettings, buildSubmission,
|
|
4660
|
+
submissionValues, accumulateCustom, customView, validateRemoteCustomSettings,
|
|
4661
|
+
checkRemoteCustomEntries, parseAltSvc, parseOrigins, received, outOfRangeValue };
|