You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
361 lines
12 KiB
PHP
361 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
final class AdminDataMasker
|
|
{
|
|
public static function shouldMask()
|
|
{
|
|
return !AdminPermission::isSuperAdmin();
|
|
}
|
|
|
|
public static function email($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
$parts = explode('@', $value, 2);
|
|
if (count($parts) !== 2 || $parts[0] === '' || $parts[1] === '') {
|
|
return self::maskAccountValue($value);
|
|
}
|
|
|
|
return self::substr($parts[0], 0, 1) . '***@' . $parts[1];
|
|
}
|
|
|
|
public static function ip($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
if (strpos($value, '/') !== false) {
|
|
list($address, $prefix) = array_pad(explode('/', $value, 2), 2, '');
|
|
if (filter_var($address, FILTER_VALIDATE_IP) && preg_match('/^\d{1,3}$/', $prefix)) {
|
|
return self::ip($address) . '/' . $prefix;
|
|
}
|
|
}
|
|
|
|
if (preg_match('/^\[([^\]]+)\]:(\d{1,5})$/', $value, $matches)
|
|
&& filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
return '[' . self::ip($matches[1]) . ']:' . $matches[2];
|
|
}
|
|
|
|
if (preg_match('/^((?:\d{1,3}\.){3}\d{1,3}):(\d{1,5})$/', $value, $matches)
|
|
&& filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
return self::ip($matches[1]) . ':' . $matches[2];
|
|
}
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
$parts = explode('.', $value);
|
|
return $parts[0] . '.' . $parts[1] . '.' . $parts[2] . '.*';
|
|
}
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
$packed = inet_pton($value);
|
|
if ($packed !== false) {
|
|
$hex = unpack('H*', $packed);
|
|
$groups = str_split($hex[1], 4);
|
|
return self::ipv6Group($groups[0]) . ':' . self::ipv6Group($groups[1]) . ':*:*:*:*:*:*';
|
|
}
|
|
}
|
|
|
|
return self::account($value);
|
|
}
|
|
|
|
public static function account($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
if (strpos($value, '@') !== false) {
|
|
return self::email($value);
|
|
}
|
|
|
|
return self::maskAccountValue($value);
|
|
}
|
|
|
|
private static function maskAccountValue($value)
|
|
{
|
|
$value = (string) $value;
|
|
|
|
$length = self::length($value);
|
|
if ($length === 1) {
|
|
return self::substr($value, 0, 1) . '***';
|
|
}
|
|
if ($length <= 5) {
|
|
return self::substr($value, 0, 1) . '***' . self::substr($value, -1);
|
|
}
|
|
|
|
return self::substr($value, 0, 2) . '***' . self::substr($value, -2);
|
|
}
|
|
|
|
private static function ipv6Group($value)
|
|
{
|
|
$value = ltrim((string) $value, '0');
|
|
return $value === '' ? '0' : $value;
|
|
}
|
|
|
|
public static function paymentAccount($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
if (strpos($value, '@') !== false) {
|
|
return self::email($value);
|
|
}
|
|
|
|
$digits = preg_replace('/\D+/', '', $value);
|
|
if (strlen($digits) >= 7) {
|
|
return substr($digits, 0, 3) . '****' . substr($digits, -4);
|
|
}
|
|
|
|
return self::account($value);
|
|
}
|
|
|
|
public static function emailList($value)
|
|
{
|
|
return self::maskList($value, [self::class, 'email']);
|
|
}
|
|
|
|
public static function ipList($value)
|
|
{
|
|
return self::maskList($value, [self::class, 'ip']);
|
|
}
|
|
|
|
public static function accountList($value)
|
|
{
|
|
return self::maskList($value, [self::class, 'account']);
|
|
}
|
|
|
|
public static function ipIfAddress($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
return filter_var($value, FILTER_VALIDATE_IP) ? self::ip($value) : $value;
|
|
}
|
|
|
|
public static function ipsInText($value)
|
|
{
|
|
$value = (string) $value;
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
$value = preg_replace_callback('/(?<![0-9.])(?:\d{1,3}\.){3}\d{1,3}(?![0-9.])/', function ($matches) {
|
|
return filter_var($matches[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
|
|
? self::ip($matches[0])
|
|
: $matches[0];
|
|
}, $value);
|
|
|
|
return preg_replace_callback('/(?<![0-9A-Fa-f:])(?:[0-9A-Fa-f]{0,4}:){2,7}[0-9A-Fa-f]{0,4}(?![0-9A-Fa-f:])/', function ($matches) {
|
|
return filter_var($matches[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
|
|
? self::ip($matches[0])
|
|
: $matches[0];
|
|
}, $value);
|
|
}
|
|
|
|
public static function sensitiveText($value)
|
|
{
|
|
$value = (string) $value;
|
|
if (!self::shouldMask() || $value === '') {
|
|
return $value;
|
|
}
|
|
|
|
$value = preg_replace('/\bBearer\s+[A-Za-z0-9._~+\/=-]+/i', 'Bearer [已移除]', $value);
|
|
$value = preg_replace('/\bsk-[A-Za-z0-9_-]{8,}\b/i', '[已移除]', $value);
|
|
$value = preg_replace('#(https?://[^:/\s]+:)[^@\s/]+@#i', '$1[已移除]@', $value);
|
|
$value = preg_replace(
|
|
'/((?:"|\')?(?:password|passwd|password_hash|apple_pass|api_key|apikey|api_token|apitoken|access_token|refresh_token|client_secret|secret|authorization|token|token_hash)(?:"|\')?\s*[:=]\s*)(?:"[^"]*"|\'[^\']*\'|[^\s,;]+)/i',
|
|
'$1[已移除]',
|
|
$value
|
|
);
|
|
$value = preg_replace_callback(
|
|
'/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i',
|
|
function ($matches) {
|
|
return self::email($matches[0]);
|
|
},
|
|
$value
|
|
);
|
|
|
|
return self::ipsInText($value);
|
|
}
|
|
|
|
public static function sanitizeArray(array $data)
|
|
{
|
|
if (!self::shouldMask()) {
|
|
return $data;
|
|
}
|
|
|
|
$secretKeys = [
|
|
'pass',
|
|
'password',
|
|
'passwd',
|
|
'password_hash',
|
|
'applepass',
|
|
'apple_pass',
|
|
'nss_pass',
|
|
'nssr_pass',
|
|
'connection_password',
|
|
'key',
|
|
'api_key',
|
|
'apikey',
|
|
'access_key',
|
|
'api_token',
|
|
'apitoken',
|
|
'token',
|
|
'token_hash',
|
|
'access_token',
|
|
'refresh_token',
|
|
'client_secret',
|
|
'api_secret',
|
|
'access_secret',
|
|
'signing_secret',
|
|
'authorization',
|
|
'authorization_header',
|
|
'auth_token',
|
|
'auth_key',
|
|
'secret',
|
|
'secret_key',
|
|
'app_secret',
|
|
'merchant_secret',
|
|
'merchant_key',
|
|
'client_key',
|
|
'signing_key',
|
|
'webhook_secret',
|
|
'private_key',
|
|
'encryption_key',
|
|
'credential_key',
|
|
'cookie',
|
|
'session',
|
|
'credential',
|
|
'credentials',
|
|
'ai_codex_credential',
|
|
'api_key_preview',
|
|
'api_key_hint',
|
|
'key_preview',
|
|
'key_hint',
|
|
'token_hint',
|
|
];
|
|
|
|
foreach ($data as $key => $value) {
|
|
$normalizedKey = preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', (string) $key);
|
|
$normalizedKey = trim((string) preg_replace('/[^a-z0-9]+/i', '_', strtolower($normalizedKey)), '_');
|
|
$secretSuffix = preg_match(
|
|
'/(?:^|_)(?:pass|password|passwd|password_hash|private_key|secret_key|app_secret|api_secret|access_secret|merchant_secret|signing_secret|signing_key|webhook_secret|client_secret|api_key|apikey|access_key|merchant_key|client_key|auth_key|api_token|apitoken|access_token|refresh_token|auth_token|bearer_token|session_token|csrf_token|relay_token|private_token|secret_token|token|token_hash|key_preview|key_hint|token_hint|authorization_header|credential_key|credential|credentials)$/',
|
|
$normalizedKey
|
|
) === 1;
|
|
if (in_array($normalizedKey, $secretKeys, true) || $secretSuffix) {
|
|
unset($data[$key]);
|
|
continue;
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
$data[$key] = self::sanitizeArray($value);
|
|
continue;
|
|
}
|
|
|
|
if (!is_scalar($value) && $value !== null) {
|
|
continue;
|
|
}
|
|
|
|
if (in_array($normalizedKey, ['email', 'user_email', 'invite_email'], true)) {
|
|
$data[$key] = self::email($value);
|
|
} elseif (in_array($normalizedKey, ['ip', 'user_ip', 'request_ip', 'report_ip', 'ip_address', 'reg_ip'], true)) {
|
|
$data[$key] = self::ip($value);
|
|
} elseif (in_array($normalizedKey, ['user_name', 'username', 'account', 'apple_id'], true)) {
|
|
$data[$key] = self::account($value);
|
|
} elseif (in_array($normalizedKey, [
|
|
'payment_account',
|
|
'zhifubao',
|
|
'alipay_account',
|
|
'wallet_address',
|
|
'receive_address',
|
|
'from_address',
|
|
'to_address',
|
|
], true)) {
|
|
$data[$key] = self::paymentAccount($value);
|
|
} elseif (in_array($normalizedKey, [
|
|
'server',
|
|
'node_ip',
|
|
'access_addr',
|
|
'access_host',
|
|
'base_url',
|
|
'url',
|
|
'uri',
|
|
'path',
|
|
'request_path',
|
|
'landing_path',
|
|
'query',
|
|
'request_url',
|
|
'request_domain',
|
|
'target_url',
|
|
'landing_url',
|
|
'referrer',
|
|
'referer',
|
|
'referrer_host',
|
|
'site_host',
|
|
'host',
|
|
'domain',
|
|
'endpoint',
|
|
], true)) {
|
|
$data[$key] = self::sensitiveText($value);
|
|
} elseif (in_array($normalizedKey, [
|
|
'message',
|
|
'last_check_message',
|
|
'error',
|
|
'error_message',
|
|
'last_error',
|
|
'details',
|
|
'reason',
|
|
'remark',
|
|
'description',
|
|
'content',
|
|
'user_agent',
|
|
'request_user_agent',
|
|
], true)) {
|
|
$data[$key] = self::sensitiveText($value);
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function maskList($value, $callback)
|
|
{
|
|
$value = (string) $value;
|
|
if (!self::shouldMask() || trim($value) === '') {
|
|
return $value;
|
|
}
|
|
|
|
return preg_replace_callback('/[^\s,;]+/', function ($matches) use ($callback) {
|
|
return call_user_func($callback, $matches[0]);
|
|
}, $value);
|
|
}
|
|
|
|
private static function length($value)
|
|
{
|
|
return function_exists('mb_strlen') ? mb_strlen($value, 'UTF-8') : strlen($value);
|
|
}
|
|
|
|
private static function substr($value, $start, $length = null)
|
|
{
|
|
if (function_exists('mb_substr')) {
|
|
return $length === null
|
|
? mb_substr($value, $start, null, 'UTF-8')
|
|
: mb_substr($value, $start, $length, 'UTF-8');
|
|
}
|
|
|
|
return $length === null ? substr($value, $start) : substr($value, $start, $length);
|
|
}
|
|
}
|