Files
pipelines/web/src/loginLimiter.js
T
alan e43f9fa50c Hash the web password and revoke sessions server-side
Store a scrypt hash (migrating a plaintext password without changing it),
sign sessions with a generation that ./password and --revoke-all rotate,
make logout a same-origin POST that revokes the token, add sign out everywhere,
and close proxied websockets whose session ends. Enforce a strength rule for
new passwords, back off logins per client and server-wide, check Origin on
login, tighten next, add security headers and a __Host- cookie over https.
2026-11-10 01:21:28 +00:00

80 lines
3.1 KiB
JavaScript

// A brake on password guessing: per client exponential backoff after a few
// free failures, plus a server-wide sliding window so many clients (or one
// client behind many addresses) cannot keep guessing. The per-client map is
// bounded; the least recently failed client is evicted first.
export class LoginLimiter {
constructor({
now = Date.now,
freeFailures = 5,
baseMs = 1000,
maxMs = 15 * 60 * 1000,
idleMs = 60 * 60 * 1000,
maxKeys = 1024,
globalLimit = 50,
globalWindowMs = 5 * 60 * 1000,
} = {}) {
Object.assign(this, { now, freeFailures, baseMs, maxMs, idleMs, maxKeys, globalLimit, globalWindowMs });
this.clients = new Map(); // key -> { count, until, last }; insertion order = recency
this.recent = []; // failure times, oldest first, at most globalLimit
}
prune(now) {
while (this.recent.length && this.recent[0] <= now - this.globalWindowMs) this.recent.shift();
}
/** Milliseconds key must wait before its next attempt (0: go ahead). */
blocked(key) {
const now = this.now();
this.prune(now);
let wait = 0;
if (this.recent.length >= this.globalLimit) wait = this.recent[0] + this.globalWindowMs - now;
const entry = this.clients.get(key);
if (entry && now - entry.last > this.idleMs) this.clients.delete(key);
else if (entry && entry.until > now) wait = Math.max(wait, entry.until - now);
return Math.max(0, wait);
}
fail(key) {
const now = this.now();
this.prune(now);
this.recent.push(now);
if (this.recent.length > this.globalLimit) this.recent.shift();
const entry = this.clients.get(key) ?? { count: 0, until: 0, last: now };
this.clients.delete(key);
entry.count += 1;
entry.last = now;
if (entry.count >= this.freeFailures) {
const doublings = Math.min(entry.count - this.freeFailures, 20);
entry.until = now + Math.min(this.baseMs * 2 ** doublings, this.maxMs);
}
this.clients.set(key, entry);
while (this.clients.size > this.maxKeys) this.clients.delete(this.clients.keys().next().value);
}
succeed(key) {
this.clients.delete(key);
}
}
const LOOPBACK = new Set(["127.0.0.1", "::1", "::ffff:127.0.0.1"]);
/**
* The client a login attempt is charged to. Behind `tailscale serve` every
* request arrives from loopback, so there (and only there: a loopback peer
* that carries Tailscale's identity headers) the tailnet user, else the
* forwarded address, names the client. Any other request is its socket
* address. A local process could forge these headers, but it already runs on
* the machine; the global window still bounds it.
*/
export function clientKey(req) {
const address = req.socket?.remoteAddress ?? "";
if (LOOPBACK.has(address) && Object.keys(req.headers).some((name) => name.startsWith("tailscale-"))) {
const login = String(req.headers["tailscale-user-login"] ?? "").trim().toLowerCase();
if (login) return `tailscale:${login.slice(0, 256)}`;
const forwarded = String(req.headers["x-forwarded-for"] ?? "").split(",")[0].trim();
if (forwarded) return `forwarded:${forwarded.slice(0, 64)}`;
}
return `address:${address}`;
}