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

Cross-platforms method to enumerate all globals? #1240

Closed
gardhr opened this Issue Jun 20, 2018 · 9 comments

Comments

Projects
None yet
5 participants
@gardhr

gardhr commented Jun 20, 2018

I'm currently working on a project to allow sandboxed loading of scripts. The basic approach is to selectively declare "shadow" variables for defined globals and native types. The problem is I'm not sure how to do this in the most generic way across all platforms. My current naive attempt is essentially:

`

    var namespaces = [this];

    if (typeof global != "undefined") namespaces.push(global);

    if (typeof window != "undefined") namespaces.push(window);

    var globals = [

      "Array",

      "RegExp",

      "Symbol",

      "Object",

      "Number",

      "Boolean",

      "String",

      "Function",

      "Date",

      "Error",

      "Iterator",

      "JSON",

      "Math",

      "eval"

    ];

    for (var index = 0; index < namespaces.length; ++index)

      globals = globals.concat(Object.keys(namespaces[index]));

`
That seems to work okay so far, but it's just too easy for things to fall through the cracks. Is there a more generic method to do this that is guaranteed to catch all possible definitions?

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb Jun 20, 2018

Member

The globals are Object.getOwnPropertyNames(global) where global is Function(‘return this’)() or the equivalent. I’m not sure what you’re asking for.

Separately, if you’re looking into sandboxing, you may want to look into prior art: Caja and SES.

Member

ljharb commented Jun 20, 2018

The globals are Object.getOwnPropertyNames(global) where global is Function(‘return this’)() or the equivalent. I’m not sure what you’re asking for.

Separately, if you’re looking into sandboxing, you may want to look into prior art: Caja and SES.

@gardhr

This comment has been minimized.

Show comment
Hide comment
@gardhr

gardhr Jun 20, 2018

@ljharb Thank you so much, precisely what I was looking for! And yes, somewhat familiar with Caja and SES but this is more of a "safe eval" type of project. Hadn't found anything yet along those lines so I decided to take a stab at it myself. Anyhow, thanks again for the prompt, helpful response. Cheers!

gardhr commented Jun 20, 2018

@ljharb Thank you so much, precisely what I was looking for! And yes, somewhat familiar with Caja and SES but this is more of a "safe eval" type of project. Hadn't found anything yet along those lines so I decided to take a stab at it myself. Anyhow, thanks again for the prompt, helpful response. Cheers!

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Jun 20, 2018

Contributor

Object.getOwnPropertyNames(global) is as close as you'll get I think, but it will only enumerate all var-scoped globals like var and function declarations, it will not enumerate lexical globals, so it won't catch let/const/class declarations in the global scope.

Whether or not that matters probably depends on the details of what you're doing, but it is worth mentioning at least.

Contributor

loganfsmyth commented Jun 20, 2018

Object.getOwnPropertyNames(global) is as close as you'll get I think, but it will only enumerate all var-scoped globals like var and function declarations, it will not enumerate lexical globals, so it won't catch let/const/class declarations in the global scope.

Whether or not that matters probably depends on the details of what you're doing, but it is worth mentioning at least.

@gardhr

This comment has been minimized.

Show comment
Hide comment
@gardhr

gardhr Jun 20, 2018

@loganfsmyth True, but as far as this project goes I really just want to provide a mechanism to prevent scripts from accessing/modifying properties of native types and platform-specific globals. Thank you for pointing that out though, definitely worth noting and I'll be sure to mention that in the documentation nonetheless.

gardhr commented Jun 20, 2018

@loganfsmyth True, but as far as this project goes I really just want to provide a mechanism to prevent scripts from accessing/modifying properties of native types and platform-specific globals. Thank you for pointing that out though, definitely worth noting and I'll be sure to mention that in the documentation nonetheless.

@ljharb ljharb added the question label Jun 20, 2018

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb Jun 20, 2018

Member

Closing; but we can continue discussing as needed.

Member

ljharb commented Jun 20, 2018

Closing; but we can continue discussing as needed.

@ljharb ljharb closed this Jun 20, 2018

@robpalme

This comment has been minimized.

Show comment
Hide comment
@robpalme

robpalme Jun 20, 2018

robpalme commented Jun 20, 2018

@IgnoredAmbience

This comment has been minimized.

Show comment
Hide comment
@IgnoredAmbience

IgnoredAmbience Jun 20, 2018

Member

The source code for Caja/SES is here, it is well worth reading to get an understanding for many of the JS edge cases that permit sandbox escapes, the startSES.js file is an appropriate place to begin reading. The codebase is well-commented.

Member

IgnoredAmbience commented Jun 20, 2018

The source code for Caja/SES is here, it is well worth reading to get an understanding for many of the JS edge cases that permit sandbox escapes, the startSES.js file is an appropriate place to begin reading. The codebase is well-commented.

@gardhr

This comment has been minimized.

Show comment
Hide comment
@gardhr

gardhr Jun 20, 2018

@robpalme Hmm, I can't seem to produce anything meaningful from that. Would you mind providing a working example?

EDIT: Also, in the above case would it be sufficient to simply call Object.freeze([].__proto__) ?

gardhr commented Jun 20, 2018

@robpalme Hmm, I can't seem to produce anything meaningful from that. Would you mind providing a working example?

EDIT: Also, in the above case would it be sufficient to simply call Object.freeze([].__proto__) ?

@gardhr

This comment has been minimized.

Show comment
Hide comment
@gardhr

gardhr Jun 20, 2018

@IgnoredAmbience Thanks, I'll have a look at that!

gardhr commented Jun 20, 2018

@IgnoredAmbience Thanks, I'll have a look at that!

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