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 returnsERROR_CODES.SIGNUPS_DISABLED.- better-auth's
sendVerificationOTPandsendMagicLinkcallbacks insrc/server/auth.ts, so the raw/api/authendpoints 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.
| Type | Written when |
|---|---|
auth.signup | An account is created |
auth.otp_failed | A wrong or expired code is submitted |
auth.magic_link_failed | A magic link fails to verify |
auth.rate_limited | better-auth's own limiter answers 429 |
rate_limit.shared | The shared (cross-process) limiter refuses a request |
admin.impersonation | An admin starts impersonating a user |
admin.signup_enabled, admin.signup_disabled | The 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.