Skip to content

Commit

Permalink
Avoid CSP errors in Chrome apps by using global var detection.
Browse files Browse the repository at this point in the history
Fixes #301.
  • Loading branch information
ljharb committed Jul 30, 2015
1 parent a7981c3 commit 2367e09
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion es6-shim.js
Expand Up @@ -144,7 +144,15 @@
}());

/*jshint evil: true */
var getGlobal = new Function('return this;');
var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
};
/*jshint evil: false */

var globals = getGlobal();
Expand Down

5 comments on commit 2367e09

@sandstrom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb Awesome! ⛵

(I think you can remove the /*jshint evil: true/false */ comments now)

@ljharb
Copy link
Collaborator Author

@ljharb ljharb commented on 2367e09 Aug 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, true

@ljharb
Copy link
Collaborator Author

@ljharb ljharb commented on 2367e09 Aug 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2f9f81c

@rsp
Copy link

@rsp rsp commented on 2367e09 Mar 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add few more checks to avoid false positives when we have a self or window global variable that is not the global object itself like I do here in the-global-object:

const getGlobalObject = () => {
  if (typeof self !== 'undefined' && self.self === self &&
    self.Array === Array && self.setInterval === setInterval) {
    return self;
  }
  if (typeof window !== 'undefined' && window.window === window &&
    window.Array === Array && window.setInterval === setInterval) {
    return window;
  }
  if (typeof global !== 'undefined' && global.global === global &&
    global.Array === Array && global.setInterval === setInterval) {
    return global;
  }
  throw new Error('Cannot find the global object');
};

It checks for circular reference that should be there for the real global object and the presence and correct values of Array and setInterval properties, which are arbitrarily chosen but unlikely to be there by accident.

See https://github.com/rsp/node-the-global-object/blob/master/index.js

Hopefully, this will not be needed when tc39/proposal-global#12 is universally supported, which I'm looking forward tp.

@ljharb
Copy link
Collaborator Author

@ljharb ljharb commented on 2367e09 Mar 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is largely unnecessary, since shims need to be loaded before any other code to work properly.

Please sign in to comment.