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
15 changes: 12 additions & 3 deletions packages/kdbx/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ export async function aesCbcDecrypt(
): Promise<Uint8Array> {
const subtle = kx_getCrypto().subtle;
const cryptoKey = await subtle.importKey('raw', kx_buf(key), 'AES-CBC', false, ['decrypt']);
return new Uint8Array(
await subtle.decrypt({ name: 'AES-CBC', iv: kx_buf(iv) }, cryptoKey, kx_buf(data)),
);
try {
return new Uint8Array(
await subtle.decrypt({ name: 'AES-CBC', iv: kx_buf(iv) }, cryptoKey, kx_buf(data)),
);
} catch {
// WebCrypto throws a DOMException with an empty message on a PKCS#7
// padding failure (deliberately, to avoid a padding-oracle side
// channel) — and a wrong key almost always produces invalid padding, so
// this is the ordinary "wrong password" case for KDBX 3.1 files, not a
// rare corruption edge case. Give callers something to show the user.
throw new Error('AES-CBC decryption failed (wrong credentials or corrupt file)');
}
}

/**
Expand Down
18 changes: 14 additions & 4 deletions packages/kdbx/tests/kdbx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,26 @@ for (const config of CONFIGS) {
});
}

test('wrong credentials are rejected', async () => {
test('wrong credentials are rejected, with a message the UI can show', async () => {
const kdbx = await Kdbx.create(Credentials.fromPassword('right'), options({ version: 4 }));
const saved = await kdbx.save();
await assert.rejects(() => Kdbx.load(saved, Credentials.fromPassword('wrong')));
await assert.rejects(
() => Kdbx.load(saved, Credentials.fromPassword('wrong')),
(err: Error) => err.message.length > 0,
);
});

test('wrong credentials are rejected (KDBX 3.1)', async () => {
test('wrong credentials are rejected, with a message the UI can show (KDBX 3.1)', async () => {
// Regression test: KDBX 3.1 decrypts the outer AES-CBC payload before any
// "wrong credentials" check runs, and WebCrypto throws an empty-message
// DOMException on the resulting padding failure — page.ts's unlock screen
// was rendering that empty message as a blank error bar.
const kdbx = await Kdbx.create(Credentials.fromPassword('right'), options({ version: 3 }));
const saved = await kdbx.save();
await assert.rejects(() => Kdbx.load(saved, Credentials.fromPassword('wrong')));
await assert.rejects(
() => Kdbx.load(saved, Credentials.fromPassword('wrong')),
(err: Error) => err.message.length > 0,
);
});

test('multiple protected fields decrypt in document order', async () => {
Expand Down