Open source CapCut alternative.
TypeScript
fix(site): the super-user host gates in the proxy, so only a super user is served the section
David Led539df277de0
diff --git a/site/src/app/su/SuShell.tsx b/site/src/app/su/SuShell.tsxindex 7664564e..7e95ad53 100644--- a/site/src/app/su/SuShell.tsx+++ b/site/src/app/su/SuShell.tsx@@ -1,39 +1,15 @@"use client";-import { useEffect, type ReactNode } from "react";+import type { ReactNode } from "react";import { NoSessionReplay } from "@/app/_components/NoSessionReplay";import { SuHeader } from "@/app/su/SuHeader";import { SuSidebar } from "@/app/su/SuSidebar";-import { Skeleton } from "@/components/ui/skeleton";-import { SU_APP_ORIGIN } from "@/cut/lib/hosts";-import { authClient } from "@/lib/auth-client";-import { useAccount } from "@/queries/credits";-// The section's own gate and two-pane shell. Sign-in lives on the app's host,-// so a signed-out visitor leaves for it with this page's full address as the-// post-auth callback, and anyone signed in without the role is sent to the app.-// Both gates are for UX; the routes these pages call are withSuperUser and-// enforce the role server-side.+// The section's two-pane shell. It mounts only for a super user: the proxy+// (src/proxy.ts) has already checked the session and the role before the+// route runs.export function SuShell({ children }: { children: ReactNode }) {- const { data: session, isPending: sessionPending } = authClient.useSession();- const account = useAccount();- const signedOut = !sessionPending && !session;- const superUser = account.data?.superUser === true;-- useEffect(() => {- if (!signedOut) return;- const here = encodeURIComponent(window.location.href);- window.location.replace(`${SU_APP_ORIGIN}/sign-in?callbackURL=${here}`);- }, [signedOut]);-- // Only a definitive answer sends someone away: a failed account request- // leaves the page waiting rather than bouncing the operator out mid-session.- useEffect(() => {- if (signedOut || !account.isSuccess || superUser) return;- window.location.replace(`${SU_APP_ORIGIN}/app`);- }, [account.isSuccess, signedOut, superUser]);-// The section paints the white product surface. `app-surface` is what// repoints the --background token for the whole document (see globals.css),// so it belongs on the outermost element here.@@ -49,27 +25,18 @@ export function SuShell({ children }: { children: ReactNode }) {className="app-surface flex h-screen bg-background font-system text-foreground antialiased"><NoSessionReplay />- {superUser ? (- <>- <SuSidebar />- <main className="flex min-w-0 flex-1 flex-col overflow-y-auto">- <SuHeader />- <div className="min-h-0 flex-1">- <div className="mx-auto h-full w-full max-w-6xl px-10">- {/* The header is sticky and opaque, so the top of the gap- below it lives here: a focus ring on the first control- needs room that the header cannot paint over. */}- <div className="h-full px-px pt-1 pb-px">{children}</div>- </div>- </div>- </main>- </>- ) : (- <div className="mx-auto w-full max-w-3xl px-8 py-10">- <Skeleton className="h-8 w-48" />- <Skeleton className="mt-6 h-40 w-full" />+ <SuSidebar />+ <main className="flex min-w-0 flex-1 flex-col overflow-y-auto">+ <SuHeader />+ <div className="min-h-0 flex-1">+ <div className="mx-auto h-full w-full max-w-6xl px-10">+ {/* The header is sticky and opaque, so the top of the gap below+ it lives here: a focus ring on the first control needs room+ that the header cannot paint over. */}+ <div className="h-full px-px pt-1 pb-px">{children}</div>+ </div></div>- )}+ </main></div>);}diff --git a/site/src/app/su/layout.tsx b/site/src/app/su/layout.tsxindex e8448d9d..5883b554 100644--- a/site/src/app/su/layout.tsx+++ b/site/src/app/su/layout.tsx@@ -4,9 +4,11 @@ import type { ReactNode } from "react";import { SuShell } from "@/app/su/SuShell";// The super-user section, served on its own host (src/proxy.ts maps-// su.donkeycut.com onto this tree). It sits outside the Cut app's layout so-// none of the product shell — the engine connection gate, the exports dock,-// the welcome sequence — mounts on an admin page.+// su.donkeycut.com onto this tree, and only for a super user: the proxy checks+// the session and the role before the route runs, so nothing here renders —+// or ships its client code — to anyone else). It sits outside the Cut app's+// layout so none of the product shell — the engine connection gate, the+// exports dock, the welcome sequence — mounts on an admin page.export const metadata: Metadata = {robots: { follow: false, index: false },title: "Super user",diff --git a/site/src/proxy.ts b/site/src/proxy.tsindex a809a210..ac60eb78 100644--- a/site/src/proxy.ts+++ b/site/src/proxy.ts@@ -1,7 +1,13 @@import { NextResponse, type NextRequest } from "next/server";import { allowedOrigin, corsHeaders, preflightHeaders } from "@/cut/server/cors";-import { DONKEYCUT_CANONICAL, isDonkeycutHost, isSuHost } from "@/cut/lib/hosts";+import {+ DONKEYCUT_CANONICAL,+ SU_APP_ORIGIN,+ SU_ORIGIN,+ isDonkeycutHost,+ isSuHost,+} from "@/cut/lib/hosts";// Cut (the video editor, publicly "Donkey Cut") lives under /cut in this single// site app: the marketing landing at /cut and the app under /cut/app. The@@ -75,12 +81,47 @@ const passesThrough = (pathname: string) =>PASSTHROUGH.some((p) => underPath(pathname, p));// The super-user host serves one section and nothing else: /su/… for pages,-// the shared /api handlers for their data. Nothing here belongs in a search-// index, so every page response carries the header that says so.+// the shared /api handlers for their data.+//+// The role gate lives here, ahead of the route. A page's document preloads+// the client code of every segment on its path whether or not that segment+// renders, so a gate inside the route tree would still hand the section's+// bundle — the charts, the tables, the shell — to whoever asked for the+// address. Here the proxy asks the account route who is calling (the session+// cookie is scoped to the apex, so it reads the same on this host) and only a+// super user reaches a page; everyone else gets a redirect and no document.+// Sign-in lives on the app's host, so a signed-out visitor leaves for it with+// this address as the post-auth callback, and a signed-in visitor without the+// role is sent to the app. The routes the pages call are withSuperUser and+// enforce the role again on every request.+//+// Nothing here belongs in a search index, so every page response carries the+// header that says so.const SU_ROOT = "/su";-function suHost(req: NextRequest, pathname: string): NextResponse {+async function suHost(req: NextRequest, pathname: string): Promise<NextResponse> {if (underPath(pathname, "/api")) return NextResponse.next();++ const account = await fetch(`${SU_APP_ORIGIN}/api/account/me`, {+ cache: "no-store",+ headers: { cookie: req.headers.get("cookie") ?? "" },+ });+ if (account.status === 401) {+ // The callback has to carry the exact origin sign-in trusts (src/lib/auth.ts),+ // so hosted names it from the constant; dev's su.localhost is plain http on+ // whatever host the browser asked for.+ const origin = HOSTED ? SU_ORIGIN : `http://${req.headers.get("host")}`;+ const here = `${origin}${pathname}${req.nextUrl.search}`;+ return NextResponse.redirect(+ `${SU_APP_ORIGIN}/sign-in?callbackURL=${encodeURIComponent(here)}`,+ );+ }+ if (!account.ok) {+ return new NextResponse("The super-user gate is unavailable.", { status: 503 });+ }+ const { superUser } = (await account.json()) as { superUser: boolean };+ if (superUser !== true) return NextResponse.redirect(`${SU_APP_ORIGIN}/app`);+const url = req.nextUrl.clone();url.pathname = `${SU_ROOT}${pathname === "/" ? "" : pathname}`;const res = NextResponse.rewrite(url);