Skip to content

Fix BrowserCryptography in web workers and service workers - #231

Open
nogringo wants to merge 1 commit into
dint-dev:masterfrom
nogringo:fix/worker-globals
Open

nogringo wants to merge 1 commit into
dint-dev:masterfrom
nogringo:fix/worker-globals

Conversation

@nogringo

Copy link
Copy Markdown

isWebCryptoAvailable reads window.isSecureContext, and fillBytesWithSecureRandom calls window.crypto.getRandomValues. Web workers and service workers (for example the background of a Manifest V3 browser extension) have no window, so the first use of Cryptography.instance throws TypeError: Cannot read properties of undefined (reading 'isSecureContext'), and generating random bytes fails the same way.

Both globals exist on globalThis in windows and in workers (WindowOrWorkerGlobalScope), so this change reads them from there. _isSecureContext is now nullable: an environment without it uses the Dart implementations instead of throwing.

This branch has not been deployed

No deployments
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.

1 participant