← Back to Pull requests

fix(h2): review batch 2 — RFC compliance, HPACK/Huffman, Response & drain hardening #55

Open
unknown opened this pull request with 575 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 29, 2026 04:25 UTC
Author

Summary

Implements Batch 2 of the comprehensive code review (code_review_report.md): RFC 9113 / 9110 / 8941 / 9218 compliance, HPACK/Huffman edge-case & OOM hardening, Response / H2Sink state-machine safety, and server/client concurrency & timeout fixes.

Protocol & HPACK (src/proto.zig, src/hpack_codec.zig, src/hpack.zig)


  • Frame & Authority validation (proto.zig):

  • - Assert length <= 0xff_ffff in putHeader.
    - Validate .push_promise (sid == 0, payload.len < 4) and .priority_update (Prioritized Element ID == 0) in validateInboundFrame.
    - Add RFC 3986 §3.2 isValidAuthority() rejecting userinfo (@), path/query/fragment delimiters (/, ?, #, \), CTLs, malformed IPv6 brackets, and invalid/out-of-range ports (> 65535).
    - Enforce RFC 8941 §4.2 / §4.2.4 Structured Fields rules in Priority parser: SP-only outer trimming (rejecting HTAB), integer (<= 15 digits) & decimal (<= 12 int, 1..3 frac) limits, and resetting duplicate "u" / "i" keys to defaults when overwritten with out-of-range or mismatched types.
  • Canonical Huffman & OOM-safe DynamicTable (hpack_codec.zig):

  • - Replace the O(256) linear scan in Huffman decoding (decode, decodeLengthOnly, decodeIntoScratch) with a comptime-verified canonical Huffman lookup table (CanonicalTable, ~450 B in .rodata).
    - Pre-allocate growth buffers in DynamicTable.addWithAllocator before evicting live entries (strong OOM exception safety) and cap min_storage <= maxInt(u32).
  • HPACK Codec & EncoderContext (hpack.zig):

  • - Preserve error.OutOfMemory in Decoder.literal instead of swallowing it as error.Truncated.
    - Fix EncoderContext.configureLocalMax revert-to-dyn.max_size TSU tracking and notePeerHeaderTableSize reduction tracking against prev_peer (RFC 7541 §4.2).
    - Reject invalid :status (< 100, > 599, 101) and suppress explicit/auto content-length on 1xx/204 in EncoderContext.encodeResponse.
    - Support plain RFC 9113 §8.5 CONNECT (valid :authority, omitting :scheme/:path) in encodeRequestEx, and reject connection-specific/te headers in EncoderContext.encodeTrailers.

    Response, Server & Client (src/types.zig, src/server.zig, src/client.zig)


  • Response & H2Sink hardening (types.zig, server.zig):

  • - Give Request safe field defaults (eliminating all 11 .req = undefined initializations in server.zig).
    - Guard Response.header (error.HeadAlreadySent), Response.trailer (error.ResponseFinished), Response.status/setContentLength post-head no-ops, Response.send post-head rejection + pre-mutation validation + 204 CL omission + consistent finish() on fallback sinks.
    - Preserve cork_buf and body_bytes when a sink write fails, and coalesce cork_buf with caller slices in writeVec.
    - Enforce RFC 9110/9113 rules in H2Sink: valid final status (200..599), suppress DATA on HEAD/204/304, omit content-length on 204, and omit trailers on 204/304.
  • Server edge cases, deadlines & wakeups (server.zig):

  • - Make drainTimerThunk wakeable via Connection.drain_wake: Io.Event (signaled on enterDraining, notePeerWaitBegin/End, and worker exit) so a tightened peer-wait deadline never waits behind a long drain_timeout_ms.
    - Enforce a total head_timeout_ms deadline across the entire CONTINUATION frame loop (instead of resetting per frame).
    - Accept single trailing HEADERS(END_STREAM) on early_closed streams cleanly (decode HPACK without spurious RST_STREAM(STREAM_CLOSED)) on both direct and race paths.
    - Replace separate rx_eof_atomic + resp_end_stream_queued Dekker pairs with a single close_flags atomic bitmask (fetchOr(..., .acq_rel)).
    - Skip half-closed (local) streams in applyInitialWindow (RFC 9113 §6.9.2).
    - Enforce isValidAuthority on :authority/host and accept standard RFC 9113 §8.5 CONNECT in buildRequestEx.
  • Client RFC compliance (client.zig):

  • - Reject server-sent PRIORITY_UPDATE frames with GOAWAY(PROTOCOL_ERROR) + teardown (RFC 9218 §7.1).
    - Support plain RFC 9113 §8.5 CONNECT (default RequestHead.path = "", strict isValidAuthority).
    - Skip local_closed streams in applyInitialWindow (RFC 9113 §6.9.2).

    Verification


  • zig test src/lib.zig -lc (Debug + leak detection): 237/237 passed (includes 6 new Batch 2 regression suites).

  • zig build test -Doptimize=ReleaseSafe --summary all: 237/237 passed (10s, MaxRSS 19M).

  • zig fmt --check src/ build.zig: clean (0 diffs).
  • Diff: 575 lines (+431 / -144) across 7 files Reviewer: cursor[bot] Review duration: 38m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.50) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 575 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.380
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #55"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 28.4% · Safe to merge
    28.4%