Add a content security policy and expire mailed export links.

Responses had no CSP, the 48-hour download token never changed, and short share slugs could be guessed without a miss limit.
This commit is contained in:
adilallo committed 2026-11-21 11:46:16 -07:00
1 parent 0fe2624002
commit 75649a9a42
18 files changed
+249 -19

No files matched your search

+17 -1
View File
@@ -1,8 +1,10 @@
import type { Metadata } from "next";
import { headers } from "next/headers";
import { notFound } from "next/navigation";
import messages from "../../../../messages/en/index";
import { isShareSlug } from "../../../../lib/sharePaths";
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
import { rateLimitKey } from "../../../../lib/server/rateLimit";
import { getSessionUser } from "../../../../lib/server/session";
import { parsePublishedDocumentForCommunityRuleDisplay } from "../../../../lib/create/publishedDocumentToDisplaySections";
import { NO_INDEX_ROBOTS, routeMetadata } from "../../../../lib/siteMetadata";
@@ -13,14 +15,28 @@ interface PageProps {
params: Promise<{ shareSlug: string }>;
}
async function clientIp(): Promise<string> {
const headerStore = await headers();
return (
headerStore.get("x-forwarded-for")?.split(",")[0]?.trim() ||
headerStore.get("x-real-ip") ||
"unknown"
);
}
async function loadSharedRule(shareSlug: string) {
if (!isShareSlug(shareSlug)) return null;
const ip = await clientIp();
if (!isShareSlug(shareSlug)) {
rateLimitKey(`share-lookup-miss:${ip}`, 1_000);
return null;
}
const user = await getSessionUser();
return getReadablePublishedRule({
idOrSlug: shareSlug,
user,
viaShareSlug: true,
requestPath: `/r/${shareSlug}`,
failedLookupKey: ip,
});
}
+7 -1
View File
@@ -52,6 +52,7 @@ export const GET = apiRoute<RouteContext>(
document: true,
createdAt: true,
userId: true,
exportTokenVersion: true,
},
});
if (!row) return notFound();
@@ -60,7 +61,12 @@ export const GET = apiRoute<RouteContext>(
let tokenOk = false;
if (token.length > 0) {
try {
tokenOk = ruleExportTokenMatches(row.id, token, getSessionPepper());
tokenOk = ruleExportTokenMatches(
row.id,
token,
getSessionPepper(),
row.exportTokenVersion,
);
} catch {
tokenOk = false;
}
@@ -42,6 +42,10 @@ export const DELETE = apiRoute<RouteContext>(
}
await prisma.ruleStakeholder.delete({ where: { id: row.id } });
await prisma.publishedRule.update({
where: { id: ruleId },
data: { exportTokenVersion: { increment: 1 } },
});
return NextResponse.json({ ok: true });
},