Honor the share-link miss limit before querying, and keep Escape inside the access menu.
This commit is contained in:
1 parent
75649a9a42
commit
1b3d055c81
8 files changed
+189
-19
No files matched your search
@@ -8,7 +8,7 @@ import {
|
||||
} from "../publishedRuleAccess";
|
||||
import { prisma } from "./db";
|
||||
import { isDatabaseConfigured } from "./env";
|
||||
import { rateLimitKey } from "./rateLimit";
|
||||
import { rateLimitCoolingDown, rateLimitKey } from "./rateLimit";
|
||||
import { browserHoldsClaimForHash } from "./guestRuleClaim";
|
||||
import { isRuleAdminEmail } from "./ruleAdmins";
|
||||
import { logAdminRuleRead } from "./adminRuleReadLog";
|
||||
@@ -141,7 +141,7 @@ 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 const SHARE_LOOKUP_MISS_INTERVAL_MS = 1_000;
|
||||
|
||||
export async function getReadablePublishedRule(opts: {
|
||||
idOrSlug: string;
|
||||
@@ -156,6 +156,16 @@ export async function getReadablePublishedRule(opts: {
|
||||
const idOrSlug = opts.idOrSlug.trim();
|
||||
if (idOrSlug.length === 0) return null;
|
||||
const lookup = opts.lookup ?? (opts.viaShareSlug ? "shareSlug" : "id");
|
||||
const missKey =
|
||||
lookup === "shareSlug" && opts.failedLookupKey
|
||||
? `share-lookup-miss:${opts.failedLookupKey}`
|
||||
: null;
|
||||
if (
|
||||
missKey &&
|
||||
rateLimitCoolingDown(missKey, SHARE_LOOKUP_MISS_INTERVAL_MS)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const row =
|
||||
lookup === "shareSlug"
|
||||
@@ -168,11 +178,9 @@ export async function getReadablePublishedRule(opts: {
|
||||
select: RULE_ACCESS_SELECT,
|
||||
});
|
||||
if (!row) {
|
||||
if (lookup === "shareSlug" && opts.failedLookupKey) {
|
||||
rateLimitKey(
|
||||
`share-lookup-miss:${opts.failedLookupKey}`,
|
||||
SHARE_LOOKUP_MISS_INTERVAL_MS,
|
||||
);
|
||||
if (missKey) {
|
||||
const limited = rateLimitKey(missKey, SHARE_LOOKUP_MISS_INTERVAL_MS);
|
||||
if (!limited.ok) return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -18,3 +18,12 @@ export function rateLimitKey(
|
||||
windows.set(key, now);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/** True when `key` is still inside the interval started by the last allowed call. */
|
||||
export function rateLimitCoolingDown(
|
||||
key: string,
|
||||
minIntervalMs: number,
|
||||
): boolean {
|
||||
const last = windows.get(key) ?? 0;
|
||||
return Date.now() - last < minIntervalMs;
|
||||
}
|
||||
Reference in new issue
Block a user