# Security audit of the own HTTP/1 server and client before they become the default (2026-11-13)

URL: https://scalws.com/docs/security/2026-11-13-own-http1-default-audit/
Source: `docs/hardening/2026-11-13-own-http1-default-audit.md` in the ScalWS repository

Focused review (security-audit skill, guidance mode) of the code that 1.1 P10 makes the
default: the own HTTP/1 server (`crates/scalws-http/src/h1server.rs`, its codec
`crates/scalws-h1`) and the own upstream client (`crates/scalws-proxy/src/own_client.rs`,
`crates/scalws-proxy/src/h1.rs`). Reviewed on branch `v1.1` at 1e6268f, by reading source;
the test host was busy, so only the H1 unit tests were run (on the development machine). hyper 1.12.0 (the current
default) is the reference where parity matters.

Earlier audits: `2026-10-09-own-http1-audit.md` (run-2, five findings, all fixed) and
`2026-10-09-fuzzing-and-wire-tests.md`. Commits to the audited code since run-2:
8e28269 (fewer allocations), 3eb9d55 (head parsing without zeroed slots), 97ab96f (header
lookups only for fields present), 3231f04 (one lazily moved head timer), 78404ed (handler
future in the connection state), 5a8ad5d (pending response dropped when the client
leaves; tiny body frames copied), ae698df (replay of a written request only if
idempotent), 1536efe and c2e784f (dedup and dead code). Each was re-read against the
scope below.

## Result

No confirmed vulnerability. One parity gap with hyper (H1, chunk extensions not capped
per body) does not cross a trust boundary but was fixed for parity on the default path,
in both chunked decoders, with regression tests.

## Scope, item by item

### Request smuggling (server)

| Item | Verdict | Evidence |
|---|---|---|
| CL and TE together | cleared: 400, close | `scalws-h1/src/request.rs:296-300` |
| TE on HTTP/1.0 | cleared: 400 | `request.rs:293-295` |
| repeated TE lines, codings other than exactly `chunked` (`gzip, chunked`, `xchunked`, quoted, empty) | cleared: 400 | `request.rs:301-313`, tests `request.rs:400-422` |
| duplicate or list CL, sign, hex, overflow | cleared: identical repeated lines only, strict decimal, at most 19 digits | `request.rs:317-324`, `scalws-h1/src/lib.rs:36-43` |
| obs-fold, whitespace before colon, NUL and control bytes, `\x0b` in TE | cleared: httparse strict defaults reject them | `request.rs:176-186`, tests `request.rs:426-443` |
| bare LF in the head | parity with hyper (accepted by both, RFC 9112 2.2 allows it); differential case `lf only` | `tests/integration/tests/h1_differential.rs` |
| chunk size: sign, `0x`, leading or trailing space without extension, overflow, empty | cleared: rejected; at most 16 hex digits, checked arithmetic | `scalws-h1/src/chunked.rs:44-70` |
| chunk line ends: bare LF, bare CR, missing CRLF after data (also split across reads) | cleared | `chunked.rs:86-98`, `chunked.rs:126-147` |
| chunk extensions | cleared for framing (skipped to CRLF, bare CR or LF rejected, 4 KiB per line); total per body capped at 16 KiB since H1 | `chunked.rs:9-14`, `chunked.rs:61-76` |
| trailers | cleared: 16 KiB cap, field shape checked, then discarded (never reach handlers or upstreams, so forbidden fields cannot act) | `chunked.rs:99-119` |
| `Seen` shortcut (97ab96f) | cleared: `HeaderName::from_bytes` lowercases, so `Content-Length` in any case sets `framing` and the strict checks run | `request.rs:264-286` |
| span arithmetic on uninitialized slots (3eb9d55) | cleared: safe httparse API; spans are slices of the parsed buffer; covered by the empty-value test (`Transfer-Encoding:`) | `request.rs:161-212` |

### Pipelining (server)

* Ordering: one exchange at a time; the next head is parsed only after the previous
  response is written (`h1server.rs:104-140`). Cleared.
* A slow early response: pipelined bytes stay in `rbuf`; while a handler runs the socket
  is read only when `rbuf` is empty (one read, for EOF detection), so pipelined input is
  bounded (`h1server.rs:359-374`, `h1server.rs:547-561`). Cleared.
* A body not read before the next request: an abandoned body is drained without waiting
  up to 64 KiB, otherwise the connection closes; a body still held but unfinished at the
  end of the response also closes it (`h1server.rs:397-419`, `h1server.rs:574-576`).
  Bytes of an unfinished body can never be parsed as the next request. Cleared.

### Upgrades (server and proxy)

* `upgrade` is set only for HTTP/1.1 with `Connection: upgrade` and an `Upgrade` field
  (`request.rs:245-248`). A 101 without a requested upgrade becomes 502 and the
  connection closes (`h1server.rs:592-596`).
* Handoff: the 101 head is flushed, then the socket with the bytes already read is handed
  over (`h1server.rs:133-138`, `h1server.rs:609-619`).
