-
Notifications
You must be signed in to change notification settings - Fork 742
Feature/add ip restriction middleware #3035
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
Octo8080X
wants to merge
43
commits into
denoland:main
Choose a base branch
from
Octo8080X:feature/add_ip_restriction_middleware
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.
+262
−4
Open
Changes from 7 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
571b272
Refactor IP restriction middleware: clean up debug logs and improve flow
Octo8080X 194b59e
Remove unnecessary comments and console.log statements from IP restri…
Octo8080X 8362db1
Remove unnecessary comments and console.log statements from IP restri…
Octo8080X 167feba
Update exports in mod.ts for ipRestriction and related types
Octo8080X 7ae6fc3
Export IPRestrictionRules type from ip_restriction in mod.ts
Octo8080X 0eb73c3
Rename test file to ip_restriction_test.ts for consistency with sourc…
Octo8080X 72ae3bd
Export getIP from ip_restriction in mod.ts for middleware completeness
Octo8080X 5987fc8
Fix ipRestriction middleware tests and improve coverage
Octo8080X 2e93114
Export IPRestrictionRules type from ip_restriction module
Octo8080X 2deeb7c
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 25c2cdb
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 4e5ae78
update
Octo8080X caae853
use @std/net
Octo8080X 9db23e6
update export
Octo8080X c93fd79
update docs
Octo8080X 8e79b17
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X bc7f51e
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 53612b9
fix
Octo8080X 2645569
Merge branch 'feature/add_ip_restriction_middleware' of github.com:Oc…
Octo8080X abd6665
fix
Octo8080X f14e7dc
fix
Octo8080X 1468cde
rename and fix
Octo8080X e37ad41
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 25d5d2d
fix
Octo8080X dfe28d4
fix
Octo8080X a7ab3de
fix
Octo8080X df3b5da
fix
Octo8080X c22c4bf
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 7236b07
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 3f3d37d
merge main
Octo8080X 007e150
fix
Octo8080X 4b15afe
add document
Octo8080X 4c2f7a6
organized method
Octo8080X 9ef84c7
Merge branch 'denoland:main' into feature/add_ip_restriction_middleware
Octo8080X 5383f74
logic update
Octo8080X 75805b8
logic update
Octo8080X f50aa69
logic update
Octo8080X 5e2be56
logic update
Octo8080X ab623c8
update coments
Octo8080X 34407e8
restore lockfile
Octo8080X ef5ff80
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 4328d63
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X 4e16d46
add @std/net
Octo8080X 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,395 @@ | ||
| import type { FreshContext } from "../context.ts"; | ||
| import type { MiddlewareFn } from "./mod.ts"; | ||
|
|
||
| export type AddressType = "IPv4" | "IPv6"; | ||
|
|
||
| export type NetAddrInfo = { | ||
| /** | ||
| * Transport protocol type | ||
| */ | ||
| transport?: "tcp" | "udp"; | ||
| /** | ||
| * Transport port number | ||
| */ | ||
| port: number; | ||
| address: string; | ||
| addressType?: AddressType; | ||
| }; | ||
|
|
||
| export interface ConnInfo { | ||
| /** | ||
| * Remote information | ||
| */ | ||
| remote: NetAddrInfo; | ||
| } | ||
|
||
|
|
||
| /** | ||
| * Helper type | ||
| */ | ||
| export type GetConnInfo = (c: FreshContext) => ConnInfo; | ||
|
|
||
| /** | ||
| * Expand IPv6 Address | ||
| * @param ipV6 Shorten IPv6 Address | ||
| * @return expanded IPv6 Address | ||
| */ | ||
|
|
||
| export const expandIPv6 = (ipV6: string): string => { | ||
| const sections = ipV6.split(":"); | ||
| if (IPV4_REGEX.test(sections.at(-1) as string)) { | ||
| sections.splice( | ||
| -1, | ||
| 1, | ||
| ...convertIPv6BinaryToString( | ||
| convertIPv4ToBinary(sections.at(-1) as string), | ||
| ) // => ::7f00:0001 | ||
| .substring(2) // => 7f00:0001 | ||
| .split(":"), // => ['7f00', '0001'] | ||
| ); | ||
| } | ||
| for (let i = 0; i < sections.length; i++) { | ||
| const node = sections[i]; | ||
| if (node !== "") { | ||
| sections[i] = node.padStart(4, "0"); | ||
| } else { | ||
| sections[i + 1] === "" && sections.splice(i + 1, 1); | ||
| sections[i] = new Array(8 - sections.length + 1).fill("0000").join(":"); | ||
| } | ||
| } | ||
| return sections.join(":"); | ||
| }; | ||
|
|
||
| const IPV4_REGEX = /^[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}$/; | ||
|
|
||
| /** | ||
| * Returns the address type ("IPv4" or "IPv6") for a given remote address string. | ||
| * | ||
| * @param remoteAddr - The remote address as a string. | ||
| * @returns The address type ("IPv4" | "IPv6") or undefined if not recognized. | ||
| */ | ||
| export const distinctRemoteAddr = ( | ||
| remoteAddr: string, | ||
| ): AddressType | undefined => { | ||
| if (IPV4_REGEX.test(remoteAddr)) { | ||
| return "IPv4"; | ||
| } | ||
| if (remoteAddr.includes(":")) { | ||
| return "IPv6"; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Converts an IPv4 address string to a bigint binary representation. | ||
| * | ||
| * @param ipv4 - The IPv4 address as a string (e.g., "192.168.0.1"). | ||
| * @returns The IPv4 address as a bigint. | ||
| */ | ||
| export const convertIPv4ToBinary = (ipv4: string): bigint => { | ||
| const parts = ipv4.split("."); | ||
| let result = 0n; | ||
| for (let i = 0; i < 4; i++) { | ||
| result <<= 8n; | ||
| result += BigInt(parts[i]); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * Converts an IPv6 address string to a bigint binary representation. | ||
| * | ||
| * @param ipv6 - The IPv6 address as a string (e.g., "2001:db8::1"). | ||
| * @returns The IPv6 address as a bigint. | ||
| */ | ||
| export const convertIPv6ToBinary = (ipv6: string): bigint => { | ||
| const sections = expandIPv6(ipv6).split(":"); | ||
| let result = 0n; | ||
| for (let i = 0; i < 8; i++) { | ||
| result <<= 16n; | ||
| result += BigInt(parseInt(sections[i], 16)); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * Converts a binary representation of an IPv4 address to its string form. | ||
| * | ||
| * @param ipV4 - The IPv4 address as a bigint. | ||
| * @returns The IPv4 address as a string (e.g., "192.168.0.1"). | ||
| */ | ||
| export const convertIPv4BinaryToString = (ipV4: bigint): string => { | ||
| const sections = []; | ||
| for (let i = 0; i < 4; i++) { | ||
| sections.push((ipV4 >> BigInt(8 * (3 - i))) & 0xffn); | ||
| } | ||
| return sections.join("."); | ||
| }; | ||
|
|
||
| /** | ||
| * Converts a binary representation of an IPv6 address to its normalized string form. | ||
| * | ||
| * @param ipV6 - The IPv6 address as a bigint. | ||
| * @returns The normalized IPv6 address as a string (e.g., "2001:db8::1"). | ||
| */ | ||
| export const convertIPv6BinaryToString = (ipV6: bigint): string => { | ||
| // IPv6-mapped IPv4 address | ||
| if (ipV6 >> 32n === 0xffffn) { | ||
| return `::ffff:${convertIPv4BinaryToString(ipV6 & 0xffffffffn)}`; | ||
| } | ||
|
|
||
| const sections = []; | ||
| for (let i = 0; i < 8; i++) { | ||
| sections.push(((ipV6 >> BigInt(16 * (7 - i))) & 0xffffn).toString(16)); | ||
| } | ||
|
|
||
| let currentZeroStart = -1; | ||
| let maxZeroStart = -1; | ||
| let maxZeroEnd = -1; | ||
| for (let i = 0; i < 8; i++) { | ||
| if (sections[i] === "0") { | ||
| if (currentZeroStart === -1) { | ||
| currentZeroStart = i; | ||
| } | ||
| } else { | ||
| if (currentZeroStart > -1) { | ||
| if (i - currentZeroStart > maxZeroEnd - maxZeroStart) { | ||
| maxZeroStart = currentZeroStart; | ||
| maxZeroEnd = i; | ||
| } | ||
| currentZeroStart = -1; | ||
| } | ||
| } | ||
| } | ||
| if (currentZeroStart > -1) { | ||
| if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) { | ||
| maxZeroStart = currentZeroStart; | ||
| maxZeroEnd = 8; | ||
| } | ||
| } | ||
| if (maxZeroStart !== -1) { | ||
| sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ":"); | ||
| } | ||
|
|
||
| return sections.join(":").replace(/:{2,}/g, "::"); | ||
| }; | ||
|
|
||
| /** | ||
| * ### IPv4 and IPv6 | ||
| * - `*` match all | ||
| * | ||
| * ### IPv4 | ||
| * - `192.168.2.0` static | ||
| * - `192.168.2.0/24` CIDR Notation | ||
| * | ||
| * ### IPv6 | ||
| * - `::1` static | ||
| * - `::1/10` CIDR Notation | ||
| */ | ||
| /** | ||
| * Type for a function that matches an IP restriction rule. | ||
| * | ||
| * @param addr - The address and its type to check. | ||
| * @returns True if the rule matches, false otherwise. | ||
| */ | ||
| type IPRestrictionRuleFunction = ( | ||
| addr: { addr: string; type: AddressType }, | ||
| ) => boolean; | ||
|
|
||
| /** | ||
| * IP restriction rule, which can be a string or a function. | ||
| */ | ||
| export type IPRestrictionRule = | ||
| | string | ||
| | ((addr: { addr: string; type: AddressType }) => boolean); | ||
|
|
||
| const IS_CIDR_NOTATION_REGEX = /\/[0-9]{0,3}$/; | ||
| const buildMatcher = ( | ||
| rules: IPRestrictionRule[], | ||
| ): (addr: { addr: string; type: AddressType; isIPv4: boolean }) => boolean => { | ||
| const functionRules: IPRestrictionRuleFunction[] = []; | ||
| const staticRules: Set<string> = new Set(); | ||
| const cidrRules: [boolean, bigint, bigint][] = []; | ||
|
|
||
| for (let rule of rules) { | ||
| if (rule === "*") { | ||
| return () => true; | ||
| } else if (typeof rule === "function") { | ||
| functionRules.push(rule); | ||
| } else { | ||
| if (IS_CIDR_NOTATION_REGEX.test(rule)) { | ||
| const separatedRule = rule.split("/"); | ||
|
|
||
| const addrStr = separatedRule[0]; | ||
| const type = distinctRemoteAddr(addrStr); | ||
| if (type === undefined) { | ||
| throw new TypeError(`Invalid rule: ${rule}`); | ||
| } | ||
|
|
||
| const isIPv4 = type === "IPv4"; | ||
| const prefix = parseInt(separatedRule[1]); | ||
|
|
||
| if (isIPv4 ? prefix === 32 : prefix === 128) { | ||
| // this rule is a static rule | ||
| rule = addrStr; | ||
| } else { | ||
| const addr = (isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)( | ||
| addrStr, | ||
| ); | ||
| const mask = ((1n << BigInt(prefix)) - 1n) << | ||
| BigInt((isIPv4 ? 32 : 128) - prefix); | ||
|
|
||
| cidrRules.push( | ||
| [isIPv4, addr & mask, mask] as [boolean, bigint, bigint], | ||
| ); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| const type = distinctRemoteAddr(rule); | ||
| if (type === undefined) { | ||
| throw new TypeError(`Invalid rule: ${rule}`); | ||
| } | ||
| staticRules.add( | ||
| type === "IPv4" | ||
| ? rule // IPv4 address is already normalized, so it is registered as is. | ||
| : convertIPv6BinaryToString(convertIPv6ToBinary(rule)), // normalize IPv6 address (e.g. 0000:0000:0000:0000:0000:0000:0000:0001 => ::1) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return (remote: { | ||
| addr: string; | ||
| type: AddressType; | ||
| isIPv4: boolean; | ||
| binaryAddr?: bigint; | ||
| }): boolean => { | ||
| if (staticRules.has(remote.addr)) { | ||
| return true; | ||
| } | ||
| for (const [isIPv4, addr, mask] of cidrRules) { | ||
| if (isIPv4 !== remote.isIPv4) { | ||
| continue; | ||
| } | ||
| const remoteAddr = (remote.binaryAddr ||= ( | ||
| isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary | ||
| )(remote.addr)); | ||
| if ((remoteAddr & mask) === addr) { | ||
| return true; | ||
| } | ||
| } | ||
| for (const rule of functionRules) { | ||
| if (rule({ addr: remote.addr, type: remote.type })) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Interface for configuring IP restriction middleware rules. | ||
| * | ||
| * @property denyList - List of rules to explicitly deny. | ||
| * @property allowList - List of rules to explicitly allow. | ||
| */ | ||
| export interface IPRestrictionRules { | ||
| denyList?: IPRestrictionRule[]; | ||
| allowList?: IPRestrictionRule[]; | ||
| } | ||
|
|
||
| function blockError(): Response { | ||
| return new Response("Forbidden", { | ||
| status: 403, | ||
| headers: { | ||
| "Content-Type": "text/plain", | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves connection information from the Fresh context. | ||
| * | ||
| * @param ctx - The Fresh context object. | ||
| * @param distinctRemoteAddr - Function to determine the address type. | ||
| * @returns The connection information. | ||
| * @throws {TypeError} If the transport protocol is not TCP. | ||
| */ | ||
| export function getIP( | ||
| ctx: FreshContext, | ||
| distinctRemoteAddr: (addr: string) => AddressType | undefined, | ||
| ): ConnInfo | never { | ||
| const remoteAddr = ctx.info.remoteAddr; | ||
|
|
||
| if (remoteAddr.transport !== "tcp") { | ||
| throw new TypeError( | ||
| "Unsupported transport protocol. Only TCP is supported.", | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| remote: { | ||
| transport: remoteAddr.transport, | ||
| port: remoteAddr.port, | ||
| address: remoteAddr.hostname, | ||
| addressType: distinctRemoteAddr(remoteAddr.hostname), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * IP restriction middleware for Fresh. | ||
| * | ||
| * @param getIP - Function to extract connection info from the context. | ||
| * @param rules - IP restriction rules (allow/deny lists). | ||
| * @param onError - Optional error handler for denied requests. | ||
| * @returns Middleware function for IP restriction. | ||
| */ | ||
| export function ipRestriction<T>( | ||
| getIP: ( | ||
| ctx: FreshContext, | ||
| distinctRemoteAddr: (addr: string) => AddressType | undefined, | ||
| ) => ConnInfo | never, | ||
| { denyList = [], allowList = [] }: IPRestrictionRules, | ||
| onError?: ( | ||
| remote: { addr: string; type: AddressType }, | ||
| ctx: FreshContext, | ||
| ) => Response | Promise<Response>, | ||
| ): MiddlewareFn<T> { | ||
| const allowLength = allowList.length; | ||
|
|
||
| const denyMatcher = buildMatcher(denyList); | ||
| const allowMatcher = buildMatcher(allowList); | ||
|
|
||
| return async function ipRestriction(ctx: FreshContext) { | ||
| const connInfo = getIP(ctx, distinctRemoteAddr); | ||
| const addr = connInfo.remote.address; | ||
| if (!addr) { | ||
| return blockError(); | ||
| } | ||
| const type = connInfo.remote.addressType; | ||
| if (!type) { | ||
| return blockError(); | ||
| } | ||
|
|
||
| const remoteData = { addr, type, isIPv4: type === "IPv4" }; | ||
|
|
||
| if (denyMatcher(remoteData)) { | ||
| if (onError) { | ||
| return onError({ addr, type }, ctx); | ||
| } | ||
| return blockError(); | ||
| } | ||
| if (allowMatcher(remoteData)) { | ||
| const res = await ctx.next(); | ||
| return res; | ||
| } | ||
|
|
||
| if (allowLength === 0) { | ||
| return await ctx.next(); | ||
marvinhagemeister marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| if (onError) { | ||
| return await onError({ addr, type }, ctx); | ||
| } | ||
| return blockError(); | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of IP address logic here that might be valuable in other domains. I recommend creating an issue for the Standard Library for any useful utilities to
@std/net.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The part you’re pointing out is indeed a generic utility — in Hono, it’s placed in utils as well.
I’ll go ahead and create an issue for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iuioiua
I have created an issue.Thanks.
denoland/std#6722