diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d981c87 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ + +name: CI +on: [push, pull_request] + +permissions: + contents: read # to fetch code (actions/checkout) + +jobs: + tests: + name: Tests + timeout-minutes: 10 + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + - name: Composer Install + run: composer install --no-interaction --no-cache + - name: Make Tests Compatiable With PHPUnit 9+ + if: contains(fromJSON('["7.3", "7.4", "8.0", "8.1", "8.2", "8.3"]'), matrix.php-version) + run: php tests/make_compatible_with_phpunit9.php + - name: PHPUnit + run: vendor/bin/phpunit + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] \ No newline at end of file diff --git a/README.md b/README.md index 5289bda..a7d44af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # mcrypt_compat -[![Build Status](https://travis-ci.org/phpseclib/mcrypt_compat.svg?branch=master)](https://app.travis-ci.com/github/phpseclib/mcrypt_compat) +[![CI Status](https://github.com/phpseclib/mcrypt_compat/actions/workflows/ci.yml/badge.svg?branch=1.0&event=push "CI Status")](https://github.com/phpseclib/mcrypt_compat/actions/workflows/ci.yml?query=branch%3A1.0) PHP 5.x-8.x polyfill for mcrypt extension. diff --git a/lib/mcrypt.php b/lib/mcrypt.php index e58796a..d014343 100644 --- a/lib/mcrypt.php +++ b/lib/mcrypt.php @@ -1178,7 +1178,7 @@ function phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv = null) */ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null) { - return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ? + return !defined('PHPSECLIB_MCRYPT_TARGET_VERSION') || version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ? phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt') : phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'encrypt'); } @@ -1198,7 +1198,7 @@ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null) */ function phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null) { - return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ? + return !defined('PHPSECLIB_MCRYPT_TARGET_VERSION') || version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ? phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt') : phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'decrypt'); } diff --git a/tests/MCryptCompatTest.php b/tests/MCryptCompatTest.php index d2a2b3d..17014ab 100644 --- a/tests/MCryptCompatTest.php +++ b/tests/MCryptCompatTest.php @@ -825,6 +825,28 @@ public function testMcryptGenericWithTwoParamsPHPPost71() phpseclib_mcrypt_generic_init($td, 'xxx'); } + public function testOldMcryptNoIV() + { + $key = 'key'; + $data = 'data'; + $iv = null; + + $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'mcrypt_encrypt(): Attempt to use an empty IV, which is NOT recommended'); + + phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt'); + } + + public function testOldMcryptShortIV() + { + $key = 'key'; + $data = 'data'; + $iv = 'iv'; + + $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'mcrypt_encrypt(): The IV parameter must be as long as the blocksize'); + + phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt'); + } + public function providerForIVSizeChecks() { $tests = [ diff --git a/tests/make_compatible_with_phpunit9.php b/tests/make_compatible_with_phpunit9.php new file mode 100644 index 0000000..1d2fe26 --- /dev/null +++ b/tests/make_compatible_with_phpunit9.php @@ -0,0 +1,26 @@ + $files */ +$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__)); +foreach ($files as $file) { + if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) { + $fileContents = file_get_contents($file->getPathname()); + if ($fileContents === false) { + throw new \RuntimeException('file_get_contents() failed: ' . $file->getPathname()); + } + $patternToReplacementMap = array( + '~(n assertIsArray\([^,\)]*,)([^,\)]*\))~' => '$1 string $2: void', + '~(n assertIsArray\([^,\)]*\))~' => '$1: void', + '~(n assertIsString\([^\)]*\))~' => '$1: void', + '~(n assertStringContainsString\([^\)]*\))~' => '$1: void' + ); + $updatedFileContents = preg_replace( + array_keys($patternToReplacementMap), + array_values($patternToReplacementMap), + $fileContents + ); + if (file_put_contents($file->getPathname(), $updatedFileContents) === false) { + throw new \RuntimeException('file_put_contents() failed: ' . $file->getPathname()); + } + } +} \ No newline at end of file