diff --git a/Tests/KeyTest.php b/Tests/KeyTest.php index e20de7c9..ce31cae0 100644 --- a/Tests/KeyTest.php +++ b/Tests/KeyTest.php @@ -89,4 +89,34 @@ public function testGetKeyType() { $this->assertSame($this->type, $this->object->getType()); } + + /** + * @testdox Debugging output does not contain the key material + */ + public function testDebugOutputIsRedacted() + { + $key = new Key('test', 'secret-private-material', 'secret-public-material'); + + ob_start(); + var_dump($key); + $dump = ob_get_clean(); + + $printed = print_r($key, true); + + foreach ([$dump, $printed] as $output) { + $this->assertStringContainsString('test', $output); + $this->assertStringNotContainsString('secret-', $output); + } + } + + /** + * @testdox The private key is not exposed in stack traces + */ + public function testPrivateKeyIsSensitiveParameter() + { + $parameter = (new \ReflectionMethod(Key::class, '__construct'))->getParameters()[1]; + + $this->assertSame('private', $parameter->getName()); + $this->assertCount(1, $parameter->getAttributes(\SensitiveParameter::class)); + } } diff --git a/Tests/SensitiveParameterTest.php b/Tests/SensitiveParameterTest.php new file mode 100644 index 00000000..e62fef9f --- /dev/null +++ b/Tests/SensitiveParameterTest.php @@ -0,0 +1,62 @@ + [Crypto::class, 'encrypt', 'key'], + 'Crypto::decrypt()' => [Crypto::class, 'decrypt', 'key'], + 'OpenSSL::encrypt()' => [OpenSSL::class, 'encrypt', 'key'], + 'OpenSSL::decrypt()' => [OpenSSL::class, 'decrypt', 'key'], + 'OpenSSL::generateKey()' => [OpenSSL::class, 'generateKey', 'options'], + 'Sodium::encrypt()' => [Sodium::class, 'encrypt', 'key'], + 'Sodium::decrypt()' => [Sodium::class, 'decrypt', 'key'], + 'Crypt::__construct()' => [Crypt::class, '__construct', 'key'], + 'Crypt::setKey()' => [Crypt::class, 'setKey', 'key'], + ]; + } + + /** + * @testdox Key material is marked as sensitive so it is hidden in stack traces + * + * @param string $class The class name + * @param string $method The method name + * @param string $parameter The parameter name + */ + #[DataProvider('dataSensitiveParameters')] + public function testParameterIsSensitive(string $class, string $method, string $parameter) + { + foreach ((new \ReflectionMethod($class, $method))->getParameters() as $reflection) { + if ($reflection->getName() === $parameter) { + $this->assertCount(1, $reflection->getAttributes(\SensitiveParameter::class)); + + return; + } + } + + $this->fail("$class::$method() has no parameter \$$parameter"); + } +} diff --git a/docs/overview.md b/docs/overview.md index 7a7f3ce5..bf267e51 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -164,6 +164,18 @@ $b = $cipher->generateKey(['passphrase' => '/var/www/keys/tenant-b.dat']); If you must keep this cipher, pass the secret itself rather than a path, make sure it is at least as long as the method's key size, and generate it with `random_bytes()`. +## Keeping key material out of logs + +`Key` hides its private and public parts from `var_dump()`, `print_r()` and other debugging output +(`__debugInfo()` shows only the type). The parameters that carry key material — the `Key` +constructor's `$private`, the `$key` of `Crypt::__construct()`, `Crypt::setKey()` and the ciphers' +`encrypt()`/`decrypt()`, and the `$options` of `OpenSSL::generateKey()` — are marked +`#[\SensitiveParameter]`, so PHP replaces them in stack traces. + +A `Key` can still be serialized, and the result contains the private key in clear text. Do not put +`Key` objects into sessions, caches or anything else that is serialized; store the private key in +your secret storage and rebuild the `Key` from it. + ## Not part of this package * No password hashing — that lives in `joomla/authentication` (`Password\BCryptHandler` and the diff --git a/src/Cipher/Crypto.php b/src/Cipher/Crypto.php index a1c19369..b58fba35 100644 --- a/src/Cipher/Crypto.php +++ b/src/Cipher/Crypto.php @@ -40,7 +40,7 @@ class Crypto implements CipherInterface * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'crypto') { @@ -69,7 +69,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'crypto') { diff --git a/src/Cipher/OpenSSL.php b/src/Cipher/OpenSSL.php index 5aa4f7a0..c85d940e 100644 --- a/src/Cipher/OpenSSL.php +++ b/src/Cipher/OpenSSL.php @@ -65,7 +65,7 @@ public function __construct(string $iv, string $method) * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'openssl') { @@ -93,7 +93,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'openssl') { @@ -119,7 +119,7 @@ public function encrypt($data, Key $key) * @since 2.0.0 * @throws InvalidKeyException if the key cannot be generated */ - public function generateKey(array $options = []) + public function generateKey(#[\SensitiveParameter] array $options = []) { $passphrase = $options['passphrase'] ?? false; diff --git a/src/Cipher/Sodium.php b/src/Cipher/Sodium.php index bae797f0..e55b38bf 100644 --- a/src/Cipher/Sodium.php +++ b/src/Cipher/Sodium.php @@ -45,7 +45,7 @@ class Sodium implements CipherInterface * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'sodium') { @@ -85,7 +85,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt($data, #[\SensitiveParameter] Key $key) { // Validate key. if ($key->getType() !== 'sodium') { diff --git a/src/Crypt.php b/src/Crypt.php index 0ffcb134..262a1556 100644 --- a/src/Crypt.php +++ b/src/Crypt.php @@ -48,7 +48,7 @@ class Crypt * * @since 1.0 */ - public function __construct(?CipherInterface $cipher = null, ?Key $key = null) + public function __construct(?CipherInterface $cipher = null, #[\SensitiveParameter] ?Key $key = null) { // Set the encryption cipher. $this->cipher = $cipher ?: new Crypto(); @@ -116,7 +116,7 @@ public function generateKey(array $options = []) * * @since 1.0 */ - public function setKey(Key $key) + public function setKey(#[\SensitiveParameter] Key $key) { $this->key = $key; diff --git a/src/Key.php b/src/Key.php index a013910b..09ea8700 100644 --- a/src/Key.php +++ b/src/Key.php @@ -49,7 +49,7 @@ class Key * * @since 1.0 */ - public function __construct(string $type, string $private, string $public) + public function __construct(string $type, #[\SensitiveParameter] string $private, string $public) { // Set the key type. $this->type = $type; @@ -59,6 +59,25 @@ public function __construct(string $type, string $private, string $public) $this->public = $public; } + /** + * Hide the key material from var_dump(), print_r() and similar debugging output. + * + * The public part is hidden as well: for a symmetric cipher it may be secret, as it is for keys generated by + * Cipher\Crypto before 4.1. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function __debugInfo(): array + { + return [ + 'type' => $this->type, + 'private' => '*** redacted ***', + 'public' => '*** redacted ***', + ]; + } + /** * Retrieve the private key *