Skip to content

[HttpFoundation] Clear IpUtils cache to prevent memory leaks #50064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpFoundation] Clear IpUtils cache to prevent memory leaks
  • Loading branch information
danielburger1337 authored and nicolas-grekas committed Apr 20, 2023
commit 21ea8624fcf933d132d4de05a764a28d6ac01e66
56 changes: 40 additions & 16 deletions src/Symfony/Component/HttpFoundation/IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,34 @@ public static function checkIp(string $requestIp, string|array $ips): bool
public static function checkIp4(string $requestIp, string $ip): bool
{
$cacheKey = $requestIp.'-'.$ip.'-v4';
if (isset(self::$checkedIps[$cacheKey])) {
return self::$checkedIps[$cacheKey];
if (null !== $cacheValue = self::getCacheResult($cacheKey)) {
return $cacheValue;
}

if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

if (str_contains($ip, '/')) {
[$address, $netmask] = explode('/', $ip, 2);

if ('0' === $netmask) {
return self::$checkedIps[$cacheKey] = false !== filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
return self::setCacheResult($cacheKey, false !== filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4));
}

if ($netmask < 0 || $netmask > 32) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}
} else {
$address = $ip;
$netmask = 32;
}

if (false === ip2long($address)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
return self::setCacheResult($cacheKey, 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask));
}

/**
Expand All @@ -120,8 +120,8 @@ public static function checkIp4(string $requestIp, string $ip): bool
public static function checkIp6(string $requestIp, string $ip): bool
{
$cacheKey = $requestIp.'-'.$ip.'-v6';
if (isset(self::$checkedIps[$cacheKey])) {
return self::$checkedIps[$cacheKey];
if (null !== $cacheValue = self::getCacheResult($cacheKey)) {
return $cacheValue;
}

if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
Expand All @@ -130,26 +130,26 @@ public static function checkIp6(string $requestIp, string $ip): bool

// Check to see if we were given a IP4 $requestIp or $ip by mistake
if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

if (str_contains($ip, '/')) {
[$address, $netmask] = explode('/', $ip, 2);

if (!filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

if ('0' === $netmask) {
return (bool) unpack('n*', @inet_pton($address));
}

if ($netmask < 1 || $netmask > 128) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}
} else {
if (!filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

$address = $ip;
Expand All @@ -160,19 +160,19 @@ public static function checkIp6(string $requestIp, string $ip): bool
$bytesTest = unpack('n*', @inet_pton($requestIp));

if (!$bytesAddr || !$bytesTest) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}

for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
$left = $netmask - 16 * ($i - 1);
$left = ($left <= 16) ? $left : 16;
$mask = ~(0xFFFF >> $left) & 0xFFFF;
if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
return self::$checkedIps[$cacheKey] = false;
return self::setCacheResult($cacheKey, false);
}
}

return self::$checkedIps[$cacheKey] = true;
return self::setCacheResult($cacheKey, true);
}

/**
Expand Down Expand Up @@ -214,4 +214,28 @@ public static function isPrivateIp(string $requestIp): bool
{
return self::checkIp($requestIp, self::PRIVATE_SUBNETS);
}

private static function getCacheResult(string $cacheKey): ?bool
{
if (isset(self::$checkedIps[$cacheKey])) {
// Move the item last in cache (LRU)
$value = self::$checkedIps[$cacheKey];
unset(self::$checkedIps[$cacheKey]);
self::$checkedIps[$cacheKey] = $value;

return self::$checkedIps[$cacheKey];
}

return null;
}

private static function setCacheResult(string $cacheKey, bool $result): bool
{
if (1000 < \count(self::$checkedIps)) {
// stop memory leak if there are many keys
self::$checkedIps = \array_slice(self::$checkedIps, 500, null, true);
}

return self::$checkedIps[$cacheKey] = $result;
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,23 @@ public static function getIsPrivateIpData(): array
['2606:4700:20::681a:e06', false],
];
}

public function testCacheSizeLimit()
{
$ref = new \ReflectionClass(IpUtils::class);

/** @var array */
$checkedIps = $ref->getStaticPropertyValue('checkedIps');
$this->assertIsArray($checkedIps);

$maxCheckedIps = 1000;

for ($i = 1; $i < $maxCheckedIps * 1.5; ++$i) {
$ip = '192.168.1.'.str_pad((string) $i, 3, '0');

IpUtils::checkIp4($ip, '127.0.0.1');
}

$this->assertLessThan($maxCheckedIps, \count($checkedIps));
}
}