magento2: Password hash should not rely on system dependant constant

Preconditions (*)

  1. Magento 2.3.3
  2. PHP 7.2.22

Steps to reproduce (*)

  1. Fresh Magento install
  2. Create some admin and customer accounts
  3. Make some system or hardware upgrade on server compiling and running PHP which change Sodium predefined constants https://www.php.net/manual/en/sodium.constants.php
  4. Try to login with any account

Expected result (*)

  1. Any admin or customer could login with their current password

Actual result (*)

  1. Admin and customer cannot login with their current password and need to use forgotten password function to get a new one

Temporary workaround

Get constant value from the old server

$ php -r 'echo SODIUM_CRYPTO_SIGN_SEEDBYTES."\n";'
32
$ php -r 'echo SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE."\n";'
4
$ php -r 'echo SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE."\n";'
33554432

Check if they differ on the new server

$ php -r 'echo SODIUM_CRYPTO_SIGN_SEEDBYTES."\n";'
32
$ php -r 'echo SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE."\n";'
2
$ php -r 'echo SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE."\n";'
67108864

On the new server, replace constant by the value from the old server into the sodium_crypto_pwhash function

https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/Encryption/Encryptor.php#L585

User could login again without need to reset their password.


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

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

This issue has been fixed in 2.4.2, 2.4.1-p1, 2.3.7, 2.3.6-p1 and as part of internal tickets MC-36789, MC-36886, MC-36790, and MC-37973 respectively.

Internal issue MC-40854 created for investigation

This seems confirmed by libsodium author and library example, constant values can change and did change, you must save the function parameters in order to recompute the same value later.

https://github.com/jedisct1/libsodium-php/issues/165#issuecomment-366599786 https://github.com/jedisct1/libsodium-php#encrypt-a-file-using-a-key-derived-from-a-password