Harden the app

This commit is contained in:
Schluffe committed 2026-10-26 20:48:24 +01:00
1 parent d92c4b50cf
commit e3a2b348d6
5 files changed
+38 -12

No files matched your search

+14 -2
View File
@@ -45,9 +45,21 @@ const isId = (value) => typeof value === 'string' && value.length === 20;
const isSdp = (value, type) => value?.type === type && typeof value.sdp === 'string';
// Behind Caddy, the only way in. Caddy replaces any X-Forwarded-For a client sends, the last entry is
// its own anyway.
// its own anyway. IPv6 counts by /64: a single host usually has a whole one, every address in it
// would get MAX_PER_IP of its own. IPv4, also mapped to IPv6, counts as it is.
function clientIp(req) {
return req.headers['x-forwarded-for']?.split(',').at(-1).trim() || req.socket.remoteAddress;
const ip = req.headers['x-forwarded-for']?.split(',').at(-1).trim() || req.socket.remoteAddress;
if (!ip?.includes(':') || ip.includes('.')) return ip;
const [head, tail] = ip.split('::');
const groups = head ? head.split(':') : [];
if (tail !== undefined) {
const rest = tail ? tail.split(':') : [];
groups.push(...Array(Math.max(0, 8 - groups.length - rest.length)).fill('0'), ...rest);
}
return `${groups
.slice(0, 4)
.map((group) => parseInt(group, 16).toString(16))
.join(':')}::/64`;
}
function refuse(socket, status) {