Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Take back a right access code in one statement, so two at once both come off
Claude3bbc89e85fbc
diff --git a/src/lib/access-code-guesses.dbtest.ts b/src/lib/access-code-guesses.dbtest.tsindex 222491e..d5ebfc0 100644--- a/src/lib/access-code-guesses.dbtest.ts+++ b/src/lib/access-code-guesses.dbtest.ts@@ -175,3 +175,50 @@ test("guesses sent at the same moment cannot pass the last try together, and rigJSON.stringify(statuses),);});++test("right codes sent at the same moment are both taken back off the count", async () => {+ const { token } = await createEnvelopeWithAccessCode("2468");++ // Both are counted before either is taken back.+ const together = 2;+ let arrived = 0;+ let releaseAll!: () => void;+ const allArrived = new Promise<void>((resolve) => {+ releaseAll = resolve;+ });+ const pool = afterStatement(+ database,+ (text) =>+ arrived < together && text.includes("insert into public.rate_limits"),+ async () => {+ arrived += 1;++ if (arrived === together) {+ releaseAll();+ }++ await allArrived;+ },+ );++ for (const result of await Promise.all(+ Array.from({ length: together }, () =>+ verifySignerCheck(+ token,+ { code: "2468", method: "access_code" },+ { database: pool },+ ),+ ),+ )) {+ assert.deepEqual(result, ["access_code"]);+ }++ // Every wrong try is still left.+ for (let attempt = 0; attempt < ACCESS_CODE_MAX_FAILURES; attempt += 1) {+ await assert.rejects(+ tryCode(token, String(8000 + attempt)),+ (error: unknown) =>+ error instanceof SigningMutationError && error.statusCode === 400,+ );+ }+});diff --git a/src/lib/security/rate-limit.ts b/src/lib/security/rate-limit.tsindex d33a6f5..ad1ed3e 100644--- a/src/lib/security/rate-limit.ts+++ b/src/lib/security/rate-limit.ts@@ -159,11 +159,11 @@ export async function consumeRateLimit(values ($1, 1, $2::bigint + $3::bigint)on conflict (key) do update setcount = case- when current.expires_at <= $2::bigint then 1+ when current.expires_at <= $2::bigint or current.count <= 0 then 1else least(current.count + 1, 1000000)end,expires_at = case- when current.expires_at <= $2::bigint+ when current.expires_at <= $2::bigint or current.count <= 0then $2::bigint + $3::bigintelse current.expires_atend@@ -239,7 +239,7 @@ export async function peekRateLimit(* Takes back one request counted by consumeRateLimit, for limits that* count every attempt up front (so parallel ones cannot all pass a peek)* but only mean to keep the ones that fail. A count that drops to nothing- * is removed, so its window starts again with the next failure.+ * starts a new window with the next request, as if it had never been.*/export async function refundRateLimit(key: string,@@ -249,18 +249,13 @@ export async function refundRateLimit(}: ConsumeRateLimitOptions & { now?: number } = {},): Promise<void> {try {- const executor = database ?? getDb();- const removed = await executor.query(- "delete from public.rate_limits where key = $1 and count <= 1",- [hashKey(key)],+ // One statement, so refunds that arrive together each take one off:+ // the row's lock puts them in turn. A count of zero is left in place;+ // consumeRateLimit treats it as no window at all.+ await (database ?? getDb()).query(+ "update public.rate_limits set count = greatest(count - 1, 0) where key = $1 and expires_at > $2",+ [hashKey(key), now],);-- if ((removed.rowCount ?? 0) === 0) {- await executor.query(- "update public.rate_limits set count = count - 1 where key = $1 and count > 1 and expires_at > $2",- [hashKey(key), now],- );- }} catch (error) {warnAboutFallback(error);