| [7bc636b] | 1 | /**
|
|---|
| 2 | * Rate limiters — anti-brute-force.
|
|---|
| 3 | *
|
|---|
| 4 | * In-memory store via express-rate-limit's default. Fine for a single Node
|
|---|
| 5 | * process. If we ever scale to multiple workers, swap to a shared store
|
|---|
| 6 | * (Redis or rate-limit-redis).
|
|---|
| 7 | *
|
|---|
| 8 | * Friendly 429 response handles HTMX requests (returns HX-Redirect to the
|
|---|
| 9 | * form page so the error is shown inline) and full requests (renders a
|
|---|
| 10 | * small message page).
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | import rateLimit from 'express-rate-limit';
|
|---|
| 14 | import { renderPage } from './render.js';
|
|---|
| 15 |
|
|---|
| [834bcc3] | 16 | // Behind Cloudflare/Caddy, req.ip can arrive as "1.2.3.4:11046" (IPv4 with
|
|---|
| 17 | // port). express-rate-limit v7 validates the IP and otherwise throws
|
|---|
| 18 | // ERR_ERL_INVALID_IP_ADDRESS — uncaught async → the process crashes (and pm2
|
|---|
| 19 | // enters a restart loop). Strip a trailing IPv4 port, fall back to the
|
|---|
| 20 | // socket address, and leave IPv6 (multiple colons) untouched.
|
|---|
| [c88783e] | 21 | function clientKey(req) {
|
|---|
| 22 | let ip = req.ip || req.socket?.remoteAddress || '';
|
|---|
| 23 | if (/^\d{1,3}(\.\d{1,3}){3}:\d+$/.test(ip)) ip = ip.split(':')[0];
|
|---|
| 24 | return ip || 'unknown';
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| [7bc636b] | 27 | function blockedHandler(viewName, bodyClass, friendlyMsg) {
|
|---|
| 28 | return (req, res, next, options) => {
|
|---|
| 29 | const message = `${friendlyMsg} Try again in a few minutes.`;
|
|---|
| 30 | if (req.headers['hx-request'] === 'true') {
|
|---|
| 31 | // HTMX: surface the error in the form's error slot via partial render.
|
|---|
| 32 | return renderPage(req, res, viewName, {
|
|---|
| 33 | pageTitle: 'Too many attempts',
|
|---|
| 34 | bodyClass,
|
|---|
| 35 | error: message,
|
|---|
| 36 | username: req.body?.username || '',
|
|---|
| 37 | email: req.body?.email || '',
|
|---|
| 38 | });
|
|---|
| 39 | }
|
|---|
| 40 | res.status(options.statusCode).type('html').send(`
|
|---|
| 41 | <div style="font-family:system-ui;max-width:520px;margin:4rem auto;padding:2rem;text-align:center;">
|
|---|
| 42 | <h1 style="font-size:2rem;margin:0 0 0.5rem;">Too many attempts</h1>
|
|---|
| 43 | <p>${message}</p>
|
|---|
| 44 | <p><a href="/" style="color:#c2410c;">← Home</a></p>
|
|---|
| 45 | </div>
|
|---|
| 46 | `);
|
|---|
| 47 | };
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | export const loginLimiter = rateLimit({
|
|---|
| 51 | windowMs: 15 * 60 * 1000, // 15 min
|
|---|
| 52 | max: 5, // 5 attempts per IP per window
|
|---|
| 53 | standardHeaders: true,
|
|---|
| 54 | legacyHeaders: false,
|
|---|
| [c88783e] | 55 | keyGenerator: clientKey,
|
|---|
| 56 | validate: { ip: false },
|
|---|
| [7bc636b] | 57 | // Only count failed attempts. Successful logins don't burn the budget.
|
|---|
| 58 | skipSuccessfulRequests: true,
|
|---|
| 59 | handler: blockedHandler('pages/auth-login', 'on-special', 'Too many login attempts.'),
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | export const registerLimiter = rateLimit({
|
|---|
| 63 | windowMs: 60 * 60 * 1000, // 1 hour
|
|---|
| 64 | max: 5, // 5 signups per IP per hour
|
|---|
| 65 | standardHeaders: true,
|
|---|
| 66 | legacyHeaders: false,
|
|---|
| [c88783e] | 67 | keyGenerator: clientKey,
|
|---|
| 68 | validate: { ip: false },
|
|---|
| [7bc636b] | 69 | skipSuccessfulRequests: false, // any attempt counts (registration spam is the concern)
|
|---|
| 70 | handler: blockedHandler('pages/auth-register', 'on-special', 'Too many signup attempts.'),
|
|---|
| 71 | });
|
|---|