Fix #5 encryption — APP_KEY decode bug + move to AEAD - #307
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bugs Fixed
Two bugs were identified and fixed in
Encryption.php:1.
APP_KEYBase64 Decoding BugThe
APP_KEYvalue was being decoded directly using:base64_decode(getenv('APP_KEY'))However, Laravel-style keys are stored with a
base64:prefix, so the entire"base64:XXXX..."string was being passed tobase64_decode()without first removing the prefix.Because PHP's non-strict Base64 decoder silently ignores the
:, it decoded"base64" + the actual key, resulting in a corrupted 32-byte key that did not match the random bytes originally generated bykey:generate.The fix strips the
base64:prefix before decoding.This was also verified empirically: the corrected key derivation now matches:
base64_decode(substr($rawEnv, 7))and produces a different result from the previous buggy implementation.
2. Incorrect CLI/Test Key Selection
The same method contained a
"for UNIT Testing"branch based on:PHP_SAPI === 'cli' || defined('STDIN')These conditions are true for essentially every PHP CLI execution, not only PHPUnit runs.
As a result, normal console processes such as queue workers, scheduled commands, or
php poolinvocations could silently use a hardcoded testing key from the framework source code instead of the application's actualAPP_KEY.This completely disconnected encryption from the configured application key in CLI environments.
The logic has been updated to:
APP_KEYwhenever it is configured, including normal CLI commands.APP_KEYis genuinely empty.This preserves the existing behavior required by the package's own test suite, which does not load a
.env, while ensuring real CLI processes use the application's configured encryption key.