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

CSP unsafe-eval in Web Worker in Chrome Browser which is always strict mode #450

Closed
edwardxia opened this issue May 8, 2021 · 4 comments · Fixed by #480
Closed

CSP unsafe-eval in Web Worker in Chrome Browser which is always strict mode #450

edwardxia opened this issue May 8, 2021 · 4 comments · Fixed by #480

Comments

@edwardxia
Copy link

try {
regeneratorRuntime = runtime;
} catch (accidentalStrictMode) {
// This module should not be running in strict mode, so the above
// assignment should always work unless something is misconfigured. Just
// in case runtime.js accidentally runs in strict mode, we can escape
// strict mode using a global Function call. This could conceivably fail
// if a Content Security Policy forbids using Function, but in that case
// the proper solution is to fix the accidental strict mode problem. If
// you've misconfigured your bundler to force strict mode and applied a
// CSP to forbid Function, and you're not willing to fix either of those
// problems, please detail your unique predicament in a GitHub issue.
Function("r", "regeneratorRuntime = r")(runtime);
}

Web workers in latest Chrome stable enforces strict mode (not sure when it started to enforce), there is no way to turn it off.

Using regenerator under CSP will cause web worker to stop due to unsafe-eval.

Setup described in #378 are somewhat configuration issue. However, now since there is no way to turn off strict mode in Web Workers in Chrome, it is no longer a configuration issue, and need to be properly fixed.

@edwardxia
Copy link
Author

The dirty hack I currently use to workaround this issue:

new require('webpack').BannerPlugin({
  test: /\.worker\.js$/,
  banner: 'self.regeneratorRuntime = undefined;',
  raw: true
})

@nicolo-ribaudo
Copy link
Contributor

@benjamn Would you accept a PR to do something like this?

 try { 
   regeneratorRuntime = runtime; 
 } catch (accidentalStrictMode) { 
  if (typeof globalThis === "object") {
    globalThis.regeneratorRuntime = runtime;
  } else {

   // This module should not be running in strict mode, so the above 
   // assignment should always work unless something is misconfigured. Just 
   // in case runtime.js accidentally runs in strict mode, we can escape 
   // strict mode using a global Function call. This could conceivably fail 
   // if a Content Security Policy forbids using Function, but in that case 
   // the proper solution is to fix the accidental strict mode problem. If 
   // you've misconfigured your bundler to force strict mode and applied a 
   // CSP to forbid Function, and you're not willing to fix either of those 
   // problems, please detail your unique predicament in a GitHub issue. 
   Function("r", "regeneratorRuntime = r")(runtime);
  } 
} 

The problem only happens in modern browsers, and they support globalThis.

A proper configuration solution would be to completely avoid regenerator in modern browsers, but many people have a single build and not multiple targets (and some libraries on npm rely on regeneratorRuntime).

@benjamn
Copy link
Collaborator

benjamn commented Jul 21, 2021

@nicolo-ribaudo Yes, I'd be happy with that!

@nicolo-ribaudo
Copy link
Contributor

Awesome, I'll prepare a PR by tomorrow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants