Skip to content

Ariadne: ws-send carries a credential the host puts in (stacked on #923) - #926

Merged
transfix merged 9 commits into
masterfrom
feat/ariadne-ws-credential
Oct 31, 2026
Merged

transfix merged 9 commits into
masterfrom
feat/ariadne-ws-credential

Conversation

@transfix

Copy link
Copy Markdown
Owner

Stacked on #923 (feat/ariadne-websocket).

A browser's WebSocket can't send request headers. So until now, a document in the wasm/xterm.js page could authenticate a live feed (txwtf's /ws/events, cvcpkg's /v1/live/ws hello) only by holding the token itself, and documents never see tokens. With this change the host puts the token into the message.

(set r (ws-open "wss://tx.wtf/ws/events"))
(ws-send (get-attr r "handle") (dict "type" "hello" "token" (dict "credential" "txwtf")))
;; the server gets {"type":"hello","token":"<the host's txwtf token>"}

What changes

  • (ws-send H MESSAGE)
  • Origin binding. The handle table now keeps each connection's origin: the http(s) twin of the URL it was opened and policy-checked as. The token is fetched with token_for(name, origin), so a credential bound to other origins is not sent. The policy is checked again before a credential goes out.
  • The token never reaches a value_t, error or log.
    • It is put in just before send_text. Capacity is reserved up front so no replace reallocates and leaves a copy in a freed buffer.
    • The text and the tokens are wiped after the send, and a send error is redacted.
    • These are errors that send nothing: a name with no token at that origin, a token outside token68, or a credential value on a host that keeps no credentials.
  • Echoes are blanked. Each connection records which credentials it has sent: auth and header values at ws-open, plus ws-send credential values. ws-recv and ws-poll blank those tokens to [redacted] in what they return, as perform() does for HTTP replies. The names are shared with a ws-recv already waiting, so a reply to an auth message sent during the wait is blanked too.
  • Flaky test fix. ws client: close before queueing the oversized-message error: the curl reader queued the Error event before close() cleared open_. So NetWsClient.AnOversizedMessageIsAnError failed about one run in five locally (6 failures in 30 runs). It now closes first, matching the peer-close path; 60 of 60 runs passed after the fix.

Not in this PR

Tests

Local build (Linux, GCC, Ninja, RelWithDebInfo, libcurl with ws):

  • ariadne_runtime_test: 220 passed, 1 skipped (AriadneTfEditor.InlineSeedIsLocaleIndependent).
    • New: AriadneNetHost.WsSendPutsInTheHostsTokenThatNoValueHolds. Against LocalWsServer, it checks that:
      • the server receives {"type":"hello","token":"<token>","also":[1,"<token>"]};
      • the server's echo reaches the document redacted;
      • an unknown credential is an error that sends nothing;
      • no state node or reactive warning contains the token.
    • WsVerbsRoundTripThroughAPolicy now also checks plain JSON, and the error on a host without credentials.
  • net_ws_client_test: 8 passed (5 full runs, plus 60 runs of the previously flaky case).

Spec: docs/ARIADNE.md §10.5.

nicotina-ci added 8 commits October 30, 2026 18:51
…ws-close)

A document can follow a server that pushes changes instead of polling it.
cvc::net gets a WebSocket client (ws_client.h) with two backends: libcurl's
WebSocket API natively, so wss:// has the same TLS and proxy handling as
https://, and the browser's WebSocket under Emscripten. Messages arrive whole
in a queue; ws-poll takes what is there without waiting, for on_tick loops,
and ws-recv waits on the background pool. The host's policy checks a ws(s)
URL as its http(s) twin, and auth/headers/credential values work as for the
HTTP verbs (natively; a browser WebSocket sends no headers).

Documented in ARIADNE.md 10.5, with client tests against a Beast server and
verb tests in ariadne_runtime_test.
The reader queued the Error event and then closed, so a recv on another
thread could take the error while open() was still true; NetWsClient.
AnOversizedMessageIsAnError failed about one run in five. Closing first
matches the peer-close path, which clears open_ before it queues.
A browser's WebSocket can't send request headers, so a document in the wasm
page could only authenticate a live feed by holding the token and sending it
in a message, and documents never see tokens. Now (ws-send H MESSAGE) sends a
dict or list as JSON (json-encode, compact), and a (dict "credential" NAME)
anywhere in it goes out as the host's token for NAME, a JSON string:

  (ws-send h (dict "type" "hello" "token" (dict "credential" "txwtf")))

