h2lean 0.8.0 → 0.8.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.
@@ -123,7 +123,8 @@ const FlatList = require('./flat-list');
123
123
  // list: the two never meet the same fill, and coupling the client module to the
124
124
  // server's scratch would buy nothing.
125
125
  const FLAT = new FlatList(64);
126
- const { nullProto, isFieldName, lowerName, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS } = require('./headers');
126
+ const { nullProto, isFieldName, lowerName, neverNames, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS,
127
+ KSensitiveHeaders } = require('./headers');
127
128
  const { ClientHttp2Stream, AbortError } = require('./client-stream');
128
129
  const { pieceByteAt, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
129
130
  // The settings tables, defaults and buffer-path constants are the server's, reused
@@ -2019,6 +2020,7 @@ class ClientHttp2Session extends EventEmitter {
2019
2020
  }
2020
2021
  const fields = flat.items;
2021
2022
  const fcount = flat.count;
2023
+ const flags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
2022
2024
  // maxReservedRemoteStreams: a pushed stream stays reserved until its
2023
2025
  // response HEADERS arrive (measured: the count releases on the HEADERS,
2024
2026
  // not on 'end'). Over the cap the promise is refused with RST_STREAM(CANCEL)
@@ -2040,7 +2042,7 @@ class ClientHttp2Session extends EventEmitter {
2040
2042
  this._frame(RST_STREAM, 0, promisedSid, body, true);
2041
2043
  return;
2042
2044
  }
2043
- const headers = this._toObject(fields, fcount);
2045
+ const headers = this._toObject(fields, fcount, neverNames(fields, fcount, flags));
2044
2046
  // Create the pushed stream. Its writable half is pre-ended: the client never
2045
2047
  // sends a request body for a push (node: writableEnded=true, writableFinished=true
2046
2048
  // at the 'stream' event, measured v26.12.1).
@@ -2118,9 +2120,14 @@ class ClientHttp2Session extends EventEmitter {
2118
2120
  const hit = shapes === null ? undefined : shapes.get(slot);
2119
2121
  // where this response's field list comes from: the scratch's backing array
2120
2122
  // 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)
2123
+ // valid length travels next to the list (same as the server session).
2124
+ // blockFlags is the never-indexed marker of the same lines: the decoder's
2125
+ // list on a decode, the cached snapshot on a hit, null when there was none
2126
+ // -- neverNames() reads it where _toObject hangs the `sensitiveHeaders`
2127
+ // symbol node puts on every inbound header object.
2122
2128
  let fields;
2123
2129
  let fcount;
2130
+ let blockFlags;
2124
2131
  try {
2125
2132
  if (hit !== undefined && (block !== null
2126
2133
  ? hit.block.equals(block)
@@ -2128,6 +2135,7 @@ class ClientHttp2Session extends EventEmitter {
2128
2135
  this._shapeHits++;
2129
2136
  fields = hit.flat;
2130
2137
  fcount = fields.length;
2138
+ blockFlags = hit.flags;
2131
2139
  } else {
2132
2140
  if (block !== null) {
2133
2141
  this._decoder.decode(block, 0, block.length, flat);
@@ -2136,6 +2144,7 @@ class ClientHttp2Session extends EventEmitter {
2136
2144
  }
2137
2145
  fields = flat.items;
2138
2146
  fcount = flat.count;
2147
+ blockFlags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
2139
2148
  if (shapes !== null) {
2140
2149
  if (++this._shapeMisses > 64 && this._shapeHits * 4 < this._shapeMisses) {
2141
2150
  this._shapes = null;
@@ -2145,7 +2154,10 @@ class ClientHttp2Session extends EventEmitter {
2145
2154
  }
2146
2155
  shapes.set(slot, {
2147
2156
  block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
2148
- flat: fields.slice(0, fcount)
2157
+ flat: fields.slice(0, fcount),
2158
+ // a snapshot copy, not the decoder's live array: the next decode
2159
+ // clears the array in place, and this entry must survive it
2160
+ flags: blockFlags !== null ? blockFlags.slice() : null
2149
2161
  });
2150
2162
  }
2151
2163
  }
@@ -2183,7 +2195,7 @@ class ClientHttp2Session extends EventEmitter {
2183
2195
  if (this._refuseHeaders(sid, stream, fields, fcount)) {
2184
2196
  return;
2185
2197
  }
2186
- const headers = this._toObject(fields, fcount);
2198
+ const headers = this._toObject(fields, fcount, neverNames(fields, fcount, blockFlags));
2187
2199
  // RFC 8336 3.2 + core.js emitStreamEvent: a 421 (Misdirected Request) answer
2188
2200
  // deletes the origin the request carried from originSet, and it does so here --
2189
2201
  // before 'response' is scheduled, so a handler that reads `session.originSet`
@@ -2249,8 +2261,15 @@ class ClientHttp2Session extends EventEmitter {
2249
2261
  return true;
2250
2262
  }
2251
2263
 
2252
- _toObject(fields, count) {
2264
+ // node's toHeaderObject semantics for a response (or a trailer block or a
2265
+ // push promise): null prototype, :status coerced to a number, set-cookie
2266
+ // always an array, repeats joined -- and the never-indexed names under the
2267
+ // `sensitiveHeaders` symbol, assigned first so the shape tree of these
2268
+ // objects shares one symbol transition (the server side explains why in
2269
+ // session.js _toObject).
2270
+ _toObject(fields, count, sensitive) {
2253
2271
  const o = nullProto();
2272
+ o[KSensitiveHeaders] = sensitive;
2254
2273
  for (let i = 0; i < count; i += 2) {
2255
2274
  const name = fields[i];
2256
2275
  let value = fields[i + 1];
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;
@@ -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
  }
package/lib/session.js CHANGED
@@ -37,7 +37,7 @@ const FlatList = require('./flat-list');
37
37
  // shrink back to the preallocation.
38
38
  const FLAT = new FlatList(64);
39
39
  const { pieceCopyTo, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
40
- const { nullProto, hasBadValue, SINGLE_VALUE_FIELDS } = require('./headers');
40
+ const { nullProto, hasBadValue, neverNames, SINGLE_VALUE_FIELDS, KSensitiveHeaders } = require('./headers');
41
41
 
42
42
  // The socket's ownership mark, shared with the client session (node keeps it as
43
43
  // socket[kBoundSession] in the one Http2Session base constructor both kinds run
@@ -2113,14 +2113,17 @@ class Http2Session extends EventEmitter {
2113
2113
  // The frozen headers object for one shape. Object.freeze is shallow and
2114
2114
  // node's toHeaderObject puts set-cookie in an array, so that array has to be
2115
2115
  // frozen too -- otherwise push() would corrupt every other request sharing it.
2116
- _sharedObject(fields, count) {
2117
- const o = this._toObject(fields, count);
2116
+ // The sensitive list under the symbol is an array of the same kind, and
2117
+ // Object.keys skips symbol keys, so it is frozen here explicitly.
2118
+ _sharedObject(fields, count, sensitive) {
2119
+ const o = this._toObject(fields, count, sensitive);
2118
2120
  for (let i = 0; i < count; i += 2) {
2119
2121
  const v = o[fields[i]];
2120
2122
  if (Array.isArray(v)) {
2121
2123
  Object.freeze(v);
2122
2124
  }
2123
2125
  }
2126
+ Object.freeze(o[KSensitiveHeaders]);
2124
2127
  return Object.freeze(o);
2125
2128
  }
2126
2129
 
@@ -2129,7 +2132,15 @@ class Http2Session extends EventEmitter {
2129
2132
  // refused, or { fields } when only fields with illegal names were dropped. See the
2130
2133
  // block comment on the constants above for why each rule exists and where the
2131
2134
  // behaviour came from.
2132
- _requestProblem(src, count, trailer) {
2135
+ //
2136
+ // `flags` is the decoder's never-indexed marker for the same block, or null. The
2137
+ // dropped lines must not reach the sensitive list either: node's list holds the
2138
+ // names that were DELIVERED, and a field this side refused never arrives at the
2139
+ // application (measured on node v26.12.1 with a never-indexed non-token name --
2140
+ // the list comes back empty). So the kept copy -- which already shifts the pair
2141
+ // indices when it drops a line -- carries a parallel flags array, sliced and
2142
+ // pushed at exactly the sites that copy and push the fields.
2143
+ _requestProblem(src, count, trailer, flags) {
2133
2144
  let method = null, path = null, scheme = null, authority = null, host = null;
2134
2145
  let protocol = null, contentLength = null;
2135
2146
  // RFC 9113 8.1: pseudo-header fields, if any, come before all regular fields. Once
@@ -2137,6 +2148,7 @@ class Http2Session extends EventEmitter {
2137
2148
  // no pseudo-header is allowed at all, so the flag starts in that state.
2138
2149
  let pseudoAllowed = !trailer;
2139
2150
  let kept = null; // copied list, only if something is dropped
2151
+ let keptFlags = null; // the never-indexed markers of the kept lines, only with flags
2140
2152
 
2141
2153
  for (let i = 0; i < count; i += 2) {
2142
2154
  const name = src[i];
@@ -2214,6 +2226,9 @@ class Http2Session extends EventEmitter {
2214
2226
  // Illegal character in a regular name: drop the field, keep the stream.
2215
2227
  if (kept === null) {
2216
2228
  kept = src.slice(0, i);
2229
+ if (flags !== null) {
2230
+ keptFlags = Array.from(flags.subarray(0, i >> 1));
2231
+ }
2217
2232
  }
2218
2233
  continue;
2219
2234
  }
@@ -2240,6 +2255,9 @@ class Http2Session extends EventEmitter {
2240
2255
  if (!AUTHORITY_RE.test(value)) {
2241
2256
  if (kept === null) {
2242
2257
  kept = src.slice(0, i);
2258
+ if (flags !== null) {
2259
+ keptFlags = Array.from(flags.subarray(0, i >> 1));
2260
+ }
2243
2261
  } continue;
2244
2262
  }
2245
2263
  if (host !== null) {
@@ -2256,16 +2274,22 @@ class Http2Session extends EventEmitter {
2256
2274
  if (hasBadValue(value)) {
2257
2275
  if (kept === null) {
2258
2276
  kept = src.slice(0, i);
2277
+ if (flags !== null) {
2278
+ keptFlags = Array.from(flags.subarray(0, i >> 1));
2279
+ }
2259
2280
  }
2260
2281
  continue;
2261
2282
  }
2262
2283
  if (kept !== null) {
2263
2284
  kept.push(name, value);
2285
+ if (keptFlags !== null) {
2286
+ keptFlags.push(flags[i >> 1]);
2287
+ }
2264
2288
  }
2265
2289
  }
2266
2290
 
2267
2291
  if (trailer) {
2268
- return kept === null ? null : { fields: kept };
2292
+ return kept === null ? null : { fields: kept, neverFlags: keptFlags };
2269
2293
  }
2270
2294
 
2271
2295
  if (method === null) {
@@ -2280,7 +2304,7 @@ class Http2Session extends EventEmitter {
2280
2304
  if (authority === null) {
2281
2305
  return { problem: 'CONNECT without :authority' };
2282
2306
  }
2283
- return kept === null ? null : { fields: kept };
2307
+ return kept === null ? null : { fields: kept, neverFlags: keptFlags };
2284
2308
  }
2285
2309
  if (protocol !== null && method !== 'CONNECT') {
2286
2310
  return { problem: ':protocol requires CONNECT' };
@@ -2303,7 +2327,7 @@ class Http2Session extends EventEmitter {
2303
2327
  && !(method === 'OPTIONS' && path === '*')) {
2304
2328
  return { problem: `:path ${path} does not start with "/"` };
2305
2329
  }
2306
- return kept === null ? null : { fields: kept };
2330
+ return kept === null ? null : { fields: kept, neverFlags: keptFlags };
2307
2331
  }
2308
2332
 
2309
2333
  // A header block that arrived whole and one that arrived in pieces are the
@@ -2335,6 +2359,12 @@ class Http2Session extends EventEmitter {
2335
2359
  let bytes = 0;
2336
2360
  // verdict of the inbound validator: null means the fields may go to the application
2337
2361
  let verdict;
2362
+ // the never-indexed markers of the DELIVERED lines: the decoder's list when
2363
+ // nothing was dropped, the verdict's parallel copy when it filtered the
2364
+ // fields, the cached snapshot on a shape hit -- null when no line was
2365
+ // never-indexed at all. neverNames() turns them into the names array the
2366
+ // header object hangs under node's `sensitiveHeaders` symbol.
2367
+ let blockFlags;
2338
2368
 
2339
2369
  try {
2340
2370
  if (hit !== undefined && (block !== null
@@ -2349,6 +2379,7 @@ class Http2Session extends EventEmitter {
2349
2379
  fcount = fields.length;
2350
2380
  shared = hit.headers;
2351
2381
  verdict = hit.verdict;
2382
+ blockFlags = hit.flags;
2352
2383
  } else {
2353
2384
  if (block !== null) {
2354
2385
  this._decoder.decode(block, 0, block.length, flat);
@@ -2362,10 +2393,14 @@ class Http2Session extends EventEmitter {
2362
2393
  }
2363
2394
  // 'fields' is name, value, name, value: one entry overhead per field line
2364
2395
  bytes += (fcount >> 1) * ENTRY_OVERHEAD;
2365
- verdict = this._requestProblem(fields, fcount, existing !== undefined);
2396
+ const neverFlags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
2397
+ verdict = this._requestProblem(fields, fcount, existing !== undefined, neverFlags);
2366
2398
  if (verdict !== null && verdict.fields !== undefined) {
2367
2399
  fields = verdict.fields;
2368
2400
  fcount = fields.length;
2401
+ blockFlags = verdict.neverFlags;
2402
+ } else {
2403
+ blockFlags = neverFlags;
2369
2404
  }
2370
2405
  if (shapes !== null) {
2371
2406
  // A client that varies one header per request (x-request-id) makes
@@ -2381,10 +2416,14 @@ class Http2Session extends EventEmitter {
2381
2416
  const entry = {
2382
2417
  block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
2383
2418
  flat: fields.slice(0, fcount), bytes, headers: null,
2384
- verdict
2419
+ verdict,
2420
+ // a snapshot copy, not the decoder's live array: the next decode
2421
+ // clears the array in place, and this entry must survive it
2422
+ flags: blockFlags !== null ? blockFlags.slice() : null
2385
2423
  };
2386
2424
  if (this._share) {
2387
- entry.headers = this._sharedObject(fields, fcount);
2425
+ entry.headers = this._sharedObject(fields, fcount,
2426
+ neverNames(fields, fcount, blockFlags));
2388
2427
  shared = entry.headers;
2389
2428
  }
2390
2429
  shapes.set(slot, entry);
@@ -2486,7 +2525,8 @@ class Http2Session extends EventEmitter {
2486
2525
  return;
2487
2526
  }
2488
2527
  const trailers = this._share
2489
- ? this._sharedObject(fields, fcount) : this._toObject(fields, fcount);
2528
+ ? this._sharedObject(fields, fcount, neverNames(fields, fcount, blockFlags))
2529
+ : this._toObject(fields, fcount, neverNames(fields, fcount, blockFlags));
2490
2530
  existing.emit('trailers', trailers);
2491
2531
  if (end) {
2492
2532
  existing._remoteEnded();
@@ -2544,8 +2584,10 @@ class Http2Session extends EventEmitter {
2544
2584
  }
2545
2585
 
2546
2586
  const headers = shared !== null ? shared
2547
- : (this._share ? Object.freeze(this._toObject(fields, fcount))
2548
- : this._toObject(fields, fcount));
2587
+ : (this._share
2588
+ ? Object.freeze(this._toObject(fields, fcount,
2589
+ neverNames(fields, fcount, blockFlags)))
2590
+ : this._toObject(fields, fcount, neverNames(fields, fcount, blockFlags)));
2549
2591
  // The one shape in which the content-length promise is already broken before a single body
2550
2592
  // octet could have arrived: the field promises a body and the HEADERS frame ends the message.
2551
2593
  // Refusing at the accept decision -- the same place the parse failures and the size limits are
@@ -2596,13 +2638,19 @@ class Http2Session extends EventEmitter {
2596
2638
  // node's toHeaderObject semantics: null prototype, :status coerced to a
2597
2639
  // number, set-cookie always an array, repeats joined -- except the fields in
2598
2640
  // SINGLE_VALUE_FIELDS, of which the first copy is kept and the rest ignored.
2641
+ // The never-indexed names under node's `sensitiveHeaders` symbol are part of
2642
+ // that surface; the assignment comes FIRST, so the whole shape tree of these
2643
+ // objects shares one symbol transition -- the string-key transitions then all
2644
+ // hang off the one map the symbol creates, and the peer-driven name space
2645
+ // below it grows no larger than before (the §8 transition-tree bound).
2599
2646
  //
2600
2647
  // The map is a fast-properties object with a null prototype rather than a
2601
2648
  // { __proto__: null } literal: the literal is in V8's dictionary mode, and this
2602
2649
  // map is built once per request and read once per request by the application --
2603
2650
  // in the stream path and in the compat path alike (OPTIMIZATION.md item 4).
2604
- _toObject(fields, count) {
2651
+ _toObject(fields, count, sensitive) {
2605
2652
  const o = nullProto();
2653
+ o[KSensitiveHeaders] = sensitive;
2606
2654
  for (let i = 0; i < count; i += 2) {
2607
2655
  const name = fields[i];
2608
2656
  let value = fields[i + 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [