Repository navigation
Fix: Encryption ignores APP_KEY from env.toml and silently uses a public test key - #364
Merged
Merged
Conversation
Member
Author
|
I'm fixing issues... |
Member
Author
|
@techmahedy now, check it. |
Member
|
@abdulmajidcse thanks, I will check it, it was a genuine security related issue. |
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.
Pull Request Checklist
Description
Encryption(behindCrypt,encrypt()anddecrypt()) read the key only throughgetenv('APP_KEY'), and fell back to a hardcodedTESTING_KEYwhen that returned nothing.Application::loadTomlEnvironment()writesenv.tomlvalues only to$_ENVand$_SERVERand never callsputenv(), sogetenv('APP_KEY')returnedfalsefor every app that sets its key inenv.toml.As a result, those apps encrypted everything with the
TESTING_KEYpublished in this repository, with no error or warning, in every environment including production. Anyone could:Crypt, such as encrypted model columns and 2FA secrets and recovery codes, from a database dump;decrypt()accepts, including remember-me cookie payloads and any app data passed throughencrypt(). Remember-me logins are still blocked by the hashed token check against the database.A missing or empty
APP_KEYwas handled the same silent way.The fix:
env()does ($_ENV, then$_SERVER, thengetenv), so a key fromenv.tomlis found;RuntimeExceptionwhenAPP_KEYis missing or empty, instead of falling back;TESTING_KEYfrom the class. The test suite now gets its key fromphpunit.xml.dist.Upgrade note
Apps that loaded
APP_KEYfromenv.tomlhave so far been encrypting with the public test key. After this change, data encrypted before the upgrade will not decrypt:There is deliberately no automatic fallback to the old key, because that would keep accepting forged ciphertexts. This should be called out in the CHANGELOG / upgrade notes.
Flaky legacy decrypt (second commit)
The first CI run failed on Windows in
test_decrypts_legacy_ciphertext_encrypted_with_pre_fix_buggy_key. The failure is not Windows-specific and was not caused by the change above.decryptLegacy()tried the correct key first and returned the first result that wasn'tfalse. AES-256-CBC has no MAC, so the wrong key still "succeeds" whenever its garbage output happens to end in valid padding. With a random IV this happened in about 0.33% of runs (66 / 20000), and it affects real legacy data as well as the test.decryptLegacy()now tries both keys and, when both decrypt, returns the result that is valid UTF-8, since wrong-key output is random bytes. A new test runs 3000 random IVs per key, so it reliably hits the ambiguous case. It fails on the old code and passes on the new.Checklist
tests/EncryptionTest.php: 5 new tests. The env.toml-loaded key, missing-key, empty-key and legacy wrong-key tests fail on the old code.)