-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1e8140
commit 1cd4339
Showing
8 changed files
with
110 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
var esprima = require('esprima') | ||
|
||
/** | ||
* remove `new Function`, `(function` and `eval` from `str` | ||
* @param {String} str | ||
* @return {String} | ||
*/ | ||
module.exports = function sanitize (str) { | ||
var tokens = esprima.tokenize(str) | ||
// console.log(tokens) | ||
var i = 0 | ||
while (i < tokens.length - 1) { | ||
var arr = tokens.slice(i, i + 2) | ||
var x = arr[0] | ||
var y = arr[1] | ||
if ((x.type === 'Identifier' && x.value === 'eval')) { | ||
tokens.splice(i, 1) | ||
} else if ( | ||
(x.type === 'Punctuator' && y.type === 'Keyword' && /Function/i.test(y.value)) || | ||
(y.type === 'Identifier' && y.value === 'eval') | ||
) { | ||
tokens.splice(i + 1, 1) | ||
} else if ( | ||
(x.type === 'Keyword' && x.value === 'new' && | ||
y.type === 'Identifier' && /Function/i.test(y.value)) || | ||
(x.type === 'Punctuator' && x.value === '(' && | ||
y.type === 'Punctuator' && y.value === ')') | ||
) { | ||
tokens.splice(i, 2) | ||
} | ||
i++ | ||
} | ||
return tokens.map(function (t) { | ||
var sp = (t.type === 'Keyword') ? ' ' : '' | ||
return t.value + sp | ||
}).join('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint no-new-func: 0 */ | ||
/* global describe, it */ | ||
|
||
'use strict' | ||
|
||
var assert = require('assert') | ||
var sanitize = require('../lib/internal/sanitize') | ||
|
||
describe('#sanitize', function () { | ||
it('should remove "function"', function () { | ||
var res = sanitize('(function(){console.log(`exploited`)}))()') | ||
var exp = '({console.log(`exploited`)}))' | ||
assert.equal(res, exp) | ||
}) | ||
it('should remove "new Function"', function () { | ||
var res = sanitize('new Function(console.log(`exploited`))()') | ||
var exp = '(console.log(`exploited`))' | ||
assert.equal(res, exp) | ||
}) | ||
it('should remove "eval"', function () { | ||
var res = sanitize('(\n\tfunction(){eval(\'console.log(`exploited`)\') })()') | ||
var exp = "({('console.log(`exploited`)')})" | ||
assert.equal(res, exp) | ||
}) | ||
it('should not join new Date', function () { | ||
var res = sanitize('new Date("1970-01-01T"00:00:00)') | ||
var exp = 'new Date("1970-01-01T"00:00:00)' | ||
assert.equal(res, exp) | ||
}) | ||
}) |