An open-source, faster Devin alternative
TypeScript
docs record api edge websocket auth findings
Igor Zalutski5d3a1145f50a
diff --git a/.agents/work/api-edge-websocket-query-auth.md b/.agents/work/api-edge-websocket-query-auth.mdnew file mode 100644index 00000000..9ce7a4db--- /dev/null+++ b/.agents/work/api-edge-websocket-query-auth.md@@ -0,0 +1,134 @@+# API edge WebSocket query auth++Status: **fix implemented on `fix/api-edge-websocket-query-auth`**.+Implementation commit: `b6e9070`.++## Summary++SDK WebSocket attach worked when pointed directly at a cell endpoint, but+failed through `app.opencomputer.dev` with:++```text+401 {"error":"missing or invalid API key"}+```++The failure was specific to WebSocket attach for sandbox exec sessions.+Buffered exec and exec-session creation through the app endpoint still worked.++## Findings++The TypeScript SDK builds exec attach URLs with `?api_key=` because browser+WebSocket APIs cannot set custom headers:++```text+wss://app.opencomputer.dev/api/sandboxes/{sandboxID}/exec/{sessionID}?api_key=...+```++The regional Go control plane already accepts query-string API keys for+WebSocket-style routes through `auth.PGAPIKeyMiddleware`, which is why the same+attach URL pattern succeeded when pointed directly at the cell endpoint.++The global `api-edge` Worker did not match that contract. Its+`authenticate()` helper only read `X-API-Key`, so the edge rejected SDK+WebSocket attaches before it could mint the short-lived bearer token used for+the cell hop.++The app endpoint could still create exec sessions because those requests are+normal HTTP requests from the SDK, which use the `X-API-Key` header.++## Root Cause++The edge-first SDK proxy path changed the auth boundary for sandbox runtime+calls:++```text+SDK -> app.opencomputer.dev/api-edge -> cell control plane -> worker+```++For normal HTTP, the SDK sends `X-API-Key`, and `api-edge` authenticates it.+For WebSocket attach, the SDK sends `?api_key=`, matching the documented+WebSocket API and the Go cell middleware. `api-edge` had not implemented that+WebSocket-specific query-auth compatibility.++## Proposed Solution++Make `api-edge` accept `?api_key=` only for WebSocket Upgrade requests, then+strip that query parameter before forwarding to the cell. The cell should see+only the edge-minted bearer token, not the customer's original API key.++This keeps the compatibility surface narrow:++- `X-API-Key` remains the normal HTTP auth path.+- `?api_key=` is accepted only for WebSocket Upgrade requests.+- The customer API key is not forwarded to the cell after edge auth succeeds.+- Non-WebSocket HTTP requests with only `?api_key=` remain rejected.++## Implemented Shape++`cloudflare-workers/api-edge/src/index.ts` now has:++- `isWebSocketUpgrade(req)` for one consistent upgrade check.+- `apiKeyFromRequest(req)` that reads `X-API-Key`, then falls back to+ `?api_key=` only for upgrades.+- `stripEdgeAuthQueryParam(target)` to remove `api_key` from the forwarded+ cell URL.++The WebSocket proxy branch now clones the inbound request against the stripped+target URL, sets `Authorization: Bearer <edge-token>`, deletes `X-API-Key`, and+lets Cloudflare forward the WebSocket upgrade transparently.++## Regression Tests++Added `cloudflare-workers/api-edge/src/index.test.ts` with focused coverage:++1. WebSocket sandbox exec attach using `?api_key=` authenticates at the edge,+ proxies to the owning cell, preserves unrelated query params, strips+ `api_key`, and injects bearer auth.+2. Non-WebSocket HTTP with only `?api_key=` still returns `401` and does not+ proxy.++## Verification++Local checks:++```text+cd cloudflare-workers/api-edge+npm test -- --run+npx tsc --noEmit+```++Both passed after installing package dependencies from the lockfile.++`tsc` also surfaced an existing Worker WebSocket typing issue in+`src/dashboard.ts` around `ArrayBufferView.buffer` possibly being a+`SharedArrayBuffer`. The fix copies the view into a fresh `ArrayBuffer` before+sending it over the WebSocket.++## Rollout++Deploy only the `api-edge` Worker. The Go cell control planes and workers do+not need changes for this specific bug because their auth and attach paths+already accept the SDK's WebSocket URL shape.++Post-deploy smoke test:++1. Create a fresh sandbox through `https://app.opencomputer.dev`.+2. Start an exec session through the SDK.+3. Attach through the SDK without overriding `sandbox.exec.apiUrl`.+4. Expect WebSocket open to succeed and `exec.shell().run("echo ok")` or+ equivalent streaming exec to return exit code `0`.++Also verify the negative case:++```text+GET /api/sandboxes/{id}/exec/{sessionID}?api_key=...+```++without `Upgrade: websocket` should still return `401`.++## Follow-ups++The edge Worker and Go control plane should keep WebSocket auth behavior aligned+as a public API contract. Any future WebSocket route added to the SDK should get+an edge-level test for query-param auth, plus a negative HTTP test so query+auth does not quietly expand across the whole API.