Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Function("...") causes unsafe-eval CSP violation in Chrome extension #336

Closed
deltaidea opened this issue Jan 7, 2018 · 5 comments
Closed

Comments

@deltaidea
Copy link

deltaidea commented Jan 7, 2018

I'm trying to use react-form in my Chrome extension options page. It has regenerator-runtime as the transitive dependency that throws the error, so I figured I'll create an issue here.

Thanks for your time! I appreciate your support of the JS community.

Expected Behavior

regenerator-runtime works silently like a good boy.

Actual Behavior

require("regenerator-runtime") // Uncaught EvalError

Offending line: node_modules/regenerator-runtime/runtime-module.js:10

var g = (function() { return this })() || Function("return this")();

Version: [email protected]

Full error:

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".

Environment

Chrome extension Options page, Chrome 63.0.3239.132 (64-bit), Windows 10

@deltaidea
Copy link
Author

#292 (comment)

... it should be safe as long as you don't blindly concatenate all your JS together with the "use strict"; at the top ...

Unfortunately, strict mode is enforced in the extension environment. I'm guessing the same is true in other secure sandboxes such as Chrome apps and Atom plugins.

@JakeChampion
Copy link

JakeChampion commented Jan 7, 2018

@deltaidea I think the line can be changed to:

var g = (function() { return this ? this : typeof self !== 'undefined' ? self : undefined})() || Function("return this")();

@deltaidea
Copy link
Author

Yeah, that works, thank you!

Can you guys please take a look and tell if this is a reasonable fix?
I'm gonna replace this package with a modified copy for now in my project.

@JakeChampion
Copy link

It should be a reasonable fix, self is the global object and works in Browsers' main thread as well as workers (Service Workers, Web Workers, Shared Workers etc).

@ijsnow
Copy link

ijsnow commented Mar 23, 2018

This is also a problem with firefox extensions

jvergeldedios added a commit to blockhq/regenerator that referenced this issue May 3, 2018
Benjamin-Dobell added a commit to Benjamin-Dobell/regenerator that referenced this issue Jun 3, 2018
Benjamin-Dobell added a commit to Benjamin-Dobell/regenerator that referenced this issue Jul 23, 2018
benjamn pushed a commit to Benjamin-Dobell/regenerator that referenced this issue Aug 3, 2018
@benjamn benjamn closed this as completed in 4efc689 Aug 3, 2018
benjamn added a commit that referenced this issue Aug 3, 2018
If this works, then it removes the need for hacks to discover the global
object, which continue to be problematic (e.g. #336, #346).

Importantly, the regenerator-runtime/runtime.js module can still be
evaluated as a non-CJS script, and it will not pollute the global scope
except for defining regeneratorRuntime.
benjamn added a commit that referenced this issue Aug 7, 2018
If this works, then it removes the need for hacks to discover the global
object, which continues to be problematic (e.g. #336, #346).

Importantly, the regenerator-runtime/runtime.js module can still be
evaluated as a non-CJS script, and it will not pollute the global scope
except for defining regeneratorRuntime.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants