Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(database): type the cached decryption salt as an ArrayBuffer-backed view

Richie McIlroy31fbce84bafa
diff --git a/apps/web/__tests__/unit/database-crypto.test.ts b/apps/web/__tests__/unit/database-crypto.test.ts
index f2329090d..da517f7f7 100644
--- a/apps/web/__tests__/unit/database-crypto.test.ts
+++ b/apps/web/__tests__/unit/database-crypto.test.ts
@@ -37,7 +37,10 @@ describe("database encryption", () => {
const stored = await encrypt("drive-config");
await decrypt(stored);
const bytes = Buffer.from(stored, "base64");
- bytes[bytes.length - 1] ^= 0xff;
+ bytes.writeUInt8(
+ bytes.readUInt8(bytes.length - 1) ^ 0xff,
+ bytes.length - 1,
+ );
await expect(decrypt(bytes.toString("base64"))).rejects.toThrow(
"Decryption failed",
);
diff --git a/packages/database/crypto.ts b/packages/database/crypto.ts
index 7312ca698..b6f24aaef 100644
--- a/packages/database/crypto.ts
+++ b/packages/database/crypto.ts
@@ -100,7 +100,7 @@ export async function encrypt(text: string): Promise<string> {
const DECRYPT_KEY_CACHE_SIZE = 256;
const decryptKeys = new Map<string, Promise<CryptoKey>>();
-function decryptKey(salt: Uint8Array): Promise<CryptoKey> {
+function decryptKey(salt: Uint8Array<ArrayBuffer>): Promise<CryptoKey> {
const id = `${ENCRYPTION_KEY() ?? ""}:${Buffer.from(salt).toString("hex")}`;
const cached = decryptKeys.get(id);
if (cached) {