* WebSocket proxying accepts only `GET` with `Upgrade: websocket`; the handshake uses a
  dedicated, non-pooled hyper connection, so the own client never carries an upgrade
  (`scalws-proxy/src/upgrade.rs:37-54`, `upgrade.rs:94-108`).
* h2c: plain-text prior knowledge is sniffed and served by hyper's HTTP/2
  (`scalws-http/src/conn.rs:34-38`); the own server serves `Protocol::Http1` only
  (`conn.rs:226-229`). `Upgrade: h2c` is a hop-by-hop field stripped before the upstream
  (`scalws-proxy/src/lib.rs:60-70`), and an unexpected 101 from an upstream is a protocol
  error in the own client (`h1.rs:171-174`). Cleared.
* Bytes sent after an upgrade request with a body enter the tunnel to the same upstream
  the client asked for: same principal, no boundary crossed.

### 100-continue (server)

* Sent only when the handler first asks for the body (`h1server.rs:635-658`), as hyper.
* Body not sent / early final response: a 100 not yet started is dropped, a partly
  written one is completed before the final head, and a body still wanted closes the
  connection after the response (`h1server.rs:461-477`); an unsent-100 body is never
  drained (`h1server.rs:709-711`). Cleared (the run-2 fix, unchanged since).

### Response framing toward the client (server)

* Interim statuses as a final response: 500 and close (`h1server.rs:387-395`).
* 204 and 1xx: no framing field, no body; HEAD and 304: no body, length describes the
  representation (`h1server.rs:428-443`).
* A body longer or shorter than its declared or exact length aborts the connection
  without a terminating chunk or the missing bytes (`h1server.rs:514-517`,
  `h1server.rs:529-532`, `h1server.rs:568-570`); handler `Content-Length` and
  `Transfer-Encoding` fields are never copied (`scalws-h1/src/response.rs:288-292`);
  header values cannot contain CR or LF (`HeaderValue`).
* Unknown length: chunked on HTTP/1.1, close-delimited (and no keep-alive) on HTTP/1.0
  (`h1server.rs:448-454`). Cleared.

### Slow clients (server)

* Headers: the first request's head within `header_read_timeout` of the accept, later
  heads within `header_read_timeout` of their first byte after up to `idle_timeout` of
  silence (`h1server.rs:204-291`). The lazily moved timer (3231f04) is re-armed whenever
  it fires before the current deadline (`h1server.rs:256-269`), so a later deadline is
  never missed and an earlier one is never skipped. Cleared.
* Body: the gap between body frames is bounded by `LimitedBody`
  (`scalws-core/src/body.rs:58`), shared by both servers; the integration test
  `slow_clients_are_disconnected` runs against the own server with
  `SCALWS_TEST_HTTP1_SERVER=experimental`. Cleared.
* Write side: `TimedIo` fails a write without progress for `write_timeout`
  (`scalws-http/src/timed.rs:31-44`), also for the 100 Continue write; output is bounded at
  256 KiB queued (`h1server.rs:504`) and a grown buffer is released (`h1server.rs:571-573`).
  Request-body queue bounded by bytes and frames (`h1server.rs:866-868`). Cleared.
* Client gone: the handler is cancelled during the handler phase and a pending response
  body is dropped during the write phase (5a8ad5d, `h1server.rs:547-561`). Half-closed
  clients are treated as gone, as hyper with its default `half_close(false)`.

### Upstream client: response parsing

| Item | Verdict | Evidence |
|---|---|---|
| status line, version (1.0 or 1.1 only), status code range | cleared | `h1.rs:149-170` |
| header limits: 64 KiB head, 128 fields | cleared | `h1.rs:28-30`, `h1.rs:153-161` |
| obs-fold, space before colon in responses | cleared: httparse defaults reject | `h1.rs:149-151` |
| 1xx: skipped; 101 refused | cleared | `h1.rs:171-177` |
| HEAD, 204, 304: no body, whatever the fields say | cleared | `h1.rs:223-228` |
| CL and TE together: TE wins, CL dropped, never reused | cleared | `h1.rs:217-222`, `h1.rs:243` |
| TE whose last coding is not `chunked`: read to close, never reused | cleared | `h1.rs:229-245` |
| CL lists must agree; bad CL refused | cleared | `h1.rs:247-256` |
| close-delimited bodies: never reused | cleared | `h1.rs:260`, `own_client.rs:336`, `own_client.rs:713-730` |
| body shorter than declared (EOF): error, connection dropped | cleared | `own_client.rs:676-681` |
| response larger than declared, or bytes after a chunked end: connection not pooled when the bytes are already buffered (`release`), discarded at checkout when they arrive later (`alive`) | cleared (parity with hyper; see note N3) | `own_client.rs:622-629`, `own_client.rs:359-371` |
| chunked decoding (separate copy in `h1.rs`) | cleared for the client side: bounded lines and trailers, bare LF refused; it trims whitespace around the size (lenient), which only affects how ScalWS reads its own upstream | `h1.rs:298-389` |

### Upstream client: connection reuse

* After an error: every error path drops the lease (`own_client.rs:313-316`,
  `own_client.rs:676-711`); a body dropped before its end is not pooled
  (`own_client.rs:748-758`). Cleared.
