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

Ignore non js identifiers #4

Merged
merged 2 commits into from Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.yml
@@ -1 +1,3 @@
extends: standard
env:
mocha: true
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -47,15 +47,15 @@
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"karma": "^3.1.4",
"karma": "^4.0.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-firefox-launcher": "^1.0.0",
"karma-mocha": "^1.3.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "~0.0.32",
"karma-webpack": "^3.0.5",
"mocha": "^5.2.0",
"mocha": "^6.0.2",
"nyc": "^13.1.0",
"rimraf": "^2.5.4",
"webpack": "^4.28.3",
Expand Down
28 changes: 19 additions & 9 deletions src/common.js
Expand Up @@ -8,6 +8,11 @@ exports.hasWindow = hasWindow
const hasGlobal = (typeof global !== 'undefined')
exports.hasGlobal = hasGlobal

const NON_IDENTIFIER = /^\d|-|^(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with|class|const|enum|export|extends|import|super|implements|interface|let|package|private|protected|public|static|yield|null|true|false)$/

const isIdentifier = key => !NON_IDENTIFIER.test(key)
exports.isIdentifier = isIdentifier

/**
* create a fresh context where nearly nothing is allowed
* @private
Expand All @@ -28,20 +33,23 @@ exports.createContext = function () {
Function: undefined
}

const fillContext = (root) => {
Object.keys(root).forEach(key => {
if (isIdentifier(key)) {
context[key] = undefined
}
})
}

// locally define all potential global vars
if (hasGlobal) {
Object.keys(global).forEach(function (key) {
context[key] = undefined
})
fillContext(global)
cloneFunctions(context)
context.Buffer = _protect('Buffer')
context.console = clones(console, console) // console needs special treatment
}
if (hasWindow) {
Object.keys(window).forEach(function (key) {
context[key] = undefined
})

fillContext(window, true)
cloneFunctions(context)
protectBuiltInObjects(context)
context.console = clones(console, console) // console needs special treatment
Expand All @@ -56,8 +64,10 @@ exports.createContext = function () {
* @private
*/
exports.allow = function (context, newContext) {
Object.keys(context || {}).forEach(function (key) {
newContext[key] = context[key] // this is harmful - objects can be overwritten
Object.keys(context || {}).forEach(key => {
if (isIdentifier(key)) {
newContext[key] = context[key] // this is harmful - objects can be overwritten
}
})
}

Expand Down