~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zttp

mirror of https://tangled.org/zat.dev/zttp

0
0
main
130 lines · 7.4 KB Raw History
# zttp deadline and HTTP framing audit

Consumer dependency pins are deliberately unchanged pending library qualification.
The starting point was local commit aa459a4, including its existing Content-Length
truncation fix, on top of v0.2.0.

## Deadline and cancellation findings

The old watchdog continued without a deadline when concurrency was unavailable.
It counted nominal sleep intervals rather than elapsed monotonic time. Retry
budgets restarted, and a caller's delay between `open` and `stream` was excluded
from the total budget. Reusing a completed Progress could bypass body monitoring.
Unread response deinitialization could drain a stalled body synchronously.

The fix uses absolute monotonic deadlines shared by attempts and body handoff,
explicit concurrency failure, cancellation followed by joining the operation,
and connection closure when abandoning unread or failed responses. Idle/rate
measurements use actual timestamps. Unguarded network cancellation is surfaced
as Canceled. Real loopback tests exercise both Threaded and zio, including a
single-executor heartbeat while another request stalls.

Completion watchers use cancellable events rather than sleeping until the next
10 ms poll. Body stall/rate sampling retains its timer, but a completed body is
not classified as idle while its caller is waiting to resume. Deadline checks
still use actual monotonic time and operations are joined on every exit path.

The repeated x86 Debug run exposed a timing-oracle problem: a 20 ms heartbeat
started before TLS setup took 121 ms, exceeding its 100 ms bound. That combined
measurement does not isolate network waiting from synchronous cryptographic
setup or VM scheduling. The TLS fixture now timestamps receipt of ClientHello
and retains the 100 ms heartbeat bound during the silent network wait. The
whole TLS attempt still has a deadline and a separate completion bound. The
ordinary HTTP header-deadline test retains its 60 ms deadline and 200 ms bound.
TLS cryptographic CPU work is synchronous in std.http and cannot be forcibly
preempted by a cooperative Io deadline.

## Protocol findings

The first 283-vector profile produced 134 passes out of 654 backend/delivery
checks on the pre-hardening implementation. The same profile passed all 654
checks after the framing changes. These are conformance observations, not 520
independent vulnerabilities.

The fixes validate response and outgoing field syntax, transfer-coding lists,
chunk sizes/extensions/trailers, framing completion after decompression, and TLS
EOF authentication. Informational responses are bounded and skipped; HEAD,
204, and 304 finish without consuming a body. Buffered unsolicited responses and
failed/truncated bodies cannot return a poisoned connection to the pool.
Redirect bodies are abandoned by closing their connection rather than draining
through a separate unchecked parser path.

The implementation retains std.http's parser, transfer reader, and decompression
code, with bounded validation around the public 0.17 reader state. This coupling
must be requalified when Zig changes. A dedicated test package supplies pinned
zio without adding a runtime dependency to the library.

## Qualification

See [the conformance profile](../../tests/conformance/README.md) for exact scope,
provenance, commands, sandbox controls, and exclusions. Tests use real sockets;
there is no production traffic or mocked network backend. The profile includes
positive controls and stateful followup requests, so rejecting everything or
closing every connection cannot satisfy the suite.

Cross-origin redirects now strip Authorization, Cookie, and Proxy-Authorization
from extra headers as well as the dedicated authorization option. The stripping
is permanent for the remainder of that redirect chain. Real two-origin fixtures
verify the source receives credentials and the target does not, under every
runtime configuration, while the redirect's incomplete body does not stall the
next request.

The first complete isolated Linux aarch64 qualification passed in both Debug and ReleaseSafe:
47 library tests, the real TLS rotation/hostname check, four runtime contract test
blocks, and all 1,736 wire checks from 370 cases across four backend modes.
The corpus SHA-256 is
`fb804404fbca924dd81583f6b80f32c822749c01321829e09dfc2a04eba8c500`.
The tested source snapshot SHA-256 is
`85aeb86bb9776ff94f2e4fd3e0912f5afc4ddda2307f088e6edc12541b32957a`;
the prepared tool image ID is
`sha256:284b6ada14d0012552979baff260f6773b790e23ed1f15b49d57af33f89ad950`.
Documentation changes do not affect the source snapshot digest. The gate rebuilds
from read-only source with no external network and fails on any unexpected
rejection, fixture failure, crash, or timeout.

## Outgoing shutdown

Zig 0.17.0's automatic pool destruction closes TCP without calling the public
`Connection.end` helper. A local TLS peer verified the missing alert under
Threaded and all three zio modes; a Python control sent the alert correctly.
The wrapper now owns connection retirement, preserving pool reuse and eviction
while sending and flushing alerts before destroying healthy TLS connections.

`Client.shutdown` rejects new work, waits for existing users, and gives all idle
connections one shared budget. Timeout/cancellation joins the operation before
forced cleanup. `deinit` attempts that bounded shutdown after callers quiesce.
Successful retirement also attempts a bounded close, while failed or abandoned
transfers abort. Fetch checks its total deadline after cleanup. The qualification
includes actual blocked sockets, cancellation, active responses, pool eviction,
concurrency exhaustion, and an independent TLS closure oracle.

The corresponding WebSocket fix sends and flushes both protocol close messages
before socket shutdown. It retains TLS/message storage until final deinit,
serializes descriptor shutdown/close, and waits for active socket reads/writes to
leave before closing the descriptor. These are maintained-library fixes; no Zig
compiler or global standard-library patch is required. Consumer pins remain held.

The shutdown implementation passed the complete local Linux aarch64 gate in
ReleaseSafe: 47 library tests, TLS certificate rotation/hostname checks, all four
runtime contract blocks (eight repetitions per configuration), 1,736 HTTP wire
checks, and 61 outgoing TLS shutdown cases. The tested source fingerprint was
`d77ba2621ce2aaa9f92f4f358470c84865a1c83119e803b94397e206380920ef`.
The 61 shutdown cases also passed in Debug with zlay's upstream `3bbd74d` zio
package. Shutdown probes use a thread-safe debug allocator and fail on leaks.

An earlier parallel local run exceeded the ordinary heartbeat bound (129 ms
against 100 ms). The complete sequential rerun passed with unchanged timing
assertions. These local results do not constitute a HeavyPad deployment or
consumer qualification.

Local x86-64 Linux/musl binaries passed all 1,736 HTTP wire checks, all 61 HTTP
shutdown cases, and all 41 WebSocket shutdown cases in ReleaseSafe under
Orbstack emulation. These are compatibility results, not native performance
measurements. Native HeavyPad CI was not triggered for this follow-up.

Commit replay exposed a separate WebSocket server race: cleanup could close the
descriptor while a shutdown-notification writer still held the write lock.
Server closure now interrupts the socket, waits for that lock, and only then
closes the descriptor. Every write entry point checks closure under the lock.
The existing concurrent shutdown test and new post-close write assertions pass
in Debug and ReleaseSafe.