auth: sign-in limits per address and name; successes free (audit A1, A8, A9)

Security audit 2026-10-17, findings A1 (medium), A8, A9 (low), stage 3.
Under the shipped Apache-on-host deployment with TRUSTED_PROXY_CIDR empty,
every visitor has the Docker gateway's address, and every public limit was
keyed on the address alone: ten POSTs to /login per 15 minutes (any body,
successes counted) locked out every administrator; likewise /login/reset,
the link pages and /setup/. IPv6 was keyed per /128.

Now (internal/web/auth/limitkeys.go):
- sign-in counts per address and username (10 / 15 min) and per address
  alone at 100; a right password refunds both (rateLimiter.Refund);
- a reset request counts per address and username (5) and per address
  alone (50); a link's page per link, by token hash (30), and per address
  alone (300); setup per address as before;
- an IPv6 address is keyed by its /64 (limitAddr);
- proxyWatch: when the first 20 requests after a start all come from one
  private or loopback peer with X-Forwarded-For and no proxy is trusted,
  the log says so once with the TRUSTED_PROXY_CIDR value to set;
- a refused sign-in or a lockout is logged, five lines per address in 15
  minutes, the typed name printable and cut (refusalLog, logName).
The shipped compose cannot know the gateway, so the guide's install step
prints it (docker network inspect) and says to set it.

Tests: TestLoginBehindOneUntrustedProxyKeepsOthersIn (the reader's case),
TestLoginSuccessesDoNotCount, TestIPv6IsLimitedPerSlash64,
TestProxyWatchAndRefusalsAreLogged; the existing lockout tests pass.

Docs: security.md § First-run setup, § Authentication; guide § Full
deployment; CHANGELOG; plan. Also the I1 plan line's Done note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mix
2026-10-17 22:10:47 +03:00
co-authored by Claude Opus 5.5
parent 1bafd78364
commit 3a8e19b71c
9 changed files with 388 additions and 38 deletions
+28 -4
View File
@@ -101,8 +101,17 @@ func (m *Module) renderLogin(w http.ResponseWriter, status int, formErr string)
m.view.Render(w, status, "login", view.NewLogin(m.cfg.Hostname, formErr))
}
// submitLogin checks a sign-in. Its limits are per address and username,
// and per address alone at ten times that, and only a failure counts: one
// stranger behind the proxy everyone shares no longer locks every
// administrator out, nor does a day of signing in (security audit
// 2026-10-17, A1). A refusal and a lockout are logged, a few lines per
// address in a window (A8).
func (m *Module) submitLogin(w http.ResponseWriter, r *http.Request) {
if !m.loginLimiter.Allow(clientIP(r, m.trustedProxies)) {
m.proxyWatch.see(r, m.trustedProxies)
addr := limitAddr(r, m.trustedProxies)
if !m.loginAddrLimiter.Allow(addr) {
m.refusals.printf(addr, "panel: sign-in from %s refused: too many attempts from the address", addr)
m.renderLogin(w, http.StatusTooManyRequests, "Too many attempts. Please wait and try again.")
return
}
@@ -112,6 +121,17 @@ func (m *Module) submitLogin(w http.ResponseWriter, r *http.Request) {
}
username := strings.TrimSpace(r.PostFormValue("username"))
password := r.PostFormValue("password")
key := userKey(addr, username)
if !m.loginLimiter.Allow(key) {
m.loginAddrLimiter.Refund(addr) // counted once, against the name
m.refusals.printf(addr, "panel: sign-in as %q from %s refused: too many attempts", logName(username), addr)
m.renderLogin(w, http.StatusTooManyRequests, "Too many attempts. Please wait and try again.")
return
}
refused := func() {
m.refusals.printf(addr, "panel: sign-in as %q from %s refused: wrong username or password", logName(username), addr)
m.renderLogin(w, http.StatusUnauthorized, "Invalid username or password.")
}
user, err := m.store.GetUserByUsername(username)
if err != nil {
@@ -122,14 +142,17 @@ func (m *Module) submitLogin(w http.ResponseWriter, r *http.Request) {
// a wrong password is: the answer's delay told which names exist
// (audit 2026-10-12, M8).
_ = bcrypt.CompareHashAndPassword(noSuchUserHash(), []byte(password))
m.renderLogin(w, http.StatusUnauthorized, "Invalid username or password.")
refused()
return
}
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil {
m.renderLogin(w, http.StatusUnauthorized, "Invalid username or password.")
refused()
return
}
// A right password is no guess: its attempts are given back.
m.loginLimiter.Refund(key)
m.loginAddrLimiter.Refund(addr)
// The session the browser presents, if any, ends here: a sign-in issues
// a new token and leaves no other behind it - one planted before the
@@ -195,7 +218,8 @@ func (m *Module) HandleLogout(w http.ResponseWriter, r *http.Request) {
// HandleSetup serves the one-time administrator creation flow at
// /setup/<token> (security.md).
func (m *Module) HandleSetup(w http.ResponseWriter, r *http.Request) {
if !m.setupLimiter.Allow(clientIP(r, m.trustedProxies)) {
m.proxyWatch.see(r, m.trustedProxies)
if !m.setupLimiter.Allow(limitAddr(r, m.trustedProxies)) {
http.Error(w, "too many requests", http.StatusTooManyRequests)
return
}