diff --git a/IpUtils.php b/IpUtils.php index 5e1e29c95..11a43238b 100644 --- a/IpUtils.php +++ b/IpUtils.php @@ -196,6 +196,16 @@ public static function anonymize(string $ip/* , int $v4Bytes = 1, int $v6Bytes = throw new \InvalidArgumentException('Cannot anonymize more than 4 bytes for IPv4 and 16 bytes for IPv6.'); } + /** + * If the IP contains a % symbol, then it is a local-link address with scoping according to RFC 4007 + * In that case, we only care about the part before the % symbol, as the following functions, can only work with + * the IP address itself. As the scope can leak information (containing interface name), we do not want to + * include it in our anonymized IP data. + */ + if (str_contains($ip, '%')) { + $ip = substr($ip, 0, strpos($ip, '%')); + } + $wrappedIPv6 = false; if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) { $wrappedIPv6 = true; diff --git a/RequestStack.php b/RequestStack.php index c358ea68c..153bd9ad7 100644 --- a/RequestStack.php +++ b/RequestStack.php @@ -114,4 +114,11 @@ public function getSession(): SessionInterface throw new SessionNotFoundException(); } + + public function resetRequestFormats(): void + { + static $resetRequestFormats; + $resetRequestFormats ??= \Closure::bind(static fn () => self::$formats = null, null, Request::class); + $resetRequestFormats(); + } } diff --git a/Tests/IpUtilsTest.php b/Tests/IpUtilsTest.php index 95044106b..5ed3e7b22 100644 --- a/Tests/IpUtilsTest.php +++ b/Tests/IpUtilsTest.php @@ -147,6 +147,7 @@ public static function anonymizedIpData() ['[2a01:198::3]', '[2a01:198::]'], ['::ffff:123.234.235.236', '::ffff:123.234.235.0'], // IPv4-mapped IPv6 addresses ['::123.234.235.236', '::123.234.235.0'], // deprecated IPv4-compatible IPv6 address + ['fe80::1fc4:15d8:78db:2319%enp4s0', 'fe80::'], // IPv6 link-local with RFC4007 scoping ]; } diff --git a/Tests/RequestStackTest.php b/Tests/RequestStackTest.php index 6fba27589..f97efe480 100644 --- a/Tests/RequestStackTest.php +++ b/Tests/RequestStackTest.php @@ -74,4 +74,18 @@ public function testGetParentRequest() $requestStack->push($secondSubRequest); $this->assertSame($firstSubRequest, $requestStack->getParentRequest()); } + + public function testResetRequestFormats() + { + $requestStack = new RequestStack(); + + $request = Request::create('/foo'); + $request->setFormat('foo', ['application/foo']); + + $this->assertSame(['application/foo'], $request->getMimeTypes('foo')); + + $requestStack->resetRequestFormats(); + + $this->assertSame([], $request->getMimeTypes('foo')); + } }