diff --git a/Tests/CryptTest.php b/Tests/CryptTest.php index bf1519df..19c12867 100644 --- a/Tests/CryptTest.php +++ b/Tests/CryptTest.php @@ -7,10 +7,13 @@ namespace Joomla\Crypt\Tests; +use Joomla\Crypt\Cipher\Crypto; use Joomla\Crypt\CipherInterface; use Joomla\Crypt\Crypt; use Joomla\Crypt\Key; +use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -144,4 +147,50 @@ public function testGenRandomBytes($length) \strlen(Crypt::genRandomBytes($length)) ); } + + /** + * Constructor arguments that rely on the deprecated defaults + * + * @return array + */ + public static function dataDeprecatedConstructorArguments(): array + { + return [ + 'no arguments' => [false, false], + 'no key' => [true, false], + 'no cipher' => [false, true], + ]; + } + + /** + * @testdox Omitting the cipher or the key triggers a deprecation + * + * @param boolean $withCipher Whether to pass a cipher + * @param boolean $withKey Whether to pass a key + */ + #[AllowMockObjectsWithoutExpectations] + #[DataProvider('dataDeprecatedConstructorArguments')] + public function testConstructorWithoutCipherOrKeyTriggersDeprecation(bool $withCipher, bool $withKey) + { + $cipher = new Crypto(); + + $this->expectUserDeprecationMessage( + 'Since joomla/crypt __DEPLOY_VERSION__: Calling Joomla\Crypt\Crypt::__construct() without a cipher and a key is deprecated.' + . ' Both arguments will be required in 5.0.' + ); + + new Crypt($withCipher ? $cipher : null, $withKey ? $cipher->generateKey() : null); + } + + /** + * @testdox The deprecated defaults still use the Crypto cipher with a generated key + */ + #[AllowMockObjectsWithoutExpectations] + #[IgnoreDeprecations] + public function testConstructorDefaultsStillWork() + { + $crypt = new Crypt(); + + $this->assertSame('data', $crypt->decrypt($crypt->encrypt('data'))); + } } diff --git a/composer.json b/composer.json index 2ce32ab3..31a2dcbc 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "homepage": "https://github.com/joomla-framework/crypt", "license": "GPL-2.0-or-later", "require": { - "php": "^8.3.0" + "php": "^8.3.0", + "symfony/deprecation-contracts": "^2|^3" }, "require-dev": { "defuse/php-encryption": "^2.0", diff --git a/docs/overview.md b/docs/overview.md index 7a7f3ce5..4cb04ce1 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -45,6 +45,11 @@ $ciphertext = $crypt->encrypt('some secret value'); $plaintext = $crypt->decrypt($ciphertext); ``` +Always pass both the cipher and the key to `Crypt`. Omitting either is deprecated since 4.1 and +triggers an `E_USER_DEPRECATED` notice; both will be required in 5.0. Without a cipher, `Crypt` +falls back to `Crypto`; without a key, it generates a new one that is never stored, so data +encrypted with it cannot be decrypted by any other instance. + To use a stored key later, rebuild the `Key` object with the same type: ```php diff --git a/src/Crypt.php b/src/Crypt.php index 0ffcb134..da937195 100644 --- a/src/Crypt.php +++ b/src/Crypt.php @@ -43,13 +43,22 @@ class Crypt * Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the * secret word from the configuration object is used. * - * @param ?CipherInterface $cipher The encryption cipher object. - * @param ?Key $key The encryption key[/pair)]. + * @param ?CipherInterface $cipher The encryption cipher object. Omitting it is deprecated. + * @param ?Key $key The encryption key[/pair)]. Omitting it is deprecated. * * @since 1.0 */ public function __construct(?CipherInterface $cipher = null, ?Key $key = null) { + if ($cipher === null || $key === null) { + trigger_deprecation( + 'joomla/crypt', + '__DEPLOY_VERSION__', + 'Calling %s() without a cipher and a key is deprecated. Both arguments will be required in 5.0.', + __METHOD__ + ); + } + // Set the encryption cipher. $this->cipher = $cipher ?: new Crypto();