Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib/db/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ export function decrypt(ciphertext: string | null | undefined): string | null |
console.warn(
"[Encryption] Found encrypted data but STORAGE_ENCRYPTION_KEY is not set. Cannot decrypt."
);
return ciphertext;
// Return null instead of encrypted ciphertext to prevent sending encrypted tokens to providers
return null;
}

const body = ciphertext.slice(PREFIX.length);
const parts = body.split(":");
if (parts.length !== 3) {
console.error("[Encryption] Malformed encrypted value");
return ciphertext;
// Return null instead of encrypted ciphertext to prevent sending malformed encrypted tokens to providers
return null;
Comment on lines +121 to +122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The security fix is incomplete. While this change correctly returns null for malformed values, the decrypt function still returns the encrypted ciphertext on line 113 when the STORAGE_ENCRYPTION_KEY is missing. To fully satisfy the security goal of preventing encrypted tokens from being sent to upstream APIs, line 113 should also be updated to return null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@/tmp/bot-response.md

}

const [ivHex, encryptedHex, authTagHex] = parts;
Expand All @@ -134,7 +136,8 @@ export function decrypt(ciphertext: string | null | undefined): string | null |
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
console.error("[Encryption] Decryption failed:", message);
return ciphertext;
// Return null instead of encrypted ciphertext to prevent sending encrypted tokens to providers
return null;
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/unit/db-encryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ test("connection field helpers encrypt and decrypt all supported credential fiel
assert.deepEqual(decrypted, connection);
});

test("decrypt returns the original ciphertext when the value is malformed or the key is wrong", async () => {
test("decrypt returns null when the value is malformed or the key is wrong", async () => {
process.env.STORAGE_ENCRYPTION_KEY = "task-304-secret-c";
const firstModule = await importFresh("src/lib/db/encryption.ts");
const encrypted = firstModule.encrypt("top-secret");

process.env.STORAGE_ENCRYPTION_KEY = "task-304-secret-d";
const secondModule = await importFresh("src/lib/db/encryption.ts");

assert.equal(secondModule.decrypt(encrypted), encrypted);
assert.equal(secondModule.decrypt("enc:v1:not-valid"), "enc:v1:not-valid");
// When decryption fails with wrong key, return null (not encrypted ciphertext)
// This prevents sending encrypted tokens to APIs
assert.equal(secondModule.decrypt(encrypted), null);
assert.equal(secondModule.decrypt("enc:v1:not-valid"), null);
Comment on lines +79 to +80

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test confirms that null is returned when the key is wrong. However, it doesn't cover the case where the encryption key is completely missing. It would be beneficial to add a test case for an unset STORAGE_ENCRYPTION_KEY to ensure it also returns null (which currently it does not, as noted in the feedback for src/lib/db/encryption.ts).

});