fix(limits): behind Cloudflare, a visitor is their CF-Connecting-IP

With a trusted local proxy the address was the last X-Forwarded-For hop,
which behind Cloudflare is Cloudflare's own edge: every visitor shared a
handful of addresses for the rate limits, and the Turnstile check was sent
an edge as remoteip. When that last hop is in Cloudflare's published
ranges (https://www.cloudflare.com/ips/), CF-Connecting-IP is used, if it
is an address. From anywhere else the header is ignored: someone who
reaches the server directly could otherwise name any address, past the
rate limits, address bans and AJ_ADMIN_ALLOW. Neither header is read
unless config.trustedProxy is on and the request came from loopback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
SnipzilandClaude Opus 5.5 committed 2026-10-21 22:42:10 -04:00
1 parent 974f68490e
commit 92ebb4d07c
1 file changed
+31 -1
+31 -1
View File
@@ -1,5 +1,6 @@
"use strict";
const net = require("net");
const config = require("../config.js");
// small in-memory rate limits for the parts of the server that strangers can
@@ -28,6 +29,25 @@ const key = address => {
const isLoopback = address => LOOPBACK.includes(String(address || "").replace(/^::ffff:/i, ""));
// the addresses Cloudflare's edge connects from, as it publishes them at
// https://www.cloudflare.com/ips/ (they seldom change; add any new range here).
const CLOUDFLARE = new net.BlockList();
for (const range of ["173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18",
"108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15",
"104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22"]) {
const [prefix, bits] = range.split("/");
CLOUDFLARE.addSubnet(prefix, Number(bits), "ipv4");
}
for (const range of ["2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32", "2405:8100::/32",
"2a06:98c0::/29", "2c0f:f248::/32"]) {
const [prefix, bits] = range.split("/");
CLOUDFLARE.addSubnet(prefix, Number(bits), "ipv6");
}
const fromCloudflare = address => {
const family = net.isIP(address);
return family !== 0 && CLOUDFLARE.check(address, family === 4 ? "ipv4" : "ipv6");
};
// is this address the server's own machine, and does that count for
// anything here? (It must not when a local reverse proxy forwards everyone.)
const isTrustedLocal = address => config.trustLocalRequests && isLoopback(address);
@@ -45,7 +65,17 @@ const clientOf = req => {
const own = String(req.socket.remoteAddress || "").replace(/^::ffff:/i, "");
const forwarded = req.headers["x-forwarded-for"] || req.headers.forwarded || req.headers["cf-connecting-ip"] || req.headers["x-real-ip"];
const hops = String(req.headers["x-forwarded-for"] || "").split(",").map(hop => hop.trim()).filter(Boolean);
const address = config.trustedProxy && isLoopback(own) && hops.length ? hops[hops.length - 1].replace(/^::ffff:/i, "").slice(0, 60) : own;
// the last X-Forwarded-For entry is the one the local proxy appended: who
// connected to it (the first is whatever the visitor sent, so it is never
// used). Behind Cloudflare that is Cloudflare's own edge, which made every
// visitor one address (and left remoteip out of the Turnstile check), so
// then CF-Connecting-IP is the visitor. Only when that hop is Cloudflare:
// anyone who reaches the server directly can send the header too. Neither
// header counts unless the local reverse proxy has been trusted.
const lastHop = (hops.at(-1) || "").replace(/^::ffff:/i, "");
const cloudflareAddress = String(req.headers["cf-connecting-ip"] || "").trim().replace(/^::ffff:/i, "");
const reported = net.isIP(cloudflareAddress) && fromCloudflare(lastHop) ? cloudflareAddress : lastHop;
const address = config.trustedProxy && isLoopback(own) && reported ? reported.replace(/^::ffff:/i, "").slice(0, 60) : own;
const direct = isLoopback(own) && !forwarded;
return {address, key: key(address), direct, local: direct && config.trustLocalRequests};
};