@@ -9,12 +9,6 @@ const localProp = 1
9
9
10
10
describe ( 'nx-compile' , ( ) => {
11
11
describe ( 'compileCode' , ( ) => {
12
- it ( 'should execute code in a sandbox' , ( ) => {
13
- const code = compiler . compileCode ( 'return prop1 + prop2' )
14
- const value = code ( { prop1 : 1 , prop2 : 2 } )
15
- expect ( value ) . to . equal ( 3 )
16
- } )
17
-
18
12
it ( 'should throw TypeError on invalid source argument' , ( ) => {
19
13
expect ( ( ) => compiler . compileCode ( { } ) ) . to . throw ( TypeError )
20
14
expect ( ( ) => compiler . compileCode ( ) ) . to . throw ( TypeError )
@@ -23,12 +17,6 @@ describe('nx-compile', () => {
23
17
} )
24
18
25
19
describe ( 'compileExpression' , ( ) => {
26
- it ( 'should execute expression in a sandbox' , ( ) => {
27
- const expression = compiler . compileExpression ( 'prop1 + prop2' )
28
- const value = expression ( { prop1 : 1 , prop2 : 2 } )
29
- expect ( value ) . to . equal ( 3 )
30
- } )
31
-
32
20
it ( 'should throw TypeError on invalid source argument' , ( ) => {
33
21
expect ( ( ) => compiler . compileExpression ( { } ) ) . to . throw ( TypeError )
34
22
expect ( ( ) => compiler . compileExpression ( ) ) . to . throw ( TypeError )
@@ -37,6 +25,31 @@ describe('nx-compile', () => {
37
25
} )
38
26
39
27
describe ( 'returned function (compiled code or expression)' , ( ) => {
28
+ it ( 'should execute in a sandbox' , ( ) => {
29
+ const expression = compiler . compileExpression ( 'prop1 + prop2' )
30
+ const value = expression ( { prop1 : 1 , prop2 : 2 } )
31
+ expect ( value ) . to . equal ( 3 )
32
+ } )
33
+
34
+ it ( 'should not expose globals to the sandbox' , ( ) => {
35
+ const expression = compiler . compileExpression ( 'prop1' )
36
+ const value = expression ( { } )
37
+ expect ( value ) . to . equal ( undefined )
38
+ } )
39
+
40
+ it ( 'should not expose globals inside functions defined in the passed code' , ( ) => {
41
+ const rawCode = '({}).__proto__.evil = function() { return prop1 + prop2 }'
42
+ const code = compiler . compileCode ( rawCode )
43
+ code ( { prop1 : 1 , prop2 : 2 } )
44
+ expect ( ( { } ) . evil ( ) ) . to . equal ( 3 )
45
+ } )
46
+
47
+ it ( '"this" should be the sandbox instead of the global object' , ( ) => {
48
+ const expression = compiler . compileExpression ( 'this.prop1 + this.prop2' )
49
+ const value = expression ( { prop1 : 1 , prop2 : 2 } , [ ] )
50
+ expect ( value ) . to . equal ( 3 )
51
+ } )
52
+
40
53
it ( 'should expose specified globals to the sandbox' , ( ) => {
41
54
const expression = compiler . compileExpression ( 'prop1 + prop2' )
42
55
const value = expression ( { } , [ 'prop1' , 'prop2' ] )
0 commit comments