66import type { Context , MiddlewareHandler } from '../..'
77import type { AddressType , GetConnInfo } from '../../helper/conninfo'
88import { HTTPException } from '../../http-exception'
9+ import type { InvalidIPAddressError } from '../../utils/ipaddr'
910import {
1011 convertIPv4MappedIPv6ToIPv4 ,
1112 convertIPv4ToBinary ,
1213 convertIPv6BinaryToString ,
1314 convertIPv6ToBinary ,
1415 distinctRemoteAddr ,
1516 isIPv4MappedIPv6 ,
17+ INVALID_IP_ADDRESS_ERROR_CODE ,
1618} from '../../utils/ipaddr'
1719
1820/**
@@ -35,13 +37,47 @@ type GetIPAddr = GetConnInfo | ((c: Context) => string)
3537type IPRestrictionRuleFunction = ( addr : { addr : string ; type : AddressType } ) => boolean
3638export type IPRestrictionRule = string | ( ( addr : { addr : string ; type : AddressType } ) => boolean )
3739
38- const IS_CIDR_NOTATION_REGEX = / \/ [ 0 - 9 ] { 0 , 3 } $ /
40+ const IS_CIDR_NOTATION_REGEX = / \/ [ ^ / ] * $ /
41+ const parseCidrPrefix = ( rule : string , prefix : string , max : number ) : number => {
42+ if ( ! / ^ [ 0 - 9 ] { 1 , 3 } $ / . test ( prefix ) ) {
43+ throw new TypeError ( `Invalid rule: ${ rule } ` )
44+ }
45+ const parsedPrefix = parseInt ( prefix )
46+ if ( parsedPrefix > max ) {
47+ throw new TypeError ( `Invalid rule: ${ rule } ` )
48+ }
49+ return parsedPrefix
50+ }
3951const buildMatcher = (
4052 rules : IPRestrictionRule [ ]
4153) : ( ( addr : { addr : string ; type : AddressType ; isIPv4 : boolean } ) => boolean ) => {
4254 const functionRules : IPRestrictionRuleFunction [ ] = [ ]
4355 const staticRules : Set < string > = new Set ( )
56+ const staticIPv4Rules : Set < bigint > = new Set ( )
57+ const staticIPv6Rules : Set < bigint > = new Set ( )
4458 const cidrRules : [ boolean , bigint , bigint ] [ ] = [ ]
59+ const registerStaticRule = ( rule : string ) : void => {
60+ const type = distinctRemoteAddr ( rule )
61+ if ( type === undefined ) {
62+ throw new TypeError ( `Invalid rule: ${ rule } ` )
63+ }
64+ if ( type === 'IPv4' ) {
65+ const ipv4binary = convertIPv4ToBinary ( rule )
66+ staticRules . add ( rule )
67+ staticRules . add ( `::ffff:${ rule } ` )
68+ staticIPv4Rules . add ( ipv4binary )
69+ staticIPv6Rules . add ( ( 0xffffn << 32n ) | ipv4binary )
70+ } else {
71+ const ipv6binary = convertIPv6ToBinary ( rule )
72+ const ipv6Addr = convertIPv6BinaryToString ( ipv6binary )
73+ staticRules . add ( ipv6Addr )
74+ staticIPv6Rules . add ( ipv6binary )
75+ if ( isIPv4MappedIPv6 ( ipv6binary ) ) {
76+ staticRules . add ( ipv6Addr . substring ( 7 ) ) // remove ::ffff: prefix
77+ staticIPv4Rules . add ( convertIPv4MappedIPv6ToIPv4 ( ipv6binary ) )
78+ }
79+ }
80+ }
4581
4682 for ( let rule of rules ) {
4783 if ( rule === '*' ) {
@@ -59,7 +95,7 @@ const buildMatcher = (
5995 }
6096
6197 let isIPv4 = type === 'IPv4'
62- let prefix = parseInt ( separatedRule [ 1 ] )
98+ let prefix = parseCidrPrefix ( rule , separatedRule [ 1 ] , isIPv4 ? 32 : 128 )
6399
64100 if ( isIPv4 ? prefix === 32 : prefix === 128 ) {
65101 // this rule is a static rule
@@ -79,21 +115,7 @@ const buildMatcher = (
79115 }
80116 }
81117
82- const type = distinctRemoteAddr ( rule )
83- if ( type === undefined ) {
84- throw new TypeError ( `Invalid rule: ${ rule } ` )
85- }
86- if ( type === 'IPv4' ) {
87- staticRules . add ( rule )
88- staticRules . add ( `::ffff:${ rule } ` )
89- } else {
90- const ipv6binary = convertIPv6ToBinary ( rule )
91- const ipv6Addr = convertIPv6BinaryToString ( ipv6binary )
92- staticRules . add ( ipv6Addr )
93- if ( isIPv4MappedIPv6 ( ipv6binary ) ) {
94- staticRules . add ( ipv6Addr . substring ( 7 ) ) // remove ::ffff: prefix
95- }
96- }
118+ registerStaticRule ( rule )
97119 }
98120 }
99121
@@ -115,6 +137,9 @@ const buildMatcher = (
115137 ? remoteAddr
116138 : convertIPv4MappedIPv6ToIPv4 ( remoteAddr )
117139 : undefined
140+ if ( ( remote . isIPv4 ? staticIPv4Rules : staticIPv6Rules ) . has ( remoteAddr ) ) {
141+ return true
142+ }
118143 for ( const [ isIPv4 , addr , mask ] of cidrRules ) {
119144 if ( isIPv4 ) {
120145 if ( remoteIPv4Addr === undefined ) {
@@ -221,14 +246,25 @@ export const ipRestriction = (
221246
222247 const remoteData = { addr, type, isIPv4 : type === 'IPv4' }
223248
224- if ( denyMatcher ( remoteData ) ) {
225- if ( onError ) {
226- return onError ( { addr, type } , c )
249+ try {
250+ if ( denyMatcher ( remoteData ) ) {
251+ if ( onError ) {
252+ return onError ( { addr, type } , c )
253+ }
254+ throw blockError ( c )
227255 }
228- throw blockError ( c )
229- }
230- if ( allowMatcher ( remoteData ) ) {
231- return await next ( )
256+ if ( allowMatcher ( remoteData ) ) {
257+ return await next ( )
258+ }
259+ } catch ( e ) {
260+ if (
261+ e instanceof TypeError &&
262+ ( e as InvalidIPAddressError ) . code === INVALID_IP_ADDRESS_ERROR_CODE
263+ ) {
264+ // If an invalid IP address is specified, treat it as if no IP address was specified
265+ throw blockError ( c )
266+ }
267+ throw e
232268 }
233269
234270 if ( allowLength === 0 ) {
0 commit comments