Skip to content

Commit bcd290a

Browse files
yusukebeusualoma
andauthored
fix(utils/ipaddr): do not compress a single 0 group to :: (#4971)
Co-authored-by: Taku Amano <[email protected]>
1 parent c968177 commit bcd290a

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

‎src/utils/ipaddr.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ describe('convertIPv6ToString', () => {
125125
${'1234:5678:9abc:def0:1234:5678:9abc:def0'} | ${'1234:5678:9abc:def0:1234:5678:9abc:def0'}
126126
${'::ffff:127.0.0.1'} | ${'::ffff:127.0.0.1'}
127127
${'fe80::1%eth0'} | ${'fe80::1'}
128+
${'1:0:2:3:4:5:6:7'} | ${'1:0:2:3:4:5:6:7'}
129+
${'0:1:2:3:4:5:6:7'} | ${'0:1:2:3:4:5:6:7'}
130+
${'1:2:3:4:5:6:7:0'} | ${'1:2:3:4:5:6:7:0'}
131+
${'1:0:0:2:3:4:5:6'} | ${'1::2:3:4:5:6'}
128132
`('convertIPv6ToString($input) === $expected', ({ input, expected }) => {
129133
expect(convertIPv6BinaryToString(convertIPv6ToBinary(input))).toBe(expected)
130134
})

‎src/utils/ipaddr.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ export const convertIPv6BinaryToString = (ipV6: bigint): string => {
355355
maxZeroEnd = 8
356356
}
357357
}
358-
if (maxZeroStart !== -1) {
358+
// RFC 5952 4.2.2: compress only zero runs of at least two 16-bit fields.
359+
if (maxZeroStart !== -1 && maxZeroEnd - maxZeroStart > 1) {
359360
sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ':')
360361
}
361362

0 commit comments

Comments
 (0)