* After a partial body: same. Cleared.
* A response that arrives before the request is fully sent: the exchange returns with
  `body_sent = false`, so the connection is not reused (`own_client.rs:394-424`,
  `own_client.rs:336`); the client-side body is dropped and the own server drains or
  closes. Cleared.
* A pooled connection the upstream closed: checked at checkout (`alive`); a write or read
  that fails as stale is retried once on a fresh connection only if nothing was written,
  or for a bodyless idempotent request after at least 1 s idle (ae698df,
  `own_client.rs:279-312`), same rule as the hyper path (`inline.rs:269`). Cleared.
* The request is always re-framed: CL and TE from the client are dropped and the framing
  is chosen from the body (`h1.rs:59-116`); a body that does not match its length fails
  the exchange (`own_client.rs:564-586`). Cleared.

## What changes now that this is the default

* Every deployment gets this code; the run-2 hardening notes now apply everywhere. Re-read:
  bare LF in request heads (parity with hyper, kept), header names with underscores (the
  PHP runtime drops them before building `HTTP_*` parameters,
  `scalws-runtime-php/src/handler.rs:660`), only the first `Expect` read (hyper too),
  `Rewind` buffer kept for a tunnel's lifetime (bounded by the head limit plus one read),
  101 not checked for `Upgrade`, idle pool sharding (identical in `inline.rs:109`),
  replay of bodyless idempotent requests (now limited by ae698df), CL lists forwarded
  (the own server writes its own framing field). None becomes a finding on the default
  path.
* `CONNECT` is answered 405 before any handler (`scalws-server/src/pipeline.rs:444`), so
  the own server never has to treat a 2xx to `CONNECT` as a tunnel (which hyper does).

## Hardening (not a finding)

**H1. Chunk extensions were not capped per body (fixed).** hyper rejects a request body once its
chunk extensions total 16 KiB (`CHUNKED_EXTENSIONS_LIMIT`, hyper 1.12.0
`src/proto/h1/decode.rs:20`, `decode.rs:443-452`); the own decoder caps each chunk-size
line at 4 KiB (`scalws-h1/src/chunked.rs:9`) but not the total. Why it is not a
vulnerability: every non-final chunk carries at least one data byte, which counts toward
the request body limit, so the extra bytes a client can make the
server read are bounded (about 4 KiB per allowed body byte), they cost the client the
same bandwidth, and they cannot hold a handler longer than a slow plain body already can
(frame gap bounded by `LimitedBody`). hyper's limit mainly guards a single unbounded
extension line, which the per-line cap already prevents here.

Fix (parity with hyper on the default path): both decoders count the bytes after the
first `;` of every chunk-size line, the last chunk included, and refuse the body once the
total exceeds `MAX_EXTENSIONS` (16 KiB), with the same `ProtocolError` path as an
over-long chunk line (the server answers it like any other framing error and closes the
connection; the client drops the upstream connection). `Chunked` became a struct holding
the state and the counter; `Chunked::Size` stays the starting value, so callers (the
server, the client, the fuzz targets) are unchanged.

* Request side: `crates/scalws-h1/src/chunked.rs`, tests
  `chunked::tests::chunk_extensions_are_capped_per_body` (16 1-byte chunks with exactly
  16 KiB of extensions accepted; one byte more refused; an extension on the last chunk
  counts) and `chunked::tests::chunk_lines_are_capped_at_max_chunk_line` (the 4 KiB
  per-line cap still holds at its boundary).
* Response side: `crates/scalws-proxy/src/h1.rs`, tests
  `h1::tests::chunk_extensions_are_capped_per_body` and
  `h1::tests::chunk_lines_are_capped_at_max_chunk_line`.

hyper refuses at 16 384 counted bytes; ScalWS accepts exactly 16 KiB and refuses above it
(one byte apart, no practical difference).

## Notes

* N1. The two chunked decoders (`scalws-h1/src/chunked.rs` for requests, `scalws-proxy/src/h1.rs`
  for responses) are separate copies with different leniency (the response one trims the
  size and does not check bare CR). Both are fuzzed (`chunked`, `h1_connection` targets);
  merging them would keep one strict implementation.
* N2. Apart from the H1 unit tests, this review is source only. The suites below are what
  shows the current behaviour on the default path.
* N3. Upstream bytes that arrive after a response was released to the pool and after the
  next checkout's `alive` check can be read as the next response on that connection.
  hyper's client has the same window. The pool is per target (`target.rs:114-125`), so
  only responses from the same upstream are affected (same principal).

## Verification to run (test host)

```
cargo test -p scalws-h1 -p scalws-http -p scalws-proxy
SCALWS_TEST_HTTP1_SERVER=experimental cargo test -p scalws-integration-tests
```

Expected: green. The H1 tests were also run on the development machine (Windows, outside
the test host): `cargo test -p scalws-h1` and `cargo test -p scalws-proxy --lib h1::tests`
pass, and with the cap check disabled `chunk_extensions_are_capped_per_body` fails in
both crates (RED) while `chunk_lines_are_capped_at_max_chunk_line` passes (it guards the
existing per-line cap).
