electron: [Bug]: Frame unable to use preload scripts when contentWindow is used to early.

Preflight Checklist

Electron Version

19.0.4, 20.1.0, 21.3.1

What operating system are you using?

Windows

Operating System Version

Windows 10 Pro 21H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Preload scripts should load in sub frames when nodeIntegrationInSubFrames is set to true.

Actual Behavior

Preload scripts wont load in sub frames.

Testcase Gist URL

https://gist.github.com/83f23ea2dea571eedb1b9048c8d8c111

Additional Information

By adding iframes dynamically to the DOM and then accessing the contentWindow or contentDocument property of the iframe the loading of preload scripts for this iframe wont happen until the iframe navigates to a different src or is reloaded.

Simple sample:

const iframe = document.createElement("iframe");
iframe.src = <Some URL>;
document.body.appendChild(iframe);

iframe.contentDocument; // this is enough to start the issue

Executing a script directly inside the webContents frame-created event will also ensure that the preload scriptes are not loaded:

webContents.on("frame-created", (event, details) => {
    const { frame } = details;
    const { parent } = frame;
    if (!parent) return;
   
    frame.executeJavaScript("location.href"); // <- preload scripts will no longer load
});

This happens very often in the wild, just browsing a little bit and I can see that many frames wont load the preload scripts.

Workaround: A way around it seems to be to check if new frames have executed the preload script which is mentioned in #32453. And as mentioned there, this is a very janky workaround

webContents.on("frame-created", (event, details) => {
    const { frame } = details;
    const { parent } = frame;
    if (!parent) return;
   
    frame.once("dom-ready", () => fixPreloadScripts(frame))
});

async function fixPreloadScripts(frame)  {
    const hasPreloaded = await frame.executeJavaScript("window._hasPreloaded");
    if (hasPreloaded) return;

    frame.reload();
}

In most cases this “workaround” will break whatever a website intended to do with such an IFrame, so use it with caution.

This issue is similar to #32453 but I created a separate issue because the way of causing this effect seems to be different.

Possible related to #32661.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 18 (2 by maintainers)

Most upvoted comments

Oh no, why is this ticket closed, we have had issues with this since version 9.x to latest version and no one has fixed it

We fixed it by way of rollback of old code in this pull request and building Electron itself () shell/renderer/electron_render_frame_observer.cc https://github.com/electron/electron/pull/29093/files#diff-c14a043029f7d1798ca101daf94f7a83c3bdf2315f386e45c4d2841626887460 #fix: illegal access errors with nodeIntegrationInSubFrames

And there are multiple tickets created with this pull request issue and I see they are all closed, and no issue is still unresolved Can anyone here help me with this?

I hope someone can help me with this easier than manually editing the library code and then building electron again, preload.js actually doesn’t work when it runs on about:blank page

Reproducable with Electron 21.3.1.