Skip to content

Commit

Permalink
Merge pull request #15 from matomo-org/ipv6fullmask
Browse files Browse the repository at this point in the history
Makes it possible to fully anonymize a IPv6 address
  • Loading branch information
sgiehl authored Oct 5, 2020
2 parents 010e496 + e69276a commit ff654b8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ php:
- 7.4

install:
- composer install
- travis_retry composer install

script:
- vendor/bin/phpunit
3 changes: 2 additions & 1 deletion src/IPv6.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function anonymize($byteCount)
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
'ffff:ffff:ffff:ffff::',
'ffff:ffff:ffff:0000::',
'ffff:ff00:0000:0000::'
'ffff:ff00:0000:0000::',
'0000::'
);

$newBinaryIp = $newBinaryIp & pack('a16', inet_pton($masks[$byteCount]));
Expand Down
6 changes: 4 additions & 2 deletions tests/IPv6Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ public function getAddressesToAnonymize()
"\x20\x01\x0d\xb8\x00\x00\x08\xd3\x00\x00\x8a\x2e\x00\x70\x73\x44",
"\x20\x01\x0d\xb8\x00\x00\x08\xd3\x00\x00\x00\x00\x00\x00\x00\x00", // mask 64 bits
"\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // mask 80 bits
"\x20\x01\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" // mask 104 bits
"\x20\x01\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // mask 104 bits
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" // mask all bits
)),
array('2001:6f8:900:724::2', array(
"\x20\x01\x06\xf8\x09\x00\x07\x24\x00\x00\x00\x00\x00\x00\x00\x02",
"\x20\x01\x06\xf8\x09\x00\x07\x24\x00\x00\x00\x00\x00\x00\x00\x00",
"\x20\x01\x06\xf8\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
"\x20\x01\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
))
);
}
Expand All @@ -91,7 +93,7 @@ public function testAnonymize($ipString, $expected)
$this->assertInstanceOf('Matomo\Network\IPv6', $ip);

// each IP is tested with 0 to 4 octets masked
for ($byteCount = 0; $byteCount < 4; $byteCount++) {
for ($byteCount = 0; $byteCount <= 4; $byteCount++) {
$result = $ip->anonymize($byteCount);
$this->assertEquals($expected[$byteCount], $result->toBinary(), "Got $result, Expected " . bin2hex($expected[$byteCount]) . ", Mask: " . $byteCount);
}
Expand Down

0 comments on commit ff654b8

Please sign in to comment.