h2lean 0.8.0 → 0.9.1

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.
@@ -103,13 +103,12 @@
103
103
  // reaches the wire, and a destroyed session reports closed false. Measured on a live
104
104
  // node client for all three calls.
105
105
  //
106
- // KNOWN DIVERGENCE: an inbound NO_ERROR GOAWAY whose lastStreamID is below the open ids
107
- // makes node refuse the unprocessed streams itself -- rstCode 7, ERR_HTTP2_STREAM_ERROR
108
- // naming NGHTTP2_REFUSED_STREAM, and the session destroys once they are gone. Here they
109
- // are not refused at all: rstCode stays 0, the streams stay open and the session stays
110
- // closed-but-not-destroyed until the connection goes. Measured with two open requests
111
- // and lastStreamID 0; the same shape with lastStreamID above them agrees on both arms
112
- // and is pinned by test/client/client-goaway-inbound.test.js.
106
+ // A NO_ERROR GOAWAY whose lastStreamID is below the open ids refuses the retired streams
107
+ // here, as node does: every locally-initiated (odd) stream above lastStreamID closes with
108
+ // NGHTTP2_REFUSED_STREAM -- rstCode 7, ERR_HTTP2_STREAM_ERROR naming it, no session-level
109
+ // error -- and the graceful close completes once they are gone. A mark above the open ids
110
+ // refuses nothing. Measured with two unanswered requests and lastStreamID 0 and 5 against
111
+ // a raw peer; both shapes pinned by test/client/client-goaway-inbound.test.js.
113
112
  'use strict';
114
113
 
115
114
  const { EventEmitter } = require('node:events');
@@ -123,7 +122,8 @@ const FlatList = require('./flat-list');
123
122
  // list: the two never meet the same fill, and coupling the client module to the
124
123
  // server's scratch would buy nothing.
125
124
  const FLAT = new FlatList(64);
126
- const { nullProto, isFieldName, lowerName, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS } = require('./headers');
125
+ const { nullProto, isFieldName, lowerName, neverNames, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS,
126
+ KSensitiveHeaders } = require('./headers');
127
127
  const { ClientHttp2Stream, AbortError } = require('./client-stream');
