- Rate limits and IP hashes use the X-Forwarded-For entry nginx adds, not the client-supplied first one - Link preview and feed fetches block IPv4-mapped and other embedded IPv6 forms of private addresses (net.BlockList) - CSRF check on /api writes; security headers and HSTS on every response - Viewers no longer receive a person's private details (readRole) - Sessions end 90 days after sign-in however active - yt-dlp ignores config files and takes the link after -- - Meta tag insert no longer expands $ patterns from record text - Links with an unsafe scheme draw as text - Migration 037: leadership lists name published home orgs only - Ignore *.db-wal, *.db-shm and *.txt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
RATE LIMIT
|
|
|
|
A fixed window counter held in process memory. It resets when
|
|
the service restarts and it doesn't survive a second instance,
|
|
both of which are fine for one systemd unit on one box.
|
|
|
|
The client IP comes from clientIp() in auth.js (the entry nginx
|
|
adds to X-Forwarded-For). That is only trustworthy because
|
|
nothing but nginx can reach this port — it binds to 127.0.0.1. Don't expose the port.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { clientIp } from "./auth.js";
|
|
|
|
export function rateLimit({ windowMs = 60_000, max = 10 } = {}) {
|
|
const hits = new Map(); // ip → { count, resetAt }
|
|
|
|
// Drop expired entries occasionally so the map can't grow forever.
|
|
setInterval(() => {
|
|
const now = Date.now();
|
|
for (const [ip, entry] of hits) {
|
|
if (entry.resetAt <= now) hits.delete(ip);
|
|
}
|
|
}, windowMs).unref();
|
|
|
|
return async (c, next) => {
|
|
const ip = clientIp(c) ?? "unknown";
|
|
|
|
const now = Date.now();
|
|
let entry = hits.get(ip);
|
|
|
|
if (!entry || entry.resetAt <= now) {
|
|
entry = { count: 0, resetAt: now + windowMs };
|
|
hits.set(ip, entry);
|
|
}
|
|
|
|
entry.count += 1;
|
|
|
|
if (entry.count > max) {
|
|
const retryAfter = Math.ceil((entry.resetAt - now) / 1000);
|
|
return c.json(
|
|
{ error: "Too many requests. Try again shortly." },
|
|
429,
|
|
{ "Retry-After": String(retryAfter) },
|
|
);
|
|
}
|
|
|
|
await next();
|
|
};
|
|
}
|