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

[8.x] Fixes AsEncrypted traits not respecting nullable columns #39848

Merged
merged 1 commit into from Dec 1, 2021
Merged

[8.x] Fixes AsEncrypted traits not respecting nullable columns #39848

merged 1 commit into from Dec 1, 2021

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Nov 30, 2021

What?

When using AsEncryptedCollection or AsEncryptedArrayObject, nulling the property of the model results in an error because the cast object returned isn't aware of null values being passed to or received from the database.

use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
use Illuminate\Database\Eloquent\Model;

class Foo extends Model
{
    protected $casts = [
        'secret_array' => AsEncryptedArrayObject::class,
        'secret_collection' => AsEncryptedCollection::class,
    ]
}

Foo::make()->fill([
    'secret_array' => null,
    'secret_collection' => null,
])

// ErrorException : Undefined array key "secret_array"
// ErrorException : Undefined array key "secret_collection"

This fixes this behaviour by checking if the key in the attributes being passed to the cast object is not null. If it's not, proceeds to encrypt/decrypt the value, otherwise it keeps it as null.

Notes

There is an EXCESSIVE amount of redundant encryption being done when setting a value into a property. For example, saving the model calls the encrypted 4 times, when it only should be called once.

$foo = Foo::make();

$foo->secret_array = ['foo' => 'bar'];

$foo->save(); // <-- Here the value encryption is called 4 times.

You can check this behaviour on the tests themselves. The encryption must be set to be expected 12 times even if only it's saved twice in the database during a single test.

@taylorotwell taylorotwell merged commit aff3151 into laravel:8.x Dec 1, 2021
@DarkGhostHunter DarkGhostHunter deleted the feat/encrypted-object-null branch December 1, 2021 19:03
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

Successfully merging this pull request may close these issues.

None yet

2 participants