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

fix: Don't reencrypt vault with no private values using Model.build #9

Merged
merged 6 commits into from
Jul 17, 2024

Conversation

Geit
Copy link

@Geit Geit commented Jul 14, 2024

We sometimes serialize a Sequelize model, and then rehydrate it using Model.build at a later time. This object is in the form:

{
// ... Some attributes ...
SubObject: [
	{
		// ... Some attributes ...
		encrypted: {}
	},
	// And many many more...
]
}

Where the encrypted field is a sequelize-encrypted vault.

As part of Model.build, Sequelize calls all custom field setters. This results in Vault.set() being called with {}, at which point it is re-encrypted in case the user ends up calling .save(). However, given the default value for a Vault call is {} anyway, this operation is pointless.

With many sub-objects, this behaviour becomes pathological and causes serious performance issues:
image

This PR simply skips the re-encryption operation if Vault.set() is called with an empty object (and there's no existing data to override).

Note, that this PR only helps performance in cases where the vault is empty - in cases where it is not, the existing behaviour is maintained. Ideally, (re)encryption would be deferred to the point that the user actually calls .save() on a field.

With the same data, after this PR a performance trace is much saner (44ms -> 17ms on 1500 subobjects):
image

@Geit Geit requested a review from a team as a code owner July 14, 2024 18:59
@Geit Geit merged commit 77ce886 into main Jul 17, 2024
1 check passed
@Geit Geit deleted the fix/model-build-perf branch July 17, 2024 09:53
@snyksec
Copy link

snyksec commented Jul 17, 2024

🎉 This PR is included in version 0.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

5 participants