Skip to content

A script tag injected into a page with a Content Security Policy that rejects inline scripts will not run [WORKING AS INTENDED] #2046

Closed
@ugawk

Description

@ugawk

Reduced test script:

// ==UserScript==
// @name Hello World
// @description Hello World
// @include *
// @Version 1.0
// @grant none
// ==/UserScript==
var script = document.createElement('script');
script.textContent = "(alert('hello'))()";
document.body.appendChild(script);

This script doesn't work on
https://github.com/greasemonkey/greasemonkey/issues

On every other page it works and alert box pops up. Is this a bug with greasemonkey?

Activity

ghost

ghost commented on Oct 30, 2014

@ghost

http://en.wikipedia.org/wiki/Content_Security_Policy

GitHub forbids scripts that don't originate from their CDN (assets-cdn.github.com, collector-cdn.github.com). Consequently, Firefox will refuse to execute inline scripts.

It certainly would be nice if Greasemonkey scripts weren't affected by CSP restrictions.

jerone

jerone commented on Oct 30, 2014

@jerone
Contributor

What's the issue with just executing the code or using @require for external scripts?

arantius

arantius commented on Oct 30, 2014

@arantius
Collaborator

AFAIK there's no way to bypass CSP for <script> tags that happened to come from a user script, without allowing them from anywhere (which is bad). So this is probably just WAI.

added this to the Pony milestone on Oct 30, 2014
ugawk

ugawk commented on Oct 30, 2014

@ugawk
Author

"AFAIK there's no way to bypass CSP for"

Your comment was cut off. Is it not possible to allow greasemonkey scripts to bypass CSP ?
@grant CSP

hectorr93

hectorr93 commented on Oct 30, 2014

@hectorr93

I hope GreaseMonkey can do something about this. Bookmarklets and addon's shouldn't be affected by CSP

Firefox does have about:config setting to turn off CSP completely but that is a security risk and userscript author's shouldn't be asking users to change browser settings for script to run correctly.

Can't GreaseMonkey addon edit response header to include GreaseMonkey scripts?

arantius

arantius commented on Oct 31, 2014

@arantius
Collaborator

A <script> tag is a <script> tag. Can't enable one without enabling them all. I don't think I'm interested in building features with the sole purpose of defeating intentional security measures.

arantius

arantius commented on Oct 31, 2014

@arantius
Collaborator

Aha .. don't try to type the word "script" surrounded by angle brackets when trying to refer to that kind of HTML tag. Anyway:

A script tag in the page is a script tag in the page. I'd be surprised if there was a way to force one of them to run, in violation of the CSP rules set, without forcing all of them to run. I'm also generally not interested in building features whose sole purpose is to defeat intentional/useful security mechanisms.

sizzlemctwizzle

sizzlemctwizzle commented on Oct 31, 2014

@sizzlemctwizzle
Contributor

pdf.js apparently just turns off CSP. Unfortunately, I'm not sure how we'd do this for a script since we don't have access to the request object during injection as far as I can tell. Maybe this could be done extension-wide? If we did, would that kill CSP everywhere (or at least on any page with a script injected into it)?

ugawk

ugawk commented on Oct 31, 2014

@ugawk
Author

This issue was added to "pony". What does that mean?

Getting to topic at hand, yes this requires changes made by extension not script. If CSP is turned off completely, then it would be defeating intentional security mechanisms against things like XSS. But greasemonkey scripts, like bookmarklets, are mostly used to make intentional changes. The security risk comes from installing the greasemonkey script, CSP doesn't help in that regard for greasemonkey.

Can't api be added "@grant CSP-inline" which would alter response header add "inline" to CSP?

The biggest issue is that greasemonkey disabled unsafeWindow and so injecting scripts into site was easiest alternative but now that's becoming a problem as sites use CSP.

arantius

arantius commented on Oct 31, 2014

@arantius
Collaborator

I do not believe there is a way to securely disable CSP only for certain scripts. Look at the example in the original report. There is no request nor response to set a header on.

And to repeat: I do not intend to create new features whose sole purpose is to defeat intentional security measures.

changed the title [-]Sandbox bug? [/-] [+]A script tag injected into a page with a Content Security Policy that rejects inline scripts will not run [WORKING AS INTENDED][/+] on Oct 31, 2014
janekptacijarabaci

janekptacijarabaci commented on Aug 30, 2015

@janekptacijarabaci
Contributor

57 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sizzlemctwizzle@jerone@arantius@rslifka@Swyter

        Issue actions

          A script tag injected into a page with a Content Security Policy that rejects inline scripts will not run [WORKING AS INTENDED] · Issue #2046 · greasemonkey/greasemonkey