Skip to content

Net: an RFC 6455 WebSocket codec, sans-IO, for Open Watch Party - #1273

Merged
GLinnik21 merged 10 commits into
plxnative:mainfrom
PilaScat:feat/ws-codec
Nov 8, 2026
Merged

GLinnik21 merged 10 commits into
plxnative:mainfrom
PilaScat:feat/ws-codec

Conversation

@PilaScat

@PilaScat PilaScat commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Part 1 of 4 for Open Watch Party (#1090), as agreed there: the WebSocket codec, SHA-1 and a ws_frame fuzz target. The CONNECT_ONLY transport over libcurl is part 2. The OWP client crate is parts 3 and 4.

Why

OWP is JSON over wss://. The sets' libcurl (7.53.1 on the oldest) predates libcurl's WebSocket API (7.86), so RFC 6455 is ours, over the socket CURLOPT_CONNECT_ONLY hands back. The spike in #1090 ran that against the public relay with libcurl 8.5.0 and 7.53.1. This PR is the half that does no IO. Bytes come in, events and bytes to write come out, and the transport will drive it.

What

  • plx_base::sha1: SHA-1 next to sha256.rs, for Sec-WebSocket-Accept, graded by the RFC 3174 vectors.
  • plx_net::ws, under the transport, so the OWP crate above sees only text messages and close codes:
    • handshake: the upgrade request with the process User-Agent (user_agent_header_line), its key from a caller-supplied nonce, and the §4.1 checks on the answer. These are 101, the Upgrade/Connection tokens case-insensitive, exactly one matching Sec-WebSocket-Accept, and no Sec-WebSocket-Extensions or Sec-WebSocket-Protocol field at all, even an empty one, since we ask for neither. The head is read as bytes across any split and refused past 16 KiB. Field names must be tokens. A bare CR or a NUL is refused, and any other value byte is opaque except in the four fields the checks read.
    • frame: an encoder that masks with a caller-supplied key, and a parser that reads across any split. From the header alone, before any payload is buffered, it refuses:
      • reserved bits and unknown opcodes;
      • a masked server frame;
      • a fragmented or long control frame;
      • a length not in its shortest form (a 16-bit length under 126, a 64-bit one under 65536);
      • a 64-bit length with the top bit set;
      • a length past the caller's limit or past what a buffer can hold (so no overflow with a 32-bit usize).
    • connection:
      • open builds a request only for a host that is an HTTP authority (a reg-name or IP address with an optional port, no userinfo) and a path that is an origin form (path and query, no fragment);
      • a close before the request is taken withdraws it;
      • pings are answered with their payload until the server's close;
      • fragments are joined around control frames;
      • text is checked as UTF-8 per fragment and again whole;
      • a message past the limit is refused;
      • the server's close is echoed with its code, and our close waits for the server's;
      • a violation sends 1002, 1007 or 1009 and ends the connection.
  • ws_frame in fuzz/: the same bytes fed whole and split at a fuzzer-chosen stride must give the same events and the same bytes out. The target also asserts what the codec promises: Opened first and once, nothing after the end, no message past the limit, and what the client wrote parses back as pongs plus at most one close, last. Seeds and a dictionary come from tools/fuzz-hud.py, and max_len is above the 16 KiB head limit. fuzz.yml runs it for changes to the codec, SHA-1 or base64.

Nonces and masking keys come from the caller (part 2 reads them from /dev/urandom), so the tests are exact. The §5.7 examples encode and parse byte for byte, masked pong included, and the §1.3 accept is reproduced.

Checks

On the committed tree, WSL2 x86_64:

  • make check on a3f475c: 39 suites, 24650 passed, 0 failed, 198 ignored.
  • make check-nodef: clean.
  • cargo check --lib --target armv7-unknown-linux-gnueabi -p plx_base -p plx_net: clean (only the usual neon / soft-float notes).
  • make test-crate C=plx_net T=ws::: 33 passed. C=plx_base T=sha1: 2 passed.
  • tools/fuzz-hud.py run --secs 600 ws_frame on nightly-2026-10-03, after the review fixes: 5,083,897 runs, coverage 792, no crash.
  • The five review fixes (23d2172, 269c18f, 1f19777, 99b27b8, a3f475c) each come with a regression test that failed before the fix.
  • pr-reviewer: one round. Its points are in this branch: pongs while our close waits, the 32-bit length overflow, max_len above the head limit and an executable bit the editor had dropped. doc-claim-auditor: its three findings are fixed (net crate description, docs/module-layers.md table, the fuzz tree's size note in the Makefile).

No FFI, dynlib! or curl change here, so no fw-compat-reviewer. That review comes with part 2.

The fuzz tree gains one binary: ws_frame adds 83 MiB on Linux x86_64. The Makefile note and docs/agent-reference.md say so, and DISK_NEED = 3.3 still covers 3.1 GiB plus that.

make build-bench

The PR touches Makefile, .github/workflows/fuzz.yml and fuzz/Cargo.toml. The review fixes after df2cacbd touch only rust-modules/net/src/ws/, so this table still applies. Base 279b66d9 against this branch df2cacbd, one after the other on the same machine (rustc 1.99.1, x86_64, 16 cores, WSL2), 3 interleaved runs per scenario, rustc front end serial. The table shows the median, with min–max in brackets.

Scenario 279b66d9 df2cacbd
No-op host test build 0.1 s 0.1 s
Edit leaf plx_base cbuf.rs, non-incremental 63.6 s (63.1–82.8) 67.7 s (66.5–74.7)
Edit leaf plx_machine landgate.rs 61.5 s (61.4–68.8) 65.0 s (64.6–75.4)
Edit leaf plx_platform devcaps.rs 61.1 s (60.6–67.6) 66.2 s (64.4–72.3)
Edit leaf plx_gfx overdraw.rs 40.8 s (40.5–63.8) 42.6 s (42.5–46.4)
Edit leaf plx_net stream_redirect.rs 55.4 s (55.3–104.5) 58.3 s (58.0–62.1)
Edit leaf plx_ui dwell.rs 39.5 s (39.0–61.3) 41.5 s (41.0–41.8)
Edit leaf plx_plex retry.rs 54.7 s (54.7–68.7) 58.4 s (57.8–58.6)
Edit leaf plx_telemetry window.rs 50.5 s (50.1–56.4) 54.2 s (52.4–56.4)
Edit leaf plx_data tape.rs 47.6 s (47.5–53.1) 51.0 s (49.4–53.7)
Edit leaf plx_session scripted.rs 33.8 s (33.4–37.3) 35.3 s (35.2–35.3)
Edit leaf plx_media units.rs 39.9 s (39.7–43.6) 41.8 s (41.4–41.8)
Edit leaf plx_appkit skip_pill.rs 34.5 s (34.3–43.6) 35.7 s (35.7–36.1)
Edit leaf plx_screens clock_readout.rs 31.1 s (31.0–43.9) 32.4 s (32.0–32.7)
Edit leaf app coldstart.rs 17.5 s (17.5–24.4) 18.4 s (18.0–18.6)
Edit hub plx_ui lib.rs, non-incremental 39.4 s (39.2–48.0) 41.0 s (40.9–41.2)
Edit leaf plx_base cbuf.rs, incremental 15.0 s (14.8–47.6) 19.0 s (18.5–24.7)
Edit hub plx_ui lib.rs, incremental 8.9 s (8.7–18.4) 10.3 s (9.3–13.8)
Unit suite (default features) 77.8 s (73.1–78.2), 12026 passed 77.2 s (75.8–81.4), 12056 passed
ARM staticlib rebuild skipped skipped
Target dirs, total 81344 MB 81366 MB

Every branch median is inside the base's min–max. The branch reads a few percent slower across the board, but that includes rows whose crates this PR does not touch (plx_screens, the app crate alone). So I read it as drift between the two sittings, not as a cost of the change. The unit suite has 30 more tests (2 SHA-1, 28 codec) in the same time. The ARM row is skipped because this checkout has no ARM archive: the webOS NDK has no Linux x86_64 build, so make cannot run here.

Refs #1090.

🤖 Generated with Claude Code

PilaScat and others added 3 commits November 7, 2026 10:15
The WebSocket upgrade proves the server read the client's key with
Sec-WebSocket-Accept, the base64 of SHA-1 over the key and the RFC 6455
GUID. The crate has no hashing dependency, so SHA-1 is written out next
to sha256.rs, as one function over a byte slice: the accept key is its
only consumer and hashes a few dozen bytes once per connection.

Graded by the RFC 3174 vectors (one block, the 56-byte message whose
padding takes a second block, a million 'a's, the 640-byte repetition)
and the empty message's FIPS 180-4 value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Open Watch Party (plxnative#1090) talks JSON over WebSocket, and the televisions'
libcurl has no WebSocket API before 7.86, so the protocol is ours, over
the CONNECT_ONLY socket the transport will open. This is the half that
does no IO: bytes in, events and bytes to write out. It lives in
plx_net under the transport, so the OWP crate above sees only text
messages and close codes.

- handshake: the upgrade request (the process User-Agent, through
  user_agent_header_line), a key from a nonce the caller supplies, and
  the RFC 6455 section 4.1 checks on the answer: 101, Upgrade and
  Connection tokens without case, exactly one Sec-WebSocket-Accept equal
  to ours, no extension or subprotocol we did not ask for. The head is
  read across any split and refused past 16 KiB; the bytes after it are
  frames.
- frames: the encoder masks with a key the caller supplies, and the
  parser reads across any split and refuses, from the first two bytes,
  reserved bits, unknown opcodes, a masked server frame, a fragmented or
  long control frame, a 64-bit length with its top bit set and a length
  past the caller's limit, so no payload is buffered for a frame it
  will refuse.
- connection: pings answered with the same payload, fragments joined
  around control frames, text checked as UTF-8 as fragments arrive and
  again whole, a message past the limit refused, the server's close
  echoed with its code, and our close waiting for the server's. A
  violation sends the close code RFC 6455 gives it (1002, 1007, 1009)
  and ends the connection; nothing is read or sent after the end.

The masking keys and the nonce come from the caller (the transport
reads them from /dev/urandom), so the tests are exact: the section 5.7
examples encode and parse byte for byte, the masked pong included, and
the section 1.3 accept is reproduced.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
The target drives a Connection with what libFuzzer writes: after a valid
upgrade answer (frames), or from the first byte (the answer itself). It
feeds the same bytes whole and in pieces whose size the first byte
picks, and asserts the two runs give the same events and the same bytes
to send. Then it asserts what the codec promises: Opened first and only
once, nothing after Closed or Failed, no message past the limit, and
what the client wrote parses back as unfragmented pongs and at most one
close, which comes last and only when the connection ended after
opening.

tools/fuzz-hud.py builds its seeds from RFC 6455 frames (the section
5.7 examples, fragments around a ping, closes, invalid text, a length
past the limit, an upgrade accepted and one refused) and a byte
dictionary. fuzz.yml runs it on a PR that touches the codec, SHA-1 or
base64, and offers it to a dispatch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Copilot AI lite review requested due to automatic review settings November 7, 2026 11:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI lite review requested due to automatic review settings November 7, 2026 11:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@fraygox

fraygox commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

A note for part 2, from #1270 (opt-in bundled curl 8.23 / OpenSSL 3.5 behind make CURLSTACK=1). Nothing in this PR needs to change.

  • The codec is still needed. The bundled libcurl does export curl_ws_send/curl_ws_recv, but it's an opt-in developer build that no release ships, so the CONNECT_ONLY path stays the only one every user gets. Also, a webOS 10.3 set here has firmware libcurl 7.82.0, still below 7.86. So curl_ws_* isn't available even on the newest firmware I can test.
  • Bind curl_easy_send/curl_easy_recv from the libcurl net bound. With the flag, net::global_init binds libcurl-plx.so.4 (DEEPBIND, LOCAL) and curlio takes its multi table from the same handle (net::bundled_curl and dynlib!'s load_from). If the send/recv table resolves through the system candidate list while the easy handle came from the bundle, it calls the firmware's functions with the bundle's handle. In Net: an opt-in bundled HTTP/3 curl stack over OpenSSL 3.5, behind CURLSTACK=1 #1270's description I called passing a handle to the other libcurl an ABI violation.
  • The OpenSSL 1.0 serial-TLS refusal should read the bound curl. With the bundle bound, TLS runs on OpenSSL 3.5 (threaded-tls=true) even on webOS 5 sets, where the firmware's OpenSSL 1.0.2 is still in the process. A check on the bound curl's version would let OWP run there in a CURLSTACK build. A check on the firmware library would refuse it for nothing.

Both only matter if #1270 lands. I'm happy to adjust either side.

@GLinnik21 GLinnik21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The split into a codec PR and a transport PR still makes sense. Please fix the frame-length and HTTP upgrade validation issues below and add focused regression tests in this PR. We reproduced the malformed frame and handshake cases against this commit in a standalone WSL harness.

For part 2, please keep the plan from #1090 and attach the Autobahn client report for the actual CONNECT_ONLY transport. Please include the suite version and explain any skipped or non-strict cases. Prior test plan

Comment thread rust-modules/net/src/ws/frame.rs Resolved
Comment thread rust-modules/net/src/ws/handshake.rs Resolved
Comment thread rust-modules/net/src/ws/handshake.rs Resolved
Comment thread rust-modules/net/src/ws/connection.rs Resolved
Comment thread rust-modules/net/src/ws/connection.rs Resolved
PilaScat and others added 5 commits November 8, 2026 11:59
RFC 6455 section 5.2 asks for the minimal number of bytes. A 16-bit length
under 126 or a 64-bit length under 65536 is now NonMinimalLength, which fails
the connection with 1002. The 125/126 and 65535/65536 transitions stay covered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
…mpty

The client offers neither, so RFC 6455 sections 4.2.2 and 9.1 leave no room
for either response header; a blank one used to open the connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
A field value is opaque bytes (RFC 9110 section 5.5), so a 101 with an
unrelated field carrying obs-text such as 0xe9 now opens instead of failing
the whole upgrade. A field name must be a non-empty token (RFC 9112 section 5),
so "Not A Field: y", a folded line and a bare CR in a field are Malformed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
… form

Connection::open used to accept any visible ASCII, so "relay.example/path" as
the host and "/v1/ws#fragment" as the path built a request. The host must now
be a reg-name, an IPv4 address or a bracketed IP literal with an optional
numeric port, and the path an absolute path with an optional query, both by
the RFC 3986 character sets with percent-encoding checked (RFC 6455 sections 3
and 4.1). A fragment is refused, a query is kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
…uest

close() while Connecting marked the codec closed but left the upgrade GET in
the outgoing buffer, so a transport that took it afterwards would still send
it. The unsent request is now dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Copilot AI lite review requested due to automatic review settings November 8, 2026 11:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@PilaScat

PilaScat commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for reproducing these. The five threads are addressed in 23d2172, 269c18f, 1f19777, 99b27b8 and a3f475c, one commit each, each with its regression test seen failing first. On a3f475c:

  • make check: 39 suites, 24650 passed, 0 failed, 198 ignored;
  • make check-nodef: clean;
  • the armv7 cargo check of plx_base and plx_net: clean;
  • ws_frame fuzzed for 600 s on nightly-2026-10-03: 5,083,897 runs, coverage 792, no crash.

For part 2, the Autobahn client report will cover the real CONNECT_ONLY transport, with the suite version and the reason for every skipped or non-strict case, as in the plan from #1090.

@fraygox, thanks for the notes on #1270. Nothing changes here, and both points will hold in the transport PR:

  • The curl_easy_send/curl_easy_recv table will bind the way Net: an opt-in bundled HTTP/3 curl stack over OpenSSL 3.5, behind CURLSTACK=1 #1270 binds curlio's multi table: load_from on net::bundled_curl() when it is Some, the system candidate list otherwise. The choice stays the one global_init made.
  • The refusal will call net::threaded_tls_ready(), not read a version of its own. That flag comes from the bound curl's curl_version(), so with the bundle it reads OpenSSL 3.5, and a CURLSTACK=1 build on a webOS 5 set will open the WebSocket.

Whichever of the two lands second makes the change: on my side if #1270 is in by then, on yours otherwise, with the same lines #1270 adds to curlio.rs.

Copilot AI lite review requested due to automatic review settings November 8, 2026 18:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@GLinnik21 GLinnik21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the requested codec fixes and the IPv6 host regression at 29c1317e. The focused WSL tests, no-default-features check, and GitHub CI passed. The Autobahn client run remains part of the transport PR as agreed in #1090.

@GLinnik21
GLinnik21 merged commit 630f8c2 into plxnative:main Nov 8, 2026
20 checks passed
@PilaScat
PilaScat deleted the feat/ws-codec branch November 8, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants