Skip to content

fix(auth): rate-limit admin logins and equalize password checks - #444

Open
misswell wants to merge 1 commit into
astaxie:mainfrom
misswell:codex/admin-login-hardening
Open

misswell wants to merge 1 commit into
astaxie:mainfrom
misswell:codex/admin-login-hardening

Conversation

@misswell

@misswell misswell commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

POST /api/admin/login accepts an unlimited number of attempts. A single client can therefore brute-force an administrator password, and the endpoint also answers in distinguishable ways: an unknown identity returns quickly while an existing identity spends bcrypt time, and a disabled account returns 403 admin_user_disabled before the password is checked. Together these turn the login form into an account-enumeration and password-guessing surface.

This change bounds failed attempts and makes the credential check itself uninformative about account state.

Related Issue

N/A

Changes

  • Add an in-memory login limiter with per-source-address and per-account failure budgets (20/minute per source, 5/minute per account), returning 429 login_rate_limited with a Retry-After header.
  • Reserve a limiter slot before verification and settle it by outcome, so parallel attempts cannot all pass the threshold before any of them is recorded.
  • Clear the per-account budget on a successful login so an operator who mistyped a password is not locked out, while the per-source budget keeps accumulating.
  • Spend comparable bcrypt work when the identity does not exist, and verify the password before evaluating account status, so unknown, disabled, and active identities are indistinguishable without the correct password.
  • Fall back to a dummy bcrypt hash when the stored hash is not a bcrypt hash, so legacy rows cannot be identified by timing.
  • Add tests for rate limiting, parallel bursts, budget expiry and eviction, unknown-account cost, and disabled-account indistinguishability.

Type of Change

  • Bug fix
  • New feature
  • Refactor or maintenance
  • Documentation
  • Deployment or configuration

Verification

  • go build ./... passes.
  • go vet ./internal/server passes.
  • go test ./internal/server -run 'TestAdminLogin|TestAdminPasswords|TestLegacyAdminPassword|TestOversizeLegacyAdminPassword' -count=1 passes.
  • go test ./internal/server still fails only in pre-existing subscription-worker tests ("The subscription runtime stopped; retry the request"). The same failures reproduce on unmodified main, so they are unrelated to this change.
  • git diff --check passes; gofmt -l reports no files.

Compatibility, Security, and Operations

  • No API shape change: the endpoint keeps its request and response bodies, and adds 429 as a possible status with Retry-After.
  • The limiter is per process and in memory. Multi-instance deployments should still rate-limit at the ingress; the in-process limits remain useful as a second layer and reset on restart.
  • A correct password presented while an identity is limited still receives 429, which is intentional: the limiter must not leak whether the credential was right.
  • Disabled accounts now require the correct password before returning 403 admin_user_disabled. Clients that relied on the previous immediate 403 will see 403 only after the password is verified.
  • No new environment variables or configuration.

Checklist

  • The PR title and body are written in English.
  • Tests were added or updated for behavior changes, or the reason they are unnecessary is documented.
  • No credentials, local .env files, databases, backups, or runtime logs are included.
  • Environment variable changes are synchronized across examples, Compose, start.sh, and deployment documentation where applicable.
  • Shared user-facing behavior is documented consistently in English, Simplified Chinese, and Japanese where applicable.
  • data/model-catalog.yaml remains tracked and catalog changes were reviewed where applicable.
  • git diff --check passes.

Failed admin logins were unbounded, so a single client could brute-force
credentials against /api/admin/login. Unknown accounts and disabled
accounts also answered faster or differently than active ones, which
turned the endpoint into an account-enumeration oracle.

Add an in-memory limiter that tracks failures per source address and per
account identity, counting in-flight attempts so parallel bursts cannot
skip the threshold, and answer with 429 plus Retry-After. Reserve a slot
before verification and settle it by outcome; a successful login clears
the account budget so operators who mistyped a password are not locked
out.

Make the credential check itself uninformative: spend comparable bcrypt
work when the account is missing, verify the password before evaluating
account status, and fall back to a dummy hash when the stored hash is not
bcrypt so legacy rows cannot be distinguished by timing.
@misswell

Copy link
Copy Markdown
Author

CI shows action_required instead of running — that's the first-time-contributor approval gate, so no job has started yet. Could a maintainer approve the workflow runs?

I pre-ran the equivalent checks locally at this exact head (753578e4): gofmt -l clean, go vet ./..., golangci-lint v2.12.2 with 0 issues, and the full 4-shard backend suite via node tools/test-backend-shard.mjs N 4.

Happy to adjust anything if you'd rather see a different shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant