Skip to content

fix: preserve TypedArray view byteOffset/byteLength in randomFill#955

Merged
boorad merged 2 commits intomainfrom
fix/random-fill-view-corruption
Feb 23, 2026
Merged

fix: preserve TypedArray view byteOffset/byteLength in randomFill#955
boorad merged 2 commits intomainfrom
fix/random-fill-view-corruption

Conversation

@boorad
Copy link
Collaborator

@boorad boorad commented Feb 23, 2026

Summary

Fixes #953randomFillSync, randomFill, and getRandomValues corrupted memory outside the intended region when called with a TypedArray view over a larger ArrayBuffer.

Changes

  • src/random.ts: Extract byteOffset and byteLength from TypedArray views and pass viewOffset + offset to the native layer, so random bytes target the correct region of the underlying ArrayBuffer
  • cpp/random/HybridRandom.cpp: Fix checkOffset to not compare offset against size (wrong param), and add proper bounds check (offset + size > buffer->size()) before calling RAND_bytes
  • cpp/dh/HybridDiffieHellman.cpp: Minor comment correction (get0get1)
  • Tests: 7 new test cases covering views over larger buffers for getRandomValues, randomFillSync (with and without offset/size), and async randomFill

Testing

Run the random test suite in the example app. The new tests verify:

  1. Surrounding bytes outside the view are not modified
  2. The view region itself is actually filled with random data
  3. User-provided offset/size are relative to the view, not the underlying ArrayBuffer

Fixes #953

randomFillSync, randomFill, and getRandomValues corrupted the entire
underlying ArrayBuffer when called with a TypedArray view (byteOffset > 0).
Capture the view's byteOffset and byteLength before abvToArrayBuffer()
strips them, then pass the correct offset and size to native.

Also fix the C++ bounds check (offset + size <= buffer length) and
update a stale comment in DiffieHellman.cpp.

Closes #953
…ecks

Add assertions that the target view regions were actually filled with
random data (not silently no-op'd), and add an async randomFill test
for the view-over-larger-buffer scenario.
@boorad boorad self-assigned this Feb 23, 2026
@github-actions
Copy link
Contributor

🤖 End-to-End Test Results - Android

Status: ✅ Passed
Platform: Android
Run: 22294139071

📸 Final Test Screenshot

Maestro Test Results - android

Screenshot automatically captured from End-to-End tests and will expire in 30 days


This comment is automatically updated on each test run.

@github-actions
Copy link
Contributor

🤖 End-to-End Test Results - iOS

Status: ✅ Passed
Platform: iOS
Run: 22294139051

📸 Final Test Screenshot

Maestro Test Results - ios

Screenshot automatically captured from End-to-End tests and will expire in 30 days


This comment is automatically updated on each test run.

@boorad boorad merged commit dfee4e1 into main Feb 23, 2026
7 checks passed
@boorad boorad deleted the fix/random-fill-view-corruption branch February 23, 2026 05:53
boorad added a commit that referenced this pull request Feb 24, 2026
New pages: PQC (ML-DSA/ML-KEM), Argon2, KMAC, Certificate (SPKAC),
and Utilities (one-shot hash, timingSafeEqual, primes, introspection).

Updated 8 existing pages with missing API sections: SubtleCrypto
(deriveBits, deriveKey, wrapKey/unwrapKey, encapsulation), Keys
(KeyObject.from, equals, toCryptoKey), Signing (standalone sign/verify),
DiffieHellman (diffieHellman function), Ed25519 (Ed448/X448), Hash
(crypto.hash one-shot, SHA3), ECDH (convertKey), and reorganized the
API index into Core/Key Exchange/Key Derivation/Advanced sections.

Annotated 7 pages with behavioral notes from recent fix PRs (#929,
#930, #932, #933, #939, #948, #949, #951, #954, #955): cipher
single-use warning, generateKeys preservation, PBKDF2 validation,
OAEP hash default, randomFill view correctness, RSA-* aliases, and
flexible curve names.

Added llms.txt index route and fixed llms-full.txt JSX stripping in
source.ts to produce clean LLM-friendly output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 getRandomValues corrupts memory when called with a TypedArray view over a larger ArrayBuffer

1 participant