Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): recover undecryptable protected content like the server

Elian Doranbea0425d3ebb
diff --git a/apps/standalone/src/lightweight/crypto_provider.spec.ts b/apps/standalone/src/lightweight/crypto_provider.spec.ts
index 9e24c9e739..d4cd120154 100644
--- a/apps/standalone/src/lightweight/crypto_provider.spec.ts
+++ b/apps/standalone/src/lightweight/crypto_provider.spec.ts
@@ -1,5 +1,6 @@
import {
createCipheriv as nodeCreateCipheriv,
+ createDecipheriv as nodeCreateDecipheriv,
createHash as nodeCreateHash,
createHmac as nodeCreateHmac,
scryptSync as nodeScrypt
@@ -138,10 +139,17 @@ describe("BrowserCryptoProvider parity with the Node.js provider", () => {
}
});
- it("throws for ciphertext that is not a whole number of blocks", () => {
- const decipher = provider.createDecipheriv("aes-128-cbc", key, iv);
- decipher.update(new Uint8Array(20));
- expect(() => decipher.final()).toThrow();
+ it("throws node:crypto's error for ciphertext that is not a whole number of blocks", () => {
+ // `data_encryption.decrypt()` recognizes this message (zadam/trilium#510).
+ for (const length of [ 0, 20 ]) {
+ const nodeDecipher = nodeCreateDecipheriv("aes-128-cbc", key, iv);
+ nodeDecipher.update(new Uint8Array(length));
+ expect(() => nodeDecipher.final()).toThrow("wrong final block length");
+
+ const decipher = provider.createDecipheriv("aes-128-cbc", key, iv);
+ decipher.update(new Uint8Array(length));
+ expect(() => decipher.final(), `${length} bytes`).toThrow("wrong final block length");
+ }
});
});
});
diff --git a/apps/standalone/src/lightweight/crypto_provider.ts b/apps/standalone/src/lightweight/crypto_provider.ts
index f6eb81b9d9..42219ec5c1 100644
--- a/apps/standalone/src/lightweight/crypto_provider.ts
+++ b/apps/standalone/src/lightweight/crypto_provider.ts
@@ -236,6 +236,11 @@ class AesCbcCipher implements Cipher {
return cbc(this.key, this.iv).encrypt(data);
}
+ // node:crypto's message, which `data_encryption.decrypt()` recognizes (zadam/trilium#510).
+ if (data.length === 0 || data.length % 16 !== 0) {
+ throw new Error("wrong final block length");
+ }
+
// The padding is stripped by hand because `cbc()` throws on invalid padding, which most
// wrong keys leave. `data_encryption.decrypt()` detects those through its digest instead.
const decrypted = cbc(this.key, this.iv, { disablePadding: true }).decrypt(data);
diff --git a/packages/trilium-core/src/services/blob.ts b/packages/trilium-core/src/services/blob.ts
index c8b873f593..2615635b95 100644
--- a/packages/trilium-core/src/services/blob.ts
+++ b/packages/trilium-core/src/services/blob.ts
@@ -175,10 +175,9 @@ function encryptTextRepresentation(textRepresentation: string, isProtected: bool
* it than a caller that has no key, and one unreadable blob should not fail the request around it.
*
* One inherited edge: a stored value whose length rules out ciphertext altogether takes
- * dataEncryptionService's recovery path for zadam/trilium#510, which hands back what it was given —
- * and only under Node, since that path is selected by a Node crypto error message. Nothing writes a
- * value of that shape here ({@link encryptTextRepresentation} is the only writer and always emits real
- * ciphertext), so this is noted rather than defended against.
+ * dataEncryptionService's recovery path for zadam/trilium#510, which hands back what it was given.
+ * Nothing writes a value of that shape here ({@link encryptTextRepresentation} is the only writer
+ * and always emits real ciphertext), so this is noted rather than defended against.
*/
function decryptTextRepresentation(textRepresentation: string | null | undefined, isProtected: boolean): string {
if (!textRepresentation) {
diff --git a/packages/trilium-core/src/services/encryption/data_encryption.spec.ts b/packages/trilium-core/src/services/encryption/data_encryption.spec.ts
index 9badb1552d..552e74694e 100644
--- a/packages/trilium-core/src/services/encryption/data_encryption.spec.ts
+++ b/packages/trilium-core/src/services/encryption/data_encryption.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { decodeUtf8, encodeUtf8 } from "../utils/binary.js";
+import { decodeUtf8, encodeBase64, encodeUtf8 } from "../utils/binary.js";
import dataEncryption from "./data_encryption.js";
// The global server spec setup (apps/server/spec/setup.ts) calls initializeCore,
@@ -91,6 +91,18 @@ describe("data_encryption", () => {
expect(recovered).toBe(false);
}
});
+
+ it("hands back a value that cannot be ciphertext unchanged (zadam/trilium#510)", () => {
+ // 40 bytes take a 13-byte IV, which leaves 27 bytes: not a whole number of blocks.
+ const misaligned = encodeBase64(Uint8Array.from({ length: 40 }, (_, i) => i));
+ // A 16-byte IV with no block after it.
+ const ivOnly = encodeBase64(new Uint8Array(16).fill(5));
+
+ for (const value of [ misaligned, ivOnly ]) {
+ expect(dataEncryption.decrypt(KEY, value), value).toEqual(encodeUtf8(value));
+ expect(dataEncryption.decrypt(KEY, encodeUtf8(value)), value).toEqual(encodeUtf8(value));
+ }
+ });
});
describe("decryptString", () => {