-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Getting ciphertext back from the database #8
Comments
Hi Franklyn, I can't decrypt the data. I use the I entered the key to encrypt in As you wrote, the decryption key must be the encryption key, so I copied and pasted the key from I have done many tests, but I cannot decrypt the data, please help :) |
The encryption key is automatically added to the list of decryption keys, so you should not need to add it yourself (this list is only for key rotation). Are you sure your environment variables are correctly loaded at the time when the middleware is initialised? |
Thank you for your answer! I have removed the key
I can explain how the file is structured. It is a javascript file with several APIs. At the beginning of the file I write the following code.
Then follow 2 APIs First
Second
Unfortunately in the second API I get the patient's name and surname encrypted :( I also share the file
If I do the "create" the data is correctly encrypted, but when I do the "findUnique" the file is not decrypted! |
Ok, I see nothing weird about your usage. By any chance, is the ciphertext returned by Prisma the same as the one stored in the database for a given cell? Just want to rule out double-encryption as mentioned in the original post. |
The ciphertext is surprisingly short, do you know if the underlying clear-text data was short too (only a few characters long I'd wager) ? This rules out double-encryption anyway. If you import your encryption key here, does it show the same fingerprint (8 hex characters next to "Use for encryption"), ie |
Franky, If I enter my encryption key in the Keychain I get this result The data I encrypted (name and surname) are both seven characters long! |
Ok, let's see if we can get some errors showing: could you do the following change please? model patients {
...
- name String @db.VarChar(50) /// @encrypted
+ name String @db.VarChar(50) /// @encrypted?strict
- surname String @db.VarChar(50) /// @encrypted
+ surname String @db.VarChar(50) /// @encrypted?strict
...
} With One last thing you could try if there are no errors, is to disable the early bailout for decryption, where we try to detect if there's anything to actually decrypt in the data coming from the database. You can do so by commenting out the function decryptOnRead(params, result, keys, models, operation, decryptFn) {
var _a;
// Analyse the query to see if there's anything to decrypt.
const model = models[params.model];
if (Object.keys(model.fields).length === 0 && !((_a = params.args) === null || _a === void 0 ? void 0 : _a.include)) {
// The queried model doesn't have any encrypted field,
// and there are no included connections.
// We can safely skip decryption for the returned data.
// todo: Walk the include/select tree for a better decision.
- return;
+ // return; <- comment this out
}
const decryptionErrors = [];
const fatalDecryptionErrors = []; |
I've released |
Hi franky, thanks for your availability! The module does not print errors in the console and does not create log files. I have tried to comment out the line of code (in my module it was in line 78) and I have also edited the schema.prisma file doing this job:
But the data is still not decrypted :( I can tell you that the text in the patients.ts file has changed:
I add: Every time I make a change I run both commands
Then I delete the data from the database and recreate it. |
I checked your edit. I have read the In the In the
|
You're finding me stumped with this issue, but we'll get to the bottom of this! What does the debug logs say when reading out the data (that comes out still encrypted)? You can log only those by setting You can either set it in the .env file, as long as it's loaded somehow when running your app (before importing the lib): DEBUG=prisma-field-encryption:* But the better way would be to set it as part of the env of the terminal running your server: # On macOS/Unix:
DEBUG="prisma-field-encryption:*" npm run my-server-start-script
# On Windows (CMD):
set DEBUG=prisma-field-encryption:* & npm run my-server-start-script
# On Windows (VSCode terminal):
$env:DEBUG="prisma-field-encryption:*"; npm run my-server-start-script You might want to also set the debug depth as well: # On macOS/Unix:
DEBUG="prisma-field-encryption:*" DEBUG_DEPTH=100 npm run my-server-start-script
# On Windows (CMD):
set DEBUG=prisma-field-encryption:* & set DEBUG_DEPTH=100 & npm run my-server-start-script
# On Windows (VSCode terminal):
$env:DEBUG="prisma-field-encryption:*"; $env:DEBUG_DEPTH=100; npm run my-server-start-script |
Ahhh search no longer, I found the issue.. I use a regexp to try and detect a ciphertext in the @47ng/cloak format, and it was wrong somehow to assume the ciphertext part of the string was 22 characters long at least (yours is 11). I'll run some more tests on this with various short clear-text lengths, fix the regexp and release a fix for you to try, sorry about that! |
Now my tests in What happened is that the ciphertext was truncated, passing under the threshold of the regexp detection, and passed through as-is. Since ciphertext is quite a bit larger than clear-text, I'd suggest raising this limit (for reference, a 50-character clear-text input encrypts into a 127-characters long encrypted string, so better set it to 130 for safety), and enforcing the actual 50 character length limit in your API before passing it to Prisma. You can calculate other lengths here: Sorry I got you into a wild goose chase, when the answer was actually right in your second message. 😅 |
waah, now it's working properly!! 😂😂😂 The data I would like to encrypt is longer, the name and surname were a test. The important thing is that we managed to reach the goal! You are very good, thank you very much Franky! |
I seem to be experiencing this issue where my data is encrypted in the database, but it is returned without decrypting. I'm using MongoDB and assume the character length is not the cause of this? Using |
@Serdans could you check that you get the same ciphertext from Prisma that the one stored in the database? Just want to rule out a double encryption case. |
Yup it's the same ciphertext. |
If you turn on debugging, does it reveal anything useful? Mind that some values may be printed in clear text there. |
I'm using NextJS and setting the
|
Looks like an import error, anyway you probably don't need to set it in the Next.js config, a |
I see it's successfully decrypting it if I'm selecting the data directly, e.g.
does not decrypt the field. |
Ah, that's a different issue then, could you open a separate issue please? I'll try and replicate from your example query, but a concise Prisma schema might help a lot with reproduction. Thanks! |
This issue somehow got turned into a thread about ciphertext not being decrypted and returned as-is from the database.
Such issues usually indicate:
Decryption will throw an error when in strict mode (using
/// @encrypted?mode=strict
), but will log a warning to the console in other modes. This could be the sign that you are missing the right key to decrypt data.If there are no errors, it usually means that the ciphertext has been corrupted somehow, and failed to be detected by the middleware (so is passed through as any other data). Make sure your field maximum length is high enough to contain the largest expected ciphertext, which is larger than the clear-text. Calculator available here.
Original issue content:
Adding a global strict decryption mode that throws errors when decryption fails (missing key) should help avoid building layers of encryption in migrations.
Use-case:
The text was updated successfully, but these errors were encountered: