-
Notifications
You must be signed in to change notification settings - Fork 554
Filter unsupported OpenSSL cipher methods on PHP 8.0-8.4 #4982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+112
−32
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use function array_filter; | ||
| use function array_map; | ||
| use function array_values; | ||
| use function function_exists; | ||
| use function in_array; | ||
| use function openssl_cipher_iv_length; | ||
| use function openssl_get_cipher_methods; | ||
| use function strtolower; | ||
|
|
||
| #[AutowiredService] | ||
| final class OpenSslCipherMethodsProvider | ||
| { | ||
|
|
||
| /** @var list<string>|null */ | ||
| private ?array $supportedCipherMethods = null; | ||
|
|
||
| /** | ||
| * Returns supported cipher methods in lowercase. | ||
| * | ||
| * Filters out methods that openssl_get_cipher_methods() reports | ||
| * but are not actually usable due to https://github.com/php/php-src/issues/19994 | ||
| * | ||
| * @return list<string> | ||
| */ | ||
| public function getSupportedCipherMethods(): array | ||
| { | ||
| if ($this->supportedCipherMethods !== null) { | ||
| return $this->supportedCipherMethods; | ||
| } | ||
|
|
||
| $methods = []; | ||
| if (function_exists('openssl_get_cipher_methods')) { | ||
| // openssl_get_cipher_methods() reports algorithms that are not actually | ||
| // supported on PHP 8.0-8.4 due to https://github.com/php/php-src/issues/19994 | ||
| // Filter by actually testing each algorithm with openssl_cipher_iv_length(). | ||
| $methods = array_values(array_filter( | ||
| openssl_get_cipher_methods(true), | ||
| static fn (string $algorithm): bool => @openssl_cipher_iv_length($algorithm) !== false, | ||
| )); | ||
| } | ||
|
|
||
| $this->supportedCipherMethods = array_map('strtolower', $methods); | ||
|
|
||
| return $this->supportedCipherMethods; | ||
| } | ||
|
|
||
| public function isSupportedCipherMethod(string $method): bool | ||
| { | ||
| return in_array(strtolower($method), $this->getSupportedCipherMethods(), true); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug13692; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function doFoo(): void | ||
| { | ||
| // aes-128-cbc-cts is reported by openssl_get_cipher_methods() on PHP 8.0-8.4 | ||
| // but is not actually supported by openssl_cipher_iv_length() due to a PHP bug | ||
| // https://github.com/php/php-src/issues/19994 | ||
| // On PHP 8.4 where PHPStan runs, this should be refined to false | ||
| // (not incorrectly refined to int) | ||
| assertType('false', openssl_cipher_iv_length('aes-128-cbc-cts')); | ||
|
|
||
| // These should still work correctly | ||
| assertType('int', openssl_cipher_iv_length('aes-128-cbc')); | ||
| assertType('false', openssl_cipher_iv_length('unknown')); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php // lint >= 8.5 | ||
|
|
||
| namespace Bug13692Php85; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function doFoo() | ||
| { | ||
| assertType('int', openssl_cipher_iv_length('aes-128-cbc-cts')); | ||
| assertType('int', openssl_cipher_iv_length('aes-128-cbc')); | ||
| assertType('false', openssl_cipher_iv_length('unknown')); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need separate assertions for this test for other php versions?