From a52fec9e8aa48e47cd3b10dffc1a4aa48c999b35 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 12 Nov 2026 23:12:06 +0100 Subject: [PATCH] Wrap defuse exceptions for malformed keys in the Crypto cipher Only WrongKeyOrModifiedCiphertextException and EnvironmentIsBrokenException were caught. A malformed key raised Defuse's BadFormatException, which does not implement CryptExceptionInterface, and whose stack trace contains the key string. Throw InvalidKeyException instead, without chaining the defuse exception, and wrap any other defuse exception as well. Co-Authored-By: Claude Opus 5.5 --- Tests/Cipher/CryptoTest.php | 25 +++++++++++++++++++++++++ docs/overview.md | 2 +- src/Cipher/Crypto.php | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Tests/Cipher/CryptoTest.php b/Tests/Cipher/CryptoTest.php index 6499e78d..57469cd9 100644 --- a/Tests/Cipher/CryptoTest.php +++ b/Tests/Cipher/CryptoTest.php @@ -9,8 +9,10 @@ use Defuse\Crypto\Key as DefuseKey; use Joomla\Crypt\Cipher\Crypto as CryptoCipher; +use Joomla\Crypt\Exception\InvalidKeyException; use Joomla\Crypt\Key; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Symfony\Polyfill\Util\Binary; @@ -76,6 +78,29 @@ public function testDataEncryptionAndDecryption($data) $this->assertSame($data, $decrypted); } + /** + * @testdox A malformed key is rejected with InvalidKeyException without exposing the key + * + * @param string $method The cipher method to call + */ + #[TestWith(['encrypt'])] + #[TestWith(['decrypt'])] + public function testMalformedKeyIsRejectedWithoutExposingIt(string $method) + { + $exceptionIgnoreArgs = ini_set('zend.exception_ignore_args', '0'); + + try { + (new CryptoCipher())->$method('data', new Key('crypto', 'not-a-key-SECRET-MATERIAL', '')); + + $this->fail('Expected an InvalidKeyException'); + } catch (InvalidKeyException $e) { + $this->assertNull($e->getPrevious()); + $this->assertStringNotContainsString('SECRET-MATERIAL', (string) $e); + } finally { + ini_set('zend.exception_ignore_args', $exceptionIgnoreArgs); + } + } + /** * @testdox Validates keys are correctly generated * diff --git a/docs/overview.md b/docs/overview.md index 7a7f3ce5..a314ac91 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -79,7 +79,7 @@ try { |---|---| | `EncryptionException` | Encryption failed | | `DecryptionException` | Decryption failed or the ciphertext was modified | -| `InvalidKeyException` | A key could not be generated | +| `InvalidKeyException` | A key could not be generated, or (`Crypto`) the key is malformed | | `InvalidKeyTypeException` | The key type does not match the cipher | | `UnsupportedCipherException` | Declared for unsupported environments, but never actually thrown | diff --git a/src/Cipher/Crypto.php b/src/Cipher/Crypto.php index a1c19369..00fb6403 100644 --- a/src/Cipher/Crypto.php +++ b/src/Cipher/Crypto.php @@ -10,6 +10,8 @@ namespace Joomla\Crypt\Cipher; use Defuse\Crypto\Crypto as DefuseCrypto; +use Defuse\Crypto\Exception\BadFormatException; +use Defuse\Crypto\Exception\CryptoException; use Defuse\Crypto\Exception\EnvironmentIsBrokenException; use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; use Defuse\Crypto\Key as DefuseKey; @@ -38,6 +40,7 @@ class Crypto implements CipherInterface * * @since 2.0.0 * @throws DecryptionException if the data cannot be decrypted + * @throws InvalidKeyException if the key is malformed * @throws InvalidKeyTypeException if the key is not valid for the cipher */ public function decrypt($data, Key $key) @@ -54,6 +57,11 @@ public function decrypt($data, Key $key) throw new DecryptionException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex); } catch (EnvironmentIsBrokenException $ex) { throw new DecryptionException('Cannot safely perform decryption', $ex->getCode(), $ex); + } catch (BadFormatException $ex) { + // The previous exception is not chained, its stack trace contains the key + throw new InvalidKeyException('The key is not a valid crypto key.', $ex->getCode()); + } catch (CryptoException $ex) { + throw new DecryptionException('Failed to decrypt data', $ex->getCode()); } } @@ -67,6 +75,7 @@ public function decrypt($data, Key $key) * * @since 2.0.0 * @throws EncryptionException if the data cannot be encrypted + * @throws InvalidKeyException if the key is malformed * @throws InvalidKeyTypeException if the key is not valid for the cipher */ public function encrypt($data, Key $key) @@ -81,6 +90,11 @@ public function encrypt($data, Key $key) return DefuseCrypto::encrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate())); } catch (EnvironmentIsBrokenException $ex) { throw new EncryptionException('Cannot safely perform encryption', $ex->getCode(), $ex); + } catch (BadFormatException $ex) { + // The previous exception is not chained, its stack trace contains the key + throw new InvalidKeyException('The key is not a valid crypto key.', $ex->getCode()); + } catch (CryptoException $ex) { + throw new EncryptionException('Unable to encrypt data', $ex->getCode()); } }