From 198a5647950ac19c3786ac91e43901e97c1ebe8b Mon Sep 17 00:00:00 2001 From: Erik Goins Date: Wed, 18 Nov 2026 16:38:38 -0500 Subject: [PATCH] fix: sign-in render crash in dev, and allow the tunnel host in next dev safeNext read window.location during the sign-in form's render, so next dev threw "window is not defined" and fell back to client rendering. The form now calls it in the submit handler, and safeNext uses a fixed origin when window does not exist (it returns only a path). A Node unit test guards it. next dev blocks its dev resources for hosts other than localhost, which broke the app behind a cloudflared tunnel (needed for GitHub App setup). next.config now allows the BETTER_AUTH_URL host in dev. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T7sHyaEvdoSPjkLy7T1aaz Signed-off-by: Erik Goins --- apps/web/next.config.ts | 13 +++++++++++++ apps/web/src/app/sign-in/sign-in-form.tsx | 4 ++-- apps/web/src/lib/api.test.ts | 14 ++++++++++++++ apps/web/src/lib/api.ts | 8 ++++++-- docs/features/github-app.md | 2 ++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 apps/web/src/lib/api.test.ts diff --git a/apps/web/next.config.ts b/apps/web/next.config.ts index 5137c52..e5796a5 100644 --- a/apps/web/next.config.ts +++ b/apps/web/next.config.ts @@ -13,7 +13,20 @@ const runtime = // from the repo root so the workspace packages come along. const standalone = process.env.GITWORK_STANDALONE === "1"; +// `next dev` blocks its dev resources (HMR, client chunks) for any host but localhost. A tunnel for +// GitHub webhooks (features/github-app.md) serves the app at BETTER_AUTH_URL, so allow that host. +// Dev only: production builds ignore allowedDevOrigins. +function authHost(): string | undefined { + try { + return new URL(process.env.BETTER_AUTH_URL ?? "").hostname; + } catch { + return undefined; + } +} +const devHost = authHost(); + const nextConfig: NextConfig = { + ...(devHost && devHost !== "localhost" && { allowedDevOrigins: [devHost] }), ...(standalone && { output: "standalone", outputFileTracingRoot: fileURLToPath(new URL("../..", import.meta.url)) }), // cacheComponents stays off: under workerd its setTimeout dependency hangs every request. reactCompiler: true, diff --git a/apps/web/src/app/sign-in/sign-in-form.tsx b/apps/web/src/app/sign-in/sign-in-form.tsx index 43dd207..a127745 100644 --- a/apps/web/src/app/sign-in/sign-in-form.tsx +++ b/apps/web/src/app/sign-in/sign-in-form.tsx @@ -8,7 +8,6 @@ import { authClient } from "@/lib/auth-client"; export function SignInForm() { const router = useRouter(); const params = useSearchParams(); - const next = safeNext(params.get("next")); const [email, setEmail] = useState(params.get("email") ?? ""); const [code, setCode] = useState(""); const [step, setStep] = useState<"email" | "code">("email"); @@ -32,7 +31,8 @@ export function SignInForm() { const { error } = await authClient.signIn.emailOtp({ email, otp: code.trim() }); setBusy(false); if (error) return setError(error.message ?? "That code did not work."); - router.push(next); + // safeNext reads window, so call it here (browser only), never during the server render. + router.push(safeNext(params.get("next"))); router.refresh(); } diff --git a/apps/web/src/lib/api.test.ts b/apps/web/src/lib/api.test.ts new file mode 100644 index 0000000..aeb2109 --- /dev/null +++ b/apps/web/src/lib/api.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { safeNext } from "./api"; + +// Vitest runs in Node, where `window` is undefined, as in a server render. Regression: the sign-in +// form called safeNext during render, and `next dev` threw "window is not defined". +describe("safeNext without window", () => { + it("keeps same-site paths and rejects off-site ones", () => { + expect(typeof window).toBe("undefined"); + expect(safeNext("/w/abc?tab=list#x")).toBe("/w/abc?tab=list#x"); + expect(safeNext("//evil.com")).toBe("/"); + expect(safeNext("https://evil.com")).toBe("/"); + expect(safeNext(null)).toBe("/"); + }); +}); diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts index 0d0cc6b..4dcc90c 100644 --- a/apps/web/src/lib/api.ts +++ b/apps/web/src/lib/api.ts @@ -14,7 +14,11 @@ export async function api(method: string, path: string, body?: unkn return data as T; } -/** Only same-site paths, so `?next=` cannot redirect off-site. */ +/** + * Only same-site paths, so `?next=` cannot redirect off-site. It returns a path, so any origin gives + * the same answer: a fixed one stands in during the server render, where `window` does not exist. + */ export function safeNext(value: string | null): string { - return safeRedirectPath(value, window.location.origin); + const origin = typeof window === "undefined" ? "http://localhost" : window.location.origin; + return safeRedirectPath(value, origin); } diff --git a/docs/features/github-app.md b/docs/features/github-app.md index 3684bc2..d2fc8e6 100644 --- a/docs/features/github-app.md +++ b/docs/features/github-app.md @@ -105,6 +105,8 @@ them and prunes the ones no workspace uses (features/tasks.md). - **GitHub must reach the webhook URL.** For local development use a tunnel (e.g. `cloudflared tunnel --url http://localhost:3000`) and set `BETTER_AUTH_URL` to it *before* creating the App: the manifest bakes in the URLs. + Restart `pnpm dev` after you set it: `next.config.ts` adds that host to `allowedDevOrigins`, + or `next dev` blocks its scripts when you open the app through the tunnel. - **Rotating `BETTER_AUTH_SECRET`** makes the stored App credentials and linked GitHub tokens unreadable. Sign-in keeps working (`createRequestDeps()` treats GitHub as not set up and sets `githubError`); `/setup` shows the error and offers "Forget the stored App and start over".