From a454875602485ceedd692c5c8431205ef5207c3f Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 24 Apr 2023 17:24:31 +0200 Subject: [PATCH 1/5] Ability to work without encryption in db --- tdrive/backend/node/config/test.json | 2 +- tdrive/backend/node/src/core/crypto/index.ts | 31 +++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/tdrive/backend/node/config/test.json b/tdrive/backend/node/config/test.json index 65aadc6f..682f49df 100644 --- a/tdrive/backend/node/config/test.json +++ b/tdrive/backend/node/config/test.json @@ -1,6 +1,6 @@ { "database": { - "secret": "ab63bb3e90c0271c9a1c06651a7c0967eab8851a7a897766", + "secret": "", "mongodb": { "uri": "mongodb://mongo:27017" }, diff --git a/tdrive/backend/node/src/core/crypto/index.ts b/tdrive/backend/node/src/core/crypto/index.ts index 417e7cad..afa35539 100644 --- a/tdrive/backend/node/src/core/crypto/index.ts +++ b/tdrive/backend/node/src/core/crypto/index.ts @@ -19,22 +19,24 @@ export type CryptoResult = { }; export function decrypt(data: string, encryptionKey: string): CryptoResult { - let result = v2.decrypt(data, encryptionKey); - if (result.done) { - return result; + if (encryptionKey) { + let result = v2.decrypt(data, encryptionKey); + if (result.done) { + return result; + } + + result = v1.decrypt(data, encryptionKey); + if (result.done) { + return result; + } + + result = legacy.decrypt(data, encryptionKey); + if (result.done) { + return result; + } } - result = v1.decrypt(data, encryptionKey); - if (result.done) { - return result; - } - - result = legacy.decrypt(data, encryptionKey); - if (result.done) { - return result; - } - - return result; + return { data, done: true }; } export function md5(value: string): string { @@ -46,5 +48,6 @@ export function encrypt( encryptionKey: any, options: { disableSalts?: boolean } = {}, ): CryptoResult { + if (!encryptionKey) return value; return v2.encrypt(value, encryptionKey, options); } From 553278ecf9d3021408707b18494c6a8321152870 Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 24 Apr 2023 19:12:23 +0200 Subject: [PATCH 2/5] Update index.ts --- tdrive/backend/node/src/core/crypto/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tdrive/backend/node/src/core/crypto/index.ts b/tdrive/backend/node/src/core/crypto/index.ts index afa35539..0f5bab29 100644 --- a/tdrive/backend/node/src/core/crypto/index.ts +++ b/tdrive/backend/node/src/core/crypto/index.ts @@ -34,6 +34,8 @@ export function decrypt(data: string, encryptionKey: string): CryptoResult { if (result.done) { return result; } + + return result; } return { data, done: true }; @@ -48,6 +50,6 @@ export function encrypt( encryptionKey: any, options: { disableSalts?: boolean } = {}, ): CryptoResult { - if (!encryptionKey) return value; + if (!encryptionKey) return ˋ${value}`; return v2.encrypt(value, encryptionKey, options); } From dfcbc6060f720f29623ae09878d6d8f29b89ff45 Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 24 Apr 2023 19:15:31 +0200 Subject: [PATCH 3/5] Update index.ts --- tdrive/backend/node/src/core/crypto/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdrive/backend/node/src/core/crypto/index.ts b/tdrive/backend/node/src/core/crypto/index.ts index 0f5bab29..290e39f2 100644 --- a/tdrive/backend/node/src/core/crypto/index.ts +++ b/tdrive/backend/node/src/core/crypto/index.ts @@ -50,6 +50,6 @@ export function encrypt( encryptionKey: any, options: { disableSalts?: boolean } = {}, ): CryptoResult { - if (!encryptionKey) return ˋ${value}`; + if (!encryptionKey) return { data:ˋ${value}`, done: true }; return v2.encrypt(value, encryptionKey, options); } From 65429a48c17c5b2872301f5da73078fc2e8c8ae8 Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 24 Apr 2023 19:21:53 +0200 Subject: [PATCH 4/5] Fix lint --- tdrive/backend/node/src/core/crypto/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdrive/backend/node/src/core/crypto/index.ts b/tdrive/backend/node/src/core/crypto/index.ts index 290e39f2..05a5ae02 100644 --- a/tdrive/backend/node/src/core/crypto/index.ts +++ b/tdrive/backend/node/src/core/crypto/index.ts @@ -50,6 +50,6 @@ export function encrypt( encryptionKey: any, options: { disableSalts?: boolean } = {}, ): CryptoResult { - if (!encryptionKey) return { data:ˋ${value}`, done: true }; + if (!encryptionKey) return { data: `${value}`, done: true }; return v2.encrypt(value, encryptionKey, options); } From 9cf68c951c1bc8d62720aead4b38e1beb0f0e084 Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Tue, 25 Apr 2023 09:10:58 +0200 Subject: [PATCH 5/5] Additional test about encryption --- .../services/encryption/encryption.test.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tdrive/backend/node/test/unit/core/services/encryption/encryption.test.ts b/tdrive/backend/node/test/unit/core/services/encryption/encryption.test.ts index 83f7ea1c..b187b2d7 100644 --- a/tdrive/backend/node/test/unit/core/services/encryption/encryption.test.ts +++ b/tdrive/backend/node/test/unit/core/services/encryption/encryption.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "@jest/globals"; -import { decrypt } from "../../../../../src/core/crypto/index"; +import { decrypt, encrypt } from "../../../../../src/core/crypto/index"; import v2 from "../../../../../src/core/crypto/v2"; import v1 from "../../../../../src/core/crypto/v1"; import legacy from "../../../../../src/core/crypto/legacy"; @@ -29,3 +29,21 @@ describe("Encryption", () => { }); }); }); + +describe("Disabled encryption", () => { + const encryptionKey = ""; + + describe("The encrypt/decrypt functions", () => { + it("should not encrypt the data", async () => { + const myData = "hello world"; + + const encrypted = encrypt(myData, encryptionKey); + const decrypted = decrypt(encrypted.data, encryptionKey); + const decrypted2 = decrypt(myData, encryptionKey); + + expect(encrypted.data).toBe(myData); + expect(decrypted.data).toBe(myData); + expect(decrypted2.data).toBe(myData); + }); + }); +});