Skip to content

Commit

Permalink
Merge pull request #4 from commenthol/issue-non-js-identifiers
Browse files Browse the repository at this point in the history
Ignore non js identifiers
  • Loading branch information
commenthol committed Feb 27, 2019
2 parents 5373e09 + 3325b7f commit b99ddfa
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 164 deletions.
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

0 comments on commit b99ddfa

Please sign in to comment.