Skip to content

Sandboxed renderer bundle throws "Cannot destructure property preloadScripts of binding.startupData" on the initial empty document of extension background pages / offscreen documents (42.x-44.x) #54149

Closed

Description

@BenItBuhner

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for a bug report that matches the one I want to file, without success. Searched startupData, preloadScripts, and sandboxed_renderer.bundle.js script failed to run; the only hits are unrelated merged pull requests.

Electron Version

44.3.0. The 44-x-y branch head still carries the same code (lib/sandboxed_renderer/init.ts is present there); 42-x-y and 43-x-y received it through the backports listed below.

What operating system(s) are you using?

Ubuntu

Operating System Version

Ubuntu 24.04. Reproduced with the bare electron binary under Xvfb and in a packaged electron-builder Linux build under Xvfb.

What arch are you using?

x64

Last Known Working Electron version

42.x before the backport of #51602 (push sandboxed renderer startup data via mojo and cache preload bytecode). That change reached 42-x-y in #51831 (merged 2026-06-03) and 43-x-y in #51792 (merged 2026-06-01); 44 inherits it. main and 45-x-y are not affected: #53974 removed the JavaScript sandbox bundle (lib/sandboxed_renderer/init.ts no longer exists on those branches) and ElectronSandboxedRendererClient::HasScriptsToInject there checks api_service->startup_data() before injecting anything.

Does the issue also appear in Chromium / Google Chrome?

No. The failing code is Electron's sandboxed renderer bootstrap, not Chromium.

Expected Behavior

The sandbox bundle does not run, or returns quietly, on a document that has never received startupData. No console error should be emitted for a document that no preload script targets, and attaching a DevTools client to an extension background page should not put an error into the page's console.

Actual Behavior

When a DevTools-protocol client enables the Runtime domain on an MV2 extension background page (or an MV3 offscreen document) before the page's first navigation commits, the renderer prints two console lines from node:electron/js2c/sandbox_bundle:2:

Electron sandboxed_renderer.bundle.js script failed to run
TypeError: Cannot destructure property 'preloadScripts' of 'binding.startupData' as it is null.

They fire once per background page and once per offscreen document (the offscreen case was observed in a packaged build with Target.setAutoAttach from a remote-debugging client; the test case below uses the simpler background page). The committed extension document then loads and works normally and registered frame preload scripts run in it, so nothing is functionally missing. The error is emitted for the initial empty document only.

Verified output of the test case below (Electron 44.3.0, Linux x64, sandbox enabled, three consecutive runs identical):

[backgroundPage 1] Electron sandboxed_renderer.bundle.js script failed to run (node:electron/js2c/sandbox_bundle:2)
[backgroundPage 1] TypeError: Cannot destructure property 'preloadScripts' of 'binding.startupData' as it is null. (node:electron/js2c/sandbox_bundle:2)
[backgroundPage 1] frame preload ran in chrome-extension://fpbglbihgicgnimhilbefblkochkblij/background.html (/tmp/startupdata-repro-30VBAY/preload.js:1)
[backgroundPage 1] background page script ran in chrome-extension://fpbglbihgicgnimhilbefblkochkblij/background.html (chrome-extension://fpbglbihgicgnimhilbefblkochkblij/background.js:1)

Two conditions are both required, which is why this is easy to miss:

  1. The renderer must be sandboxed. With --no-sandbox the non-sandboxed renderer client is used, the sandbox bundle never runs, and the identical steps print nothing.
  2. Something must create a main-world script context on the background page's initial empty document before ReadyToCommitNavigation. Without a debugger attached the initial empty document never gets a script context and the error does not appear. Runtime.enable from any DevTools-protocol client (webContents.debugger, --remote-debugging-port with Target.setAutoAttach, or DevTools itself) is sufficient.

Testcase Gist URL

No response

Additional Information

Minimal reproduction, a single file with no dependencies. Save as main.js and run electron main.js without --no-sandbox:

// Electron 44.3.0 repro: the sandboxed renderer bundle throws on the initial empty document of an
// extension background page when a DevTools-protocol client enables the Runtime domain before the
// page's first navigation commits.
//
// Run with the sandbox enabled (do NOT pass --no-sandbox): the bundle that throws only runs in
// sandboxed renderers. Expected output, once per background page:
//   [backgroundPage 1] Electron sandboxed_renderer.bundle.js script failed to run
//   [backgroundPage 1] TypeError: Cannot destructure property 'preloadScripts' of 'binding.startupData' as it is null.
// followed by the committed document working normally and receiving the registered frame preload.
const { app, session } = require('electron')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')

