Skip to content
Open
Show file tree
Hide file tree
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 Jun 14, 2025
194b59e
Remove unnecessary comments and console.log statements from IP restri…
Octo8080X Jun 14, 2025
8362db1
Remove unnecessary comments and console.log statements from IP restri…
Octo8080X Jun 14, 2025
167feba
Update exports in mod.ts for ipRestriction and related types
Octo8080X Jun 14, 2025
7ae6fc3
Export IPRestrictionRules type from ip_restriction in mod.ts
Octo8080X Jun 14, 2025
0eb73c3
Rename test file to ip_restriction_test.ts for consistency with sourc…
Octo8080X Jun 14, 2025
72ae3bd
Export getIP from ip_restriction in mod.ts for middleware completeness
Octo8080X Jun 14, 2025
5987fc8
Fix ipRestriction middleware tests and improve coverage
Octo8080X Jun 14, 2025
2e93114
Export IPRestrictionRules type from ip_restriction module
Octo8080X Jun 14, 2025
2deeb7c
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Jun 21, 2025
25c2cdb
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Aug 6, 2025
4e5ae78
update
Octo8080X Oct 6, 2025
caae853
use @std/net
Octo8080X Oct 6, 2025
9db23e6
update export
Octo8080X Oct 6, 2025
c93fd79
update docs
Octo8080X Oct 6, 2025
8e79b17
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Oct 7, 2025
bc7f51e
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Oct 14, 2025
53612b9
fix
Octo8080X Oct 20, 2025
2645569
Merge branch 'feature/add_ip_restriction_middleware' of github.com:Oc…
Octo8080X Oct 20, 2025
abd6665
fix
Octo8080X Oct 20, 2025
f14e7dc
fix
Octo8080X Oct 20, 2025
1468cde
rename and fix
Octo8080X Oct 21, 2025
e37ad41
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Oct 21, 2025
25d5d2d
fix
Octo8080X Oct 21, 2025
dfe28d4
fix
Octo8080X Oct 21, 2025
a7ab3de
fix
Octo8080X Oct 21, 2025
df3b5da
fix
Octo8080X Oct 21, 2025
c22c4bf
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Oct 21, 2025
7236b07
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Nov 11, 2025
3f3d37d
merge main
Octo8080X Nov 11, 2025
007e150
fix
Octo8080X Nov 11, 2025
4b15afe
add document
Octo8080X Nov 11, 2025
4c2f7a6
organized method
Octo8080X Nov 11, 2025
9ef84c7
Merge branch 'denoland:main' into feature/add_ip_restriction_middleware
Octo8080X Nov 22, 2025
5383f74
logic update
Octo8080X Nov 22, 2025
75805b8
logic update
Octo8080X Nov 22, 2025
f50aa69
logic update
Octo8080X Nov 22, 2025
5e2be56
logic update
Octo8080X Nov 22, 2025
ab623c8
update coments
Octo8080X Nov 22, 2025
34407e8
restore lockfile
Octo8080X Nov 22, 2025
ef5ff80
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Dec 23, 2025
4328d63
Merge branch 'main' into feature/add_ip_restriction_middleware
Octo8080X Jan 22, 2026
4e16d46
add @std/net
Octo8080X Jan 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
395 changes: 395 additions & 0 deletions src/middlewares/ip_restriction.ts
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types already exist within the Deno runtime API. See https://docs.deno.com/api/deno/network.


/**
* 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();
} else {
if (onError) {
return await onError({ addr, type }, ctx);
}
return blockError();
}
};
}
Loading
Loading