Skip to content

Commit

Permalink
Mask obfuscated bit according to its length
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Dec 11, 2024
1 parent fff849a commit 25961e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Admin/src/Service/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function logAdminVisit(array $serverParams, string $name, string $status)
$organization = $this->locationService->getOrganization($ipAddress)->getName();

$adminLogin = (new AdminLogin())
->setAdminIp($this->locationService->obfuscateIpAddress($ipAddress))
->setAdminIp(IpService::obfuscateIpAddress($ipAddress))
->setContinent($continent)
->setCountry($country)
->setOrganization($organization)
Expand Down
16 changes: 16 additions & 0 deletions src/App/src/Service/IpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use function filter_var;
use function getenv;
use function preg_replace_callback;
use function str_repeat;
use function strlen;

use const FILTER_FLAG_IPV4;
use const FILTER_FLAG_IPV6;
Expand Down Expand Up @@ -53,4 +56,17 @@ public static function isPublicIp(string $ipAddress): bool
FILTER_FLAG_NO_RES_RANGE
) === $ipAddress;
}

public static function obfuscateIpAddress(string $ipAddress, string $mask = 'x'): string
{
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$pattern = '/\d+$/';
} elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$pattern = '/[a-z0-9]+$/i';
} else {
return $ipAddress;
}

return preg_replace_callback($pattern, fn (array $last) => str_repeat($mask, strlen($last[0])), $ipAddress);
}
}

0 comments on commit 25961e7

Please sign in to comment.