Repository navigation
Phase 2 Stage 1: verify who is connecting - #11
Merged
Merged
Conversation
First piece of the identity check ([P4]). Pure verification: given a token and the issuer's DID document, is this really who it says, minted for us, for the one method we accept, and still valid? Resolving the DID document and wiring this into the WebSocket upgrade come next. Two things worth knowing before reading it. WebCrypto cannot do this. atproto's default repo signing key is secp256k1 and `alg: ES256K` is not a WebCrypto algorithm at all — its ECDSA curves are P-256/384/521, and asking for secp256k1 throws NotSupportedError (checked, not assumed). Hence @noble/curves. P-256 keys are also legal in atproto, so both prefixes are handled. Nothing about `aud` protects us. The PDS checks it for syntax and never resolves it, so a token naming our DID is not evidence anyone vouched for us. Checking `aud` against our own DID, and refusing the method-less tokens the PDS is happy to mint, is entirely this module's job. The tests mint real signatures rather than using fixtures, and cover the refusals that matter: another audience, another method, no method at all, expired, wrong key, and claims edited after signing. They also nearly shipped a bug. noble v2's verify() hashes its input by default, so signing and verifying a digest on both sides is self -consistent and wrong: 16 green unit tests, and every token a real PDS minted rejected. A token from the devnet PDS now passes through the same path, which is the only reason this is known to work. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The entry point now refuses an upgrade that does not carry a service auth token minted for us, and the room is only reachable through it. The token travels as a WebSocket subprotocol, not a query parameter. A browser's WebSocket constructor cannot set headers, and a URL reaches logs, referrers and history, which is no place for a bearer credential. Base64url and `.` are all valid subprotocol characters, so a JWT needs no re-encoding — but the 101 must echo the subprotocol back or the browser fails the connection. resolveDid handles did:plc and did:web and refuses every other method: the DID arrives inside a token nobody has verified yet, so the set of things it can make the service fetch should be small and stated. Documents are cached through the Workers cache rather than an instance field, because an evicted object's memory is a cache whose lifetime nobody chose. The DID becomes a socket tag, which survives hibernation and makes getWebSockets(did) the thing Stage 2's access control revokes against. The token's expiry rides in the attachment, and a socket is closed with 4401 once it passes — checked on activity rather than by a timer, since tokens last an hour at most and re-presenting one on reconnect is the re-check [P5] asks for. A timer would not survive hibernation anyway. Verified against devnet: no token, a wrong subprotocol, a garbage token, a token for another audience and a token for another method are each refused, a valid token connects, and two different accounts — alice and bob, each with their own token — converge on one document. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Completes the identity check. The app asks its PDS for a token naming the collaboration server, hands it over as the second WebSocket subprotocol, and re-mints before reconnecting. [P19]'s scope change, which is the part with a cost. Granular scopes do not grant RPC by default, so every client metadata file and the devnet loopback builder gain `rpc:com.knightlab.drafts.connect?aud=<service did>`, with the audience differing per environment ([P26]): drafts.knightlab.com, drafts.knilab.com and drafts.devnet.internal. A session authorized before this will fail to mint, so everyone currently signed in re-consents once — [P20], happening now rather than later because the string is the expensive thing to change and there are few users to inconvenience. Refreshing is a reassignment, not a timer. The server closes a socket when its token expires (4401) and y-websocket reads `provider.protocols` afresh on every attempt, so replacing the token in place is the whole mechanism. A failed mint leaves the old token alone: it may still be valid, and a refused reconnect beats one that never happens. The collab path now awaits initAuth rather than racing it, because minting needs a session — the ordinary startup still does not wait, since only this path depends on identity. A failure is shown rather than swallowed: without it the symptom is an editor that looks fine and syncs with nobody. Verified in the browser that the unauthenticated path fails loudly and says why. The signed-in round trip is for Joe to run — it needs the devnet OAuth flow, and the first thing it will do is ask him to re-consent, which is [P20] arriving exactly as predicted. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…notes go Two things wrong with the dev affordance, both found by using it. Signing in from a collaboration session landed on the home screen. The OAuth redirect returns to a bare redirect_uri, so `?collab=<room>` is gone by the time the app starts and there was nothing else to read the room from. It now rides in sessionStorage alongside the `tl-post-auth-view` and `tl-current-doc` the app already keeps across that round trip — the same idea for a session with no draft id to restore. Going Back clears it, so leaving actually leaves rather than being undone by the next reload. The failure banner was at the template root, above the branding bar, which is nowhere any other message in this app appears. It was there because that was the easiest place to reach from App.svelte. EditorView already has the convention — a full-width line under the subnav, .sync-note for something that happened and .fork-note for something still outstanding — so this is a third in that family, in the register that says nothing being typed is reaching anyone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A session authorized before [P19] added the rpc: permission cannot mint a service auth token, and atproto has no way to widen an existing session's scope — authorizing again is the only remedy. The app was reporting the PDS's own words for this: Missing required scope "rpc:com.knightlab.drafts.connect?aud=did:web:drafts.devnet.internal". Accurate, names the exact string the PDS wanted, and leaves the person with nothing to do. So the failure is now classified rather than printed. CollabUnavailable carries whether authorizing again would fix it, and when it would, the note says the app needs permission it did not ask for before, that the account is fine, and offers a button that re-runs the flow for the handle already signed in. The room survives the redirect, so it comes back to the session it was clicked from. Matching on the message is not lovely — the PDS builds it from RpcPermission.scopeNeededFor(), so the shape is at least stable — but the alternative is treating every mint failure as equally fatal, and this is the one with an obvious remedy. Everything else still shows its reason. This is [P20] turning from a prediction into a screen. Worth noting it arrives for every existing user on the first collaboration attempt, not just in dev. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Granting the permission appeared to work and changed nothing: the scope was dropped on the way in. A rpc: permission's audience must be an absolute DID *service reference*, and RpcPermission.fromString returns null for a bare did:web:host — checked against the PDS's own @atproto/oauth-scopes rather than inferred: rpc:…?aud=did:web:drafts.devnet.internal REJECTED rpc:…?aud=did:web:drafts.devnet.internal%23collab PARSES [P27] claimed the fragment was unnecessary and "would only buy a %23 in every copy of the string". Exactly backwards, and the plan is corrected rather than quietly patched, since the wrong version is merged on main. The failure mode is the reason this cost an afternoon: an unparseable permission is dropped from the granted scope instead of being refused, so authorization succeeds, the consent screen looks right, and the first getServiceAuth fails naming a scope string that looks identical to the one requested. getServiceAuth itself accepts either form, so the two halves of atproto disagree and only the scope is strict. So the audience is did:web:<host>#collab everywhere it is compared, and %23 everywhere it appears inside a scope. The env var is quoted, because dotenv treats an unquoted # as a comment and would truncate it silently — the same class of bug one layer down. Also rewrites the banner copy, which was bad: no reassurance nobody asked for, and nothing about acting "on your behalf", which describes the mechanism and alarms the reader. One sentence saying what is missing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every connection to the collaboration server now proves which atproto
account it belongs to. Implements [P4], [P5], [P19] and [P20] from
docs/phase2-plan.md.How it works
The client asks its own PDS for a short-lived JWT naming our service
(
com.atproto.server.getServiceAuth), and the server checks the signatureagainst the signing key in the issuer's DID document. We never hold
anything that can act as the user — that is the point of the design, and
why Phase 2 needs no server-held credentials at all ([P1]).
The token rides as a WebSocket subprotocol, not a query parameter: a
browser cannot set a header on a WebSocket, and a bearer credential has no
business in a URL. The 101 must echo the subprotocol back or the browser
fails the connection.
Three things that were not in the plan
WebCrypto cannot verify atproto's default signing curve.
ES256Kisnot a WebCrypto algorithm at all — its ECDSA curves are P-256/384/521, and
secp256k1throwsNotSupportedError. So@noble/curvesis arequirement, not a preference. P-256 keys are also legal in atproto, so
both multicodec prefixes are handled.
noble v2 hashes its input by default. Signing and verifying a digest
on both sides is self-consistent and wrong: 16 green unit tests, and every
token a real PDS minted rejected. Caught only by pointing the verifier at
a devnet-minted token. Now
{ prehash: false }, with a comment sayingwhy.
A
rpc:scope's audience must be a DID service reference.RpcPermission.fromStringreturns null for a baredid:web:host, and anunparseable permission is dropped from the granted scope rather than
refused — so authorization succeeds, the consent screen looks right, and
the first
getServiceAuthfails naming a scope string that looksidentical to the one requested.
getServiceAuthitself accepts eitherform, so only the scope is strict.
[P27]said the fragment wasunnecessary; this corrects it.
Re-consent, which affects existing users
[P19]'s scope change means every currently signed-in account must
authorize again before it can collaborate — atproto cannot widen an
existing session's scope. Rather than printing the PDS's "missing required
scope", the editor says what is missing and offers a button that re-runs
the flow. The room survives the redirect, so it returns where it started.
Verified
same path.
for another audience and a token for another method are each refused; a
valid token connects; two different accounts converge on one document.
connects.
Not verified
Hibernation.
wrangler devcannot force eviction, so the wake path isstill correct-by-construction rather than tested — flagged in the plan for
the first real deploy.
Still not deployable
The worker refuses to deploy: any room name still opens a room to any
authenticated account. Access control is Stage 2.
🤖 Generated with Claude Code