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(encryption): decrypt fields in nested read results #1934

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/runtime/src/enhancements/node/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,29 @@ class EncryptedHandler extends DefaultPrismaProxyHandler {
const realModel = this.queryUtils.getDelegateConcreteModel(model, entityData);

for (const field of getModelFields(entityData)) {
const fieldInfo = await resolveField(this.options.modelMeta, realModel, field);
// Don't decrypt null, undefined or empty string values
if (!entityData[field]) continue;

const fieldInfo = await resolveField(this.options.modelMeta, realModel, field);
if (!fieldInfo) {
continue;
}

const shouldDecrypt = fieldInfo.attributes?.find((attr) => attr.name === '@encrypted');
if (shouldDecrypt) {
// Don't decrypt null, undefined or empty string values
if (!entityData[field]) continue;

try {
entityData[field] = await this.decrypt(fieldInfo, entityData[field]);
} catch (error) {
this.logger.warn(`Decryption failed, keeping original value: ${error}`);
if (fieldInfo.isDataModel) {
const items =
fieldInfo.isArray && Array.isArray(entityData[field]) ? entityData[field] : [entityData[field]];
for (const item of items) {
// recurse
await this.doPostProcess(item, fieldInfo.type);
}
} else {
const shouldDecrypt = fieldInfo.attributes?.find((attr) => attr.name === '@encrypted');
if (shouldDecrypt) {
try {
entityData[field] = await this.decrypt(fieldInfo, entityData[field]);
} catch (error) {
this.logger.warn(`Decryption failed, keeping original value: ${error}`);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ describe('Encrypted test', () => {
model User {
id String @id @default(cuid())
encrypted_value String @encrypted()

@@allow('all', true)
}`,
{
enhancements: ['encryption'],
Expand Down Expand Up @@ -62,15 +60,59 @@ describe('Encrypted test', () => {
expect(rawRead.encrypted_value).not.toBe('abc123');
});

it('Decrypts nested fields', async () => {
const { enhance, prisma } = await loadSchema(
`
model User {
id String @id @default(cuid())
posts Post[]
}

model Post {
id String @id @default(cuid())
title String @encrypted()
author User @relation(fields: [authorId], references: [id])
authorId String
}
`,
{
enhancements: ['encryption'],
enhanceOptions: {
encryption: { encryptionKey },
},
}
);

const db = enhance();

const create = await db.user.create({
data: {
id: '1',
posts: { create: { title: 'Post1' } },
},
include: { posts: true },
});
expect(create.posts[0].title).toBe('Post1');

const read = await db.user.findUnique({
where: {
id: '1',
},
include: { posts: true },
});
expect(read.posts[0].title).toBe('Post1');

const rawRead = await prisma.user.findUnique({ where: { id: '1' }, include: { posts: true } });
expect(rawRead.posts[0].title).not.toBe('Post1');
});

it('Multi-field encryption test', async () => {
const { enhance } = await loadSchema(
`
model User {
id String @id @default(cuid())
x1 String @encrypted()
x2 String @encrypted()

@@allow('all', true)
}`,
{
enhancements: ['encryption'],
Expand Down Expand Up @@ -105,8 +147,6 @@ describe('Encrypted test', () => {
model User {
id String @id @default(cuid())
encrypted_value String @encrypted()

@@allow('all', true)
}`);

const sudoDb = enhance(undefined, { kinds: [] });
Expand Down Expand Up @@ -203,7 +243,6 @@ describe('Encrypted test', () => {
model User {
id String @id @default(cuid())
encrypted_value String @encrypted() @length(0, 6)

@@allow('all', true)
}`,
{
Expand Down
Loading