Hide `window` using an AST transform
- Dominant language
- JavaScript
- Stars
- 776
- Forks
- 180
- PR merge metrics
- No merged PRs in 30d
Description
There are certain properties on window that we'd like to hide from users such as `console`, `location`, `document` (at least for processing-js), etc. Using regexes to ban these terms prevents their use in comments and strings. Searching for identifier nodes in the AST with these names is a little better but it's still not perfect because it might make sense to have a `location` property on an object. The proposed solution avoids these inaccuracies by replacing the `window` object with our own object that exposes only those functions/properties we want to expose.
Basic idea: transform
```
function Foo() {
this.bar = "hello, world";
}
```
into
```
function Foo() {
var that = this === window ? myWindow : this;
that.bar = "hello, world";
}
```
In order to deal with someone redefining `that` we should rewrite all user variable references to be properties on a local `scope` variable, e.g. transform
```
function Foo() {
var x = 5;
var y = 10;
}
```
into
```
function Foo() {
var scope = {};
scope.x = 5;
scope.y = 10;
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue names no files, tests, or existing entry points. First locate the JavaScript AST transformation pipeline and its tests, then compare the proposed window replacement and scope-rewriting behavior against current sandboxing. Done means user code can access only the intended window properties without incorrectly changing comments, strings, or object properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- security
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100