Harden sign-in: __Host- session cookie and a per-email login lock
- The session cookie is __Host-ngu_session when Secure (production), so a sibling subdomain can't set or overwrite it; plain ngu_session over local http. Every admin signs in again once after deploying. - Five wrong passwords for one email lock that email for 15 minutes, from any address; a correct password clears it. On top of the per-IP limit. - CLAUDE.md: noImplicitAny stays off on purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d1a6332a45
commit
b165e612c2
5 changed files with 83 additions and 5 deletions
|
|
@ -11,8 +11,9 @@
|
|||
|
||||
Three deliberate choices:
|
||||
|
||||
Login is rate limited where it's mounted, and answers wrong
|
||||
password and unknown account identically. Telling an attacker
|
||||
Login is rate limited where it's mounted (per IP) and here (per
|
||||
email, failures only), and answers wrong password and unknown
|
||||
account identically. Telling an attacker
|
||||
which addresses exist is free reconnaissance.
|
||||
|
||||
A failed login still runs a hash. Otherwise the response time
|
||||
|
|
@ -39,6 +40,12 @@ import {
|
|||
verifyPassword,
|
||||
} from "../auth.js";
|
||||
import { GoogleLoginError, finishLogin, googleConfig, startLogin } from "../googleAuth.js";
|
||||
import { failureLimit } from "../rateLimit.js";
|
||||
|
||||
// On top of the per-IP limit where /login is mounted: five wrong
|
||||
// passwords for one email in 15 minutes locks that email for the
|
||||
// rest of the window, from anywhere.
|
||||
const loginFailures = failureLimit({ windowMs: 15 * 60_000, max: 5 });
|
||||
|
||||
const auth = new Hono();
|
||||
|
||||
|
|
@ -63,6 +70,16 @@ auth.post("/login", async (c) => {
|
|||
return c.json({ error: "Email and password are required." }, 400);
|
||||
}
|
||||
|
||||
const wait = loginFailures.blockedFor(email);
|
||||
if (wait) {
|
||||
console.warn(`login locked for ${email}`);
|
||||
return c.json(
|
||||
{ error: "Too many wrong passwords for this account. Try again in a few minutes." },
|
||||
429,
|
||||
{ "Retry-After": String(wait) },
|
||||
);
|
||||
}
|
||||
|
||||
const db = c.get("db");
|
||||
|
||||
const user = db
|
||||
|
|
@ -79,10 +96,12 @@ auth.post("/login", async (c) => {
|
|||
: (verifyPassword(password, DUMMY_HASH), false);
|
||||
|
||||
if (!ok) {
|
||||
loginFailures.fail(email);
|
||||
console.warn(`login failed for ${email}`);
|
||||
return c.json({ error: "That email and password don't match." }, 401);
|
||||
}
|
||||
|
||||
loginFailures.clear(email);
|
||||
createSession(db, c, user.id);
|
||||
|
||||
db.prepare("UPDATE admin_users SET last_login_at = datetime('now') WHERE id = ?").run(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue