Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Record failed sign-ins to unknown addresses only up to 20 an hour per address

Claude373c4655f979
diff --git a/src/lib/auth/sign-in-events.ts b/src/lib/auth/sign-in-events.ts
index 554fb59..64b7f7a 100644
--- a/src/lib/auth/sign-in-events.ts
+++ b/src/lib/auth/sign-in-events.ts
@@ -8,6 +8,7 @@ import {
import { getDb, queryOne } from "../db/connection.ts";
import { logSecurityEvent } from "../security/logging.ts";
+import { consumeRateLimit } from "../security/rate-limit.ts";
import { enforceSessionLimits } from "../session-controls.ts";
import { getSettings, isMissingSchemaError } from "../settings.ts";
import {
@@ -156,6 +157,21 @@ async function findUserIdByEmail(email: string): Promise<string | null> {
return row?.id ?? null;
}
+/** Failed sign-ins to addresses with no account recorded per hour per address. */
+export const UNKNOWN_ADDRESS_FAILURES_RECORDED_PER_HOUR = 20;
+
+async function mayRecordUnknownAddressFailure(
+ ctx: HookContext,
+): Promise<boolean> {
+ const decision = await consumeRateLimit({
+ key: `audit-unknown-sign-in:${requestDetails(ctx).ipAddress ?? "unknown"}`,
+ limit: UNKNOWN_ADDRESS_FAILURES_RECORDED_PER_HOUR,
+ windowMs: 60 * 60 * 1000,
+ });
+
+ return decision.allowed;
+}
+
/**
* Writes one event. A sign-in is never refused or undone because its record
* could not be written; the failure is logged instead.
@@ -413,6 +429,22 @@ async function afterPasswordSignIn(
const counted = wrongPassword ? await countFailedSignIn(email) : null;
const userId = await findUserIdByEmail(email).catch(() => null);
+ // Tries at addresses with no account are kept in the audit trail, which
+ // is never cut, only up to a number an hour from each address they come
+ // from. The rest are logged, so trying made-up addresses cannot grow
+ // the trail without end.
+ if (!userId && !(await mayRecordUnknownAddressFailure(ctx))) {
+ logSecurityEvent("warn", {
+ category: "sign_in_failed_unrecorded",
+ ipAddress: requestDetails(ctx).ipAddress,
+ message:
+ "A sign-in to an address with no account failed; too many from this address this hour to record each one.",
+ userId: null,
+ });
+
+ return;
+ }
+
await record(ctx, {
actorEmail: email,
actorUserId: userId,
diff --git a/src/lib/sign-in-security.dbtest.ts b/src/lib/sign-in-security.dbtest.ts
index ba2a9c4..12e2955 100644
--- a/src/lib/sign-in-security.dbtest.ts
+++ b/src/lib/sign-in-security.dbtest.ts
@@ -9,6 +9,7 @@ import type { createAppAuth as CreateAppAuth } from "./auth/server.ts";
import { replaceDbPoolForTests } from "./db/connection.ts";
import { openTestDatabase } from "./db/test-database.ts";
import { clearRateLimitStoreForTests } from "./security/rate-limit.ts";
+import { UNKNOWN_ADDRESS_FAILURES_RECORDED_PER_HOUR } from "./auth/sign-in-events.ts";
import {
listSystemAuditEvents,
verifySystemAuditChain,
@@ -306,6 +307,27 @@ test("an address with no account is stopped the same way", async () => {
assert.equal(locked.payload?.code, "SIGN_IN_LOCKED");
});
+test("tries at made-up addresses from one place are recorded only up to an hourly number", async () => {
+ for (
+ let attempt = 0;
+ attempt < UNKNOWN_ADDRESS_FAILURES_RECORDED_PER_HOUR + 5;
+ attempt += 1
+ ) {
+ await signIn(
+ `nobody-${String(attempt)}@acme.com`,
+ "not the right password!!",
+ );
+ }
+
+ const failures = await events("sign_in_failed");
+
+ assert.ok(failures.length > 0);
+ assert.ok(
+ failures.length <= UNKNOWN_ADDRESS_FAILURES_RECORDED_PER_HOUR,
+ String(failures.length),
+ );
+});
+
test("a sign-in that works forgets the wrong passwords before it", async () => {
await createVerifiedUser("[email protected]");