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

Add support for obfuscated id or alternate entity id #3261

Merged
merged 4 commits into from
Aug 11, 2024

Conversation

justin-tay
Copy link
Collaborator

Resolves #522

Description

This adds support for 2 different ways of not exposing the primary key of the entity if the primary key undesirably leaks information.

  • Allows registering a IdObfuscator that can obfuscate the id. Typically an encryption algorithm is used.
  • Allows designating another field or property as the @EntityId that will be used to look up the record instead of the @Id.

Two alternate means are provided to let users choose which best suits their needs.

The IdObfuscator means that no additional column is required but some computation is required to always required to convert the ids. These ids will not be visible on the database level. Also sorting will typically take place on the underlying primary key and not the obfuscated value. If encryption is used for the obfuscation the user will need to safeguard those keys appropriately, also any changes to those keys or algorithm may mean that the ids will also correspondingly change.

Using the @EntityId allows the entity to have a totally separate column to identify the record that is not linked to the primary key in any way. A user could for instance choose Cuid2 as the entity id for its records. Sorting on id will sort on the entity id as this is a real column.

Motivation and Context

The primary key may undesirably leak information, however at the same time using a random value as the primary key may affect performance.

How Has This Been Tested?

Added the appropriate unit and integration tests.

License

I confirm that this contribution is made under an Apache 2.0 license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

byte[] encrypt(byte[] byteArray) {
try {
byte[] key = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this encryption include randomization or padding? The ID should be stable so we should hash instead of encrypt.

Copy link
Collaborator Author

@justin-tay justin-tay Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is just for testing purposes. I felt it was best that users used a dedicated crypto library eg. https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/encrypt/BytesEncryptor.html which would provide more options and this approach is less likely to introduce a CVE into the Elide library.

The com.yahoo.elide.core.security.obfuscation.FunctionIdObfuscator should make it easy to integrate with other libraries as they just need to supply a obfuscation and deobfuscation function.

For Spring I added a com.yahoo.elide.spring.security.obfuscation.BytesEncryptorIdObfuscator that would allow easy integration with org.springframework.security.crypto.encrypt.BytesEncryptor.

@justin-tay justin-tay force-pushed the entityid branch 2 times, most recently from 5b9f63b to 9df7c85 Compare July 29, 2024 10:19
@aklish aklish merged commit 8643a2f into yahoo:master Aug 11, 2024
2 checks passed
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.

Support for Encrypted ID's
2 participants