Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Off Vercel, read the visitor's address from the end of X-Forwarded-For

Claude2b1f26dcaa7f
diff --git a/src/lib/security/request.test.ts b/src/lib/security/request.test.ts
index 3de366e..dfda9df 100644
--- a/src/lib/security/request.test.ts
+++ b/src/lib/security/request.test.ts
@@ -52,6 +52,30 @@ test("off Vercel, Vercel's header means nothing and the proxy headers are used",
);
});
+test("off Vercel, addresses a visitor puts at the start of X-Forwarded-For do not count", () => {
+ const read = (value: string) =>
+ getClientIpAddress(new Headers({ "x-forwarded-for": value }), elsewhere);
+
+ // A reverse proxy adds the address it saw at the end.
+ assert.equal(read("1.2.3.4, 198.51.100.7"), "198.51.100.7");
+ // The install's own proxies on a private network are passed over.
+ assert.equal(read("1.2.3.4, 198.51.100.7, 10.0.0.1"), "198.51.100.7");
+ assert.equal(read("198.51.100.7, 172.16.0.2, fd00::1"), "198.51.100.7");
+ // Nothing but private addresses: the first, as before.
+ assert.equal(read("192.168.1.20, 10.0.0.1"), "192.168.1.20");
+ // On Vercel, Vercel's own header decides, and the list is not read.
+ assert.equal(
+ getClientIpAddress(
+ new Headers({
+ "x-forwarded-for": "1.2.3.4, 198.51.100.7",
+ "x-vercel-forwarded-for": "203.0.113.5",
+ }),
+ onVercel,
+ ),
+ "203.0.113.5",
+ );
+});
+
test("an empty header is passed over, and no header at all gives no address", () => {
assert.equal(
getClientIpAddress(
diff --git a/src/lib/security/request.ts b/src/lib/security/request.ts
index 035e0f2..099b2f5 100644
--- a/src/lib/security/request.ts
+++ b/src/lib/security/request.ts
@@ -1,9 +1,69 @@
+import { isIP } from "node:net";
+
type IpEnv = Record<string, string | undefined>;
function firstAddress(value: string | null): string | null {
return value?.split(",")[0]?.trim() || null;
}
+/** An address on a private network, or the machine itself. */
+function isInternalAddress(address: string): boolean {
+ const version = isIP(address);
+
+ if (version === 4) {
+ const [a = 0, b = 0] = address.split(".").map(Number);
+
+ return (
+ a === 10 ||
+ a === 127 ||
+ (a === 100 && b >= 64 && b <= 127) ||
+ (a === 169 && b === 254) ||
+ (a === 172 && b >= 16 && b <= 31) ||
+ (a === 192 && b === 168)
+ );
+ }
+
+ if (version === 6) {
+ const lower = address.toLowerCase();
+
+ return (
+ lower === "::1" ||
+ lower.startsWith("fc") ||
+ lower.startsWith("fd") ||
+ lower.startsWith("fe80:") ||
+ lower.startsWith("::ffff:127.") ||
+ lower.startsWith("::ffff:10.") ||
+ lower.startsWith("::ffff:192.168.")
+ );
+ }
+
+ return false;
+}
+
+/**
+ * The visitor's address from an X-Forwarded-For list. Each proxy adds the
+ * address it was reached from at the end, so anything at the start may be
+ * the visitor's own invention. Read from the end, passing over the
+ * install's own proxies on a private network: the first address left is
+ * the one the outermost proxy saw.
+ */
+function forwardedForAddress(value: string | null): string | null {
+ const addresses = (value ?? "")
+ .split(",")
+ .map((address) => address.trim())
+ .filter(Boolean);
+
+ for (let index = addresses.length - 1; index >= 0; index -= 1) {
+ const address = addresses[index]!;
+
+ if (!isInternalAddress(address)) {
+ return address;
+ }
+ }
+
+ return addresses[0] ?? null;
+}
+
/**
* The headers that carry the visitor's address, most trustworthy first.
*
@@ -12,7 +72,10 @@ function firstAddress(value: string | null): string | null {
* https://vercel.com/docs/headers/request-headers
*
* Anywhere else those two names mean nothing special and anyone could send
- * them, so the usual proxy headers are used instead.
+ * them, so the usual proxy headers are used instead. There the app has to
+ * sit behind a reverse proxy that sets them: X-Forwarded-For is read from
+ * its end (see forwardedForAddress), so what a visitor puts at its start
+ * does not count.
*/
export function getClientIpHeaderNames(env: IpEnv = process.env): string[] {
return env.VERCEL === "1"
@@ -24,8 +87,13 @@ export function getClientIpAddress(
headers: Headers,
env: IpEnv = process.env,
): string | null {
+ const offVercel = env.VERCEL !== "1";
+
for (const name of getClientIpHeaderNames(env)) {
- const address = firstAddress(headers.get(name));
+ const address =
+ offVercel && name === "x-forwarded-for"
+ ? forwardedForAddress(headers.get(name))
+ : firstAddress(headers.get(name));
if (address) {
return address;