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

+21 -7
View File
@@ -1,16 +1,30 @@
import { describe, expect, it } from "vitest";
import {
RULE_EXPORT_TOKEN_TTL_MS,
ruleExportToken,
ruleExportTokenMatches,
} from "../../lib/server/ruleExportToken";
describe("ruleExportToken", () => {
it("accepts the matching token and rejects another rule or token", () => {
const pepper = "test-pepper-16chars";
const token = ruleExportToken("rule-1", pepper);
expect(ruleExportTokenMatches("rule-1", token, pepper)).toBe(true);
expect(ruleExportTokenMatches("rule-2", token, pepper)).toBe(false);
expect(ruleExportTokenMatches("rule-1", `${token}x`, pepper)).toBe(false);
expect(ruleExportTokenMatches("rule-1", "", pepper)).toBe(false);
const pepper = "test-pepper-16chars";
const now = new Date("2026-11-21T12:00:00.000Z");
it("accepts the matching token and rejects another rule, version, or token", () => {
const token = ruleExportToken("rule-1", pepper, 0, now);
expect(ruleExportTokenMatches("rule-1", token, pepper, 0, now)).toBe(true);
expect(ruleExportTokenMatches("rule-2", token, pepper, 0, now)).toBe(false);
expect(ruleExportTokenMatches("rule-1", token, pepper, 1, now)).toBe(false);
expect(ruleExportTokenMatches("rule-1", `${token}x`, pepper, 0, now)).toBe(
false,
);
expect(ruleExportTokenMatches("rule-1", "", pepper, 0, now)).toBe(false);
});
it("rejects a token after it expires", () => {
const token = ruleExportToken("rule-1", pepper, 0, now);
const afterExpiry = new Date(now.getTime() + RULE_EXPORT_TOKEN_TTL_MS + 1);
expect(
ruleExportTokenMatches("rule-1", token, pepper, 0, afterExpiry),
).toBe(false);
});
});