magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.47k stars 9.29k forks source link

Encryption key change doesn't re-encrypt any config values using the new key #35061

Open denis-zyk opened 2 years ago

denis-zyk commented 2 years ago

Preconditions (*)

  1. Magento Commerce 2.4.3-p1

Steps to reproduce (*)

  1. Follow instructions for changing crypt key, set Auto-generate Key to "Yes"

Expected result (*)

  1. All data is re-encrypted using new crypt key, as per official documentation (also note the confusion of "re-encrypted" vs. "re-encoded" in the official docs):

    Whenever the key is changed, all legacy data is re-encoded using the new key.

  2. New crypt key is stored in app/etc/env.php, while the previous crypt key is overwritten

Actual result (*)

  1. None of the encrypted config values (in core_config_data DB table) are re-encrypted using new crypt key during crypt key change
  2. New crypt key is appended to app/etc/env.php, meaning there is now a multiline string containing a previous crypt key, e.g.
'crypt' => [
    'key' => '391569b674c6f1e6fc9d545eaa1ef41e
bad91f79f8136c07098faae8c5102a76'
],

Where 391569b674c6f1e6fc9d545eaa1ef41e is the previous crypt key, and bad91f79f8136c07098faae8c5102a76 is a newly generated crypt key. All the following crypt key changes will append another new crypt key to the list preserving all previous crypt keys.

  1. Encrypted config values are re-encrypted only during manual value update (e.g. via the backend)

Doing such changes on a regular basis (again, as suggested by official documentation to be a good security practice, and I totally agree with that point), will actually keep "polluting" app/etc/env.php configuration file.

The encryption key should be changed on a regular basis to improve security [..]

Moreover, since the actual encrypted values are not re-encrypted using the new crypt key automatically, it imposes a security risk, and creates a general mess with different versions of crypt key being used all over the place.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 2 years ago

Hi @denis-zyk. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

denis-zyk commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @denis-zyk, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

denis-zyk commented 2 years ago

Could anyone here take a look, please?

denis-zyk commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @denis-zyk, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

m2-assistant[bot] commented 2 years ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 2 years ago

Verified the issue on Magento 2.4-devvelop branch and the issue is reproducible. On changing crypt key from Admin, new crypt key is getting added every time in app/etc/env.php file. None of the encrypted config values (in core_config_data DB table) are re-encrypted using new crypt key during crypt key change image

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-2466 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

engcom-November commented 2 years ago

Verified the issue again on Magento 2.4-develop branch as per developer comments. Please find the observations/findings below:

  1. . Auto-generate - New Encryption Key - Always getting appended in app/etc/env.php file: image

As per dev docs, The encryption key should be changed on a regular basis to improve security, as well as at any time the original key might be compromised. Whenever the key is changed, all legacy data is re-encoded using the new key. Needed clarification on expected behavior of appending crypt key in env.php file every time.

Note: On removing old crypt key values from env.php file - Magento instance is not working and setup:upgrade gives following error: image

  1. None of the values in "core_config_data" DB table are re-encrypted for PayPal Express checkout payment method. Old values are displayed in the table even after generating encryption key. - Issue

image

Note: There is no issue for UPS shipping method. - No issue "core_config_data" table is updated with new encrypted values for UPS shipping method after generating the encryption key. image

image

Skullsneeze commented 1 year ago

I've just created a PR #37705 which should resolve the issue. The underlying cause if the way the XML paths were retrieved, and how those were checked against the DB. More info on that is in the PR in case anyone is curious

chelevich commented 2 months ago

I tried to apply your changes over 2.4.2 and 'key' still did not get overwritten but added to the existing value with a line break @Skullsneeze

Skullsneeze commented 2 months ago

Hi @chelevich, this is actually expected behaviour. The PR I created fixes an issue where certain encrypted config values were not re-encrypted using the new key.

The underlying reason for this was that the config_path directory was ignored during this re-encryption process originally.

asorokinaamc commented 2 months ago

So, after hundred crypt key changes we will have env.php file with hundred lines with previous crypt keys? Is it expected behavior?

chelevich commented 2 months ago

So, after hundred crypt key changes we will have env.php file with hundred lines with previous crypt keys? Is it expected behavior?

Yes, seems so.

