fix(crypto): use Uint8Array for WebCrypto BufferSource (unblocks vitest crypto tests)#50
Open
devin-ai-integration[bot] wants to merge 1 commit into
Open
Conversation
The previous code cast Uint8Array.buffer to ArrayBuffer when calling crypto.subtle.encrypt/decrypt/importKey/deriveKey. Under stricter WebCrypto realms (e.g. jsdom in vitest), the cross-realm ArrayBuffer identity check rejects this as 'not an instance of ArrayBuffer'. Pass Uint8Array directly (BufferSource accepts ArrayBufferView) and constrain helpers to Uint8Array<ArrayBuffer> to satisfy lib.dom's BufferSource overload (TypeScript 5.7+ Uint8Array is generic over ArrayBufferLike, which permits SharedArrayBuffer). Fixes the 3 failing tests in src/__tests__/lib/crypto.test.ts: - encrypt and decrypt roundtrip - different users produce different ciphertexts - wrong key fails to decrypt Public API of deriveKey/encrypt/decrypt is unchanged.
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The 3 tests in
src/__tests__/lib/crypto.test.ts(encrypt and decrypt roundtrip,different users produce different ciphertexts,wrong key fails to decrypt) have been failing onmainanddevelopwith:Root cause:
src/lib/crypto.tscastUint8Array.buffertoArrayBufferbefore handing it tocrypto.subtle.encrypt/decrypt/importKey/deriveKey. Under jsdom's stricter WebCrypto realm, the cross-realmArrayBufferidentity check rejects this as "not an instance of ArrayBuffer". The runtime acceptsBufferSource = ArrayBuffer | ArrayBufferView, so passing theUint8Arraydirectly works in Node, jsdom, browsers, and Edge.Changes in
src/lib/crypto.ts:textToBuffer/bufferToHex/hexToBuffer→textToBytes/bytesToHex/hexToBytesand returnUint8Array<ArrayBuffer>(notArrayBuffer).Uint8Array<ArrayBuffer>(vs the defaultUint8Array<ArrayBufferLike>) so TS 5.7+'sBufferSourceoverload doesn't rejectSharedArrayBuffer-backed views.crypto.subtle.*call sites now pass theUint8Arraydirectly — no more.buffer as ArrayBuffercasts.Public API (
deriveKey,encrypt,decrypt) signatures and behavior unchanged —encryptstill returns{ ciphertext: hexString, iv: hexString }anddecryptaccepts the same inputs. Existing ciphertexts in Convex remain decryptable (encoding/decoding path is identical; only the WebCrypto argument types changed).Verified locally on this branch:
bun run test→ 175/175 pass (was 171/174 on develop)npx tsc --noEmit→ cleanbun run build→ cleanReview & Testing Checklist for Human
deriveKeyoutput. (Hex strings are realm-agnostic, so this should be a no-op, but worth confirming against Convex prod data before merging.)bun run testin CI to confirm the crypto suite is green.src/hooks/use-key-store.ts(the only consumer) still behaves correctly in the browser — save a key, reload, retrieve it.Notes
src/lib/crypto.tsis touched. The repo also has 46 unrelated ESLint errors (mostlyreact-hooks/set-state-in-effectand@convex-dev/explicit-table-ids) that I left alone — happy to do a follow-up PR if you want.bun run testlocally is now green again.Link to Devin session: https://app.devin.ai/sessions/a990fb55f97e422ebe20a0d96226d3ba
Requested by: @Jing-yilin