Skip to content

[4.x] Throw UnsupportedCipherException when a cipher is not supported - #38

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-unsupported-cipher-exception
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-unsupported-cipher-exception

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

CipherInterface and Crypt document UnsupportedCipherException on every method, but nothing ever throws it, and two isSupported() implementations are wrong:

  • defuse/php-encryption is only in require-dev/suggest, yet Cipher\Crypto is the default cipher of Crypt. Without the library, new Crypt() and every Crypto method fail with Error: Class "Defuse\Crypto\Key" not found, and Crypto::isSupported() itself fatals (it references Defuse\Crypto\RuntimeTests) instead of returning false.
  • Sodium::isSupported() returns true unconditionally, although ext-sodium is optional.

This PR:

  • Crypto::isSupported() returns false when defuse/php-encryption is not installed (before running the library's self-test).
  • Sodium::isSupported() returns function_exists('sodium_crypto_box'), which is also true when paragonie/sodium_compat provides the functions.
  • encrypt(), decrypt() and generateKey() of Crypto, Sodium and OpenSSL throw UnsupportedCipherException when static::isSupported() is false. This covers new Crypt() without arguments, which generates a Crypto key in the constructor.

Behaviour change: in an environment where defuse's runtime self-test fails, Crypto now throws UnsupportedCipherException up front instead of EncryptionException/DecryptionException/InvalidKeyException ("Cannot safely …"). All of them implement CryptExceptionInterface.

The alternative of moving defuse/php-encryption to require is not part of this PR; it would make the package depend on it even for users of the other ciphers. The removal of the unused ParagonIE\Sodium\Compat import is left to a separate PR.

Testing Instructions

New Tests/Cipher/UnsupportedCipherTest.php:

  • for each cipher, a subclass whose isSupported() returns false must throw UnsupportedCipherException from encrypt(), decrypt() and generateKey() (fails before: the methods ran or failed differently);
  • testCryptoWithoutDefuseLibrary runs Tests/Cipher/stubs/crypto-without-defuse.php in a separate PHP process that can only autoload the package's own classes, and expects Crypto::isSupported() === false and UnsupportedCipherException from the Crypto methods and new Crypt() (fails before with Error);
  • Sodium::isSupported() matches function_exists('sodium_crypto_box').
php vendor/bin/phpunit

Documentation Changes Required

docs/overview.md ("Handling failures"): UnsupportedCipherException is now thrown; a table explains when each cipher's isSupported() is true, replacing the note about Sodium::isSupported() always returning true.

🤖 Generated with Claude Code

UnsupportedCipherException was documented on every cipher method but never
thrown. Without defuse/php-encryption, which is only suggested, new Crypt()
and the Crypto cipher failed with "Class Defuse\Crypto\Key not found", and
Crypto::isSupported() itself fatalled instead of returning false.
Sodium::isSupported() returned true unconditionally.

Crypto::isSupported() now checks that the library is installed and
Sodium::isSupported() that the sodium functions exist. encrypt(), decrypt()
and generateKey() of all ciphers throw UnsupportedCipherException when the
cipher is not supported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant