pixiebrix / pixiebrix-extension

PixieBrix browser extension
https://www.pixiebrix.com
GNU Affero General Public License v3.0
83 stars 22 forks source link

Bugfix: `match_origin_as_fallback` failing to fix content script injection into iframes with `sandbox` and `srcdoc` attrs #8927

Open mnholtz opened 2 months ago

mnholtz commented 2 months ago

Follow-up to https://github.com/pixiebrix/pixiebrix-extension/pull/8922

We've identified setting match_origin_as_fallback in the manifest as a preferred approach to fixing https://github.com/pixiebrix/pixiebrix-extension/issues/8921

But neither @fungairino nor @mnholtz were able to replicate a fix locally with the PixieBrix Extension, despite being able to reproduce the fix with a simple reproduction detailed in https://issues.chromium.org/issues/355256366

This implies that there must be something specific to the new version of Chrome and the PixieBrix extension that is preventing the content script injection with these sort of iframes <iframe srcdoc="..." sandbox/>

fungairino commented 2 months ago

We modified the linked manifest.json (https://github.com/pixiebrix/pixiebrix-extension/blob/main/src/manifest.json ) with the following changes (shown in this commit):

changed "matches" to [""],

added: "match_origin_as_fallback": true

Running our extension on the test page then produces this error in the console:

Blocked script execution in 'about:srcdoc' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

Followed immediately by this log:

Chrome bug https://crbug.com/816121 hit.

If I modify the iframe sandbox attribute to sandbox="allow-scripts", then the content script injects fine.

I posted this on the chrome issue to see what else we can do. https://issues.chromium.org/issues/355256366

fungairino commented 2 months ago

Another dev reported this:

ed...@pixiebrix.com, adding match_origin_as_fallback works for me in a local copy of PixieBrix from the web store loaded as an unpacked extension. I see PixieBrix in devtools console context switcher under both about:srcdoc frames as well as 3 messages about setRuntimeLogging.

https://issues.chromium.org/issues/355256366#comment11

mnholtz commented 2 months ago

re

ed...@pixiebrix.com, adding match_origin_as_fallback works for me in a local copy of PixieBrix from the web store loaded as an unpacked extension. I see PixieBrix in devtools console context switcher under both about:srcdoc frames as well as 3 messages about setRuntimeLogging.

I am able to confirm that the content script is loaded in srcdoc + sandbox iframes, but the initialization logic is failing to execute with the following error:

Blocked script execution in 'about:srcdoc' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Chrome bug https://crbug.com/816121 hit.

Via logging (I can't set breakpoints directly inside the offending iframes), I am able to narrow down the issue to something going wrong in this promise: https://github.com/pixiebrix/pixiebrix-extension/blob/36a717b5b6052cda176dfc0021848664a537e0c8/src/contentScript/contentScript.ts#L96

twschiller commented 2 months ago

Via logging (I can't set breakpoints directly inside the offending iframes), I am able to narrow down the issue to something going wrong in this promise:

@mnholtz it's likely due to how dynamic imports work. We use https://github.com/awesome-webextension/webpack-target-webextension which might be using script tags to inject the modules at runtime

fungairino commented 2 months ago

We have resolved this by modifying the hack to look for these iframe and remove the sandbox attribute. We also hid this behind the "iframe-srcdoc-sandbox-hack" flag.

As a follow-up we have this issue to remove this hack once the related chromium issues are resolved: https://github.com/pixiebrix/pixiebrix-extension/issues/8951