fix(utils/ipaddr): support IPv6-mapped IPv4 address (#3727)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent fc8fb0f · Browse files · 3 files·+49 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+49 added−0 removed
- tests:+22 added−0 removed
- other:+27 added−0 removed
Provenance
- Commit
af5affc- Tree
7611339- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
fc8fb0f
3 files changed+49 added−0 removed
modified:src/middleware/ip-restriction/index.test.ts+4 added
98 unmodified lines991001011021031041051061071081091108 unmodified lines98 unmodified lines }) 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() }) 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/utils/ipaddr.test.ts+18 added
1234566 unmodified lines131415161718192021222324252627288 unmodified lines37383940414243444546474849505152535455565758596061626364655 unmodified lines717273747576777879import { convertIPv4BinaryToString, convertIPv4ToBinary, convertIPv6BinaryToString, convertIPv6ToBinary, distinctRemoteAddr,6 unmodified lines expect(expandIPv6('::1')).toBe('0000:0000:0000:0000:0000:0000:0000:0001') expect(expandIPv6('2001:2::')).toBe('2001:0002:0000:0000:0000:0000:0000:0000') expect(expandIPv6('2001:2::')).toBe('2001:0002:0000:0000:0000:0000:0000:0000') expect(expandIPv6('2001:0:0:db8::1')).toBe('2001:0000:0000:0db8:0000:0000:0000:0001') expect(expandIPv6('::ffff:127.0.0.1')).toBe('0000:0000:0000:0000:0000:ffff:7f00:0001') })})describe('distinctRemoteAddr', () => { it('Should result be valid', () => { expect(distinctRemoteAddr('1::1')).toBe('IPv6') expect(distinctRemoteAddr('::1')).toBe('IPv6') expect(distinctRemoteAddr('::ffff:127.0.0.1')).toBe('IPv6')
expect(distinctRemoteAddr('192.168.2.0')).toBe('IPv4') expect(distinctRemoteAddr('192.168.2.0')).toBe('IPv4')
8 unmodified lines
expect(convertIPv4ToBinary('0.0.1.0')).toBe(1n << 8n) })})
describe('convertIPv4ToString', () => { // add tons of test cases here test.each` input | expected ${'0.0.0.0'} | ${'0.0.0.0'} ${'0.0.0.1'} | ${'0.0.0.1'} ${'0.0.1.0'} | ${'0.0.1.0'} `('convertIPv4ToString($input) === $expected', ({ input, expected }) => { expect(convertIPv4BinaryToString(convertIPv4ToBinary(input))).toBe(expected) })})
describe('convertIPv6ToBinary', () => { it('Should result is valid', () => { expect(convertIPv6ToBinary('::0')).toBe(0n) expect(convertIPv6ToBinary('::1')).toBe(1n)
expect(convertIPv6ToBinary('::f')).toBe(15n) expect(convertIPv6ToBinary('1234:::5678')).toBe(24196103360772296748952112894165669496n) expect(convertIPv6ToBinary('::ffff:127.0.0.1')).toBe(281472812449793n) })})
describe('convertIPv6ToString', () => {5 unmodified lines ${'1234:::5678'} | ${'1234::5678'} ${'2001:2::'} | ${'2001:2::'} ${'2001::db8:0:0:0:0:1'} | ${'2001:0:db8::1'} ${'1234:5678:9abc:def0:1234:5678:9abc:def0'} | ${'1234:5678:9abc:def0:1234:5678:9abc:def0'} ${'::ffff:127.0.0.1'} | ${'::ffff:127.0.0.1'} `('convertIPv6ToString($input) === $expected', ({ input, expected }) => { expect(convertIPv6BinaryToString(convertIPv6ToBinary(input))).toBe(expected) })})modified:src/utils/ipaddr.ts+27 added
10 unmodified lines11 * @return expanded IPv6 Address12 */13 export const expandIPv6 = (ipV6: string): string => {14 const sections = ipV6.split(':')15+ if (IPV4_REGEX.test(sections.at(-1) as string)) {16+ sections.splice(17+ -1,18+ 1,19+ ...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1) as string)) // => ::7f00:000120+ .substring(2) // => 7f00:000121+ .split(':') // => ['7f00', '0001']22+ )23+ }24 for (let i = 0; i < sections.length; i++) {25 const node = sections[i]26 if (node !== '') {27 sections[i] = node.padStart(4, '0')51 unmodified lines79 return result80 }81 82 /**83+ * Convert a binary representation of an IPv4 address to a string.84+ * @param ipV4 binary IPv4 Address85+ * @return IPv4 Address in string86+ */87+export const convertIPv4BinaryToString = (ipV4: bigint): string => {88+ const sections = []89+ for (let i = 0; i < 4; i++) {90+ sections.push((ipV4 >> BigInt(8 * (3 - i))) & 0xffn)91+ }92+ return sections.join('.')93+}94+95+/**96 * Convert a binary representation of an IPv6 address to a string.97 * @param ipV6 binary IPv6 Address98 * @return normalized IPv6 Address in string99 */100 export const convertIPv6BinaryToString = (ipV6: bigint): string => {101+ // IPv6-mapped IPv4 address102+ if (ipV6 >> 32n === 0xffffn) {103+ return `::ffff:${convertIPv4BinaryToString(ipV6 & 0xffffffffn)}`104+ }105+106 const sections = []107 for (let i = 0; i < 8; i++) {108 sections.push(((ipV6 >> BigInt(16 * (7 - i))) & 0xffffn).toString(16))109 }31 unmodified lines