Skip to content

Removing mcrypt compat and replacing it with OpenSSL lib #40053

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

Open
wants to merge 6 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

jakwinkler
Copy link

Description (*)

Replacing mcrypt with OpenSSL lib to use up to date libraries.

Fixed Issues (if relevant)

  1. Fixes phpseclib Mcrypt performance slow #30556

Manual testing scenarios (*)

  1. ...
  2. ...

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Jakub Winkler added 3 commits July 8, 2025 21:29
This commit replaces the deprecated mcrypt extension usage with OpenSSL for encryption and decryption operations within the Magento framework. A new OpenSsl adapter has been introduced, and the Encryptor class has been updated to utilize it. Composer dependencies have been updated to reflect these changes.
Copy link

m2-assistant bot commented Jul 8, 2025

Hi @jakwinkler. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@hostep
Copy link
Contributor

hostep commented Jul 8, 2025

I always wondered about mcrypt in Magento, because it's considered deprecated since many many years.

Is it actually still being used in Magento, I mean for real in practice, not in code? If yes, where or in which circumstances?
If no, we should just remove all code that references it and don't replace it in my opinion.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the deprecated mcrypt-based encryption with an OpenSSL-backed implementation and removes the mcrypt compatibility library.

  • Swaps out the Mcrypt adapter for a new OpenSsl adapter in the core Encryptor
  • Adds explicit cipher-mode constants, updates method signatures with return types, and refines type hints
  • Removes the phpseclib/mcrypt_compat dependency from composer

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
lib/internal/Magento/Framework/Encryption/Encryptor.php Replaced Mcrypt usage with OpenSsl, added MODE constants, and tightened type hints/return types
lib/internal/Magento/Framework/Encryption/Adapter/OpenSsl.php Introduced new OpenSsl adapter implementing EncryptionAdapterInterface
composer.json Removed the phpseclib/mcrypt_compat package from dependencies
Comments suppressed due to low confidence (2)

lib/internal/Magento/Framework/Encryption/Adapter/OpenSsl.php:100

  • The decrypt() method can throw RuntimeException on failure but lacks a @throws annotation in the docblock; please document thrown exceptions for clarity.
    public function decrypt(string $data): string

lib/internal/Magento/Framework/Encryption/Adapter/OpenSsl.php:16

  • This new OpenSsl adapter is not covered by unit tests; consider adding tests to validate encrypt/decrypt flows for different ciphers and modes.
class OpenSsl implements EncryptionAdapterInterface


/**
* Array of encryption keys
*
* @var string[]
*/
protected $keys = [];
protected array|false $keys = [];
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property $keys is declared as array|false but initialized to an empty array; consider refining the type to string[] or just array to reflect actual usage and improve clarity.

Suggested change
protected array|false $keys = [];
protected string[] $keys = [];

Copilot uses AI. Check for mistakes.


/**
* @var KeyValidator
*/
private $keyValidator;
private mixed $keyValidator;
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing $keyValidator as mixed reduces type safety; consider using the explicit KeyValidator type or an interface to maintain strong typing.

Suggested change
private mixed $keyValidator;
private KeyValidator $keyValidator;

Copilot uses AI. Check for mistakes.

// For CBC mode, if IV is not provided for encryption, generate one.
// For decryption, the IV is expected to be part of the data or provided.
if ($this->mode === Encryptor::MODE_CBC && $this->initVector === null) {
$this->initVector = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipherMethod));
Copy link
Preview

Copilot AI Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openssl_random_pseudo_bytes can return false on failure; handle the false case to avoid unexpected null or boolean values in IV generation.

Suggested change
$this->initVector = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipherMethod));
$ivLength = openssl_cipher_iv_length($this->cipherMethod);
$generatedIv = openssl_random_pseudo_bytes($ivLength);
if ($generatedIv === false) {
throw new \RuntimeException('Failed to generate initialization vector using OpenSSL.');
}
$this->initVector = $generatedIv;

Copilot uses AI. Check for mistakes.

@ihor-sviziev
Copy link
Contributor

@jakwinkler, please review the comments from Copilot and let us know if you would like anything to be fixed based on them. If no, I'll review the pull request

@ihor-sviziev
Copy link
Contributor

@magento run all tests

@engcom-Charlie engcom-Charlie added the Project: Community Picked PRs upvoted by the community label Jul 14, 2025
@engcom-Charlie engcom-Charlie moved this to Pending Review in Community Dashboard Jul 14, 2025
@engcom-Bravo engcom-Bravo added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Jul 14, 2025
@engcom-Charlie engcom-Charlie added feature request Priority: P2 A defect with this priority could have functionality issues which are not to expectations. labels Jul 15, 2025
@github-project-automation github-project-automation bot moved this to Pending Review in Pull Requests Dashboard Jul 15, 2025
@engcom-Charlie engcom-Charlie moved this to Pending Review in Community Dashboard Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review Progress: review Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Pending Review
Development

Successfully merging this pull request may close these issues.

phpseclib Mcrypt performance slow
5 participants