128
128
  const { pieceByteAt, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
129
129
  // The settings tables, defaults and buffer-path constants are the server's, reused
@@ -140,7 +140,7 @@ const {
140
140
  DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
141
141
  WINDOW_UPDATE, CONTINUATION, ALTSVC, ORIGIN,
142
142
  F_END_STREAM, F_ACK, F_END_HEADERS, F_PADDED, F_PRIORITY,
143
- NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FRAME_SIZE_ERROR, COMPRESSION_ERROR, CANCEL, REFUSED_STREAM,
143
+ NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR, FRAME_SIZE_ERROR, COMPRESSION_ERROR, CANCEL, REFUSED_STREAM,
144
144
  HEADER_LEN, PREFACE, PREFACE_LEN, readHeader, writeHeader, trimPadding
145
145
  } = f;
146
146
 
@@ -785,6 +785,20 @@ class ClientHttp2Session extends EventEmitter {
785
785
  let sawMethod = false;
786
786
 
787
787
  const src = headersParam === undefined ? {} : headersParam;
788
+ // node reads the never-indexed names off the headers param's sensitiveHeaders
789
+ // symbol in both shapes -- array or object (buildNgHeaderString reads
790
+ // arrayOrMap[kSensitiveHeaders], internal/http2/util.js) -- lowercases them,
791
+ // and flags every field line whose name is in the list as a never-indexed
792
+ // literal. The wire half is pinned by the server-side receive in
793
+ // test/upstream/sensitive-headers.test.js, the object half by sentHeaders.
794
+ const sensitive = src[KSensitiveHeaders];
795
+ let neverList = null;
796
+ if (Array.isArray(sensitive) && sensitive.length > 0) {
797
+ neverList = new Set();
798
+ for (let i = 0; i < sensitive.length; i++) {
799
+ neverList.add(lowerName(sensitive[i]));
800
+ }
801
+ }
788
802
  if (this._strictSingle) {
789
803
  // the duplicate check rides _addField's own loop, the way node's
790
804
  // processHeader runs it inside buildNgHeaderString's one pass; the scratch
@@ -794,23 +808,26 @@ class ClientHttp2Session extends EventEmitter {
794
808
  }
795
809
  if (Array.isArray(src)) {
796
810
  for (let i = 0; i + 1 < src.length; i += 2) {
797
- this._addField(list, src[i], src[i + 1]);
811
+ this._addField(list, neverList, src[i], src[i + 1]);
798
812
  }
799
813
  } else {
800
814
  const keys = Object.keys(src);
801
815
  for (let i = 0; i < keys.length; i++) {
802
- this._addField(list, keys[i], src[keys[i]]);
816
+ this._addField(list, neverList, keys[i], src[keys[i]]);
803
817
  }
804
818
  }
805
819
  // list may already contain app pseudo-headers; extract method for the endStream
806
820
  // default and the pseudo-header completion node performs.
807
- for (let i = 0; i < list.length; i += 2) {
821
+ for (let i = 0; i < list.length; i += 3) {
808
822
  if (list[i] === ':method') {
809
823
  method = list[i + 1]; sawMethod = true; break;
810
824
  }
811
825
  }
812
826
  if (!sawMethod) {
813
- list.push(':method', method);
827
+ // node fills the pseudo-header into the map before the flags are computed,
828
+ // so a name the application marked reaches the fill too (processHeader
829
+ // applies neverIndex.includes(key) to pseudo lines as well).
830
+ list.push(':method', method, neverList !== null && neverList.has(':method'));
814
831
  }
815
832
 
816
833
  let endStream = options === undefined ? undefined : options.endStream;
@@ -834,7 +851,7 @@ class ClientHttp2Session extends EventEmitter {
834
851
  // headers are only filled into the outbound list later, in _sendRequest, so the
835
852
  // same resolution is done here against the list as it stands.
836
853
  let originScheme, originAuthority;
837
- for (let i = 0; i < list.length; i += 2) {
854
+ for (let i = 0; i < list.length; i += 3) {
838
855
  const key = list[i];
839
856
  if (key === ':scheme') {
840
857
  if (originScheme === undefined) {
@@ -894,7 +911,7 @@ class ClientHttp2Session extends EventEmitter {
894
911
  // object across requests, the HPACK encoder produces identical bytes every time
895
912
  // after the dynamic table stabilises, and _sendRequest can use _blocks to replay
896
913
  // the encoded block without re-encoding. (The server's shareHeaders equivalent.)
897
- const req = { stream, list, end: endStream, hp: headersParam };
914
+ const req = { stream, list, nl: neverList, end: endStream, hp: headersParam };
898
915
  if (requestError !== undefined) {
899
916
  // node's requestOnError: destroy the stream with the captured error on the
900
917
  // next tick, and submit nothing. The id reserved above is never used -- the
@@ -945,7 +962,7 @@ class ClientHttp2Session extends EventEmitter {
945
962
  // the peer's number and not the 65535 default the peer may never have chosen.
946
963
  req.stream._sendWin = this.remoteSettings.initialWindowSize;
947
964
  this._streams.set(req.stream.id, req.stream);
948
- this._sendRequest(req.stream, req.list, req.end, req.hp);
965
+ this._sendRequest(req.stream, req.list, req.nl, req.end, req.hp);
949
966
  }
950
967
 
951
968
  // A slot just freed (a stream closed): start queued requests while the peer's
@@ -967,10 +984,14 @@ class ClientHttp2Session extends EventEmitter {
967
984
  }
968
985
  }
969
986
 
970
- // Normalises one application-supplied field into the flat [name, value, ...] list:
971
- // lower-cases names, coerces pseudo/regular, rejects connection-specific fields
972
- // (RFC 9113 8.2.2) and a non-token name (RFC 9113 8.2.1) exactly as node's
973
- // prepareRequestHeaders* does for a client request.
987
+ // Normalises one application-supplied field into the flat [name, value, never, ...]
988
+ // list: lower-cases names, coerces pseudo/regular, rejects connection-specific
989
+ // fields (RFC 9113 8.2.2) and a non-token name (RFC 9113 8.2.1) exactly as node's
990
+ // prepareRequestHeaders* does for a client request. The third slot carries the
991
+ // never-indexed flag the encoder writes into the literal form (0x10): node flags
992
+ // each line whose lower-cased name is in the headers param's sensitiveHeaders
993
+ // list (buildNgHeaderString, internal/http2/util.js), and `neverList` is that
994
+ // list as a Set, or null when the application marked nothing.
974
995
  //
975
996
  // The strict single-value rule rides this same loop, the way node's processHeader
976
997
  // runs it inside buildNgHeaderString's one pass (internal/http2/util.js): a
@@ -980,11 +1001,12 @@ class ClientHttp2Session extends EventEmitter {
980
1001
  // at, which is why the check stands above the pseudo branch; an empty array is
981
1002
  // skipped before any bookkeeping (node's `case 0: return`), so it neither pushes
982
1003
  // a value nor spends a slot in _singlesSeen.
983
- _addField(list, rawKey, rawValue) {
1004
+ _addField(list, neverList, rawKey, rawValue) {
984
1005
  if (rawValue === undefined) {
985
1006
  return;
986
1007
  }
987
1008
  const key = lowerName(rawKey);
1009
+ const never = neverList !== null && neverList.has(key);
988
1010
  const isArray = Array.isArray(rawValue);
989
1011
  if (isArray && rawValue.length === 0) {
990
1012
  return;
@@ -1010,7 +1032,7 @@ class ClientHttp2Session extends EventEmitter {
1010
1032
  && key !== ':protocol') {
1011
1033
  throw httpErr('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', `cannot send ${key}`);
1012
1034
  }
1013
- list.push(key, String(isArray ? rawValue[0] : rawValue));
1035
+ list.push(key, String(isArray ? rawValue[0] : rawValue), never);
1014
1036
  return;
1015
1037
  }
1016
1038
  if (!isFieldName(key)) {
@@ -1022,17 +1044,20 @@ class ClientHttp2Session extends EventEmitter {
1022
1044
  }
1023
1045
  if (isArray) {
1024
1046
  for (let j = 0; j < rawValue.length; j++) {
1025
- list.push(key, String(rawValue[j]));
1047
+ list.push(key, String(rawValue[j]), never);
1026
1048
  }
1027
1049
  } else {
1028
- list.push(key, String(rawValue));
1050
+ list.push(key, String(rawValue), never);
1029
1051
  }
1030
1052
  }
1031
1053
 
1032
- _sendRequest(stream, list, end, hp) {
1054
+ _sendRequest(stream, list, neverList, end, hp) {
1033
1055
  // Fill the pseudo-headers the application omitted, in the canonical order, then
1034
1056
  // hand the whole list to HPACK. node completes :path to '/', :scheme and
1035
1057
  // :authority from the session; we mirror that so `request({})` is a GET /.
1058
+ // The fills carry the never-indexed flag the way node's does: the names land
1059
+ // in the map before buildNgHeaderString computes the flags, so an application
1060
+ // that marked ':path' marks the fill with it.
1036
1061
  //
1037
1062
  // The outbound block cache: when the same headersParam object is passed to
1038
1063
  // request() again and the encoder's dynamic-table epoch has not moved since
@@ -1053,13 +1078,14 @@ class ClientHttp2Session extends EventEmitter {
1053
1078
  bound = cached.bound;
1054
1079
  } else {
1055
1080
  if (!list.includes(':path')) {
1056
- list.push(':path', '/');
1081
+ list.push(':path', '/', neverList !== null && neverList.has(':path'));
1057
1082
  }
1058
1083
  if (!list.includes(':scheme')) {
1059
- list.push(':scheme', this._scheme);
1084
+ list.push(':scheme', this._scheme, neverList !== null && neverList.has(':scheme'));
1060
1085
  }
1061
1086
  if (!list.includes(':authority')) {
1062
- list.push(':authority', this._authority);
1087
+ list.push(':authority', this._authority,
1088
+ neverList !== null && neverList.has(':authority'));
1063
1089
  }
1064
1090
  const ordered = orderPseudo(list);
1065
1091
  block = this._encodeRequest(ordered);
@@ -1100,7 +1126,7 @@ class ClientHttp2Session extends EventEmitter {
1100
1126
  // alongside the buffer: the block cache below has to carry it too, and a second
1101
1127
  // return value would mean an object per request.
1102
1128
  let bound = 12;
1103
- for (let i = 0; i < list.length; i += 2) {
1129
+ for (let i = 0; i < list.length; i += 3) {
1104
1130
  cap += Encoder.maxBytesOf(list[i], list[i + 1]);
1105
1131
  bound += 12 + list[i].length + list[i + 1].length;
1106
1132
  }
@@ -1118,8 +1144,8 @@ class ClientHttp2Session extends EventEmitter {
1118
1144
  const buf = Buffer.allocUnsafe(ul + cap);
1119
1145
  enc.unsettled = false;
1120
1146
  let off = ul;
1121
- for (let i = 0; i < list.length; i += 2) {
1122
- off = enc.write(buf, off, list[i], list[i + 1], false);
1147
+ for (let i = 0; i < list.length; i += 3) {
1148
+ off = enc.write(buf, off, list[i], list[i + 1], list[i + 2]);
1123
1149
  }
1124
1150
  if (ul === 0) {
1125
1151
  return buf.subarray(0, off);
@@ -1301,14 +1327,24 @@ class ClientHttp2Session extends EventEmitter {
1301
1327
  const list = [];
1302
1328
  this._singlesSeen = [];
1303
1329
  const src = trailers === undefined || trailers === null ? {} : trailers;
1330
+ // The never-indexed mark rides the trailers param's sensitiveHeaders symbol the
1331
+ // same way it does on a request (buildNgHeaderString reads it in both shapes).
1332
+ const sensitive = src[KSensitiveHeaders];
1333
+ let neverList = null;
1334
+ if (Array.isArray(sensitive) && sensitive.length > 0) {
1335
+ neverList = new Set();
1336
+ for (let i = 0; i < sensitive.length; i++) {
1337
+ neverList.add(lowerName(sensitive[i]));
1338
+ }
1339
+ }
1304
1340
  if (Array.isArray(src)) {
1305
1341
  for (let i = 0; i + 1 < src.length; i += 2) {
1306
- this._addField(list, src[i], src[i + 1]);
1342
+ this._addField(list, neverList, src[i], src[i + 1]);
1307
1343
  }
1308
1344
  } else {
1309
1345
  const keys = Object.keys(src);
1310
1346
  for (let i = 0; i < keys.length; i++) {
1311
- this._addField(list, keys[i], src[keys[i]]);
1347
+ this._addField(list, neverList, keys[i], src[keys[i]]);
1312
1348
  }
1313
1349
  }
1314
1350
  // RFC 9113 8.1: a trailer block carries no pseudo-header. _addField throws
@@ -2019,6 +2055,7 @@ class ClientHttp2Session extends EventEmitter {
2019
2055
  }
2020
2056
  const fields = flat.items;
2021
2057
  const fcount = flat.count;
2058
+ const flags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
2022
2059
  // maxReservedRemoteStreams: a pushed stream stays reserved until its
2023
2060
  // response HEADERS arrive (measured: the count releases on the HEADERS,
2024
2061
  // not on 'end'). Over the cap the promise is refused with RST_STREAM(CANCEL)
@@ -2040,7 +2077,7 @@ class ClientHttp2Session extends EventEmitter {
2040
2077
  this._frame(RST_STREAM, 0, promisedSid, body, true);
2041
2078
  return;
2042
2079
  }
2043
- const headers = this._toObject(fields, fcount);
2080
+ const headers = this._toObject(fields, fcount, neverNames(fields, fcount, flags));
2044
2081
  // Create the pushed stream. Its writable half is pre-ended: the client never
2045
2082
  // sends a request body for a push (node: writableEnded=true, writableFinished=true
2046
2083
  // at the 'stream' event, measured v26.12.1).
@@ -2118,9 +2155,14 @@ class ClientHttp2Session extends EventEmitter {
2118
2155
  const hit = shapes === null ? undefined : shapes.get(slot);
2119
2156
  // where this response's field list comes from: the scratch's backing array
2120
2157
  // on a decode, a cached copy on a shape hit -- both read by index, so the
2121
- // valid length travels next to the list (same as the server session)
2158
+ // valid length travels next to the list (same as the server session).
2159
+ // blockFlags is the never-indexed marker of the same lines: the decoder's
2160
+ // list on a decode, the cached snapshot on a hit, null when there was none
2161
+ // -- neverNames() reads it where _toObject hangs the `sensitiveHeaders`
2162
+ // symbol node puts on every inbound header object.
2122
2163
  let fields;
2123
2164
  let fcount;
2165
+ let blockFlags;
2124
2166
  try {
2125
2167
  if (hit !== undefined && (block !== null
2126
2168
  ? hit.block.equals(block)
@@ -2128,6 +2170,7 @@ class ClientHttp2Session extends EventEmitter {
2128
2170
  this._shapeHits++;
2129
2171
  fields = hit.flat;
2130
2172
  fcount = fields.length;
2173
+ blockFlags = hit.flags;
2131
2174
  } else {
2132
2175
  if (block !== null) {
2133
2176
  this._decoder.decode(block, 0, block.length, flat);
@@ -2136,6 +2179,7 @@ class ClientHttp2Session extends EventEmitter {
2136
2179
  }
2137
2180
  fields = flat.items;
2138
2181
  fcount = flat.count;
2182
+ blockFlags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
2139
2183
  if (shapes !== null) {
2140
2184
  if (++this._shapeMisses > 64 && this._shapeHits * 4 < this._shapeMisses) {
2141
2185
  this._shapes = null;
@@ -2145,7 +2189,10 @@ class ClientHttp2Session extends EventEmitter {
2145
2189
  }
2146
2190
  shapes.set(slot, {
2147
2191
  block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
2148
- flat: fields.slice(0, fcount)
2192
+ flat: fields.slice(0, fcount),
2193
+ // a snapshot copy, not the decoder's live array: the next decode
2194
+ // clears the array in place, and this entry must survive it
2195
+ flags: blockFlags !== null ? blockFlags.slice() : null
2149
2196
  });
2150
2197
  }
2151
2198
  }
@@ -2183,7 +2230,7 @@ class ClientHttp2Session extends EventEmitter {
2183
2230
  if (this._refuseHeaders(sid, stream, fields, fcount)) {
2184
2231
  return;
2185
2232
  }
2186
- const headers = this._toObject(fields, fcount);
2233
+ const headers = this._toObject(fields, fcount, neverNames(fields, fcount, blockFlags));
2187
2234
  // RFC 8336 3.2 + core.js emitStreamEvent: a 421 (Misdirected Request) answer
2188
2235
  // deletes the origin the request carried from originSet, and it does so here --
2189
2236
  // before 'response' is scheduled, so a handler that reads `session.originSet`
@@ -2249,8 +2296,15 @@ class ClientHttp2Session extends EventEmitter {
2249
2296
  return true;
2250
2297
  }
2251
2298
 
2252
- _toObject(fields, count) {
2299
+ // node's toHeaderObject semantics for a response (or a trailer block or a
2300
+ // push promise): null prototype, :status coerced to a number, set-cookie
2301
+ // always an array, repeats joined -- and the never-indexed names under the
2302
+ // `sensitiveHeaders` symbol, assigned first so the shape tree of these
2303
+ // objects shares one symbol transition (the server side explains why in
2304
+ // session.js _toObject).
2305
+ _toObject(fields, count, sensitive) {
2253
2306
  const o = nullProto();
2307
+ o[KSensitiveHeaders] = sensitive;
2254
2308
  for (let i = 0; i < count; i += 2) {
2255
2309
  const name = fields[i];
2256
2310
  let value = fields[i + 1];
@@ -2352,7 +2406,17 @@ class ClientHttp2Session extends EventEmitter {
2352
2406
  if (inc === 0) {
2353
2407
  this._fatal(PROTOCOL_ERROR, 'WINDOW_UPDATE increment of 0'); return;
2354
2408
  }
2409
+ // RFC 9113 6.9.1: a WINDOW_UPDATE that would push a send window past 2^31-1 is a
2410
+ // connection error, not a stream error -- nghttp2 ends the whole session for both
2411
+ // the per-stream and the per-connection window (measured on a live node v26.12.1
2412
+ // client: GOAWAY(FLOW_CONTROL_ERROR) outbound, ERR_HTTP2_ERROR on the stream and
2413
+ // the session, the stream closed with rstCode NGHTTP2_INTERNAL_ERROR). Without the
2414
+ // cap the client's window simply absorbs every increment, an unbounded number a
2415
+ // peer controls.
2355
2416
  if (sid === 0) {
2417
+ if (this._connSendWin + inc > 0x7fffffff) {
2418
+ this._fatal(FLOW_CONTROL_ERROR, 'connection window overflow'); return;
2419
+ }
2356
2420
  this._connSendWin += inc;
2357
2421
  for (const s of this._streams.values()) {
2358
2422
  this._pump(s);
@@ -2363,6 +2427,9 @@ class ClientHttp2Session extends EventEmitter {
2363
2427
  if (stream === undefined) {
2364
2428
  return;
2365
2429
  }
2430
+ if (stream._sendWin + inc > 0x7fffffff) {
2431
+ this._fatal(FLOW_CONTROL_ERROR, 'stream window overflow'); return;
2432
+ }
2366
2433
  stream._sendWin += inc;
2367
2434
  this._pump(stream);
2368
2435
  }
@@ -2421,7 +2488,33 @@ class ClientHttp2Session extends EventEmitter {
2421
2488
  // 0, 1, 3 and 5: the session and every open stream error and close in that order,
2422
2489
  // destroyed true and closed false, GOAWAY(0,0) alone on the wire with no RST_STREAM.
2423
2490
  // Pinned by test/client/client-goaway-inbound.test.js.
2491
+ //
2492
+ // nghttp2 refuses the streams the GOAWAY retires itself: every locally-initiated
2493
+ // stream above lastStreamID closes with NGHTTP2_REFUSED_STREAM (measured on a live
2494
+ // node v26.12.1 client with two unanswered requests and lastStreamID 0: the stream
2495
+ // errors and closes NEWEST id first -- req on id 3 before the one on id 1, the
2496
+ // reverse of the non-zero destroy's ascending order -- each with ERR_HTTP2_STREAM_ERROR
2497
+ // naming NGHTTP2_REFUSED_STREAM and close rstCode 7, destroyed true and aborted false
2498
+ // because the request had ended its writable, then the session 'close' with no session
2499
+ // 'error'; lastStreamID above the open ids refuses nothing and matches h2lean's old
2500
+ // behaviour, which stays pinned). Without the refusal the streams lived on at rstCode
2501
+ // 0 and the graceful close waited for work the peer has already disowned.
2424
2502
  if (code === NO_ERROR) {
2503
+ if (lastStreamID < this._nextSid - 2) {
2504
+ const refused = [];
2505
+ for (const [sid, stream] of this._streams) {
2506
+ if ((sid & 1) !== 0 && sid > lastStreamID) {
2507
+ refused.push([sid, stream]);
2508
+ }
2509
+ }
2510
+ // Client-initiated ids are opened ascending, so the map's insertion order is
2511
+ // ascending and walking the list backwards is node's newest-first refusal.
2512
+ for (let i = refused.length - 1; i >= 0; i--) {
2513
+ const [sid, stream] = refused[i];
2514
+ this._dropStream(sid);
2515
+ stream._onRst(REFUSED_STREAM);
2516
+ }
2517
+ }
2425
2518
  this.close();
2426
2519
  } else {
2427
2520
  this.destroy(httpErr('ERR_HTTP2_SESSION_ERROR',
@@ -3123,31 +3216,32 @@ class ClientHttp2Session extends EventEmitter {
3123
3216
  function orderPseudo(list) {
3124
3217
  const pseudo = [];
3125
3218
  const fields = [];
3126
- for (let i = 0; i < list.length; i += 2) {
3127
- (list[i].charCodeAt(0) === 58 ? pseudo : fields).push(list[i], list[i + 1]);
3219
+ for (let i = 0; i < list.length; i += 3) {
3220
+ (list[i].charCodeAt(0) === 58 ? pseudo : fields).push(list[i], list[i + 1],
3221
+ list[i + 2]);
3128
3222
  }
3129
- const byName = (n) => {
3130
- for (let i = 0; i < pseudo.length; i += 2) {
3223
+ const byIdx = (n) => {
3224
+ for (let i = 0; i < pseudo.length; i += 3) {
3131
3225
  if (pseudo[i] === n) {
3132
- return pseudo[i + 1];
3226
+ return i;
3133
3227
  }
3134
3228
  }
3135
- return undefined;
3229
+ return -1;
3136
3230
  };
3137
3231
  const out = [];
3138
3232
  for (const n of [':method', ':path', ':scheme', ':authority', ':protocol']) {
3139
- const v = byName(n);
3140
- if (v !== undefined) {
3141
- out.push(n, v);
3233
+ const idx = byIdx(n);
3234
+ if (idx !== -1) {
3235
+ out.push(pseudo[idx], pseudo[idx + 1], pseudo[idx + 2]);
3142
3236
  }
3143
3237
  }
3144
- for (let i = 0; i < pseudo.length; i += 2) {
3238
+ for (let i = 0; i < pseudo.length; i += 3) {
3145
3239
  if (out.indexOf(pseudo[i]) === -1) {
3146
- out.push(pseudo[i], pseudo[i + 1]);
3240
+ out.push(pseudo[i], pseudo[i + 1], pseudo[i + 2]);
3147
3241
  }
3148
3242
  }
3149
- for (let i = 0; i < fields.length; i += 2) {
3150
- out.push(fields[i], fields[i + 1]);
3243
+ for (let i = 0; i < fields.length; i += 3) {
3244
+ out.push(fields[i], fields[i + 1], fields[i + 2]);
3151
3245
  }
3152
3246
  return out;
3153
3247
  }
package/lib/headers.js CHANGED
@@ -226,6 +226,27 @@ function hasBadValue(value) {
226
226
  return badValue.test(value);
227
227
  }
228
228
 
229
+ // The names of the field lines that arrived as never-indexed HPACK literals,
230
+ // one entry per delivered line, duplicates and arrival order kept. node hangs
231
+ // this under its `sensitiveHeaders` symbol on every inbound header object --
232
+ // stream headers, compat req.headers, trailers, client responses -- and always
233
+ // allocates a fresh array, [] when no line was never-indexed, so the
234
+ // application may mutate it without touching any other request's list
235
+ // (toHeaderObject in internal/util.js, measured on node v26.12.1). `flags` is
236
+ // the decoder's neverFlags for the same block: null means there was none, and
237
+ // the whole walk is skipped.
238
+ function neverNames(fields, count, flags) {
239
+ const names = [];
240
+ if (flags !== null) {
241
+ for (let i = 0; i < count; i += 2) {
242
+ if (flags[i >> 1] === 1) {
243
+ names.push(fields[i]);
244
+ }
245
+ }
246
+ }
247
+ return names;
248
+ }
249
+
229
250
  function httpError(code, msg) {
230
251
  const err = new Error(msg);
231
252
  err.code = code;
@@ -281,12 +302,12 @@ function refreshDate() {
281
302
  // need no iterator.
282
303
  // `info` marks the informational (1xx) block that additionalHeaders() sends: node applies
283
304
  // a different status rule and different error codes there, and it generates no date for it.
284
- function parse(headers, sensitive,
305
+ function parse(headers,
285
306
  { trailer = false, sendDate = false, info = false, strict = true, request = false } = {}) {
286
307
  if (headers === null || typeof headers !== 'object') {
287
308
  throw httpError('ERR_HTTP2_INVALID_HEADER_VALUE', 'headers must be an object');
288
309
  }
289
- const neverSet = sensitive || headers[KSensitiveHeaders];
310
+ const neverSet = headers[KSensitiveHeaders];
290
311
  // Inserted in one loop, not new Set(neverSet.map(...)): the map's intermediate array
291
312
  // and its closure are one allocation each per sensitive response, measured at
292
313
  // bench/micro/parse-singles.js.
@@ -455,7 +476,7 @@ function parse(headers, sensitive,
455
476
  } else {
456
477
  status = status || 200;
457
478
  if (status < 200 || status > 599) {
458
- throw httpError('ERR_HTTP2_STATUS_INVALID', `invalid status code: ${status}`);
479
+ throw httpError('ERR_HTTP2_STATUS_INVALID', `Invalid status code: ${status}`);
459
480
  }
460
481
  pseudo.push(':status', String(status));
461
482
  }
@@ -506,7 +527,7 @@ const sent = { status: 0, date: undefined, bound: 0 };
506
527
  // the table's epoch: cached bytes may name an index that a later insertion moved.
507
528
  // The returned Buffer is a view into a shared scratch block: valid until the
508
529
  // next call to block(), which is why every caller copies it immediately.
509
- function block(headers, sensitive, opts, session) {
530
+ function block(headers, opts, session) {
510
531
  const sendDate = !!(opts && opts.sendDate);
511
532
  const enc = session._encoder;
512
533
  // The second the generated date belongs to, or -1 when the block carries none. A
@@ -527,7 +548,7 @@ function block(headers, sensitive, opts, session) {
527
548
  }
528
549
 
529
550
  const update = enc.takeSizeUpdate();
530
- const parsed = parse(headers, sensitive,
551
+ const parsed = parse(headers,
531
552
  { trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
532
553
  strict: session._strictSingle });
533
554
  sent.status = parsed.status;
@@ -613,10 +634,10 @@ function encodeAt(parsed, enc, at) {
613
634
  // so the block shares the connection's dynamic table with every other outbound block.
614
635
  // Returns a view into the shared scratch, valid until the next encode call -- the
615
636
  // caller (session.sendPushPromise) copies it into the frame immediately.
616
- function pushBlock(headers, sensitive, session) {
637
+ function pushBlock(headers, session) {
617
638
  const enc = session._encoder;
618
639
  const update = enc.takeSizeUpdate();
619
- const parsed = parse(headers, sensitive, { request: true, strict: session._strictSingle });
640
+ const parsed = parse(headers, { request: true, strict: session._strictSingle });
620
641
  const at = update === null ? 0 : update.length;
621
642
  const end = encodeAt(parsed, enc, at);
622
643
  if (at !== 0) {
@@ -761,5 +782,5 @@ function requestView(src, scheme, authority) {
761
782
  }
762
783
 
763
784
  module.exports = { block, pushBlock, encode, parse, nullProto, isFieldName, trimName, lowerName,
764
- nameCacheSize, hasBadValue, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
785
+ nameCacheSize, hasBadValue, neverNames, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS,
765
786
  sent, responseView, requestView };
package/lib/hpack.js CHANGED
@@ -526,6 +526,33 @@ class Decoder {
526
526
  this.epoch = 0; // bumped on every table change (see §4.4, 4.3)
527
527
  this.names = new Map(); // interned field names
528
528
  this.scratch = Buffer.allocUnsafe(4096);
529
+ // The never-indexed markers of the block being decoded, one slot per field
530
+ // line (slot i = the line at pair index 2i), kept beside the field list
531
+ // because the decoder itself must not touch the caller's output shape: a
532
+ // plain array and a FlatList both arrive here. node's toHeaderObject hangs
533
+ // the names of the lines that arrived as never-indexed literals (RFC 7541
534
+ // 6.2.3, prefix 0001 -- the N bit exists only on the two literal forms) on
535
+ // the header object under its `sensitiveHeaders` symbol, one entry per
536
+ // delivered line, duplicates and arrival order kept (measured on node
537
+ // v26.12.1). The array is the decoder's own: a caller that retains the
538
+ // flags past the next decode copies them, because reset() clears in place.
539
+ this.neverFlags = null;
540
+ this.neverCount = 0;
541
+ }
542
+
543
+ // Record one never-indexed line. Grown on demand and cleared in bulk at the
544
+ // start of the next decode: the common block has none, so the array is
545
+ // usually never allocated at all.
546
+ neverLine(line) {
547
+ if (this.neverFlags === null) {
548
+ this.neverFlags = new Uint8Array(16);
549
+ } else if (line >= this.neverFlags.length) {
550
+ const grown = new Uint8Array(this.neverFlags.length * 2);
551
+ grown.set(this.neverFlags);
552
+ this.neverFlags = grown;
553
+ }
554
+ this.neverFlags[line] = 1;
555
+ this.neverCount++;
529
556
  }
530
557
 
531
558
  // HPACK index (1-based, newest dynamic entry first) -> [name, value]
@@ -594,6 +621,10 @@ class Decoder {
594
621
  if (scratch) {
595
622
  out.reset();
596
623
  }
624
+ if (this.neverCount > 0) {
625
+ this.neverFlags.fill(0);
626
+ this.neverCount = 0;
627
+ }
597
628
  let n = 0;
598
629
  let p = start;
599
630
  while (p < end) {
@@ -631,6 +662,9 @@ class Decoder {
631
662
  this.setMaxSize(RI);
632
663
  p = RP;
633
664
  } else { // 0000/0001 literal
665
+ if ((b & 0x10) !== 0) {
666
+ this.neverLine(n >> 1);
667
+ }
634
668
  p = this.literal(buf, p, end, out, scratch, n, 4);
635
669
  n += 2;
636
670
  }
@@ -709,6 +743,10 @@ class Decoder {
709
743
  if (scratch) {
710
744
  out.reset();
711
745
  }
746
+ if (this.neverCount > 0) {
747
+ this.neverFlags.fill(0);
748
+ this.neverCount = 0;
749
+ }
712
750
  let n = 0;
713
751
  const cur = new PieceCursor(pieces, start, end);
714
752
  while (cur.left > 0) {
@@ -739,6 +777,9 @@ class Decoder {
739
777
  cur.readInt(5);
740
778
  this.setMaxSize(cur.value);
741
779
  } else { // 0000/0001 literal
780
+ if ((b & 0x10) !== 0) {
781
+ this.neverLine(n >> 1);
782
+ }
742
783
  n = this.literalPieces(cur, out, scratch, n, 4);
743
784
  }
744
785
  }
@@ -1141,6 +1182,21 @@ class Encoder {
1141
1182
  this.remember(name, value, neverIndex, bytes);
1142
1183
  return this.put(dst, off, bytes);
1143
1184
  }
1185
+ // nghttp2's entropy rule (lib/nghttp2_hd.c deflate_nv): authorization always,
1186
+ // and a cookie whose value is under 20 octets with it, go out never-indexed
1187
+ // even when the application marked nothing -- "the same criteria used in
1188
+ // Firefox codebase". A node peer reports such lines under its
1189
+ // sensitiveHeaders symbol on arrival, so the forms have to match for that
1190
+ // list to agree across stacks (test/upstream/sensitive-headers.test.js).
1191
+ // An exact table match still took the indexed reference above -- nghttp2
1192
+ // keeps name_value_match under NEVER_INDEXING, and an indexed octet
1193
+ // remembers nothing anyway.
1194
+ if (name === 'authorization' || (name === 'cookie' &&
1195
+ Buffer.byteLength(value) < 20)) {
1196
+ const bytes = this.literal(name, value, 0x10);
1197
+ this.remember(name, value, neverIndex, bytes);
1198
+ return this.put(dst, off, bytes);
1199
+ }
1144
1200
  // Its name can never reach the table -- insert() is only reached past this line --
1145
1201
  // so the bytes reference nothing but static indices and are safe to cache despite
1146
1202
  // being literals.