Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Firefox) Handlebars no longer work (call to Function() blocked by CSP) #126

Closed
Aquafina-water-bottle opened this issue Apr 7, 2023 · 2 comments · Fixed by #171
Closed
Labels
browser/firefox The issue is Firefox-only kind/bug The issue or PR is regarding a bug priority/high High priority issue/PR triage/accepted The issue or PR is ready to be actively worked on

Comments

@Aquafina-water-bottle
Copy link

Description
Handlebars no longer work, and spits out a call to Function() blocked by CSP error on every marker. Doesn't happen on Chrome. I'm guessing this function is called in the Handlebars source code somewhere, and this is some basic permissions issue with Manifest v3.

To reproduce:

  • Install the latest version of the extension
  • Install your favorite dictionary
  • Enable advanced options
  • Head over to Configure Anki card templates… and test any marker.

image

Browser version
112.0b8 (64-bit)

Yomitan version
23.4.7.0 (master branch at the same date)

@Aquafina-water-bottle Aquafina-water-bottle added kind/bug The issue or PR is regarding a bug browser/firefox The issue is Firefox-only labels Apr 7, 2023
@Aquafina-water-bottle
Copy link
Author

Aquafina-water-bottle commented Apr 8, 2023

TODO a bit of research, the conclusion that I came across is that Function and related things that can evaluate arbitrary code is no longer supported by Manifest v3, so this is unfortunately expected behavior.

You can no longer execute external logic using executeScript(), eval(), and new Function(). 1

The reason why handlebars still works on Chrome is because Chrome has a specific manifest key sandbox, which allows running eval'd code safely (Firefox does not support this):

A sandboxed page is not subject to the Content Security Policy (CSP) used by the rest of the extension (it has its own separate CSP value). This means that, for example, it can use inline script and eval. 2

Potential Solutions

  1. A potential alternative currently is some sort of user script (currently Firefox support only): https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts
    Note that it appears that Chrome will be supporting userscripts sometime in the near future as well: [Chrome] Switch to manifest v3 Tampermonkey/tampermonkey#644 (comment)
    (edit: userscripts can only be ran on page loads, and cannot be easily controlled / communicate to web extensions)

  2. Another potential alternative is to somehow use WASM, since wasm-unsafe-eval also exists (which seems to allow usage of eval): https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution (edit: read the MDN doc wrong)

  3. Use a templating library (or really any other language) that doesn't require eval, and can interact with custom javascript helper functions. Possible candidates:

  4. Pray that the rust handlebars library works with JS implemented helper functions one day (see: WebAssembly API for JavaScript sunng87/handlebars-rust#208)

  5. Pray that Mozilla implements the sandbox manifest key one day

  6. Push the work to an Anki add-on (i.e. export the entire JSON, and have an add-on hook on note add to generate the fields correctly)

  7. Pray for eval-less handlebars: Support for eval-less template execution handlebars-lang/handlebars.js#1934

Further Reading

Current priority list

Footnotes

  1. https://developer.chrome.com/docs/extensions/migrating/improve-security/#remove-execution-of-strings

  2. https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/

@Aquafina-water-bottle Aquafina-water-bottle changed the title Handlebars no longer work (call to Function() blocked by CSP) (Firefox) Handlebars no longer work (call to Function() blocked by CSP) Apr 8, 2023
@djahandarie djahandarie added priority/high High priority issue/PR triage/accepted The issue or PR is ready to be actively worked on labels Apr 9, 2023
@Aquafina-water-bottle
Copy link
Author

Got a version working with kbn/handlebars, will submit a PR on it soon (tm).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser/firefox The issue is Firefox-only kind/bug The issue or PR is regarding a bug priority/high High priority issue/PR triage/accepted The issue or PR is ready to be actively worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants