Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(ip-restriction): canonicalize IPv4-mapped IPv6 addresses * refine the match rule * refactor: improve IPv4-mapped IPv6 address handling in IP restriction middleware Co-authored-by: Taku Amano <taku@taaas.jp> ---------
authored by Yusuke Wada · committed by GitHub · parent b470278 · Browse files · 3 files·+83 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Taku Amano <taku@taaas.jp>
Diff stats
3 files+83 added−15 removed
- tests:+26 added−0 removed
- other:+57 added−15 removed
Provenance
- Commit
48fa223- Tree
87c08f6- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
b470278
3 files changed+83 added−15 removed
modified:src/middleware/ip-restriction/index.test.ts+26 added
94 unmodified lines95969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351368 unmodified lines94 unmodified lines expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.1/32')).toBeTruthy() expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.2/32')).toBeFalsy()
expect(await isMatch({ addr: '::0', type: 'IPv6' }, '::0/1')).toBeTruthy() expect(await isMatch({ addr: '::1', type: 'IPv6' }, '0.0.0.0/24')).toBeFalsy() expect(await isMatch({ addr: '::abcd:1', type: 'IPv6' }, '127.0.0.0/8')).toBeFalsy() expect(await isMatch({ addr: '::1', type: 'IPv6' }, '::ffff:1.0.0.0/96')).toBeFalsy()
expect( await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::ffff:192.168.1.0/120') ).toBeTruthy() expect( await isMatch({ addr: '192.168.1.1', type: 'IPv4' }, '::ffff:192.168.1.0/120') ).toBeTruthy() expect( await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '192.168.1.0/24') ).toBeTruthy() expect(await isMatch({ addr: '::ffff:10.0.0.1', type: 'IPv6' }, '192.168.1.0/24')).toBeFalsy() expect(await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::/0')).toBeTruthy() expect( await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::ffff:0:0/95') ).toBeTruthy() }) it('Static Rules', async () => { expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.1')).toBeTruthy() expect(await isMatch({ addr: '1234::5678', type: 'IPv6' }, '1234::5678')).toBeTruthy() expect( await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '::ffff:127.0.0.1') ).toBeTruthy() expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '::ffff:7f00:1')).toBeTruthy() expect(await isMatch({ addr: '127.0.0.1', type: 'IPv4' }, '::ffff:127.0.0.1')).toBeTruthy() // IPv4-mapped IPv6 addresses are canonicalized to IPv4 expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '127.0.0.1')).toBeTruthy() expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '127.0.0.2')).toBeFalsy() // non-dotted IPv4-mapped forms are not canonicalized (treated as IPv6) expect(await isMatch({ addr: '::ffff:7f00:1', type: 'IPv6' }, '127.0.0.1')).toBeFalsy() // regular IPv6 is not affected expect(await isMatch({ addr: '::1', type: 'IPv6' }, '::1')).toBeTruthy() }) it('Function Rules', async () => { expect(await isMatch({ addr: '0.0.0.0', type: 'IPv4' }, () => true)).toBeTruthy() expect(await isMatch({ addr: '0.0.0.0', type: 'IPv4' }, () => false)).toBeFalsy()8 unmodified linesmodified:src/middleware/ip-restriction/index.ts+41 added-12 removed
5 unmodified lines6 import type { Context, MiddlewareHandler } from '../..'7 import type { AddressType, GetConnInfo } from '../../helper/conninfo'8 import { HTTPException } from '../../http-exception'9 import {10+ convertIPv4MappedIPv6ToIPv4,11 convertIPv4ToBinary,12 convertIPv6BinaryToString,13 convertIPv6ToBinary,14 distinctRemoteAddr,15+ isIPv4MappedIPv6,16 } from '../../utils/ipaddr'17 18 /**19 * Function to get IP Address37 unmodified lines57 if (type === undefined) {58 throw new TypeError(`Invalid rule: ${rule}`)59 }60 59- const isIPv4 = type === 'IPv4'60- const prefix = parseInt(separatedRule[1])61+ let isIPv4 = type === 'IPv4'62+ let prefix = parseInt(separatedRule[1])63 64 if (isIPv4 ? prefix === 32 : prefix === 128) {65 // this rule is a static rule66 rule = addrStr67 } else {66- const addr = (isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(addrStr)68+ let addr = (isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(addrStr)69+ if (type === 'IPv6' && isIPv4MappedIPv6(addr) && prefix >= 96) {70+ isIPv4 = true71+ addr = convertIPv4MappedIPv6ToIPv4(addr)72+ prefix -= 9673+ }74+75 const mask = ((1n << BigInt(prefix)) - 1n) << BigInt((isIPv4 ? 32 : 128) - prefix)76 77 cidrRules.push([isIPv4, addr & mask, mask] as [boolean, bigint, bigint])78 continue3 unmodified lines82 const type = distinctRemoteAddr(rule)83 if (type === undefined) {84 throw new TypeError(`Invalid rule: ${rule}`)85 }78- staticRules.add(79- type === 'IPv4'80- ? rule // IPv4 address is already normalized, so it is registered as is.81- : convertIPv6BinaryToString(convertIPv6ToBinary(rule)) // normalize IPv6 address (e.g. 0000:0000:0000:0000:0000:0000:0000:0001 => ::1)82- )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: prefix95+ }96+ }97 }98 }99 100 return (remote: {4 unmodified lines105 }): boolean => {106 if (staticRules.has(remote.addr)) {107 return true108 }109+ const remoteAddr = (remote.binaryAddr ||= (110+ remote.isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary111+ )(remote.addr))112+ const remoteIPv4Addr =113+ remote.isIPv4 || isIPv4MappedIPv6(remoteAddr)114+ ? remote.isIPv4115+ ? remoteAddr116+ : convertIPv4MappedIPv6ToIPv4(remoteAddr)117+ : undefined118 for (const [isIPv4, addr, mask] of cidrRules) {96- if (isIPv4 !== remote.isIPv4) {119+ if (isIPv4) {120+ if (remoteIPv4Addr === undefined) {121+ continue122+ }123+ if ((remoteIPv4Addr & mask) === addr) {124+ return true125+ }126 continue127 }99- const remoteAddr = (remote.binaryAddr ||= (100- isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary101- )(remote.addr))128+ if (remote.isIPv4) {129+ continue130+ }131 if ((remoteAddr & mask) === addr) {132 return true133 }134 }109 unmodified linesmodified:src/utils/ipaddr.ts+16 added-3 removed
92 unmodified lines93 return sections.join('.')94 }95 96 /**97+ * Check if a binary IPv6 address is an IPv4-mapped IPv6 address (::ffff:x.x.x.x)98+ * @param ipv6binary binary IPv6 Address99+ * @return true if the address is an IPv4-mapped IPv6 address100+ */101+export const isIPv4MappedIPv6 = (ipv6binary: bigint): boolean => ipv6binary >> 32n === 0xffffn102+103+/**104+ * Extract the IPv4 portion from an IPv4-mapped IPv6 address105+ * @param ipv6binary binary IPv4-mapped IPv6 Address106+ * @return binary IPv4 Address107+ */108+export const convertIPv4MappedIPv6ToIPv4 = (ipv6binary: bigint): bigint => ipv6binary & 0xffffffffn109+110+/**111 * Convert a binary representation of an IPv6 address to a string.112 * @param ipV6 binary IPv6 Address113 * @return normalized IPv6 Address in string114 */115 export const convertIPv6BinaryToString = (ipV6: bigint): string => {102- // IPv6-mapped IPv4 address103- if (ipV6 >> 32n === 0xffffn) {104- return `::ffff:${convertIPv4BinaryToString(ipV6 & 0xffffffffn)}`116+ if (isIPv4MappedIPv6(ipV6)) {117+ return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`118 }119 120 const sections = []121 for (let i = 0; i < 8; i++) {33 unmodified lines