Skip to content

fix: throw spec-correct DOMException types in WebCrypto error paths#1018

Merged
boorad merged 2 commits into
mainfrom
fix/webcrypto-dom-exceptions
May 3, 2026
Merged

fix: throw spec-correct DOMException types in WebCrypto error paths#1018
boorad merged 2 commits into
mainfrom
fix/webcrypto-dom-exceptions

Conversation

@boorad
Copy link
Copy Markdown
Collaborator

@boorad boorad commented May 3, 2026

Summary

Closes #998. Aligns three WebCrypto error paths with the Web IDL spec so applications that catch by err.name (e.g. InvalidAccessError, QuotaExceededError, TypeMismatchError) handle them correctly instead of seeing a generic Error.

  • crypto.getRandomValues
    • byteLength > 65536 → QuotaExceededError with quota: 65536, requested: byteLength (was plain Error)
    • Float16/32/64 / non-integer view → TypeMismatchError (previously silently corrupted via randomFillSync)
  • subtle.exportKey on non-extractable key → InvalidAccessError (was plain Error)
  • lazyDOMException helper now uses globalThis.DOMException when available, otherwise an Error subclass with .name and .code set per the spec table — Hermes does not implement DOMException, so the fallback is what RN apps will see.
  • Adds QuotaExceededError class exporting quota/requested fields per WebIDL.

Test plan

  • random suite — new tests verify getRandomValues throws QuotaExceededError (with quota/requested) on > 65536 bytes
  • random suite — new tests verify getRandomValues throws TypeMismatchError on Float32Array, Float64Array, DataView
  • subtle.importKey/exportKey suite — new test verifies exportKey of a non-extractable AES-GCM key throws InvalidAccessError
  • Existing randomFillSync tests (Node API) still accept Float/DataView — unchanged

- getRandomValues: throw QuotaExceededError (with quota/requested) on
  byteLength > 65536, and TypeMismatchError on non-integer TypedArray views
  (Float32/Float64/DataView), per WebCrypto §getRandomValues
- subtle.exportKey: throw InvalidAccessError (DOMException) on non-extractable
  keys instead of a plain Error
- lazyDOMException: use globalThis.DOMException when available; otherwise an
  Error subclass with .name and .code set so consumers branching on
  `err.name === 'InvalidAccessError'` see the spec-correct value
- Add QuotaExceededError class with quota/requested fields per WebIDL

Closes #998
@vercel
Copy link
Copy Markdown

vercel Bot commented May 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-native-quick-crypto Ready Ready Preview, Comment May 3, 2026 11:42pm

Request Review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

🤖 End-to-End Test Results - Android

Status: ✅ Passed
Platform: Android
Run: 25294201765

📸 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
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

🤖 End-to-End Test Results - iOS

Status: ✅ Passed
Platform: iOS
Run: 25294201757

📸 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.

Spec-correct DOMException errors carry the type identifier ('DataError',
'NotSupportedError', etc.) on `err.name` rather than embedded in the
message. The old `[Name]: message` formatting was non-standard. Update
the helper to look in either field so the existing tests that pass a
type-name as the expected snippet keep working.
@boorad boorad merged commit 5a10d66 into main May 3, 2026
9 checks passed
@boorad boorad deleted the fix/webcrypto-dom-exceptions branch May 3, 2026 23:55
@boorad boorad self-assigned this May 4, 2026
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.

WebCrypto: throw correct DOMException types (QuotaExceededError, TypeMismatchError, InvalidAccessError)

1 participant