Skip to content

Commit

Permalink
fix(sec): add encryption tag to db
Browse files Browse the repository at this point in the history
  • Loading branch information
arielweinberger committed Apr 28, 2024
1 parent 8a02fa4 commit 7dec36e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `encryptionTag` to the `ProviderApiKey` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "ProviderApiKey" ADD COLUMN "encryptionTag" TEXT NOT NULL;
8 changes: 5 additions & 3 deletions apps/server/src/app/credentials/provider-api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from "@nestjs/common";
import { PrismaService } from "../prisma.service";
import { EncryptionService } from "../encryption/encryption.service";
import { ProviderApiKey } from "@prisma/client";
import { StringFilter } from "../../@generated/prisma/string-filter.input";

@Injectable()
export class ProviderApiKeysService {
Expand All @@ -21,7 +20,8 @@ export class ProviderApiKeysService {
async decryptProviderApiKey(key: ProviderApiKey): Promise<string> {
const decrypted = await this.encryptionService.decrypt(
key.encryptedData,
key.encryptedDataKey
key.encryptedDataKey,
key.encryptionTag
);
return decrypted;
}
Expand All @@ -42,7 +42,7 @@ export class ProviderApiKeysService {
where: { provider, organizationId },
});

const { encryptedData, encryptedDataKey } =
const { encryptedData, encryptedDataKey, encryptionTag } =
await this.encryptionService.encrypt(value);

const censoredValue = this.censorApiKey(value);
Expand All @@ -55,6 +55,7 @@ export class ProviderApiKeysService {
data: {
encryptedData,
encryptedDataKey,
encryptionTag,
censoredValue,
},
});
Expand All @@ -67,6 +68,7 @@ export class ProviderApiKeysService {
provider,
encryptedData,
encryptedDataKey,
encryptionTag,
censoredValue,
organizationId,
},
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/app/encryption/encryption.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class EncryptionService {
async encrypt(data: string): Promise<{
encryptedData: string;
encryptedDataKey: string;
tag: string; // Include the authentication tag in the encryption result
encryptionTag: string;
}> {
this.logger.info("Encrypting data");

Expand All @@ -61,7 +61,7 @@ export class EncryptionService {
return {
encryptedData: Buffer.concat([iv, encrypted]).toString("hex"),
encryptedDataKey: Buffer.from(dataKey.ciphertext).toString("base64"),
tag: cipher.getAuthTag().toString("hex"), // Store the tag for verification during decryption
encryptionTag: cipher.getAuthTag().toString("hex"), // Store the tag for verification during decryption
};
}

Expand Down

0 comments on commit 7dec36e

Please sign in to comment.