-
Notifications
You must be signed in to change notification settings - Fork 65
/
generate-policy.spec.js
109 lines (97 loc) · 1.96 KB
/
generate-policy.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'ses'
import test from 'ava'
import { createGeneratePolicyMacros } from './macros.js'
const { testPolicyForModule, testPolicyForScript, testPolicyForJSON } =
createGeneratePolicyMacros(test)
test(testPolicyForJSON, 'builtins.json')
test(testPolicyForJSON, 'kitchen-sink.json')
test(testPolicyForJSON, 'native.json')
test('basic nested global access', testPolicyForModule, 'location.href', {
resources: {
test: {
globals: {
'location.href': true,
},
},
},
})
test(
'CJS-specific ignores',
testPolicyForScript,
`
const js = [this]
const ignored = [global, require, module, exports, arguments]
const globalRefs = [typeof globalThis, typeof self, typeof window]
const xyz = nonIgnoredGlobal
`,
{
resources: {
test: {
globals: {
nonIgnoredGlobal: true,
},
},
},
}
)
test(
'ESM-specific ignores',
testPolicyForModule,
`
const js = [this]
const ignored = [global, require, module, exports, arguments]
const globalRefs = [typeof globalThis, typeof self, typeof window]
const xyz = nonIgnoredGlobal
export {xyz}
`,
{
resources: {
test: {
globals: {
nonIgnoredGlobal: true,
require: true,
module: true,
exports: true,
},
},
},
}
)
test(
'ignored global refs',
testPolicyForModule,
`
const href = window.location.href
const xhr = new window.XMLHttpRequest()
`,
{
resources: {
test: {
globals: {
'location.href': true,
XMLHttpRequest: true,
},
},
},
}
)
test(
'ignored global refs when properties are not accessed',
testPolicyForModule,
'typeof window !== "undefined"',
{ resources: {} }
)
test(
'ignored global refs accessed w/ whitelist items',
testPolicyForModule,
`window.Object === Object`,
{
resources: {},
}
)
test(
'generatePolicy - ignore newer intrinsics',
testPolicyForModule,
'BigInt(123)',
{ resources: {} }
)