/
generatePolicy.spec.js
150 lines (140 loc) · 3.84 KB
/
generatePolicy.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-disable no-unused-vars, ava/use-t-well */
const test = require('ava')
const { autoConfigForScenario } = require('./util')
const { createScenarioFromScaffold } = require('lavamoat-core/test/util')
test('generatePolicy - empty policy', async (t) => {
const scenario = createScenarioFromScaffold({
defineEntry: () => {},
defaultPolicy: false,
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, { resources: {} }, 'policy matches expected')
})
test('generatePolicy - basic policy', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
module.exports = global.two
},
defaultPolicy: false,
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {
one: {
globals: {
two: true,
},
},
},
})
})
test('generatePolicy - ignore various refs', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
const js = [this]
// eslint-disable-next-line no-undef
const ignored = [global, require, module, exports, arguments]
const globalRefs = [typeof globalThis, typeof self, typeof window]
global.xyz
},
defaultPolicy: false,
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {
one: {
globals: {
xyz: true,
},
},
},
})
})
test('generatePolicy - policy ignores global refs', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
const href = window.location.href
const xhr = new window.XMLHttpRequest()
},
defaultPolicy: false,
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {
one: {
globals: {
'location.href': true,
XMLHttpRequest: true,
},
},
},
})
})
test('generatePolicy - policy ignores global refs when properties are not accessed', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
// XXX: this is probably wrong. either use `typeof window === 'undefined'` or `window === undefined`
// eslint-disable-next-line valid-typeof
typeof window !== undefined
},
defaultPolicy: false,
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {},
})
})
test('generatePolicy - policy ignores global refs accessed with allowlist items', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
window.Object === Object
},
defaultPolicy: false,
expectedResult: {
resources: {},
},
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {},
})
})
test('generatePolicy - policy endows "process" properly', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
const x = process.nextTick
},
defaultPolicy: false,
files: {
'package.json': {
content: `${JSON.stringify({
dependencies: {
one: '1.0.0',
two: '1.0.0',
three: '1.0.0',
},
// must include browserify so we know to find the builtin deps
devDependencies: {
browserify: '^16.2.3',
},
})}`,
},
},
})
const policy = await autoConfigForScenario({ scenario, log: t.log.bind(t) })
t.deepEqual(policy, {
resources: {
one: {
packages: {
'browserify>process': true,
},
},
'browserify>process': {
globals: {
clearTimeout: true,
setTimeout: true,
},
},
},
})
})