- The token is fetched for the origin the connection was opened and checked
  as (its URL's http(s) twin, now kept in the handle table), so credential
  bindings apply as for HTTP. It is put in on the host just before the send,
  with room reserved so no replace leaves a copy in a freed buffer, then
  wiped; a send error is redacted.
- A name with no token at that origin, or a credential value on a host that
  keeps no credentials, is an error and sends nothing.
- Each connection remembers the credentials it has sent (handshake auth and
  header values too), and ws-recv / ws-poll blank them to [redacted] in what
  they return, as perform() does for an HTTP reply, should the server echo one.
- A string message is sent as it is, as before.

Tests: AriadneNetHost.WsSendPutsInTheHostsTokenThatNoValueHolds checks, against
LocalWsServer, that the server gets the substituted token while no state node,
error or warning holds it, that an echo comes back redacted and that a missing
credential sends nothing; the round-trip test checks plain JSON and a host
without credentials. Spec: docs/ARIADNE.md section 10.5.

Merges master for #883's builtin_json_encode.
…t/ariadne-ws-credential

# Conflicts:
#	src/cvc/ariadne/net_intrinsics.cpp
#	src/cvc/tests/ariadne_runtime_test.cpp
Base automatically changed from feat/ariadne-websocket to master October 31, 2026 01:50
…dential

# Conflicts:
#	docs/ARIADNE.md
#	src/cvc/ariadne/net_intrinsics.cpp
#	src/cvc/net/ws_client.cpp
#	src/cvc/tests/ariadne_runtime_test.cpp
@transfix
transfix merged commit fe53da7 into master Oct 31, 2026
27 checks passed
@transfix
transfix deleted the feat/ariadne-ws-credential branch October 31, 2026 17:42
transfix pushed a commit that referenced this pull request Nov 6, 2026
…ter, without #925)

This rebuilds the branch on master, which now has #923, #914 and #926, and drops
the #925 merge: #925 (feat/ariadne-ws-nonblocking) is still open and fails its
own AriadneNetL4.WebSocket* tests on macOS, which also failed this PR. This
commit's tree is master plus the feature alone; its first parent keeps the
branch's earlier history, so nothing is rewritten.

A browser's WebSocket can send no request headers, but it can offer
subprotocols, so a credential there is the one handshake auth a document in the
wasm page can use. ws-open takes two new options:

- "protocols": a list of subprotocols to offer (Sec-WebSocket-Protocol), each an
  HTTP token.
- "auth_protocol": (dict "credential" NAME ["prefix" P] ["encoding" "raw"|
  "base64url"]) offers the host's token for NAME, fetched for the URL's origin as
  for auth, as one more subprotocol: P then the token, or its unpadded base64url
  (a token68's '/' or '=' can't be in a subprotocol), as Kubernetes does with
  "base64url.bearer.authorization.k8s.io.".

cvc::net: WsOptions::protocols is sent natively as a Sec-WebSocket-Protocol line
and in the browser through createAttributes.protocols. Both fail the connect
unless the server picks one of the offered subprotocols, as browsers do
(RFC 6455 4.1; natively from the 101's headers, which libcurl files under
CURLH_1XX). An invalid or repeated subprotocol is refused, with no error
quoting one, and the joined strings are wiped. ws_valid_protocol is public for
the option checks.

The token is wiped after the connect, and errors are redacted of it and of its
base64url spelling. The connection records the name, so ws-recv and ws-poll
blank either spelling if the server echoes it.

Tests: NetWsClient covers offering, a server that picks none or another
subprotocol, and invalid subprotocols. AriadneNetHost.
WsOpenOffersTheHostsTokenAsASubprotocol checks the handshake line, the
redacted echo, a raw token with '/' that is refused without connecting, and that
no state node holds the token. LocalWsServer can pick a subprotocol. Spec:
docs/ARIADNE.md section 10.5.
transfix added a commit that referenced this pull request Nov 11, 2026
Master moved ws-open and ws-recv on since this branch began: subprotocols and an
auth_protocol credential (#928), credentials in ws-send and blanked in what
ws-recv and ws-poll return (#926), connections kept as WsEntry with their origin.
The non-blocking split is put back on top of that:

- build_ws_open takes master's protocols and auth_protocol; prepare_ws_open puts
  the auth_protocol token in as a subprotocol; finish_ws_open files a WsEntry
  with the names of the credentials it sent. WsPrepared wipes its tokens when it
  goes, so a started connect that is abandoned leaves none behind either.
- ws-recv's started wait scrubs the event as the kernel does. Its callback keeps
  the origin and the sent names, not the connection that holds the callback.
- The browser's started connect (ws_start) checks and offers the subprotocols as
  master's ws_connect does.

And the macOS failure: libcurl there has no WebSocket support, so
have_ws_backend() was false, the ws-* verbs were never registered, and
AriadneNetL4's two WebSocket tests never reached their connector. A connector
set_ws_connector puts in place now counts as a backend; the libcurl connect
still checks only what libcurl was built with.
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.

1 participant