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:
1 parent
0fe2624002
commit
75649a9a42
18 files changed
+249
-19
No files matched your search
@@ -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,
|
||||
|
||||
@@ -1,19 +1,55 @@
|
||||
import { createHmac, timingSafeEqual } from "crypto";
|
||||
|
||||
export function ruleExportToken(ruleId: string, pepper: string): string {
|
||||
/** How long a mailed export link stays valid after it is issued. */
|
||||
export const RULE_EXPORT_TOKEN_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
function signExportMac(
|
||||
ruleId: string,
|
||||
version: number,
|
||||
exp: number,
|
||||
pepper: string,
|
||||
): string {
|
||||
return createHmac("sha256", pepper)
|
||||
.update(`rule-export:${ruleId}`)
|
||||
.update(`rule-export:${ruleId}:${version}:${exp}`)
|
||||
.digest("base64url");
|
||||
}
|
||||
|
||||
function timingSafeStringEqual(left: string, right: string): boolean {
|
||||
const expected = Buffer.from(left);
|
||||
const actual = Buffer.from(right);
|
||||
if (expected.length !== actual.length) return false;
|
||||
return timingSafeEqual(expected, actual);
|
||||
}
|
||||
|
||||
export function ruleExportToken(
|
||||
ruleId: string,
|
||||
pepper: string,
|
||||
version: number,
|
||||
now = new Date(),
|
||||
): string {
|
||||
const exp = Math.floor((now.getTime() + RULE_EXPORT_TOKEN_TTL_MS) / 1000);
|
||||
const mac = signExportMac(ruleId, version, exp, pepper);
|
||||
return `${exp}.${version}.${mac}`;
|
||||
}
|
||||
|
||||
export function ruleExportTokenMatches(
|
||||
ruleId: string,
|
||||
token: string,
|
||||
pepper: string,
|
||||
version: number,
|
||||
now = new Date(),
|
||||
): boolean {
|
||||
if (token.length === 0) return false;
|
||||
const expected = Buffer.from(ruleExportToken(ruleId, pepper));
|
||||
const actual = Buffer.from(token);
|
||||
if (expected.length !== actual.length) return false;
|
||||
return timingSafeEqual(expected, actual);
|
||||
const parts = token.split(".");
|
||||
if (parts.length !== 3) return false;
|
||||
const exp = Number(parts[0]);
|
||||
const tokenVersion = Number(parts[1]);
|
||||
const mac = parts[2] ?? "";
|
||||
if (!Number.isFinite(exp) || !Number.isInteger(tokenVersion) || mac.length === 0) {
|
||||
return false;
|
||||
}
|
||||
if (tokenVersion !== version) return false;
|
||||
if (exp * 1000 <= now.getTime()) return false;
|
||||
const expected = signExportMac(ruleId, version, exp, pepper);
|
||||
return timingSafeStringEqual(expected, mac);
|
||||
}
|
||||
@@ -46,6 +46,7 @@ export async function sendDueRuleFollowUpEmails(
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
exportTokenVersion: true,
|
||||
user: { select: { email: true } },
|
||||
},
|
||||
});
|
||||
@@ -66,7 +67,7 @@ export async function sendDueRuleFollowUpEmails(
|
||||
continue;
|
||||
}
|
||||
|
||||
const token = ruleExportToken(row.id, pepper);
|
||||
const token = ruleExportToken(row.id, pepper, row.exportTokenVersion);
|
||||
try {
|
||||
await sendRuleFollowUpEmail({
|
||||
to: email,
|
||||
|
||||
@@ -147,11 +147,18 @@ export async function patchOwnedRuleShare(opts: {
|
||||
shareSlug = await allocateShareSlug();
|
||||
}
|
||||
|
||||
const turningLinkOff =
|
||||
opts.visibility === RULE_VISIBILITY.private &&
|
||||
row.visibility !== RULE_VISIBILITY.private;
|
||||
|
||||
await prisma.publishedRule.update({
|
||||
where: { id: row.id },
|
||||
data: {
|
||||
visibility: opts.visibility,
|
||||
shareSlug,
|
||||
...(turningLinkOff
|
||||
? { exportTokenVersion: { increment: 1 } }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user