Any of the keys could be used to encrypt existing data and they have to keep all of them in env.php Key version is part of encrypted string @see \Magento\Framework\Encryption\Encryptor::decrypt

Core just cannot rebuild all encrypted strings across all custom modules.

Magento automatically re-encrypts encrypted values only in core_config_data and sales_order_payment.cc_number_enc column when you change the key in admin panel.

chelevich commented 2 months ago

This has become more important after the latest Security Bulletin which suggests to refresh Encryption Key

Skullsneeze commented 2 months ago

Just to further clear up any confusion about the keys in your env file. When decrypting Magento gets the existing keys in an array by taking the keys from env.php and splitting them on newlines.

Then they look at the first number in the encrypted string. This number corresponds to the number of the key in the array of encryption keys.

So your initial encrypted data will be prefixed with a 0 (first key in the array), and after re-encrypting your data should be prefixed with the a 1 (second/new key in the array).

This logic all works fine, except that when re-encrypting, config values which use a config_path in system.xml are not correctly updated when the provider config path does not match the path generated using the ids of the xml elements. This should be fixed with the PR

JMLucas96 commented 2 months ago

Community launched this module: https://github.com/genecommerce/module-encryption-key-manager To solve magento issues with that, I don't tested yet but it seems to work correctly ;)

nrdevau commented 1 month ago

Can anyone give us a dummy's guide to the gotchas of changing a production encryption key. The guide makes it seem so easy, I'm suspicious, after finding this thread.

If we follow the guide at https://experienceleague.adobe.com/en/docs/commerce-admin/systems/security/encryption-key:

  1. Turn on maintenance mode (downtime)
  2. Disable cron
  3. Change the key System > Other Settings > Manage Encryption Key.
  4. Flush the cache.
  5. Enable cron again
  6. Turn off maintenance mode (no more downtime)

Do we need to also do some config changes?

Rickertje commented 1 month ago

Why would Adobe tell us urgently to rotate the encryption key using admin after applying the CosmicSting isolated patch if this does not work properly? Or does it? For instance genecommerce states in its module description that the JWT factory continues to accept the old key when the new key is generated using admin. Also this very issue is still open. If a Magento installation still is a possible victim of CosmicSting even after applying the patch (with an compromised old key) and rotating keys is a drag than I would expect a lot of panic. At least I am in panic :) Can someone elaborate on this please?

Seppo-Konttinen-Solteq commented 1 month ago

@Rickertje check this page:

https://experienceleague.adobe.com/en/docs/commerce-knowledge-base/kb/troubleshooting/known-issues-patches-attached/security-update-available-for-adobe-commerce-apsb24-40-revised-to-include-isolated-patch-for-cve-2024-34102

There is a hotfix (https://experienceleague.adobe.com/docs/commerce-knowledge-base/assets/AC-12485_Hotfix_COMPOSER_patch.zip?lang=en) which should deal with the problem that JWT Factory will use the old key. That should be applied among with the latest security patch. If you have applied the isolated patch, that should already contain the hotfix.

What comes for rotating the key, as @Skullsneeze wrote, there seems to be bug that not all of core_config_data secrets are re-encrypted. There is fix in PR https://github.com/magento/magento2/pull/37705 which is still open probably because some of the tests was not passed.

In my understanding even with that PR fix, some 3rd party modules / custom tables that has secrets might not be re-encrypted. This module was already mentioned https://github.com/genecommerce/module-encryption-key-manager/#allows-you-to-keep-your-existing-media-cache-directories but you should also read this https://www.linkedin.com/pulse/magento-2-encryption-key-rotation-how-we-worked-around-core-deficiencies-inqzc/?published=t and check the stand-alone script for scanning fields that has encrypted data https://github.com/bemeir/magento2-rotate-encryption-keys

Still highlighting that both of those (module and stand-alone script) are provided as-is without any warranty. So you are using those at your own risk.

Rickertje commented 1 month ago

Thanks Seppo, that is a relief. I applied the isolated patch and I will now generate a new encryption key using admin. Also I will check for secrets that might not have been re-encrypted and re-encrypt them using the Bemeir module. Thanks for clarifying.

ecoprince commented 4 weeks ago

I face issue of blank value showing in admin configuration after upgrade Magento to 2.4.7 in local. I fixed it with by copying latest production encrypt key(env.php) in local which is rotated recently with last security patch.