Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/web/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/sign-in/sign-in-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
}

Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/lib/api.test.ts
Original file line number Diff line number Diff line change
@@ -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("/");
});
});
8 changes: 6 additions & 2 deletions apps/web/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export async function api<T = unknown>(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);
}
2 changes: 2 additions & 0 deletions docs/features/github-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
Loading