Skip to content

Commit dfd5c83

Browse files
pranavjain97claude
andcommitted
fix(sdk-api): address PR review nits for v2 encrypt
- Throw on invalid JSON in decryptAsync instead of silently falling through - Use static import for @bitgo/argon2 instead of dynamic import - Add test for invalid JSON input in decryptAsync Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 933e2f8 commit dfd5c83

3 files changed

Lines changed: 6 additions & 3 deletions

File tree

modules/sdk-api/src/encrypt.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ export function decrypt(password: string, ciphertext: string): string {
5656
export async function decryptAsync(password: string, ciphertext: string): Promise<string> {
5757
let isV2 = false;
5858
try {
59-
// Peek at v field only to route -- internal format we produce, not external input.
6059
const envelope = JSON.parse(ciphertext);
6160
isV2 = envelope.v === 2;
6261
} catch {
63-
// Not valid JSON -- fall through to v1.
62+
throw new Error('decrypt: ciphertext is not valid JSON');
6463
}
6564
if (isV2) {
6665
// Do not catch: wrong password on v2 must not silently fall through to v1.

modules/sdk-api/src/encryptV2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { argon2id } from '@bitgo/argon2';
12
import { base64String, boundedInt, decodeWithCodec } from '@bitgo/sdk-core';
23
import { randomBytes } from 'crypto';
34
import * as t from 'io-ts';
@@ -60,7 +61,6 @@ async function argon2Hash(
6061
salt: Uint8Array,
6162
params: { memorySize: number; iterations: number; parallelism: number }
6263
): Promise<Uint8Array> {
63-
const { argon2id } = await import('@bitgo/argon2');
6464
return argon2id({
6565
password,
6666
salt,

modules/sdk-api/test/unit/encrypt.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ describe('encryption methods tests', () => {
195195
await assert.rejects(() => decryptAsync('wrong', v2ct));
196196
});
197197

198+
it('throws on invalid JSON input', async () => {
199+
await assert.rejects(() => decryptAsync(password, 'not-json'), /ciphertext is not valid JSON/);
200+
});
201+
198202
it('wrong password on v2 data does not fall through to v1 decrypt', async () => {
199203
const v2ct = await encryptV2(password, plaintext, { memorySize: 1024, iterations: 1, parallelism: 1 });
200204
let caughtError: Error | undefined;

0 commit comments

Comments
 (0)