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

Feat: Allow custom cipher and decipher functions #23

Open
wants to merge 13 commits into
base: next
Choose a base branch
from

Conversation

Vitu-77
Copy link

@Vitu-77 Vitu-77 commented May 25, 2022

It is currently not possible to pass custom encryption and decryption methods as params, and this creates a problem: It is not possible to filter db data by encrypted fields, as the current encryption library always generates a different hash for the same text.

This PR aims to allow custom encryption/decryption function to be passed as params, so if the developer wants to filter data by ciphertext, he can use any lib that generates static hash's, for example.

@coveralls
Copy link

coveralls commented May 26, 2022

Pull Request Test Coverage Report for Build 2415774562

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 31 of 36 (86.11%) changed or added relevant lines in 3 files are covered.
  • 17 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.3%) to 80.69%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/errors.ts 0 1 0.0%
src/index.ts 7 8 87.5%
src/encryption.ts 24 27 88.89%
Files with Coverage Reduction New Missed Lines %
src/errors.ts 8 31.25%
src/encryption.ts 9 69.41%
Totals Coverage Status
Change from base Build 2238191975: 0.3%
Covered Lines: 135
Relevant Lines: 164

💛 - Coveralls

@franky47
Copy link
Member

franky47 commented May 26, 2022

I like the ability to provide a custom ciphersuite, mostly to ensure compatibility with existing encrypted data with a different cipher or serialisation format.

Though I wouldn't recommend using this feature to implement stable encryption, which is insecure (usually because of nonce or IV reuse). What ciphersuite were you thinking about?

The way I circumvented this in my apps that use this middleware is to use a separate field for search that contains a stable hash (one-way function) of the clear text data, and abstracted that in a middleware (if you search via an encrypted field, hash the search and look for a match on the hash field instead).

@Vitu-77
Copy link
Author

Vitu-77 commented May 26, 2022

I'm not thinking of any specific cipher suite, this feature is more to give the developer freedom of choice about what he wants to use.

I aware that stable hashes are not the safest ways to encrypt, but the example i mentioned is just a use case, since with this feat any ciphersuite can be used.

Copy link
Member

@franky47 franky47 left a comment

Choose a reason for hiding this comment

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

I'll be down to merge this feature, but with a few changes to the API: since providing a custom encryption method most likely results in having to provide the corresponding decryption method, we should enforce this using TypeScript.

src/types.ts Outdated Show resolved Hide resolved
const cipherText = encryptStringSync(clearText, keys.encryptionKey)
const cipherText =
encryptFn !== undefined
? encryptFn(clearText)
Copy link
Member

Choose a reason for hiding this comment

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

Here we would need a way to pass the encryption key, or are you assuming that providing a custom cipher opts the user out of key management?

Copy link
Author

Choose a reason for hiding this comment

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

We don't need this, since the idea is to provide the user total control about encrypt/decrypt methods, only the function are needed for this.
The key management would be part of the provided cipher logic.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. Can you add some documentation about the feature in the README please? Emphasizing that key management is left to the user's appreciation would be good, since the options to pass keys in code are still available (or we could disallow them if a custom cipher is used, via a TS discriminating union, up to you).

Copy link
Author

Choose a reason for hiding this comment

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

I liked your suggestion, to remove the option to provide keys when custom functions are passed. I will implement this along with the documentation.

@franky47
Copy link
Member

franky47 commented Jul 6, 2022

After some consideration, I don't think advertising this solution as a way to solve the where filtering problem is a safe one. Reuse of nonces and IVs is a big red flag in cryptography.

I have published an alternative solution to do filtering in 1.4.0-beta.4, you can check out the documentation.

That being said, I like the idea of swapping out the internal ciphersuite, so I could merge this PR but without the advocacy for weak cryptography in the documentation.

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.

4 participants