Security events and the signup kill switch

Security events and the signup kill switch

Two admin tools on one page, /dashboard/admin/security-events:

  • Signup kill switch. One switch stops new accounts without a deploy. Existing users keep signing in.
  • Security events. A table of the probing and admin actions the app itself sees, with an email when one source keeps getting refused.

Signup kill switch

The switch is the signup_enabled row of platform_settings on the shared DB (src/lib/db/platform-settings.ts). Never set, or anything but "false", means signups are on.

signupBlockReason(db, email) in src/server/signup-gate.ts is the one check. It returns null for an email that already has an account, so a disabled signup never locks a customer out. It is called in three places, because there are three ways to ask for a code:

  • sendSignupOtp (src/server/functions/signup-otp.ts), which the signup page uses. It returns ERROR_CODES.SIGNUPS_DISABLED.
  • better-auth's sendVerificationOTP and sendMagicLink callbacks in src/server/auth.ts, so the raw /api/auth endpoints are covered too.

accountCreationBlocked(db, path) in the same file is the second check, at account creation itself (databaseHooks.user.create.before in src/server/auth.ts). The send checks alone left "Continue with GitHub" and Google sign-up open while the switch was off; this one runs for every provider. An admin adding a user (/admin/create-user) is let through.

Every flip is recorded as admin.signup_enabled or admin.signup_disabled.

Security events

recordSecurityEventWithAlert(db, { type, userId?, ip?, payload? }) in src/features/security-events/server/security-events.server.ts writes a row to security_events and never throws: recording must not block the action it records. Call it with void.

TypeWritten when
auth.signupAn account is created
auth.otp_failedA wrong or expired code is submitted
auth.magic_link_failedA magic link fails to verify
auth.rate_limitedbetter-auth's own limiter answers 429
rate_limit.sharedThe shared (cross-process) limiter refuses a request
admin.impersonationAn admin starts impersonating a user
admin.signup_enabled, admin.signup_disabledThe kill switch is flipped

Payloads hold shape, never secrets: no codes, no tokens, no SQL. The page is deliberately not a log viewer, so a stolen admin session reading it learns nothing it can reuse.

Burst alert. Ten or more denial events (DENIAL_EVENT_TYPES) from the same user, or the same IP before sign-in, inside a 5-minute bucket send one email to the first address in ADMIN_EMAILS. The "already sent" marker is a platform_settings row, so it holds across cluster workers.

Adding an event

Call recordSecurityEventWithAlert at the refusal, with a new dotted type. Add the type to DENIAL_EVENT_TYPES if a burst of it should alert.