|
17 | 17 | * # As a server without the phone app's sign-in hand-off: every /api/auth/shell/* answers 404. |
18 | 18 | * MOCK_NO_HANDOFF=1 MOCK_SIGNED_OUT=1 bun app/workbench/mock-server.ts |
19 | 19 | * |
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: |
22 | 23 | * MOCK_ENTER_NO_HEADER_CHECK=1 bun app/workbench/mock-server.ts |
23 | 24 | * |
24 | 25 | * # Another person in another organization (Sam Rivera in Acme when unset), say for a second mock |
|
31 | 32 | * POST /__mock/session {"signedIn": bool} signed in, and to change it at run time. |
32 | 33 | * GET /api/auth/shell/config?platform=ios |
33 | 34 | * 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. |
35 | 36 | * GET /api/auth/shell/handoff?challenge=C[&platform=ios][&auto=1] |
36 | 37 | * The page the phone app opens in its sign-in sheet. C is a PKCE S256 challenge (43 |
37 | 38 | * 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. |
39 | 41 | * 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. |
48 | 53 | * POST /api/auth/sign-out |
49 | 54 | * Ends the mock's session: its cookie stops counting until the next /enter. |
50 | 55 | * |
@@ -101,6 +106,12 @@ const ENTER_HEADER_CHECK = process.env.MOCK_ENTER_NO_HEADER_CHECK !== "1"; |
101 | 106 | /** The header the phone app's web view sends on /api/auth/shell/enter, and what it may say. */ |
102 | 107 | const SHELL_HEADER = "OpenBot-Shell"; |
103 | 108 | 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"; |
104 | 115 | // MOCK_REPLY=1: a run answers with one canned sentence (the iOS shell's video). Off by default. |
105 | 116 | const REPLY = process.env.MOCK_REPLY === "1"; |
106 | 117 | const TICKET_MS = 60_000; |
@@ -688,13 +699,13 @@ function handoffPage(url: URL): Response { |
688 | 699 | const challenge = url.searchParams.get("challenge") ?? ""; |
689 | 700 | if (!CHALLENGE.test(challenge)) |
690 | 701 | return new Response("A PKCE S256 challenge is required.", { status: 400 }); |
691 | | - const ticket = base64url(randomBytes(24)); |
| 702 | + const ticket = base64url(randomBytes(32)); |
692 | 703 | tickets.set(ticket, { |
693 | 704 | challenge, |
694 | 705 | expires: Date.now() + TICKET_MS, |
695 | 706 | used: false, |
696 | 707 | }); |
697 | | - const target = escapeHtml(`openbotshell://signed-in?ticket=${ticket}`); |
| 708 | + const target = escapeHtml(`${SCHEME_CALLBACK}?ticket=${ticket}`); |
698 | 709 | const auto = url.searchParams.get("auto") === "1"; |
699 | 710 | const html = `<!doctype html> |
700 | 711 | <html lang="en"> |
@@ -728,21 +739,23 @@ function enter(url: URL, request: Request): Response { |
728 | 739 | if (ENTER_HEADER_CHECK) { |
729 | 740 | // Only the phone app's web view, which says so in a header a link elsewhere can't set, and |
730 | 741 | // never a request another site started. |
731 | | - const shell = request.headers.get(SHELL_HEADER)?.toLowerCase() ?? ""; |
| 742 | + const shell = request.headers.get(SHELL_HEADER) ?? ""; |
732 | 743 | const site = request.headers.get("sec-fetch-site")?.toLowerCase(); |
733 | 744 | const mode = request.headers.get("sec-fetch-mode")?.toLowerCase(); |
734 | 745 | if (!SHELL_PLATFORMS.has(shell)) return failed; |
735 | 746 | // Only a load the web view started itself: no site, or `none`; a top-level navigation. |
736 | 747 | if (site !== undefined && site !== "none") return failed; |
737 | 748 | if (mode !== undefined && mode !== "navigate") return failed; |
738 | 749 | } |
| 750 | + // Spent as it is read, as the server deletes its row: whatever is refused from here, it is gone. |
739 | 751 | 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; |
743 | 757 | const challenge = base64url(createHash("sha256").update(verifier).digest()); |
744 | 758 | if (challenge !== ticket.challenge) return failed; |
745 | | - ticket.used = true; |
746 | 759 | mockSessionEnded = false; |
747 | 760 | return new Response(null, { |
748 | 761 | status: 303, |
@@ -811,7 +824,7 @@ async function start() { |
811 | 824 | return Response.json({ |
812 | 825 | handoff: true, |
813 | 826 | callback: "scheme", |
814 | | - callbackUrl: "openbotshell://signed-in", |
| 827 | + callbackUrl: SCHEME_CALLBACK, |
815 | 828 | }); |
816 | 829 | } |
817 | 830 |
|
|
0 commit comments