if (process.argv.includes('--no-sandbox')) {
  console.warn('This repro needs sandboxed renderers; do not pass --no-sandbox.')
}

app.on('web-contents-created', (_event, wc) => {
  wc.on('console-message', (event) => {
    console.log(`[${wc.getType()} ${wc.id}] ${event.message} (${event.sourceId}:${event.lineNumber})`)
  })
  if (wc.getType() === 'backgroundPage') {
    // Any DevTools client attached before the first commit is enough; Runtime.enable forces a
    // main-world script context on the initial empty document, which runs the sandbox bundle.
    wc.debugger.attach('1.3')
    wc.debugger.sendCommand('Runtime.enable').catch((err) => console.log('Runtime.enable failed:', err.message))
  }
})

app.whenReady().then(async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'startupdata-repro-'))
  fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify({
    manifest_version: 2,
    name: 'startupData repro',
    version: '1.0',
    background: { page: 'background.html' }
  }))
  fs.writeFileSync(path.join(dir, 'background.html'), '<script src="background.js"></script>')
  fs.writeFileSync(path.join(dir, 'background.js'), 'console.log("background page script ran in " + location.href)')
  fs.writeFileSync(path.join(dir, 'preload.js'), 'console.log("frame preload ran in " + location.href)')

  // Shows startupData does arrive for the committed document: the preload runs there without error.
  session.defaultSession.registerPreloadScript({ type: 'frame', id: 'repro-frame', filePath: path.join(dir, 'preload.js') })
  await session.defaultSession.extensions.loadExtension(dir)
  setTimeout(() => app.quit(), 4000)
})

Root cause, with v44.3.0 file and line references:

  • lib/sandboxed_renderer/init.ts:22 destructures binding.startupData unconditionally: const { preloadScripts, process: processProps } = binding.startupData;.
  • shell/renderer/electron_sandboxed_renderer_client.cc:135-166 (DidCreateScriptContext) compiles and runs the bundle whenever ShouldLoadPreload is true (:144, :159). InitializeBindings sets startupData to v8::Null when api_service->startup_data() is empty (:107-114), and the comment at :101-106 says the only documents that reach it without startup data are the ones ShouldLoadPreload() filters out, naming the initial empty document.
  • That filter does not exist. shell/renderer/renderer_client_base.cc:217-229 (ShouldLoadPreload) returns true for any main frame that is not a webview frame and never checks IsOnInitialEmptyDocument(). Electron does apply that guard elsewhere in the same renderer, in shell/renderer/electron_render_frame_observer.cc:71-72 (DidClearWindowObject).
  • startup_data() is populated only by the browser from shell/browser/api/electron_api_web_contents.cc:2443-2491 (MaybeSendRendererStartupData), whose sole caller is ReadyToCommitNavigation (:2493-2498), or by the window.open path through SetPendingNewWindowStartupData. A background page created by ExtensionHost (and an offscreen document created by chrome.offscreen.createDocument) is neither: its initial empty document exists before any navigation is ready to commit, so a script context created there sees a null startupData. The committed document lands in the same RenderFrameHost after the mojo push, which is why it works.

The isolated trigger is therefore: a sandboxed extension renderer whose initial empty document gets a main-world context from a DevTools Runtime.enable before its first navigation commits.

Suggested fix for the release lines: mirror the main guard by having ShouldLoadPreload (or DidCreateScriptContext) skip the bundle when api_service->startup_data() is null or when web_frame->IsOnInitialEmptyDocument(), which is what HasScriptsToInject does on main after #53974; alternatively make init.ts return early on a null startupData. Either removes the console error without changing behaviour for committed documents, which already receive startupData before their script context is created.

This was found in Zenium, an Electron-based browser with extension support, where the line shows up only in debugger-attached runs (test harnesses and DevTools); the application is unaffected functionally.

Activity

  1. moved this to 👀 Unsorted Items in 44-x-yon Sep 20, 2026
  2. added 4 commits that reference this issue on Sep 21, 2026
    859c0d7
    af6e064
    850ecf7
    4821dc2
  3. MarshallOfSound commented on Sep 21, 2026

    @MarshallOfSound
    Member

    Fixed by #54155 (44-x-y), backported to 43-x-y in #54176 and 42-x-y in #54175.

  4. moved this from 👀 Unsorted Items to 🛠️ Fixed for Next Release in 44-x-yon Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions