-
Notifications
You must be signed in to change notification settings - Fork 65
/
policy-generator.js
241 lines (217 loc) · 6.88 KB
/
policy-generator.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { createModuleInspector } from 'lavamoat-core'
import { isBuiltin as nodeIsBuiltin } from 'node:module'
import { PolicyGeneratorContext } from '../policy-gen/policy-generator-context.js'
import { defaultReadPower } from '../power.js'
import { LMRCache } from './lmr-cache.js'
const { entries } = Object
/**
* Options for {@link PolicyGenerator} and {@link PolicyGenerator.generatePolicy}
*
* @typedef PolicyGeneratorOptions
* @property {import('@endo/compartment-mapper').ReadFn} [read]
* @property {import('lavamoat-core').LavaMoatPolicyOverrides} [policyOverride]
*/
/**
* Service which generates a LavaMoat policy from a
*/
export class PolicyGenerator {
/**
* Cache of `LavamoatModuleRecord` objects
*
* @type {Readonly<LMRCache>}
*/
#lmrCache
/**
* Creates {@link PolicyGeneratorContext} instances for each compartment in
* `compartmentMap`.
*
* @param {Readonly<
* import('@endo/compartment-mapper').CompartmentMapDescriptor
* >} compartmentMap
* @param {Readonly<import('@endo/compartment-mapper').Sources>} sources
* @param {Readonly<Record<string, string>>} renames
* @param {PolicyGeneratorOptions} opts
*/
constructor(
compartmentMap,
sources,
renames,
{ read = defaultReadPower, policyOverride } = {}
) {
/**
* Compartment sources
*
* @type {Readonly<import('@endo/compartment-mapper').Sources>}
* @internal
*/
this.sources = sources
/**
* Compartment map
*
* @type {Readonly<
* import('@endo/compartment-mapper').CompartmentMapDescriptor
* >}
* @internal
*/
this.compartmentMap = compartmentMap
/**
* Cache of `LavamoatModuleRecord` objects
*/
this.#lmrCache = new LMRCache()
const entryCompartment =
compartmentMap.compartments[compartmentMap.entry.compartment]
/**
* Mapping of compartment names to {@link PolicyGeneratorContext} instances
*
* @type {Readonly<Map<string, PolicyGeneratorContext>>}
* @internal
*/
this.contexts = new Map(
entries(compartmentMap.compartments).map(
([compartmentName, compartment]) => [
compartmentName,
new PolicyGeneratorContext(compartment, renames, this.#lmrCache, {
isEntry: entryCompartment === compartment,
readFn: read,
}),
]
)
)
/**
* Override policy, if any
*
* @type {Readonly<
* import('lavamoat-core').LavaMoatPolicyOverrides | undefined
* >}
* @internal
*/
this.policyOverride = policyOverride
}
/**
* Builds `LavamoatModuleRecord` objects from the `CompartmentMapDescriptor`
*
* @returns {Promise<import('lavamoat-core').LavamoatModuleRecord[]>} Module
* records
* @internal
*/
async buildModuleRecords() {
await Promise.resolve()
let moduleRecords =
/** @type {import('lavamoat-core').LavamoatModuleRecord[]} */ (
(
await Promise.all(
entries(this.sources).map(
async ([compartmentName, compartmentSources]) => {
if (!this.contexts.has(compartmentName)) {
// this means that the compartment with this name was not actually used
return
}
const compartment = /** @type {PolicyGeneratorContext} */ (
this.contexts.get(compartmentName)
)
return compartment.buildModuleRecords(compartmentSources)
}
)
)
)
.flat()
.filter(Boolean)
)
moduleRecords = [...new Set(moduleRecords)]
this.#lmrCache.clear()
return moduleRecords
}
/**
* Uses `inspector` to inspect a compartment map and sources.
*
* @param {import('lavamoat-core').ModuleInspector} inspector Module inspector
* @param {import('lavamoat-core').LavamoatModuleRecord[]} moduleRecords
* Module records
* @returns {import('lavamoat-core').ModuleInspector} The inspector
* @internal
*/
inspectModuleRecords(inspector, moduleRecords) {
// FIXME: should we sort here?
for (const record of moduleRecords) {
inspector.inspectModule(record)
}
return inspector
}
/**
* Generates a LavaMoat policy
*
* Policy generation occurs in three (3) steps:
*
* 1. Build module records from the `CompartmentMapDescriptor` and associated
* `Sources`
* 2. Inspect the module records using LavaMoat's `ModuleInspector`
* 3. Generate the policy using the `ModuleInspector`
*
* @param {boolean} [debug] - If `true`, the result will be a debug policy
* @returns {Promise<
* | import('lavamoat-core').LavaMoatPolicy
* | import('lavamoat-core').LavaMoatPolicyDebug
* >}
* Generated policy
* @internal
* @todo Overload this to handle the `debug` parameter
*/
async generatePolicy(debug) {
const moduleRecords = await this.buildModuleRecords()
const inspector = createModuleInspector({
isBuiltin: nodeIsBuiltin,
includeDebugInfo: debug,
})
return this.inspectModuleRecords(inspector, moduleRecords).generatePolicy({
policyOverride: this.policyOverride,
})
}
/**
* Instantiates a {@link PolicyGenerator} and generates a policy.
*
* @overload
* @param {Readonly<
* import('@endo/compartment-mapper').CompartmentMapDescriptor
* >} compartmentMap
* @param {Readonly<import('@endo/compartment-mapper').Sources>} sources
* @param {Readonly<Record<string, string>>} renames
* @param {PolicyGeneratorOptions} [opts]
* @returns {Promise<import('lavamoat-core').LavaMoatPolicy>} Generated policy
*/
/**
* Instantiates a {@link PolicyGenerator} and generates a debug policy.
*
* @overload
* @param {Readonly<
* import('@endo/compartment-mapper').CompartmentMapDescriptor
* >} compartmentMap
* @param {Readonly<import('@endo/compartment-mapper').Sources>} sources
* @param {Readonly<Record<string, string>>} renames
* @param {PolicyGeneratorOptions & { debug: true }} opts
* @returns {Promise<import('lavamoat-core').LavaMoatPolicyDebug>} Generated
* debug policy
* @public
*/
/**
* Instantiates a {@link PolicyGenerator} and generates a policy.
*
* @param {Readonly<
* import('@endo/compartment-mapper').CompartmentMapDescriptor
* >} compartmentMap
* @param {Readonly<import('@endo/compartment-mapper').Sources>} sources
* @param {Readonly<Record<string, string>>} renames
* @param {PolicyGeneratorOptions & { debug?: boolean }} [opts]
* @returns {Promise<import('lavamoat-core').LavaMoatPolicy>} Generated policy
* @public
*/
static async generatePolicy(compartmentMap, sources, renames, opts) {
const { debug, ...restOpts } = opts ?? {}
const generator = new PolicyGenerator(
compartmentMap,
sources,
renames,
restOpts
)
return generator.generatePolicy(debug)
}
}