Skip to content

Commit b50c884

Browse files
ralyodioclaude
andauthored
Auto-allow the ?dn parked domain as a CSP frame-ancestor (#14)
Masked-forwarded parked domains (Porkbun frameset, e.g. moshscript.com) iframe moshcoding.com/?dn=<self>, but the CSP frame-ancestors only listed the domains in FRAME_ANCESTORS, so any domain not hand-added there was blocked by the browser and rendered blank. The framed request carries its own ?dn, so trust it to iframe just its own tenant page — new parked domains now work without editing FRAME_ANCESTORS each time. 'self' + the env list still apply; the dn is hostname-validated before being added. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 6f0b79a commit b50c884

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

middleware.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ import { NextRequest, NextResponse } from "next/server";
44
// e.g. moshcode.sh). Read at RUNTIME so FRAME_ANCESTORS env changes take effect
55
// on redeploy without a code edit. 'self' lets the app frame itself; no other
66
// origin can (anti-clickjacking).
7-
function frameAncestors(): string {
7+
function frameAncestors(req: NextRequest): string {
88
const parked = (process.env.FRAME_ANCESTORS || "https://moshcode.sh").split(/\s+/).filter(Boolean);
9-
return ["'self'", ...parked].join(" ");
9+
const allow = ["'self'", ...parked];
10+
11+
// Auto-allow the parked domain currently being rendered. A masked-forwarded
12+
// domain frames moshcoding.com/?dn=<self>, so the frame request carries its
13+
// own ?dn; trust it to iframe just its own tenant page. This means new parked
14+
// domains work without hand-editing FRAME_ANCESTORS for each one.
15+
const dn = (req.nextUrl.searchParams.get("dn") || "").trim().toLowerCase();
16+
if (dn.length <= 253 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/.test(dn)) {
17+
allow.push(`https://${dn}`, `https://www.${dn}`);
18+
}
19+
return allow.join(" ");
1020
}
1121

1222
// Redirect www.moshcoding.com → https://moshcoding.com (apex, permanent).
@@ -21,7 +31,7 @@ export function middleware(req: NextRequest) {
2131
return NextResponse.redirect(url, 308);
2232
}
2333
const res = NextResponse.next();
24-
res.headers.set("Content-Security-Policy", `frame-ancestors ${frameAncestors()}`);
34+
res.headers.set("Content-Security-Policy", `frame-ancestors ${frameAncestors(req)}`);
2535

2636
// Referral attribution: a ?ref=<code> visit drops a 90-day cookie (first-touch
2737
// — the first ref a visitor arrives with sticks). Signup/waitlist read it so

0 commit comments

Comments
 (0)