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

Plaintext values with mode=strict do not throw errors #106

Open
richmirza opened this issue Apr 24, 2024 · 1 comment · May be fixed by #107
Open

Plaintext values with mode=strict do not throw errors #106

richmirza opened this issue Apr 24, 2024 · 1 comment · May be fixed by #107

Comments

@richmirza
Copy link

richmirza commented Apr 24, 2024

Given this schema:

model EncryptionTest {
  id                 String                 @id
  data               String? /// @encrypted?mode=strict
}

and this test:

    await prisma.$queryRawUnsafe(
      `INSERT INTO encryption_test (id, data) VALUES ('id', 'plaintextdata')`,
    );

    const encryptionTest = await prisma.encryptionTest.findFirst({
      where: { id: 'id' },
    });

I would expect this test case to throw an exception. Is my understanding incorrect?

It's a simple fix if indeed it's unexpected behaviour. https://github.com/47ng/prisma-field-encryption/blob/next/src/encryption.ts#L179 is currently

        if (!cloakedStringRegex.test(cipherText)) {
          return
        }

so if a field is in the database as plaintext (which doesn't match the cloakedStringRegex) then strict mode is not taken into account. Proposed fix:

        if (!cloakedStringRegex.test(cipherText)) {
          if (fieldConfig.strictDecryption) {
            throw new Error('Value is not encrypted and mode=strict')
          }
          return
        }
@franky47
Copy link
Member

franky47 commented Apr 24, 2024

That is indeed a missing branch, could you open a PR with your proposed fix please? Thanks!

Note: please add an error for it in this file, with some context (model/field where the cleartext data was found) to help users debug their application.

@richmirza richmirza linked a pull request Apr 24, 2024 that will close this issue
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 a pull request may close this issue.

2 participants