-
-
Notifications
You must be signed in to change notification settings - Fork 90
fix(selfhost): require setup token for app wizard #1233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JSONbored
merged 3 commits into
main
from
codex/fix-public-first-run-setup-vulnerability
Jun 24, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,14 @@ import { serve } from "@hono/node-server"; | |
| import worker from "./index"; | ||
| import { processJob } from "./queue/processors"; | ||
| import { createSelfHostAi } from "./selfhost/ai"; | ||
| import { credentialsToEnv, exchangeManifestCode, renderSetupPage } from "./selfhost/setup-wizard"; | ||
| import { | ||
| cookieValue, | ||
| credentialsToEnv, | ||
| exchangeManifestCode, | ||
| isValidSetupAuthCookie, | ||
| renderSetupPage, | ||
| setupAuthCookieValue, | ||
| } from "./selfhost/setup-wizard"; | ||
| import { exportOrbBatch } from "./selfhost/orb-collector"; | ||
| import { createD1Adapter, nodeSqliteDriver } from "./selfhost/d1-adapter"; | ||
| import { readiness } from "./selfhost/health"; | ||
|
|
@@ -237,6 +244,10 @@ async function main(): Promise<void> { | |
| if (path === "/metrics") return new Response(await renderMetrics(), { headers: { "content-type": "text/plain; version=0.0.4" } }); | ||
| // First-run GitHub App setup wizard — only while no App is configured (can't rebind a live install). | ||
| if ((path === "/setup" || path === "/setup/callback") && !process.env.GITHUB_APP_ID) { | ||
| const setupToken = process.env.SELFHOST_SETUP_TOKEN; | ||
| if (!setupToken) { | ||
| return new Response("SELFHOST_SETUP_TOKEN must be set before using the setup wizard", { status: 400 }); | ||
| } | ||
| // PUBLIC_API_ORIGIN is required: falling back to request.url.origin would let an attacker spoof | ||
| // the Host header and redirect the App-creation callback to an attacker-controlled domain, where | ||
| // they could exchange the one-time code for the App private key and webhook secret. | ||
|
|
@@ -248,13 +259,20 @@ async function main(): Promise<void> { | |
| ); | ||
| } | ||
| if (path === "/setup") { | ||
| const suppliedToken = | ||
| new URL(request.url).searchParams.get("token") ?? | ||
| request.headers.get("x-setup-token") ?? | ||
| request.headers.get("authorization")?.replace(/^Bearer\s+/i, ""); | ||
| if (suppliedToken !== setupToken) return new Response("invalid setup token", { status: 403 }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Setup token compared with timing-unsafe string operator Setup token comparison uses Use AI prompt |
||
| // Generate a per-visit CSRF nonce, embed it in the manifest's redirect_url, and bind it to | ||
| // this browser session via an HttpOnly cookie so the callback can validate it. | ||
| // this browser session via an HttpOnly signed cookie so the callback can validate it came | ||
| // from an operator-authorized setup visit, not just any unauthenticated browser. | ||
| const state = randomUUID(); | ||
| return new Response(renderSetupPage(origin, state), { | ||
| headers: { | ||
| "content-type": "text/html; charset=utf-8", | ||
| "Set-Cookie": `setup_state=${state}; Path=/setup; HttpOnly; SameSite=Lax; Max-Age=3600`, | ||
| "Referrer-Policy": "no-referrer", | ||
| "Set-Cookie": `setup_auth=${setupAuthCookieValue(setupToken, state)}; Path=/setup; HttpOnly; SameSite=Lax; Max-Age=3600`, | ||
| }, | ||
| }); | ||
| } | ||
|
|
@@ -264,8 +282,8 @@ async function main(): Promise<void> { | |
| // Validate the CSRF state: must match the cookie set when /setup was served. | ||
| const stateParam = params.get("state"); | ||
| const cookieHeader = request.headers.get("cookie") ?? ""; | ||
| const cookieState = cookieHeader.split(";").map((c) => c.trim()).find((c) => c.startsWith("setup_state="))?.slice("setup_state=".length); | ||
| if (!stateParam || !cookieState || stateParam !== cookieState) { | ||
| const setupAuth = cookieValue(cookieHeader, "setup_auth"); | ||
| if (!stateParam || !isValidSetupAuthCookie(setupToken, stateParam, setupAuth)) { | ||
| return new Response("invalid state parameter", { status: 403 }); | ||
| } | ||
| try { | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Setup token accepted from URL query parameter, leaking secret to logs and history
Setup token can be passed in the URL query string, leaking it to logs and browser history.
Remove query parameter support; accept the setup token only via secure headers.
AI prompt