Skip to content

js_sys::global uses eval, and is not compatible with strict CSP #1641

Closed
@mitchchn

Description

@mitchchn

js_sys::global uses the Function constructor to get a handle to the global object:
https://docs.rs/js-sys/0.3.24/src/js_sys/lib.rs.html#4420.

This is a type of eval and is disallowed by strict content security policies that exclude script-src 'self' 'unsafe-eval', even if they would otherwise permit WASM:

Screen Shot 2019-07-02 at 11 08 27 AM

This behaviour causes unpredictable runtime exceptions, since several important APIs in the web_sys and js_sys ecosystem go through the global object.

Alternatives to the Function constructor

  • globalThis is the (very!) modern answer to get a handle to the global object in all JS environments. MDN recommends globalThis as the replacement for the Function constructor approach. Its browser requirements are demanding, however.
  • The same article recommends a series of runtime check for the existence of well-known global objects, which is the approach used by es6-shim.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions