-
Notifications
You must be signed in to change notification settings - Fork 1.1k
sec(server): block direct IP requests bypassing Cloudflare proxy in p… #4023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
berkelmali
wants to merge
2
commits into
openfrontio:main
Choose a base branch
from
berkelmali:sec/direct-ip-access-protection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import net from "net"; | ||
|
|
||
| export const CLOUDFLARE_IPV4_CIDRS = [ | ||
| "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", | ||
| ]; | ||
|
|
||
| export const CLOUDFLARE_IPV6_CIDRS = [ | ||
| "2400:cb00::/32", | ||
| "2606:4700::/32", | ||
| "2803:f800::/32", | ||
| "2405:b500::/32", | ||
| "2405:8100::/32", | ||
| "2a06:98c0::/29", | ||
| "2c0f:f248::/32", | ||
| ]; | ||
|
|
||
| function ip4ToInt(ip: string): number { | ||
| const parts = ip.split(".").map((part) => parseInt(part, 10)); | ||
| return ( | ||
| ((parts[0] << 24) >>> 0) + (parts[1] << 16) + (parts[2] << 8) + parts[3] | ||
| ); | ||
| } | ||
|
|
||
| function checkIPv4InCIDR(ip: string, cidr: string): boolean { | ||
| const [range, bitsStr] = cidr.split("/"); | ||
| const bits = parseInt(bitsStr, 10); | ||
| const ipInt = ip4ToInt(ip); | ||
| const rangeInt = ip4ToInt(range); | ||
| const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0; | ||
| return (ipInt & mask) === (rangeInt & mask); | ||
| } | ||
|
|
||
| function ip6ToBigInt(ip: string): bigint { | ||
| let fullIp = ip; | ||
| if (ip.includes("::")) { | ||
| const parts = ip.split("::"); | ||
| const left = parts[0] ? parts[0].split(":") : []; | ||
| const right = parts[1] ? parts[1].split(":") : []; | ||
| const missingCount = 8 - (left.length + right.length); | ||
| const middle = Array(missingCount).fill("0"); | ||
| fullIp = [...left, ...middle, ...right].join(":"); | ||
| } | ||
| const parts = fullIp | ||
| .split(":") | ||
| .map((part) => (part === "" ? 0n : BigInt(parseInt(part, 16)))); | ||
| let result = 0n; | ||
| for (const part of parts) { | ||
| result = (result << 16n) + part; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function checkIPv6InCIDR(ip: string, cidr: string): boolean { | ||
| const [range, bitsStr] = cidr.split("/"); | ||
| const bits = parseInt(bitsStr, 10); | ||
| const ipVal = ip6ToBigInt(ip); | ||
| const rangeVal = ip6ToBigInt(range); | ||
| const mask = | ||
| bits === 0 ? 0n : ((1n << 128n) - 1n) ^ ((1n << BigInt(128 - bits)) - 1n); | ||
| return (ipVal & mask) === (rangeVal & mask); | ||
| } | ||
|
|
||
| export function isCloudflareIp(ip: string): boolean { | ||
| if (!ip) return false; | ||
| // Convert mapped IPv4 address (e.g. ::ffff:173.245.48.5) to standard IPv4 | ||
| if (ip.startsWith("::ffff:")) { | ||
| ip = ip.substring(7); | ||
| } | ||
| if (net.isIPv4(ip)) { | ||
| return CLOUDFLARE_IPV4_CIDRS.some((cidr) => checkIPv4InCIDR(ip, cidr)); | ||
| } | ||
| if (net.isIPv6(ip)) { | ||
| return CLOUDFLARE_IPV6_CIDRS.some((cidr) => checkIPv6InCIDR(ip, cidr)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export function isLoopbackIp(ip: string): boolean { | ||
| if (!ip) return false; | ||
| if (ip.startsWith("::ffff:")) { | ||
| ip = ip.substring(7); | ||
| } | ||
| return ip === "127.0.0.1" || ip === "::1" || ip === "localhost"; | ||
| } | ||
|
|
||
| export function isCloudflareOrLoopbackIp(ip: string): boolean { | ||
| return isLoopbackIp(ip) || isCloudflareIp(ip); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { | ||
| isCloudflareIp, | ||
| isCloudflareOrLoopbackIp, | ||
| isLoopbackIp, | ||
| } from "../../src/server/Cloudflare"; | ||
|
|
||
| describe("Cloudflare IP Validation", () => { | ||
| describe("isLoopbackIp", () => { | ||
| test("identifies standard loopback IPs", () => { | ||
| expect(isLoopbackIp("127.0.0.1")).toBe(true); | ||
| expect(isLoopbackIp("::1")).toBe(true); | ||
| expect(isLoopbackIp("::ffff:127.0.0.1")).toBe(true); | ||
| expect(isLoopbackIp("localhost")).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false for non-loopback IPs", () => { | ||
| expect(isLoopbackIp("8.8.8.8")).toBe(false); | ||
| expect(isLoopbackIp("192.168.1.1")).toBe(false); | ||
| expect(isLoopbackIp("")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isCloudflareIp", () => { | ||
| test("returns true for valid Cloudflare IPv4 addresses", () => { | ||
| expect(isCloudflareIp("173.245.48.5")).toBe(true); | ||
| expect(isCloudflareIp("::ffff:173.245.48.5")).toBe(true); | ||
| expect(isCloudflareIp("103.21.244.1")).toBe(true); | ||
| expect(isCloudflareIp("104.16.0.2")).toBe(true); | ||
| expect(isCloudflareIp("172.64.0.9")).toBe(true); | ||
| }); | ||
|
|
||
| test("returns true for valid Cloudflare IPv6 addresses", () => { | ||
| expect(isCloudflareIp("2400:cb00:0000:0000:0000:0000:0000:0001")).toBe( | ||
| true, | ||
| ); | ||
| expect(isCloudflareIp("2400:cb00::1")).toBe(true); | ||
| expect(isCloudflareIp("2606:4700::ffff")).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false for non-Cloudflare IPs", () => { | ||
| expect(isCloudflareIp("8.8.8.8")).toBe(false); | ||
| expect(isCloudflareIp("1.1.1.1")).toBe(false); | ||
| expect(isCloudflareIp("192.168.1.1")).toBe(false); | ||
| expect(isCloudflareIp("2001:4860:4860::8888")).toBe(false); | ||
| expect(isCloudflareIp("")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isCloudflareOrLoopbackIp", () => { | ||
| test("returns true for both loopback and Cloudflare IPs", () => { | ||
| expect(isCloudflareOrLoopbackIp("127.0.0.1")).toBe(true); | ||
| expect(isCloudflareOrLoopbackIp("173.245.48.5")).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false for regular external IPs", () => { | ||
| expect(isCloudflareOrLoopbackIp("8.8.8.8")).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.