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
|
|
@ -48,3 +48,45 @@ export function rateLimit({ windowMs = 60_000, max = 10 } = {}) {
|
|||
await next();
|
||||
};
|
||||
}
|
||||
|
||||
/* ── Failures per key ──────────────────────────────────────────
|
||||
For login: count only failed attempts per account, so guessing
|
||||
one person's password from many addresses still stops, and their
|
||||
own successful sign-in clears it. Unknown emails are counted the
|
||||
same way, so the limit says nothing about which accounts exist.
|
||||
ponytail: in-memory like the IP limit (resets on restart, one
|
||||
instance); a spray of random emails is swept each window.
|
||||
───────────────────────────────────────────────────────────── */
|
||||
|
||||
export function failureLimit({ windowMs = 15 * 60_000, max = 5 } = {}) {
|
||||
const failures = new Map(); // key → { count, resetAt }
|
||||
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [key, entry] of failures) {
|
||||
if (entry.resetAt <= now) failures.delete(key);
|
||||
}
|
||||
}, windowMs).unref();
|
||||
|
||||
const live = (key) => {
|
||||
const entry = failures.get(key);
|
||||
if (entry && entry.resetAt <= Date.now()) failures.delete(key);
|
||||
return failures.get(key);
|
||||
};
|
||||
|
||||
return {
|
||||
/** Seconds until the key may try again, or 0. */
|
||||
blockedFor(key) {
|
||||
const entry = live(key);
|
||||
return entry && entry.count >= max ? Math.ceil((entry.resetAt - Date.now()) / 1000) : 0;
|
||||
},
|
||||
fail(key) {
|
||||
const entry = live(key) ?? { count: 0, resetAt: Date.now() + windowMs };
|
||||
entry.count += 1;
|
||||
failures.set(key, entry);
|
||||
},
|
||||
clear(key) {
|
||||
failures.delete(key);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue