Skip to content
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

Possible bug with new StringProvider using base64-encoded key #104

Open
E-Mans-Application opened this issue Aug 2, 2024 · 1 comment
Open

Comments

@E-Mans-Application
Copy link

Hi,

I've just experienced a strange behaviour with StringProvider (I'm not sure if it's a bug or not).
I have generated key kME8GX8+2nhtfVXyLZkPeHLRfkUO99nbJpjAnbC9bZA= using base64_encode(random_bytes(256 / 8)). I'm trying to use the key to create a StringProvider, but the base64-encoded form is not accepted.

Code:

$provider = new StringProvider("kME8GX8+2nhtfVXyLZkPeHLRfkUO99nbJpjAnbC9bZA=");

This code throws:
RangeException: Base64::decode() only expects characters in the correct base64 alphabet in /vendor/paragonie/constant_time_encoding/src/Base64.php:228
Stack trace:
#0 /vendor/paragonie/ciphersweet/src/KeyProvider/StringProvider.php(41): ParagonIE\ConstantTime\Base64::decode('kME8GX8+2nhtfVX...')
#1 /www/xxx.php(xxx): ParagonIE\CipherSweet\KeyProvider\StringProvider->__construct('kME8GX8+2nhtfVX...')

Using new StringProvider(base64_decode("kME8GX8+2nhtfVXyLZkPeHLRfkUO99nbJpjAnbC9bZA=")) instead works fine.

@paragonie-security
Copy link
Contributor

Change your code in this way:

+ use ParagonIE\ConstantTime\Base64;

// ...
- $provider = new StringProvider("kME8GX8+2nhtfVXyLZkPeHLRfkUO99nbJpjAnbC9bZA=");
+ $provider = new StringProvider(Base64::decode("kME8GX8+2nhtfVXyLZkPeHLRfkUO99nbJpjAnbC9bZA="));

What's happening here is, if your input string is 44 characters, it assumes URL-encoded base64, not the other base64.

PHP's base64_decode() is not constant-time. See https://github.com/paragonie/constant_time_encoding for more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants