Skip to content

Commit e8fdc47

Browse files
jbtronicsnicolas-grekas
authored andcommitted
[HttpFoundation] Fixed IpUtils::anonymize exception when using IPv6 link-local addresses with RFC4007 scoping
1 parent 431771b commit e8fdc47

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

IpUtils.php

+10
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ public static function checkIp6(string $requestIp, string $ip): bool
182182
*/
183183
public static function anonymize(string $ip): string
184184
{
185+
/**
186+
* If the IP contains a % symbol, then it is a local-link address with scoping according to RFC 4007
187+
* In that case, we only care about the part before the % symbol, as the following functions, can only work with
188+
* the IP address itself. As the scope can leak information (containing interface name), we do not want to
189+
* include it in our anonymized IP data.
190+
*/
191+
if (str_contains($ip, '%')) {
192+
$ip = substr($ip, 0, strpos($ip, '%'));
193+
}
194+
185195
$wrappedIPv6 = false;
186196
if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) {
187197
$wrappedIPv6 = true;

Tests/IpUtilsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public static function anonymizedIpData()
147147
['[2a01:198::3]', '[2a01:198::]'],
148148
['::ffff:123.234.235.236', '::ffff:123.234.235.0'], // IPv4-mapped IPv6 addresses
149149
['::123.234.235.236', '::123.234.235.0'], // deprecated IPv4-compatible IPv6 address
150+
['fe80::1fc4:15d8:78db:2319%enp4s0', 'fe80::'], // IPv6 link-local with RFC4007 scoping
150151
];
151152
}
152153

0 commit comments

Comments
 (0)