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".