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

Circumvent TrustedHTML errors #1873

Open
psultan opened this issue Aug 17, 2023 · 5 comments
Open

Circumvent TrustedHTML errors #1873

psultan opened this issue Aug 17, 2023 · 5 comments

Comments

@psultan
Copy link

psultan commented Aug 17, 2023

Sequence of actions:

  1. Install Userscript
// ==UserScript==
// @name        Hello
// @namespace   Violentmonkey Scripts
// @match       https://calendar.google.com/calendar/*
// @require     http://code.jquery.com/jquery-latest.min.js
// @icon        https://www.google.com/s2/favicons?domain=google.com
// @grant       none
// @version     1.0
// ==/UserScript==
(function() {
    'use strict';
    console.log("Hello")
})();
  1. Goto https://calendar.google.com/calendar/

Problem:

In Chrome Beta some sites are throwing a 'This document requires 'TrustedHTML' assignment.' error when the script sources a require file. This is only happening in Chrome Beta now, but will likely be an issue in standard Chrome in the future.

This tampermonkey ticket seems related: Tampermonkey/tampermonkey#1334. Using the linked 'Trusted-Types Helper' with violentmonkey fixes the issue for toplevel matches, but does not fix it for sub-frames (goto gmail and open calander sidepanel). If I use the latest tampermonkey the require works on both toplevel and sub-frames (without the helper).

Devtools console contents:

This document requires 'TrustedHTML' assignment.
    (anonymous)	@	Hello.user.js:2
    ib	@	Hello.user.js:2
    fb.setDocument	@	Hello.user.js:2
    (anonymous)	@	Hello.user.js:2
    (anonymous)	@	Hello.user.js:2
    (anonymous)	@	Hello.user.js:2
    (anonymous)	@	Hello.user.js:2
    VMl5m11cb4xv	@	Hello.user.js:20
    Ut	@	injected-web.js:1
    (anonymous)	@	Hello.user.js:1
    Et	@	injected.js:1
    Dt	@	injected.js:1
    await in Dt (async)		
    St	@	injected.js:1
    $t	@	injected.js:1
    await in $t (async)		
    (anonymous)	@	injected.js:1
    await in (anonymous) (async)		
    (anonymous)	@	injected.js:1
    (anonymous)	@	injected.js:1

Environment:

  • OS: Windows
  • Browser: Chrome Version 117.0.5938.11 (Official Build) beta (64-bit)
  • Violentmonkey Version: 2.15.0
@psultan psultan added the bug label Aug 17, 2023
@tophf
Copy link
Member

tophf commented Aug 17, 2023

We don't change CSP of the page currently, but seeing how more and more sites get hostile to userscripts I guess we'll have to do it.

@tophf tophf changed the title [BUG] Require causes TrustedHTML error Circumvent TrustedHTML errors Aug 17, 2023
@tophf
Copy link
Member

tophf commented Aug 17, 2023

Until then the workaround is to reuse the policy of the site: https://github.com/tophf/trust-any-html

@tophf
Copy link
Member

tophf commented Aug 23, 2023

After thinking this over, we can't remove the CSP of sites by default because it reduces security of sites, so having your script depend on removal of this header is not a good idea. More and more sites will use trustedTypes, so the most universal solution is to reuse the policy of the site without removing it.

@gera2ld, maybe it can be implemented as a separate library similarly to vm-url?

@gera2ld
Copy link
Member

gera2ld commented Aug 24, 2023

maybe it can be implemented as a separate library similarly to vm-url?

Sounds a good idea.

To make it clear, this issue is actually caused by jQuery trying to inject unsafe code, instead of sourcing a @required file. In other words, jQuery won't work even if it is used directly by the page rather than from a userscript.
So it is not something we should fix in the extension.

@cyfung1031
Copy link
Contributor

cyfung1031 commented Sep 19, 2023

Just don't use jQuery.
Here is an excellent alternative https://github.com/fabiospampinato/cash

// ==UserScript==
// @name        Hello
// @namespace   Violentmonkey Scripts
// @match       https://calendar.google.com/calendar/*
// @require     https://cdn.jsdelivr.net/npm/cash-dom/dist/cash.min.js
// @icon        https://www.google.com/s2/favicons?domain=google.com
// @grant       none
// @version     1.0
// ==/UserScript==
(function() {
    'use strict';
    console.log("Hello")
})();

jQuery is already a past. It just wastes your PC power to do the compatibility checks.

caeldom added a commit to caeldom/userscripts that referenced this issue Apr 4, 2024
Jquery was failing and causing CSP error "This document requires 'TrustedHTML' assignment." due to using innerHTML which is not allowed under updated strict CSP
See violentmonkey/violentmonkey#1873
Replace jquery require with cash
Replace jquery initialize with mutation observer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants