rwebtransport 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -65,9 +65,70 @@ impl Representation {
65
65
  }
66
66
  }
67
67
 
68
+ /// Helper for tracking decoded field list sizes.
69
+ ///
70
+ /// The size of a field list is calculated based on the uncompressed size of
71
+ /// fields, including the length of the name and value in bytes plus an overhead
72
+ /// of 32 bytes for each field. See
73
+ /// <https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2>
74
+ struct FieldListSizeTracker {
75
+ remaining: u64,
76
+ }
77
+
78
+ impl FieldListSizeTracker {
79
+ /// Initialize tracker with the maximum field list size.
80
+ ///
81
+ /// The `max_size` parameter is the maximum size in bytes of the full
82
+ /// decoded field list. See
83
+ /// <https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2>
84
+ fn new(max_size: u64) -> Self {
85
+ Self {
86
+ remaining: max_size,
87
+ }
88
+ }
89
+
90
+ /// Mark the start of parsing a new field.
91
+ ///
92
+ /// Must be called when a new field is ready to be parsed.
93
+ fn on_field_start(&mut self) -> Result<()> {
94
+ // Each complete field has a 32-byte overhead, so subtract that first.
95
+ self.remaining = self
96
+ .remaining
97
+ .checked_sub(32)
98
+ .ok_or(Error::HeaderListTooLarge)?;
99
+
100
+ Ok(())
101
+ }
102
+
103
+ /// Marks when a field part (either name or value) has been decoded.
104
+ ///
105
+ /// The `len` parameter is the size in bytes of the decoded part.
106
+ ///
107
+ /// Must be called when a new field part has been decoded.
108
+ fn on_field_part_decoded(&mut self, len: u64) -> Result<()> {
109
+ self.remaining = self
110
+ .remaining
111
+ .checked_sub(len)
112
+ .ok_or(Error::HeaderListTooLarge)?;
113
+
114
+ Ok(())
115
+ }
116
+
117
+ /// The remaining number of bytes in the tracker.
118
+ fn left(&self) -> u64 {
119
+ self.remaining
120
+ }
121
+ }
122
+
68
123
  /// A QPACK decoder.
69
124
  #[derive(Default)]
70
- pub struct Decoder {}
125
+ pub struct Decoder {
126
+ // TODO: hold a dynamic table whose maximum capacity is the
127
+ // SETTINGS_QPACK_MAX_TABLE_CAPACITY value advertised to the peer.
128
+ //
129
+ // TODO: queue decoder instructions (RFC 9204, Section 4.4) for the
130
+ // connection to send on the decoder stream.
131
+ }
71
132
 
