Skip to content

Ariadne: ws-open offers subprotocols, and a credential as one - #928

Merged
transfix merged 16 commits into
masterfrom
feat/ariadne-ws-auth-protocol
Nov 6, 2026
Merged

transfix merged 16 commits into
masterfrom
feat/ariadne-ws-auth-protocol

Conversation

@transfix

@transfix transfix commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

A browser's WebSocket can't send request headers, but it can offer subprotocols. That makes a credential in Sec-WebSocket-Protocol the one handshake auth a document in the wasm page can use. #926 covered auth in a message; this covers the handshake. The document still never holds the token.

(ws-open "wss://k8s.example/api/v1/watch"
         (dict "protocols" (list "v4.channel.k8s.io")
               "auth_protocol" (dict "credential" "kube"
                                     "prefix" "base64url.bearer.authorization.k8s.io."
                                     "encoding" "base64url")))

What changes

  • ws-open options
    • protocols: a list of subprotocols to offer, each an HTTP token.
    • auth_protocol, (dict "credential" NAME ["prefix" P] ["encoding" "raw"|"base64url"]): offers the host's token for NAME as one more subprotocol.
      • The token is fetched for the URL's origin, as for auth.
      • base64url is unpadded RFC 4648 §5. A token68's / or = can't be in a subprotocol, so a raw token holding them is refused and the error suggests base64url. It never quotes the token.
  • cvc::net::WsOptions::protocols
    • Natively, it is sent as a Sec-WebSocket-Protocol line.
    • In the browser, it goes through createAttributes.protocols.
    • Both backends fail the connect unless the server picks one of the offered subprotocols, as browsers do (RFC 6455 §4.1). Natively this is read from the 101 reply's headers: curl_easy_header, which files them under CURLH_1XX, guarded for libcurl < 7.84.
    • An invalid or repeated subprotocol is refused, as is a subprotocol together with a Sec-WebSocket-Protocol header. No error quotes a subprotocol, and the joined strings are wiped.
  • Token hygiene, as in Ariadne: ws-send carries a credential the host puts in (stacked on #923) #926. The token is wiped after the connect, and errors are redacted of both spellings. The connection records the name, so ws-recv and ws-poll blank either spelling if the server echoes it.
  • No query-string credentials, by design.

History

This was stacked on #926 with #925 merged in. #926 has landed. #925 (feat/ariadne-ws-nonblocking) is still open and fails its own AriadneNetL4.WebSocket* tests on macOS, which is what failed this PR's macOS lanes.

The last commit sets the tree to master plus this feature alone, with master as its second parent. The diff is only this feature, and no history was rewritten.

When #925 lands, its started connect (ws_start) needs the same attrs.protocols lines as connect_compiled.

Tests

Local build on master 8f468c1 (Linux, GCC, Ninja, RelWithDebInfo, libcurl 8.13 with ws):

  • net_ws_client_test: 11 passed.
    • New: offering subprotocols; a server that picks none, or one not offered, fails without quoting any; invalid subprotocols are refused.
  • ariadne_runtime_test: 259 passed, 1 skipped (AriadneTfEditor.InlineSeedIsLocaleIndependent).
    • New: AriadneNetHost.WsOpenOffersTheHostsTokenAsASubprotocol checks that:
      • the handshake line is events.v1, bearer.<base64url(token)>;
      • the server's echo of it comes back redacted;
      • a raw token with / is refused before connecting;
      • no state node holds either spelling.
  • src/cvc/net/ws_client.cpp compiles with em++, with and without -pthread. Not run in a browser.

Spec: docs/ARIADNE.md §10.5.

nicotina-ci added 15 commits October 30, 2026 16:46
In a browser build without threads the background pool's jobs run on the page's main thread,
so an http verb's kernel ran inside the frame pump and spun on emscripten_sleep(0) until the
reply came: nothing drew or took input for the whole request (100-300 ms, up to the 30 s
timeout).

- cvc::net: start(req[, policy], done) -> HttpTransfer (abort()), and http_starts_async().
  The browser fetch backend issues an emscripten_fetch and returns; onsuccess/onerror deliver
  the reply from the event loop. It reports a timeout as one, enforces the 64 MiB cap as the
  body arrives, and aborts cleanly (no callback after abort). Elsewhere start() sends in place.
  The policy check follows redirects hop by hop, as send(req, policy) does (shared next_hop).
- state_exec: async_work {kernel, start} and a register_async_fn overload. Where the call can
  park, start() begins the work and the call is an external one with the registration's
  timeout and charging; the work settles and posts it. async_call::on_cancel(stop) is how a
  kill, a timeout or the host's shutdown stops it. Elsewhere the kernel runs as before.
- The net verbs (http-get, http-request) return an async_work when the transport starts
  requests without blocking: credentials go in and captures come out in C++ around the
  started request (perform split into prepare/complete). fetch of an http(s) URI uses the
  http handler's new non-blocking form (register_uri_async_handler / resolve_async); a host's
  own handler for the scheme drops it.
- AppRuntime::idle() counts the scheduler's calls in flight (started ones included), and
  settle() yields to the browser's event loop in wasm, so ari snapshot waits for the reply.

Tests: AsyncCalls started-call cases (park, timed await slices, errors, kill, timeout,
shutdown, kernel fallback), NetStart (in place, later, policy hop by hop, abort),
AriadneNetL4 (frames go on while a program is parked on a request, await slices, teardown
aborts, credentials and capture on the started path, policy refusal, fetch). CI: the wasm
ari's node job runs tests/l4_frames.ari against a 1 s endpoint and fails unless frames ran
while the request was out (218 here; 0 with the blocking path).

Docs: ARIADNE.md 10.2, STATE_EXEC_EXCEPTIONS.md 3.12.
…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.
…eaded wasm

#923's browser WebSocket waited in loops on emscripten_sleep(10): WebWsConnection::wait_open
inside ws_connect (up to the 15 s connect timeout) and recv (up to ws-recv's seconds). Without
-pthread those kernels run on the pump thread, so ws-open and ws-recv froze the page, as http-get
did before #914. This moves them onto #914's path.

- cvc::net: ws_start(url, opts, done) and WsConnection::recv_async(timeout, done), plus
  ws_starts_async() and a set_ws_connector seam (WsConnector), as set_http_client is for HTTP.
  The browser backend in a build without threads answers from the WebSocket's own callbacks
  (on_open / on_error / on_close / on_message) and emscripten_set_timeout timers; the returned
  stop function abandons a connect (closing the socket, a connecting one too) or drops a wait.
  Elsewhere both wait in place, as before.
- ws-open and ws-recv return a state_exec async_work (#914) when ws_starts_async(): the program
  parks, frames go on, and the callbacks post the reply; a killed or timed-out call abandons its
  connect or drops its wait (on_cancel). ws-open's checks, credentials and handle table are
  shared by the kernel and the started path (build / prepare / finish_ws_open).
- WebWsConnection::close sends 1000 for a code a page may not send (WebSocket.close throws on
  anything but 1000 and 3000-4999, the 1001 and 1013 this file uses included).

Tests: AriadneNetL4.WebSocketOpenAndRecvAreStartedWhileFramesGoOn and
ATornDownDocumentAbandonsItsConnectAndItsWait (a deferred connector and connection the test
answers between frames; forcing ws-open back to its kernel fails both). CI: the wasm ari's node
job runs tests/l4_ws.ari against tests/ws_probe_server.py (stdlib; a 1 s handshake, then a
message 1 s later) and fails unless 20 frames ran during each wait: 354 and 374 here, 0 and 0
with the blocking path.

Docs: ARIADNE.md 10.5.
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
…o feat/ariadne-ws-auth-protocol

# Conflicts:
#	src/cvc/ariadne/net_intrinsics.cpp
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, in both the blocking and
the started connect. 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. The wasm
file compiles with em++, with and without threads. Spec: docs/ARIADNE.md
section 10.5.
…dential

# Conflicts:
#	docs/ARIADNE.md
#	src/cvc/ariadne/net_intrinsics.cpp
#	src/cvc/net/ws_client.cpp
#	src/cvc/tests/ariadne_runtime_test.cpp
Base automatically changed from feat/ariadne-ws-credential to master October 31, 2026 17:42
…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
transfix enabled auto-merge (squash) November 6, 2026 20:26
@transfix transfix changed the title Ariadne: ws-open offers subprotocols, and a credential as one (stacked on #926, #925) Ariadne: ws-open offers subprotocols, and a credential as one Nov 6, 2026
@transfix
transfix merged commit e9755bb into master Nov 6, 2026
17 of 28 checks passed
@transfix
transfix deleted the feat/ariadne-ws-auth-protocol branch November 6, 2026 21:09
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