fuco-php / masked
掩盖敏感数据:用红字值替换黑名单中的元素
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^8.5
README
Fuko\Masked 是一个小型的 PHP 库,用于掩盖敏感数据:它将黑名单中的元素替换为它们的红字值。
它旨在非常易于使用。如果你有尝试对日志数据进行清理或清理公开可访问信息的经验,你就知道在数据转储的各个位置弹出密码或安全令牌是多么令人烦恼。 Fuko\Masked 就是为了解决这一问题而设计的。
基本用法
为了使用它,你只需要将你的敏感数据(密码、令牌、凭证)输入到 Fuko\Masked\Protect
use Fuko\Masked\Protect; Protect::hideValue($secret_key); // hide the value inside the $secret_key var Protect::hideInput('password', INPUT_POST); // hide the value of $_POST['password'] $redacted = Protect::protect($_POST);
...就是这样。黑名单值和输入将被掩盖。上述代码的输出将是
// consider these values for the vars used // $secret_key = '12345678'; // $_POST = array('username' => 'Bob', 'password' => 'WaldoPepper!', 'messages' => 'The secret key is 12345678'); $redacted = Protect::protect($_POST); print_r($redacted); /* ... and the output is Array ( [username] => Bob [password] => ████████████ [messages] => The secret key is ████████ ) */
它是如何工作的?
Fuko\Masked 做了两件事
- 首先,有一个用于掩盖敏感数据的
\Fuko\Masked\Redact
类 - 其次,
\Fuko\Masked\Protect
类用于收集你的敏感数据,并对其进行红字
通过以上操作,你将获得所有敏感详细信息都被黑名单掩盖的内容。你不需要在所有创建的转储中寻找密码或凭证,只需将它们注册到 \Fuko\Masked\Protect
,该类就会在找到它们的地方掩盖它们:字符串、数组、大文本转储。就这么简单。理念不是要有一个笨拙且过度装饰的库,而是一个工作得很好的简单工具。
示例
以下是几个基本的 Fuko\Masked 用例示例。
隐藏值
你知道你的密码和凭证在哪里,你想要在任何你创建的转储中黑名单它们。下面是如何做到这一点的
use \Fuko\Masked\Protect; // consider these values inside $config // $config = array( // 'project_title' => 'My New Project!', // 'mysql_username' => 'me', // 'mysql_password' => 'Mlyk!', // 'mysql_database' => 'project', // 'root' => '/var/www/niakade/na/majnata/si', // 'i.am.stupid' => 'Mlyk! e egati parolata za moya project', // ); Protect::hideValue($config['mysql_username']); Protect::hideValue($config['mysql_password']); Protect::hideValue($config['mysql_database']); print_r(Protect::protect($config)); /* ... and the output is Array ( [project_title] => My New Project! [mysql_username] => ██ [mysql_password] => █████ [mysql_database] => ███████ [root] => /var/www/niakade/na/majnata/si [i.am.stupid] => █████ e egati parolata za moya ███████ ) */
敏感详细信息可能位于数组内的嵌套数组中,它们仍然会被掩盖
use \Fuko\Masked\Protect; Protect::hideValue($password); $a = ['b' => ['c' => ['d' => $password]]]; print_r(Protect::protect($a)); /* ... and the output is Array ( [b] => Array ( [c] => Array ( [d] => ██████ ) ) ) */
隐藏输入
在某些情况下,你知道用户提交的数据或其他超级全局输入可能包含敏感数据。在这些情况下,你不需要隐藏实际值,而是可以替换输入数组。在这个例子中,我们将掩盖 "password" POST 值
use \Fuko\Masked\Protect; Protect::hideInput('password', INPUT_POST); // later you need to do a dump of $_POST and ... $_POST_redacted = Protect::protect($_POST); /* ... and the output is Array ( [email] => Bob@sundance.kid [password] => ███████ ) */
与对象一起工作
\Fuko\Masked\Protect::protect()
只适用于字符串和数组。如果你需要在对象转储中掩盖敏感数据,你首先创建转储,然后将其输入到 Protect::protect()
,如下所示
use \Fuko\Masked\Protect; Protect::hideValue($password); $a = new stdClass; $a->b = new stdClass; $a->b->c = new stdClass; $a->password = $a->b->secret = $a->b->c->d = $password; echo Protect::protect(print_r($a, true)); /* ... and the output is stdClass Object ( [b] => stdClass Object ( [c] => stdClass Object ( [d] => ██████████████████ ) [secret] => ██████████████████ ) [password] => ██████████████████ ) */
对于实现了 ::__toString()
方法的类,对象将转换为该方法的字符串。这里有一个异常的例子,异常类就有这个方法
use \Fuko\Masked\Protect; Protect::hideValue($password); $e = new \Exception('Look, look, his password is ' . $password); echo Protect::protect($e); /* ... and the output is Exception: Look, look, his password is ████████ in /tmp/egati-probata.php:123 Stack trace: #0 {main} */
不同的掩盖
你可以在项目中使用 \Fuko\Masked\Redact
作为掩盖数据的库。默认情况下,该类使用 \Fuko\Masked\Redact::disguise()
方法进行掩盖,默认设置是掩盖一切,使用 █
作为掩盖符号。以下是你可以更改其行为的示例
use \Fuko\Masked\Redact; /* leave 4 chars unmasked at the end, and use '*' as masking symbol */ Redact::setRedactCallback( [Redact::class, 'disguise'], [4, '*']); echo Redact::redact('1234567890'); // Output is '******7890' /* leave 4 chars unmasked at the beginning, and use '🤐' as masking symbol */ Redact::setRedactCallback( [Redact::class, 'disguise'], [-4, '🤐']); echo Redact::redact('1234567890'); // Output is '1234🤐🤐🤐🤐🤐🤐'
你可以为 \Fuko\Masked\Redact
类设置自己的掩盖回调
use \Fuko\Masked\Redact; Redact::setRedactCallback( function($var) { return '💩'; } ); echo Redact::redact('1234567890'); // Output is '💩'