fix(build): replace lodash's Function('return this')() with globalThis to bypass Firefox MV3 CSP#84
Closed
laurenthdl wants to merge 1 commit intoCrawlerCode:mainfrom
Closed
Conversation
…This` to bypass Firefox MV3 CSP
lodash uses `Function('return this')()` as a fallback when `global`/`self`
checks fail. In a Firefox MV3 content script, the addon runs in an isolated
world whose realm differs from the page's, so `self.Object === Object` is
`false` and the fallback path is taken. The expression is then blocked by
the extension's default MV3 CSP (`script-src 'self'`), throwing
`EvalError: call to Function() blocked by CSP` and preventing the content
script from mounting.
Add a small Vite plugin scoped to lodash modules that rewrites the
expression to `globalThis` at build time. No runtime/dependency change.
Fixes CrawlerCode#80
Owner
|
First of all, thank you very much for your contribution. You’re right, and your solution would work, but I’ve actually already fixed the issue (see #83).
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #80: in Firefox the time tracker UI never appears on issue pages because the content script crashes at module init with
EvalError: call to Function() blocked by CSP.Root cause
The bundle includes
lodash.debounce(transitively viausehooks-ts). At module init, lodash runs the historical globalThis fallback:In a Firefox MV3 content script, the addon executes in an isolated world whose realm is distinct from the page's. As a consequence:
globalis undefined →uis falsyself.Object === Objectisfalse(the content script's ownObjectis not the page'sObject) →dis falsyFunction('return this')()is takenscript-src 'self', no'unsafe-eval')This is reproducible on Firefox 127+ with v2.0.1 of the addon. Chrome is unaffected because Chrome's isolated world keeps
Objectidentity with the page.The exact location in the v2.0.1 build is
content-scripts/content.js:1329(col ~165050) and the lazy chunkchunks/timer-*.js.End-user CSP relaxation is not a workaround: Firefox MV3 does not allow
'unsafe-eval'incontent_security_policy.extension_pages, and the page-level CSP is irrelevant since this is the extension's own CSP that is enforced.Fix
Add a small Vite plugin scoped to
lodashmodules that rewrites the expression toglobalThisat build time:Function('return this')()expressions elsewhere.globalThisis available since Firefox 65 / Chrome 71, well below the addon'sstrict_min_version: 127.0.Verification
pnpm run build:firefox. Bundle no longer containsFunction(\return this`)(). The substituted line is nowf = u || d || globalThis,in bothcontent-scripts/content.js` and the relevant lazy chunk..xpiwith the same substitution and loading it as a temporary addon in Firefox: the timer mounts and works as expected on issue pages.Test plan
pnpm installpnpm run build:firefox— succeedscontent-scripts/content.jsandchunks/timer-*.jsno longer containFunction('return this')()🤖 Generated with Claude Code