Skip to content

[HttpFoundation] Add a way to anonymize IPs #32194

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
Nov 9, 2019
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ CHANGELOG
make sure to run `CREATE INDEX EXPIRY ON sessions (sess_lifetime)` to update your database
to speed up garbage collection of expired sessions.
* added `SessionHandlerFactory` to create session handlers with a DSN

* added `IpUtils::anonymize()` to help with GDPR compliance.

4.3.0
-----

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/HttpFoundation/IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,36 @@ public static function checkIp6($requestIp, $ip)

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

/**
* Anonymizes an IP/IPv6.
*
* Removes the last byte for v4 and the last 8 bytes for v6 IPs
*/
public static function anonymize(string $ip): string
{
$wrappedIPv6 = false;
if ('[' === substr($ip, 0, 1) && ']' === substr($ip, -1, 1)) {
$wrappedIPv6 = true;
$ip = substr($ip, 1, -1);
}

$packedAddress = inet_pton($ip);
if (4 === \strlen($packedAddress)) {
$mask = '255.255.255.0';
} elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
$mask = '::ffff:ffff:ff00';
} elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
$mask = '::ffff:ff00';
} else {
$mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
}
$ip = inet_ntop($packedAddress & inet_pton($mask));

if ($wrappedIPv6) {
$ip = '['.$ip.']';
}

return $ip;
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,30 @@ public function invalidIpAddressData()
'invalid request IP with invalid proxy wildcard' => ['0.0.0.0', '*'],
];
}

/**
* @dataProvider anonymizedIpData
*/
public function testAnonymize($ip, $expected)
{
$this->assertSame($expected, IpUtils::anonymize($ip));
}

public function anonymizedIpData()
{
return [
['192.168.1.1', '192.168.1.0'],
['1.2.3.4', '1.2.3.0'],
['2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603::'],
['2a01:198:603:10:396e:4789:8e99:890f', '2a01:198:603:10::'],
['::1', '::'],
['0:0:0:0:0:0:0:1', '::'],
['1:0:0:0:0:0:0:1', '1::'],
['0:0:603:50:396e:4789:8e99:0001', '0:0:603:50::'],
['[0:0:603:50:396e:4789:8e99:0001]', '[0:0:603:50::]'],
['[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
];
}
}