fix v2 hardening review findings

This commit is contained in:
ajnart committed 2026-07-28 15:28:58 +02:00
1 parent b82a6a7abc
commit a7eae3ff81
35 files changed
+1147 -277

No files matched your search

@@ -15,12 +15,50 @@ const managedLabels = [
"reports:delete",
"users:update",
];
const legacyCreateRules = [
{ label: "submissions:create", audience: "@auth", duration: 60, maxRequests: 10 },
{ label: "votes:create", audience: "@auth", duration: 10, maxRequests: 20 },
{ label: "comments:create", audience: "@auth", duration: 60, maxRequests: 20 },
{ label: "reports:create", audience: "@auth", duration: 60, maxRequests: 5 },
];
const legacyCreateLabels = legacyCreateRules.map((rule) => rule.label);
const cloneJson = (value) => JSON.parse(JSON.stringify(value));
const withoutManagedRules = (rules) => rules.filter((rule) => !managedLabels.includes(rule.label));
const managedRules = (rules) => rules.filter((rule) => managedLabels.includes(rule.label));
const deriveLegacyWriteRateLimitState = (snapshot) => ({
enabled: true,
managedRules: managedRules([
...snapshot.rateLimits.rules.filter((rule) => !legacyCreateLabels.includes(rule.label)),
...legacyCreateRules,
]),
});
const findMigrationState = (app) => {
try {
const collection = app.findCollectionByNameOrId("workshop_migration_state");
const record = app.findFirstRecordByFilter(collection.id, "");
const snapshot = JSON.parse(record.getString("snapshot"));
if (!snapshot || typeof snapshot !== "object" || Array.isArray(snapshot)) {
throw new Error("Workshop migration state snapshot is invalid");
}
return { record, snapshot };
} catch {
throw new Error("Workshop write rate-limit migration state is unavailable");
}
};
migrate(
(app) => {
const settings = app.settings();
const state = findMigrationState(app);
state.snapshot.writeRateLimits = {
enabled: settings.rateLimits.enabled,
managedRules: cloneJson(managedRules(settings.rateLimits.rules)),
};
state.record.set("snapshot", JSON.stringify(state.snapshot));
app.save(state.record);
settings.rateLimits.enabled = true;
settings.rateLimits.rules = [
...withoutManagedRules(settings.rateLimits.rules),
@@ -42,13 +80,13 @@ migrate(
},
(app) => {
const settings = app.settings();
settings.rateLimits.rules = [
...withoutManagedRules(settings.rateLimits.rules),
{ label: "submissions:create", audience: "@auth", duration: 60, maxRequests: 10 },
{ label: "votes:create", audience: "@auth", duration: 10, maxRequests: 20 },
{ label: "comments:create", audience: "@auth", duration: 60, maxRequests: 20 },
{ label: "reports:create", audience: "@auth", duration: 60, maxRequests: 5 },
];
const snapshot = findMigrationState(app).snapshot;
const state = snapshot.writeRateLimits || deriveLegacyWriteRateLimitState(snapshot);
if (!state || typeof state.enabled !== "boolean" || !Array.isArray(state.managedRules)) {
throw new Error("Workshop write rate-limit rollback snapshot is missing or invalid");
}
settings.rateLimits.enabled = state.enabled;
settings.rateLimits.rules = [...withoutManagedRules(settings.rateLimits.rules), ...cloneJson(state.managedRules)];
app.save(settings);
},
);
@@ -9,7 +9,6 @@ const legacyReportUpdateRule =
const identityFieldsUnchanged = [
"@request.body.email:changed = false",
"@request.body.displayName:changed = false",
"@request.body.avatar:changed = false",
"@request.body.avatarUrl:changed = false",
"@request.body.githubUsername:changed = false",
"@request.body.githubProfileUrl:changed = false",
@@ -0,0 +1,61 @@
/// <reference path="../pb_data/types.d.ts" />
const managedLabels = [
"submissions:create",
"submissions:update",
"submissions:delete",
"votes:create",
"votes:update",
"votes:delete",
"comments:create",
"comments:update",
"comments:delete",
"reports:create",
"reports:update",
"reports:delete",
"users:update",
];
const legacyCreateRules = [
{ label: "submissions:create", audience: "@auth", duration: 60, maxRequests: 10 },
{ label: "votes:create", audience: "@auth", duration: 10, maxRequests: 20 },
{ label: "comments:create", audience: "@auth", duration: 60, maxRequests: 20 },
{ label: "reports:create", audience: "@auth", duration: 60, maxRequests: 5 },
];
const legacyCreateLabels = legacyCreateRules.map((rule) => rule.label);
const managedRules = (rules) => rules.filter((rule) => managedLabels.includes(rule.label));
const findMigrationState = (app) => {
try {
const collection = app.findCollectionByNameOrId("workshop_migration_state");
const record = app.findFirstRecordByFilter(collection.id, "");
const snapshot = JSON.parse(record.getString("snapshot"));
if (!snapshot || typeof snapshot !== "object" || Array.isArray(snapshot)) {
throw new Error("Workshop migration state snapshot is invalid");
}
return { record, snapshot };
} catch {
throw new Error("Workshop rate-limit compatibility state is unavailable");
}
};
migrate(
(app) => {
const state = findMigrationState(app);
if (state.snapshot.writeRateLimits) return;
if (!state.snapshot.rateLimits || !Array.isArray(state.snapshot.rateLimits.rules)) {
throw new Error("Workshop legacy rate-limit snapshot is missing or invalid");
}
state.snapshot.writeRateLimits = {
enabled: true,
managedRules: managedRules([
...state.snapshot.rateLimits.rules.filter((rule) => !legacyCreateLabels.includes(rule.label)),
...legacyCreateRules,
]),
};
state.record.set("snapshot", JSON.stringify(state.snapshot));
app.save(state.record);
},
() => {
// Keep the backfill until 1784240001 is rolled back; 1784240000 removes the state collection afterward.
},
);