Client IP: rightmost X-Forwarded-For (the one Coolify's proxy adds); cf-connecting-ip only with CF_PROXIED=1. The domain is not on Cloudflare, so a visitor could send a fake cf-connecting-ip and count as many unique viewers. Smoke 76/76

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-13 16:02:08 -06:00
parent 7aa6bb6ed1
commit 2c0e0dcedc
+7 -1
View File
@@ -61,7 +61,13 @@ function readRaw(req, max) {
async function readBody(req) { const b = await readRaw(req, 32 * 1024); try { return b.length ? JSON.parse(b.toString('utf8')) : {}; } catch (e) { return {}; } }
function cookies(req) { const o = {}; for (const p of (req.headers.cookie || '').split(';')) { const i = p.indexOf('='); if (i > 0) { try { o[p.slice(0, i).trim()] = decodeURIComponent(p.slice(i + 1).trim()); } catch (e) {} } } return o; }
function cookie(name, val, maxAgeSec, httpOnly) { return name + '=' + encodeURIComponent(val) + '; Path=/; SameSite=Lax; Max-Age=' + maxAgeSec + (httpOnly ? '; HttpOnly' : '') + (C.isProd ? '; Secure' : ''); }
function ip(req) { return String(req.headers['cf-connecting-ip'] || (req.headers['x-forwarded-for'] || '').split(',')[0] || req.socket.remoteAddress || '').trim(); }
// Client IP. Behind Coolify's proxy the RIGHTMOST X-Forwarded-For entry is the one the proxy added; earlier entries and cf-connecting-ip
// are whatever the client sent (this domain is not on Cloudflare) and could fake many unique visitors. CF_PROXIED=1 only if Cloudflare proxies it.
function ip(req) {
if (process.env.CF_PROXIED === '1' && req.headers['cf-connecting-ip']) return String(req.headers['cf-connecting-ip']).trim();
const xff = String(req.headers['x-forwarded-for'] || '').split(',').map(s => s.trim()).filter(Boolean);
return (xff.length ? xff[xff.length - 1] : String(req.socket.remoteAddress || '')).replace(/^::ffff:/, '');
}
const isBot = req => BOT_UA.test(req.headers['user-agent'] || '') || !req.headers['user-agent'];
// visitor = IP + user agent (not a cookie: traffic-exchange frames drop cookies, and the gate must work there)
function visitorKey(req) { return crypto.createHash('sha256').update(ip(req) + '|' + (req.headers['user-agent'] || '')).digest('hex').slice(0, 20); }