Skip to content

fix: prevent "document is not defined" error in non-DOM environments - #428

Open
ZayanKhan-12 wants to merge 2 commits into
MetaMask:mainfrom
ZayanKhan-12:fix/191-document-undefined-guard
Open

fix: prevent "document is not defined" error in non-DOM environments#428
ZayanKhan-12 wants to merge 2 commits into
MetaMask:mainfrom
ZayanKhan-12:fix/191-document-undefined-guard

Conversation

@ZayanKhan-12

@ZayanKhan-12 ZayanKhan-12 commented Aug 4, 2026

Copy link
Copy Markdown

Description

Constructing MetaMaskInpageProvider with shouldSendMetadata enabled throws a document is not defined ReferenceError in non-DOM environments (e.g. Chrome extension background scripts / service workers), because the constructor unconditionally reads document.readyState and registers a DOMContentLoaded listener on window.

This PR guards the DOM access:

  • MetaMaskInpageProvider: if typeof document === 'undefined', the missing document is treated as "already loaded" and the site metadata is sent immediately, instead of reading document.readyState / waiting for DOMContentLoaded.
  • siteMetadata.ts: the site name/icon extraction now receives globalThis and falls back to location.hostname (and a null icon) when no document is available, so the metadata request no longer fails with an unhandled ReferenceError logged as an error. Also fixes a small inconsistency where getSiteName read the global window instead of its windowObject parameter for the hostname fallback.

Behavior in normal window contexts is unchanged.

Fixes #191

Testing

  • yarn jest — all 9 suites / 135 tests pass, including new tests:
    • constructor sends site metadata immediately when the document has already loaded
    • constructor waits for DOMContentLoaded when the document is still loading
    • constructor does not throw and sends metadata immediately when document is undefined (simulated by mocking the document getter in jsdom)
    • sendSiteMetadata sends DOM-derived metadata, and falls back to the hostname when document is undefined
  • yarn lint:eslint — passes (the two touched test files were added to the existing mixed-environment no-restricted-globals override, following the pattern used for EIP6963.test.ts et al.)
  • yarn lint:misc --check — passes
  • yarn build — passes

🤖 Generated with Claude Code


Note

Low Risk
Small defensive guards on optional metadata sending; normal in-page behavior is preserved and covered by new tests.

Overview
Fixes document is not defined when MetaMaskInpageProvider is created with shouldSendMetadata in environments without a DOM (extension backgrounds, service workers).

The constructor now treats a missing document like a fully loaded page and calls sendSiteMetadata immediately instead of reading document.readyState or waiting on DOMContentLoaded. siteMetadata extraction uses globalThis and, when document is absent, falls back to location.hostname and a null icon (and uses the passed object for the hostname fallback instead of the global window).

Browser behavior is unchanged; tests cover DOM, loading, and no-document paths.

Reviewed by Cursor Bugbot for commit 9638968. Bugbot is set up for automated code reviews on this repo. Configure here.

The MetaMaskInpageProvider constructor reads document.readyState (and
registers a DOMContentLoaded listener on window) whenever
shouldSendMetadata is enabled. In non-DOM environments, such as
extension background pages and service workers, the document global
does not exist, so constructing the provider throws a
"document is not defined" ReferenceError.

Guard the document access with a typeof check and treat a missing
document as "already loaded", sending the site metadata immediately.
Also guard the DOM reads in siteMetadata.ts so that the metadata
request falls back to the hostname (and a null icon) instead of
failing when no document is available. Behavior in normal window
contexts is unchanged.

Fixes MetaMask#191

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ZayanKhan-12
ZayanKhan-12 requested a review from a team as a code owner August 4, 2026 00:32
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

No document object in background.js

1 participant