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

+14 -1
View File
@@ -8,6 +8,7 @@ import {
} from "../publishedRuleAccess";
import { prisma } from "./db";
import { isDatabaseConfigured } from "./env";
import { rateLimitKey } from "./rateLimit";
import { browserHoldsClaimForHash } from "./guestRuleClaim";
import { isRuleAdminEmail } from "./ruleAdmins";
import { logAdminRuleRead } from "./adminRuleReadLog";
@@ -140,12 +141,16 @@ async function resolveAccessForRow(opts: {
* invalidates it. Returns `null` for missing rows, denied readers, and DB
* failures — callers render the same 404 as a missing Rule.
*/
const SHARE_LOOKUP_MISS_INTERVAL_MS = 1_000;
export async function getReadablePublishedRule(opts: {
idOrSlug: string;
user: User | null;
viaShareSlug: boolean;
requestPath: string;
lookup?: "id" | "shareSlug";
/** Client IP (or similar) used to slow failed `/r/{slug}` guesses. */
failedLookupKey?: string;
}): Promise<ReadablePublishedRule | null> {
if (!isDatabaseConfigured()) return null;
const idOrSlug = opts.idOrSlug.trim();
@@ -162,7 +167,15 @@ export async function getReadablePublishedRule(opts: {
where: { id: idOrSlug },
select: RULE_ACCESS_SELECT,
});
if (!row) return null;
if (!row) {
if (lookup === "shareSlug" && opts.failedLookupKey) {
rateLimitKey(
`share-lookup-miss:${opts.failedLookupKey}`,
SHARE_LOOKUP_MISS_INTERVAL_MS,
);
}
return null;
}
return resolveAccessForRow({
row,
viaShareSlug: opts.viaShareSlug,