Skip to content
Myr

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

3 files+83 added−15 removed

modified:src/middleware/ip-restriction/index.test.ts+26 added
modified:src/middleware/ip-restriction/index.ts+41 added-12 removed
5 unmodified lines
6import type { Context, MiddlewareHandler } from '../..'7import type { AddressType, GetConnInfo } from '../../helper/conninfo'8import { HTTPException } from '../../http-exception'9import {10 convertIPv4MappedIPv6ToIPv4,11 convertIPv4ToBinary,12 convertIPv6BinaryToString,13 convertIPv6ToBinary,14 distinctRemoteAddr,15 isIPv4MappedIPv6,16} from '../../utils/ipaddr'1718/**19 * Function to get IP Address
37 unmodified lines
57 if (type === undefined) {58 throw new TypeError(`Invalid rule: ${rule}`)59 }6059 const isIPv4 = type === 'IPv4'60 const prefix = parseInt(separatedRule[1])61 let isIPv4 = type === 'IPv4'62 let prefix = parseInt(separatedRule[1])6364 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 }7475 const mask = ((1n << BigInt(prefix)) - 1n) << BigInt((isIPv4 ? 32 : 128) - prefix)7677 cidrRules.push([isIPv4, addr & mask, mask] as [boolean, bigint, bigint])78 continue
3 unmodified lines
82 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 }99100 return (remote: {
4 unmodified lines
105 }): 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 lines
modified:src/utils/ipaddr.ts+16 added-3 removed
92 unmodified lines
93 return sections.join('.')94}9596/**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 */101export const isIPv4MappedIPv6 = (ipv6binary: bigint): boolean => ipv6binary >> 32n === 0xffffn102103/**104 * Extract the IPv4 portion from an IPv4-mapped IPv6 address105 * @param ipv6binary binary IPv4-mapped IPv6 Address106 * @return binary IPv4 Address107 */108export const convertIPv4MappedIPv6ToIPv4 = (ipv6binary: bigint): bigint => ipv6binary & 0xffffffffn109110/**111 * Convert a binary representation of an IPv6 address to a string.112 * @param ipV6 binary IPv6 Address113 * @return normalized IPv6 Address in string114 */115export 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 }119120 const sections = []121 for (let i = 0; i < 8; i++) {
33 unmodified lines