72
133
  impl Decoder {
73
134
  /// Creates a new QPACK decoder.
@@ -77,7 +138,12 @@ impl Decoder {
77
138
 
78
139
  /// Processes control instructions from the encoder.
79
140
  pub fn control(&mut self, _buf: &mut [u8]) -> Result<()> {
80
- // TODO: process control instructions
141
+ // TODO: parse encoder instructions (RFC 9204, Section 4.3), including
142
+ // ones split across calls, and apply them to the dynamic table. Their
143
+ // errors are connection errors of type QPACK_ENCODER_STREAM_ERROR.
144
+ // TODO: if the advertised maximum table capacity is zero, reject
145
+ // all encoder instructions (even Set Dynamic Table Capacity 0) before
146
+ // dispatching them to the table (RFC 9204, Section 3.2.3).
81
147
  Ok(())
82
148
  }
83
149
 
@@ -87,16 +153,23 @@ impl Decoder {
87
153
 
88
154
  let mut out = Vec::new();
89
155
 
90
- let mut left = max_size;
156
+ let mut size_tracker = FieldListSizeTracker::new(max_size);
91
157
 
92
158
  let req_insert_count = decode_int(&mut b, 8)?;
93
159
  let base = decode_int(&mut b, 7)?;
94
160
 
161
+ // TODO: decode the Required Insert Count and Base (RFC 9204, Section
162
+ // 4.5.1). With SETTINGS_QPACK_BLOCKED_STREAMS set to 0, a Required
163
+ // Insert Count above the dynamic table's insert count is a connection
164
+ // error of type QPACK_DECOMPRESSION_FAILED (RFC 9204, Section 2.1.2).
165
+
95
166
  trace!("Header count={req_insert_count} base={base}");
96
167
 
97
168
  while b.cap() > 0 {
98
169
  let first = b.peek_u8()?;
99
170
 
171
+ size_tracker.on_field_start()?;
172
+
100
173
  match Representation::from_byte(first) {
101
174
  Representation::Indexed => {
102
175
  const STATIC: u8 = 0x40;
@@ -113,13 +186,11 @@ impl Decoder {
113
186
 
114
187
  let (name, value) = lookup_static(index)?;
115
188
 
116
- left = left
117
- .checked_sub((name.len() + value.len()) as u64)
118
- .ok_or(Error::HeaderListTooLarge)?;
189
+ size_tracker.on_field_part_decoded((name.len() + value.len()) as u64)?;
119
190
 
120
191
  let hdr = Header::new(name, value);
121
192
  out.push(hdr);
122
- },
193
+ }
123
194
 
124
195
  Representation::IndexedWithPostBase => {
125
196
  let index = decode_int(&mut b, 4)?;
@@ -128,7 +199,7 @@ impl Decoder {
128
199
 
129
200
  // TODO: implement dynamic table
130
201
  return Err(Error::InvalidHeaderValue);
131
- },
202
+ }
132
203
 
133
204
  Representation::Literal => {
134
205
  let name_huff = b.as_ref()[0] & 0x08 == 0x08;
@@ -137,63 +208,63 @@ impl Decoder {
137
208
  let mut name = b.get_bytes(name_len)?;
138
209
 
139
210
  let name = if name_huff {
140
- name.get_huffman_decoded()?
211
+ name.get_huffman_decoded_with_max_length(size_tracker.left() as usize)
212
+ .map_err(|_| Error::HeaderListTooLarge)?
141
213
  } else {
214
+ if name_len > size_tracker.left() as usize {
215
+ return Err(Error::HeaderListTooLarge);
216
+ }
142
217
  name.to_vec()
143
218
  };
144
219
 
145
- let name = name.to_vec();
146
- let value = decode_str(&mut b)?;
220
+ size_tracker.on_field_part_decoded(name.len() as u64)?;
221
+
222
+ let value = decode_str(&mut b, size_tracker.left() as usize)?;
147
223
 
148
- trace!(
149
- "Literal Without Name Reference name={name:?} value={value:?}",
150
- );
224
+ trace!("Literal Without Name Reference name={name:?} value={value:?}",);
151
225
 
152
- left = left
153
- .checked_sub((name.len() + value.len()) as u64)
154
- .ok_or(Error::HeaderListTooLarge)?;
226
+ size_tracker.on_field_part_decoded(value.len() as u64)?;
155
227
 
156
228
  // Instead of calling Header::new(), create Header directly
157
- // from `name` and `value`, which are already String.
229
+ // from `name` and `value`.
158
230
  let hdr = Header(name, value);
159
231
  out.push(hdr);
160
- },
232
+ }
161
233
 
162
234
  Representation::LiteralWithNameRef => {
163
235
  const STATIC: u8 = 0x10;
164
236
 
165
237
  let s = first & STATIC == STATIC;
166
- let name_idx = decode_int(&mut b, 4)?;
167
- let value = decode_str(&mut b)?;
168
-
169
- trace!(
170
- "Literal name_idx={name_idx} static={s} value={value:?}"
171
- );
172
238
 
173
239
  if !s {
174
240
  // TODO: implement dynamic table
175
241
  return Err(Error::InvalidHeaderValue);
176
242
  }
177
243
 
244
+ let name_idx = decode_int(&mut b, 4)?;
245
+
178
246
  let (name, _) = lookup_static(name_idx)?;
179
247
 
180
- left = left
181
- .checked_sub((name.len() + value.len()) as u64)
182
- .ok_or(Error::HeaderListTooLarge)?;
248
+ size_tracker.on_field_part_decoded(name.len() as u64)?;
249
+
250
+ let value = decode_str(&mut b, size_tracker.left() as usize)?;
251
+
252
+ trace!("Literal name_idx={name_idx} static={s} value={value:?}");
253
+
254
+ size_tracker.on_field_part_decoded(value.len() as u64)?;
183
255
 
184
256
  // Instead of calling Header::new(), create Header directly
185
- // from `value`, which is already String, but clone `name`
186
- // as it is just a reference.
257
+ // from `value`, but clone `name` as it is just a reference.
187
258
  let hdr = Header(name.to_vec(), value);
188
259
  out.push(hdr);
189
- },
260
+ }
190
261
 
191
262
  Representation::LiteralWithPostBase => {
192
263
  trace!("Literal With Post Base");
193
264
 
194
265
  // TODO: implement dynamic table
195
266
  return Err(Error::InvalidHeaderValue);
196
- },
267
+ }
197
268
  }
198
269
  }
199
270
 
@@ -240,7 +311,7 @@ fn decode_int(b: &mut octets::Octets, prefix: usize) -> Result<u64> {
240
311
  Err(Error::BufferTooShort)
241
312
  }
242
313
 
243
- fn decode_str(b: &mut octets::Octets) -> Result<Vec<u8>> {
314
+ fn decode_str(b: &mut octets::Octets, max_len: usize) -> Result<Vec<u8>> {
244
315
  let first = b.peek_u8()?;
245
316
 
246
317
  let huff = first & 0x80 == 0x80;
@@ -250,8 +321,12 @@ fn decode_str(b: &mut octets::Octets) -> Result<Vec<u8>> {
250
321
  let mut val = b.get_bytes(len)?;
251
322
 
252
323
  let val = if huff {
253
- val.get_huffman_decoded()?
324
+ val.get_huffman_decoded_with_max_length(max_len)
325
+ .map_err(|_| Error::HeaderListTooLarge)?
254
326
  } else {
327
+ if len > max_len {
328
+ return Err(Error::HeaderListTooLarge);
329
+ }
255
330
  val.to_vec()
256
331
  };
257
332
 
@@ -285,4 +360,143 @@ mod tests {
285
360
 
286
361
  assert_eq!(decode_int(&mut b, 8), Ok(42));
287
362
  }
363
+
364
+ /// LiteralWithNameRef with the dynamic table flag (S=0) must be
365
+ /// rejected since dynamic table references are not decoded yet.
366
+ #[test]
367
+ fn literal_with_name_ref_dynamic_table_rejected() {
368
+ let mut dec = Decoder::new();
369
+
370
+ // QPACK header block:
371
+ // [0x00, 0x00] required insert count=0, base=0
372
+ // [0x40] LiteralWithNameRef (0b0100_0000), S=0 (dynamic),
373
+ // name_idx=0
374
+ // [0x01, 0x61] value: non-huffman, length=1, 'a'
375
+ let encoded = [0x00, 0x00, 0x40, 0x01, 0x61];
376
+
377
+ assert_eq!(
378
+ dec.decode(&encoded, u64::MAX),
379
+ Err(Error::InvalidHeaderValue),
380
+ );
381
+ }
382
+
383
+ /// Non-Huffman value in decode_str must be rejected before allocation
384
+ /// when it exceeds the remaining budget (max_len).
385
+ ///
386
+ /// Uses a LiteralWithNameRef with static `:authority` (index 0, 10
387
+ /// bytes) and a non-Huffman value of 5 bytes. Budget is set so only
388
+ /// 4 bytes remain for the value after the name is charged.
389
+ #[test]
390
+ fn non_huffman_value_exceeding_budget_rejected() {
391
+ // QPACK header block:
392
+ // [0x00, 0x00] required insert count=0, base=0
393
+ // [0x50] LiteralWithNameRef, S=1 (static), name_idx=0
394
+ // (:authority, 10 bytes)
395
+ // [0x05] value: non-huffman (bit 7=0), length=5
396
+ // "abcde" value bytes
397
+ let encoded = [0x00, 0x00, 0x50, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65];
398
+
399
+ // Exact fit: 32 (overhead) + 10 (name) + 5 (value) = 47.
400
+ assert!(Decoder::new().decode(&encoded, 47).is_ok());
401
+
402
+ // One byte too small: budget = 46.
403
+ // After overhead (32) and name (10): 4 bytes remain, but
404
+ // value is 5 bytes → rejected.
405
+ assert_eq!(
406
+ Decoder::new().decode(&encoded, 46),
407
+ Err(Error::HeaderListTooLarge),
408
+ );
409
+ }
410
+
411
+ /// Non-Huffman name in the Literal arm must be rejected before
412
+ /// allocation when it exceeds the remaining budget.
413
+ ///
414
+ /// Uses a Literal with a 10-byte non-Huffman name and a 1-byte
415
+ /// non-Huffman value. Budget is set so only 9 bytes remain for the
416
+ /// name after overhead.
417
+ #[test]
418
+ fn non_huffman_name_exceeding_budget_rejected() {
419
+ // QPACK header block:
420
+ // [0x00, 0x00] required insert count=0, base=0
421
+ // [0x27, 0x03] Literal (0b0010_0000), N=0, H=0 (no huffman),
422
+ // name_len=10 (3-bit prefix 0b111 + overflow 0x03)
423
+ // "x-custom99" name bytes (10 bytes)
424
+ // [0x01, 0x61] value: non-huffman, length=1, 'a'
425
+ let encoded = [
426
+ 0x00, 0x00, // header block prefix
427
+ 0x27, 0x03, // Literal, name_len=10
428
+ 0x78, 0x2d, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x39, 0x39, // "x-custom99"
429
+ 0x01, 0x61, // value: length=1, 'a'
430
+ ];
431
+
432
+ // Exact fit: 32 (overhead) + 10 (name) + 1 (value) = 43.
433
+ assert!(Decoder::new().decode(&encoded, 43).is_ok());
434
+
435
+ // Two bytes too small: budget = 41.
436
+ // After overhead (32): 9 bytes remain, name is 10 bytes →
437
+ // rejected at name check.
438
+ assert_eq!(
439
+ Decoder::new().decode(&encoded, 41),
440
+ Err(Error::HeaderListTooLarge),
441
+ );
442
+ }
443
+
444
+ /// Verify that both Literal and LiteralWithNameRef charge the name to
445
+ /// the budget *before* Huffman-decoding the value, so the max_len
446
+ /// passed to the Huffman decoder is equally strict in both paths.
447
+ #[test]
448
+ fn literal_with_name_ref_value_budget_ordering() {
449
+ use crate::h3::qpack;
450
+
451
+ // Static table index 0 = `:authority` (10 bytes).
452
+ // We'll encode the same value two ways:
453
+ // 1. LiteralWithNameRef using `:authority` (encoder matches static
454
+ // table)
455
+ // 2. Literal using a custom 10-byte name not in the static table
456
+ //
457
+ // Both have name_len=10, so budget arithmetic is comparable.
458
+ let value = b"aaaaaaaaaaaaaaaa"; // 16 bytes; Huffman compresses to 10
459
+
460
+ // -- Encode as LiteralWithNameRef --
461
+ let headers_nameref = vec![Header::new(b":authority", value)];
462
+ let mut buf = [0u8; 64];
463
+ let mut enc = qpack::Encoder::new();
464
+ let nameref_len = enc.encode(&headers_nameref, &mut buf).unwrap();
465
+ let encoded_nameref = buf[..nameref_len].to_vec();
466
+
467
+ // -- Encode as Literal (name not in static table) --
468
+ let headers_literal = vec![Header::new(b"x-custom99", value)];
469
+ let mut buf = [0u8; 64];
470
+ let mut enc = qpack::Encoder::new();
471
+ let literal_len = enc.encode(&headers_literal, &mut buf).unwrap();
472
+ let encoded_literal = buf[..literal_len].to_vec();
473
+
474
+ // Exact budget: 32 (overhead) + 10 (name) + 16 (value) = 58.
475
+ // Both representations succeed.
476
+ assert_eq!(
477
+ Decoder::new().decode(&encoded_nameref, 58),
478
+ Ok(headers_nameref.clone()),
479
+ );
480
+ assert_eq!(
481
+ Decoder::new().decode(&encoded_literal, 58),
482
+ Ok(headers_literal.clone()),
483
+ );
484
+
485
+ // One byte too small: budget = 57.
486
+ // Total decoded field size = 10 + 16 = 26, overhead = 32,
487
+ // so 32 + 26 = 58 > 57. Both reject with HeaderListTooLarge.
488
+ //
489
+ // Both paths now follow the same budget ordering:
490
+ // 1. name charged first (10 bytes) → remaining = 15
491
+ // 2. decode_str max_len = 15
492
+ // 3. Huffman decode produces 16 bytes > 15 → rejected
493
+ assert_eq!(
494
+ Decoder::new().decode(&encoded_nameref, 57),
495
+ Err(Error::HeaderListTooLarge),
496
+ );
497
+ assert_eq!(
498
+ Decoder::new().decode(&encoded_literal, 57),
499
+ Err(Error::HeaderListTooLarge),
500
+ );
501
+ }
288
502
  }