Skip to content

Commit 4ba84af

Browse files
giarc3skeet70
andauthored
Fail when trying to double-encrypt a document (#95)
* Fail when trying to double-encrypt a document * Update CHANGELOG.md Co-authored-by: Murph Murphy <murph@clurictec.com> --------- Co-authored-by: Murph Murphy <murph@clurictec.com>
1 parent f6ddd96 commit 4ba84af

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 4.0.0
2+
3+
- Encryption now throws a `TscException` when trying to encrypt a document that has already been IronCore encrypted.
4+
- If you have a use case for double-encrypting a document, please open an issue explaining and we can work on accommodating you.
5+
- If you aren't double-encrypting documents there are no breaking changes affecting you in this version bump
6+
17
## 3.0.1
28

39
- Fixed issue with constructing `FieldMetadata`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ironcorelabs/tenant-security-nodejs",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "NodeJS client library for the IronCore Labs Tenant Security Proxy.",
55
"homepage": "https://ironcorelabs.com/docs",
66
"main": "src/index.js",

src/kms/Crypto.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ const verifyHeaderSignature = (header: ironcorelabs.proto.Iv3DocumentHeader | un
116116
* Encrypt the provided document with the provided DEK. Encrypts all fields within the document with the same key but with separate IVs per field.
117117
*/
118118
export const encryptDocument = (document: PlaintextDocument, dek: Base64String, tenantId: string): Future<TenantSecurityException, EncryptedDocument> => {
119+
const invalidDocuments = Object.entries(document).filter(([, value]) => Util.isCmkEncryptedDocument(value));
120+
if (invalidDocuments.length > 0) {
121+
const invalidKeys = invalidDocuments.map(([key]) => `\`${key}\``);
122+
return Future.reject(
123+
new TscException(
124+
TenantSecurityErrorCode.DOCUMENT_ENCRYPT_FAILED,
125+
`One or more provided documents is already IronCore encrypted. IDs: ${invalidKeys.join(", ")}`
126+
)
127+
);
128+
}
119129
const dekBytes = Buffer.from(dek, "base64");
120130
const futuresMap = Object.entries(document).reduce((currentMap, [fieldId, fieldBytes]) => {
121131
currentMap[fieldId] = encryptField(fieldBytes, dekBytes, {tenantId});

src/tests/DevIntegration.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ describe("INTEGRATION dev environment tests", () => {
8181
await TestUtils.runSingleDocumentRoundTripForTenant(client, AZURE_TENANT_ID);
8282
await TestUtils.runSingleExistingDocumentRoundTripForTenant(client, AZURE_TENANT_ID);
8383
});
84+
85+
it("fails to double encrypt data", async () => {
86+
const data = TestUtils.getDocumentToEncrypt();
87+
const meta = TestUtils.getMetadata(GCP_TENANT_ID);
88+
const encrypted = await client.encryptDocument(data, meta);
89+
90+
try {
91+
await client.encryptDocument(encrypted.encryptedDocument, meta);
92+
fail("Should fail because documents are double encrypted");
93+
} catch (e) {
94+
expect(e).toBeInstanceOf(TenantSecurityException);
95+
expect(e.errorCode).toEqual(TenantSecurityErrorCode.DOCUMENT_ENCRYPT_FAILED);
96+
expect(e.message).toContain("`field1`, `field2`, `field3`");
97+
}
98+
});
8499
});
85100

86101
describe("roundtrip streaming encrypt and decrypt", () => {

0 commit comments

Comments
 (0)