Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Prevent re-encryption of unchanged values. #13

Closed
wants to merge 8 commits into from

Conversation

MarkTierney1975
Copy link

This is my first pull request, not sure if I've done the right thing, apologies if I've messed up :)

In the app I'm building I noticed that all my encrypted attributes were being re-encrypted on save even when they hadn't changed. This pull request adds to the existing originalIsEquivalent from the \Illuminate\Database\Eloquent\Concerns\HasAttributes trait. Compares the decrypted values and adds them to the dirty array only if they are different.

Commit:
Added amended originalIsEquivalent function in HasEncryptedAttributes trait.
Added new testDontEncryptUnchangedAttributes test in DirtyTest.

… trait. Added new testDontEncryptUnchangedAttributes test in DirtyTest.
@austinheap austinheap added the bug label Feb 22, 2018
@austinheap
Copy link
Owner

Thanks for the pull request @MarkTierney1975! Which version of Laravel framework did you pull the \Illuminate\Database\Eloquent\Concerns\HasAttributes trait code from? Looks like it needs some minor tweaks and it'll be good to go.

@MarkTierney1975
Copy link
Author

Ah, it may have been 5.5.

@MarkTierney1975
Copy link
Author

I've used the new 6.6 function, there's only minor style differences from what I can see. I'm not sure how to fix the codeclimate and CI issues that are being highlighted, they seem to be with the original Laravel code style :)

@MarkTierney1975
Copy link
Author

Apologies for all the commits! The codeclimate issues are in the original Laravel function and I don't really want to mess with that. Anything else I need to do?

@austinheap
Copy link
Owner

Hey @MarkTierney1975 -- I haven't had time to reproduce the bug you're trying to fix here. Do you have a small snippet you can post? In theory the unit tests for getDirty should be catching an issue like this.

@MarkTierney1975
Copy link
Author

This was the test I added to tests/Traits/DirtyTest.php

public function testDontEncryptUnchangedAttributes()
    {
        $string = 'Hello world!';
        
        $model = DatabaseModel::create(['should_be_encrypted' => $string]);
        
        $this->assertTrue($model->exists);
        
        $model->should_be_encrypted = $string;
        
        $changedAttributes = $model->getDirty();
        
        $this->assertTrue(empty($changedAttributes));
    }

@austinheap austinheap closed this May 19, 2018
@austinheap austinheap added wontfix and removed bug labels May 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants