Skip to content

Commit 153c095

Browse files
committed
Mock server: the phone app's sign-in as the server now does it
- enter reads the PKCE verifier from the OpenBot-Shell-Verifier header only. A verifier in the address is not read, so an enter that carries it only there fails, and spends the ticket. - Past the OpenBot-Shell and Sec-Fetch refusals (which still leave the ticket unused), the ticket is spent as it is read, whatever is refused after. - OpenBot-Shell must be exactly ios or android, lowercase. - A malformed verifier (not 43 to 128 unreserved characters) is refused. - The custom scheme is bot.open.shell:/signed-in (1 slash), in the sign-in page's link and in the config answer. - Tickets are 32 random bytes, 43 characters, as the server's are. MOCK_ENTER_NO_HEADER_CHECK=1 now skips only the OpenBot-Shell and Sec-Fetch checks: the verifier is still read from its header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
1 parent 5aeaec3 commit 153c095

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

‎app/src/lib/auth/resume.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function resumeUrlFor(
5454
}
5555

5656
/**
57-
* The phone app's sign-in hand-off, on this origin (docs/shell-sign-in.md on the server, "Returning
57+
* The phone app's sign-in hand-off, on this origin (the server's hand-off contract, "Returning
5858
* through /sign"). On a self-hosted OpenBot a signed-out sign-in sheet lands on `/sign` carrying
5959
* `shell_challenge`, `shell_platform` and `shell_callback`; once signed in, the sheet goes on to the
6060
* hand-off, which shows "Continue to the OpenBot app", rather than into the web app inside the sheet.

‎app/workbench/mock-server.ts‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
* # As a server without the phone app's sign-in hand-off: every /api/auth/shell/* answers 404.
1818
* MOCK_NO_HANDOFF=1 MOCK_SIGNED_OUT=1 bun app/workbench/mock-server.ts
1919
*
20-
* # As a server from before /api/auth/shell/enter asked for the OpenBot-Shell header (below), for
21-
* # phone app builds that don't send it yet:
20+
* # As a server from before /api/auth/shell/enter asked for the OpenBot-Shell header and checked
21+
* # Sec-Fetch-Site and Sec-Fetch-Mode (below), for phone app builds that don't send the first yet.
22+
* # The verifier is still read from its own header only, as OpenBot's server reads it:
2223
* MOCK_ENTER_NO_HEADER_CHECK=1 bun app/workbench/mock-server.ts
2324
*
2425
* # Another person in another organization (Sam Rivera in Acme when unset), say for a second mock
@@ -31,20 +32,24 @@
3132
* POST /__mock/session {"signedIn": bool} signed in, and to change it at run time.
3233
* GET /api/auth/shell/config?platform=ios
3334
* What the phone app asks before signing in: {"handoff":true,"callback":"scheme",
34-
* "callbackUrl":"openbotshell://signed-in"}. 404 under MOCK_NO_HANDOFF.
35+
* "callbackUrl":"bot.open.shell:/signed-in"}. 404 under MOCK_NO_HANDOFF.
3536
* GET /api/auth/shell/handoff?challenge=C[&platform=ios][&auto=1]
3637
* The page the phone app opens in its sign-in sheet. C is a PKCE S256 challenge (43
3738
* characters of base64url). A page titled "Mock sign-in" with one link, #mock-sign-in, "Sign in
38-
* as test person", to openbotshell://signed-in?ticket=T. T is single use and lasts 60 s.
39+
* as test person", to bot.open.shell:/signed-in?ticket=T (one slash). T is single use and
40+
* lasts 60 s.
3941
* With auto=1 the page follows its link by itself after 800 ms. 400 without a valid C.
40-
* GET /api/auth/shell/enter?ticket=T&verifier=V&to=/path
41-
* Loaded by the web view, which sends the header `OpenBot-Shell: ios` (or `android`). With that
42-
* header, a Sec-Fetch-Site that is absent or `none`, a Sec-Fetch-Mode that is absent or
43-
* `navigate`, and a known, unused, unexpired T whose C is base64url(sha256(V)): sets the
44-
* cookie better-auth.session_token=mock, marks T used and answers 303 to `to` (an own path,
45-
* else /). Otherwise 303 to /sign?native-sign-in=failed, setting nothing; a refusal for any of
46-
* those headers is made before T is looked at, so it leaves T unused. MOCK_ENTER_NO_HEADER_CHECK=1 skips every
47-
* header check, as the server did before it asked for them.
42+
* GET /api/auth/shell/enter?ticket=T&to=/path
43+
* Loaded by the web view, which sends the headers `OpenBot-Shell: ios` (or `android`, exactly)
44+
* and `OpenBot-Shell-Verifier: V`. Refused before T is looked at, leaving it unused: no such
45+
* OpenBot-Shell, a Sec-Fetch-Site that is present and not `none`, a Sec-Fetch-Mode that is
46+
* present and not `navigate`. Past those, a known T is spent whatever follows: a T that is
47+
* unknown, used or expired, a missing or malformed V (43 to 128 of A-Z a-z 0-9 - . _ ~), or a
48+
* V whose base64url(sha256(V)) is not T's C is refused. A `verifier` in the query is never
49+
* read, so an enter that carries it only there fails, and spends T. Otherwise it sets the
50+
* cookie better-auth.session_token=mock and answers 303 to `to` (an own path, else /). Every
51+
* refusal is a 303 to /sign?native-sign-in=failed, setting nothing. MOCK_ENTER_NO_HEADER_CHECK=1
52+
* skips the OpenBot-Shell and Sec-Fetch checks, not the verifier's header.
4853
* POST /api/auth/sign-out
4954
* Ends the mock's session: its cookie stops counting until the next /enter.
5055
*
@@ -101,6 +106,12 @@ const ENTER_HEADER_CHECK = process.env.MOCK_ENTER_NO_HEADER_CHECK !== "1";
101106
/** The header the phone app's web view sends on /api/auth/shell/enter, and what it may say. */
102107
const SHELL_HEADER = "OpenBot-Shell";
103108
const SHELL_PLATFORMS = new Set(["ios", "android"]);
109+
/** The header that carries the PKCE verifier on /api/auth/shell/enter: never the address. */
110+
const VERIFIER_HEADER = "OpenBot-Shell-Verifier";
111+
/** A PKCE verifier (RFC 7636): 43 to 128 unreserved characters. */
112+
const VERIFIER = /^[A-Za-z0-9._~-]{43,128}$/;
113+
/** Where the ticket goes: the custom scheme, reverse-domain, with one slash after the colon. */
114+
const SCHEME_CALLBACK = "bot.open.shell:/signed-in";
104115
// MOCK_REPLY=1: a run answers with one canned sentence (the iOS shell's video). Off by default.
105116
const REPLY = process.env.MOCK_REPLY === "1";
106117
const TICKET_MS = 60_000;
@@ -688,13 +699,13 @@ function handoffPage(url: URL): Response {
688699
const challenge = url.searchParams.get("challenge") ?? "";
689700
if (!CHALLENGE.test(challenge))
690701
return new Response("A PKCE S256 challenge is required.", { status: 400 });
691-
const ticket = base64url(randomBytes(24));
702+
const ticket = base64url(randomBytes(32));
692703
tickets.set(ticket, {
693704
challenge,
694705
expires: Date.now() + TICKET_MS,
695706
used: false,
696707
});
697-
const target = escapeHtml(`openbotshell://signed-in?ticket=${ticket}`);
708+
const target = escapeHtml(`${SCHEME_CALLBACK}?ticket=${ticket}`);
698709
const auto = url.searchParams.get("auto") === "1";
699710
const html = `<!doctype html>
700711
<html lang="en">
@@ -728,21 +739,23 @@ function enter(url: URL, request: Request): Response {
728739
if (ENTER_HEADER_CHECK) {
729740
// Only the phone app's web view, which says so in a header a link elsewhere can't set, and
730741
// never a request another site started.
731-
const shell = request.headers.get(SHELL_HEADER)?.toLowerCase() ?? "";
742+
const shell = request.headers.get(SHELL_HEADER) ?? "";
732743
const site = request.headers.get("sec-fetch-site")?.toLowerCase();
733744
const mode = request.headers.get("sec-fetch-mode")?.toLowerCase();
734745
if (!SHELL_PLATFORMS.has(shell)) return failed;
735746
// Only a load the web view started itself: no site, or `none`; a top-level navigation.
736747
if (site !== undefined && site !== "none") return failed;
737748
if (mode !== undefined && mode !== "navigate") return failed;
738749
}
750+
// Spent as it is read, as the server deletes its row: whatever is refused from here, it is gone.
739751
const ticket = tickets.get(url.searchParams.get("ticket") ?? "");
740-
const verifier = url.searchParams.get("verifier") ?? "";
741-
if (!ticket || ticket.used || ticket.expires < Date.now() || !verifier)
742-
return failed;
752+
if (!ticket || ticket.used || ticket.expires < Date.now()) return failed;
753+
ticket.used = true;
754+
// From its header only: a `verifier` in the address is not read.
755+
const verifier = request.headers.get(VERIFIER_HEADER) ?? "";
756+
if (!VERIFIER.test(verifier)) return failed;
743757
const challenge = base64url(createHash("sha256").update(verifier).digest());
744758
if (challenge !== ticket.challenge) return failed;
745-
ticket.used = true;
746759
mockSessionEnded = false;
747760
return new Response(null, {
748761
status: 303,
@@ -811,7 +824,7 @@ async function start() {
811824
return Response.json({
812825
handoff: true,
813826
callback: "scheme",
814-
callbackUrl: "openbotshell://signed-in",
827+
callbackUrl: SCHEME_CALLBACK,
815828
});
816829
}
817830

0 commit comments

Comments
 (0)