File tree Expand file tree Collapse file tree 3 files changed +14
-47
lines changed Expand file tree Collapse file tree 3 files changed +14
-47
lines changed Original file line number Diff line number Diff line change 1
1
var vm = require ( 'vm' )
2
2
3
- function clearContext ( ) {
4
- // eslint-disable-next-line no-global-assign
5
- Function = undefined
6
- const keys = Object . getOwnPropertyNames ( this ) . concat ( [ 'constructor' ] )
7
- keys . forEach ( ( key ) => {
8
- const item = this [ key ]
9
- if ( ! item ) return
10
- if ( typeof Object . getPrototypeOf ( item ) . constructor === 'function' ) {
11
- Object . getPrototypeOf ( item ) . constructor = undefined
12
- }
13
- if ( typeof item . constructor === 'function' ) {
14
- this [ key ] . constructor = undefined
15
- }
16
- } )
17
- }
18
-
19
3
module . exports = function safeEval ( code , context , opts ) {
20
4
var sandbox = { }
21
5
var resultKey = 'SAFE_EVAL_' + Math . floor ( Math . random ( ) * 1000000 )
22
6
sandbox [ resultKey ] = { }
23
- var clearContextCall = `(${ clearContext . toString ( ) } )();`
24
- code = `${ clearContextCall } ${ resultKey } =${ code } `
7
+ var clearContext = `
8
+ (function() {
9
+ Function = undefined;
10
+ const keys = Object.getOwnPropertyNames(this).concat(['constructor']);
11
+ keys.forEach((key) => {
12
+ const item = this[key];
13
+ if (!item || typeof item.constructor !== 'function') return;
14
+ this[key].constructor = undefined;
15
+ });
16
+ })();
17
+ `
18
+ code = clearContext + resultKey + '=' + code
25
19
if ( context ) {
26
20
Object . keys ( context ) . forEach ( function ( key ) {
27
- if ( context [ key ] === Function ) return
28
21
sandbox [ key ] = context [ key ]
29
22
} )
30
23
}
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " safe-eval" ,
3
- "version" : " 0.4.2 " ,
3
+ "version" : " 0.4.1 " ,
4
4
"description" : " Safer version of eval()" ,
5
5
"main" : " index.js" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -44,33 +44,7 @@ describe('safe-eval', function () {
44
44
} )
45
45
} )
46
46
47
- it ( 'should not have access to Node.js objects using context (CWE-265)' , function ( ) {
48
- var code = 'test(\'return process\')()'
49
- assert . throws ( function ( ) {
50
- safeEval ( code , {
51
- // eslint-disable-next-line no-new-func
52
- test : new Function ( ) . constructor
53
- } )
54
- } )
55
- } )
56
-
57
- it ( 'should not have access to Node.js objects using Object.getPrototypeOf (CWE-265)' , function ( ) {
58
- var code = `Object.getPrototypeOf(Object).constructor('return process')();`
59
- assert . throws ( function ( ) {
60
- safeEval ( code )
61
- } )
62
- } )
63
-
64
- it ( 'should not have access to Node.js objects using Object.getPrototypeOf with context (CWE-265)' , function ( ) {
65
- var code = `Object.getPrototypeOf(obj).constructor.constructor("return process")();`
66
- assert . throws ( function ( ) {
67
- safeEval ( code , {
68
- obj : Object
69
- } )
70
- } )
71
- } )
72
-
73
- it ( 'should not have access to Node.js objects using this.constructor (CWE-265)' , function ( ) {
47
+ it ( 'should not have access to Node.js objects (CWE-265)' , function ( ) {
74
48
var code = 'this.constructor.constructor(\'return process\')()'
75
49
assert . throws ( function ( ) {
76
50
safeEval ( code )
You can’t perform that action at this time.
0 commit comments