Enforce that any SharedArrayBuffers that get passed to crypto operations get cloned as non-shared#1116
Conversation
🦋 Changeset detectedLatest commit: bc62790 The changes in this PR will be included in the next version bump. This PR includes changesets to release 42 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
BundleMonFiles updated (3)
Unchanged files (130)
Total files change +79B +0.02% Final result: ✅ View report in BundleMon website ➡️ |
| type BaseEncoder<TFrom> = { | ||
| /** Encode the provided value and return the encoded bytes directly. */ | ||
| readonly encode: (value: TFrom) => ReadonlyUint8Array; | ||
| readonly encode: (value: TFrom) => ReadonlyUint8Array<ArrayBuffer>; |
There was a problem hiding this comment.
What do you think about this narrowing, @lorisleiva. I presume that encoders should never return a SharedArrayBuffer.
There was a problem hiding this comment.
Looks good to me. I can't think of a scenario where you'd want the returned bytes to be shared — and if that was the case, you'd probably want to be super explicit about it and wrap the encoder into something else.
| * ``` | ||
| */ | ||
| export interface ReadonlyUint8Array extends Omit<Uint8Array, TypedArrayMutableProperties> { | ||
| export interface ReadonlyUint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> extends Omit< |
There was a problem hiding this comment.
Add the ability to say ‘no really, this ReadonlyUint8Array is not a SharedArrayBuffer’.
| import { ED25519_ALGORITHM_IDENTIFIER } from './algorithm'; | ||
|
|
||
| function addPkcs8Header(bytes: ReadonlyUint8Array): ReadonlyUint8Array { | ||
| function addPkcs8Header(bytes: ReadonlyUint8Array): ReadonlyUint8Array<ArrayBuffer> { |
There was a problem hiding this comment.
Here, we're able to express that it's readonly, and non-shared (because we just created it inline).
| export async function signBytes(key: CryptoKey, data: ReadonlyUint8Array): Promise<SignatureBytes> { | ||
| assertSigningCapabilityIsAvailable(); | ||
| const signedData = await crypto.subtle.sign(ED25519_ALGORITHM_IDENTIFIER, key, data); | ||
| const signedData = await crypto.subtle.sign(ED25519_ALGORITHM_IDENTIFIER, key, toArrayBuffer(data)); |
There was a problem hiding this comment.
Allows people to pass SharedArrayBuffers in here for signing, without triggering a runtime error.
dfb2fab to
ae1c4e9
Compare
|
Documentation Preview: https://kit-docs-41gy0z0mn-anza-tech.vercel.app |
| type BaseEncoder<TFrom> = { | ||
| /** Encode the provided value and return the encoded bytes directly. */ | ||
| readonly encode: (value: TFrom) => ReadonlyUint8Array; | ||
| readonly encode: (value: TFrom) => ReadonlyUint8Array<ArrayBuffer>; |
There was a problem hiding this comment.
Looks good to me. I can't think of a scenario where you'd want the returned bytes to be shared — and if that was the case, you'd probably want to be super explicit about it and wrap the encoder into something else.
ae1c4e9 to
0769816
Compare
aeaf04e to
d6e2b8e
Compare
Merge activity
|
…tions get cloned as non-shared
0769816 to
bc62790
Compare
|
🔎💬 Inkeep AI search and chat service is syncing content for source 'Solana Kit Docs' |
|
Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up. |

Problem
When you pass a
SharedArrayBufferto a crypto operation (eg.SubtleCrypto#sign) it fatals. TypeScript recently got more strict about this, resulting in type errors that make this PR necessary.Summary of Changes
In this PR we clone
SharedArrayBufferas non-shared when it's used in a crypto operation.