/
generatePolicy.js
557 lines (528 loc) · 17.1 KB
/
generatePolicy.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// @ts-check
const EventEmitter = require('node:events')
const path = require('node:path')
const jsonStringify = require('json-stable-stringify')
const {
parse,
inspectGlobals,
inspectImports,
inspectSesCompat,
codeSampleFromAstNode,
utils: {
mergePolicy: mergeGlobalsPolicy,
mapToObj,
reduceToTopmostApiCallsFromStrings,
},
inspectEsmImports,
// @ts-expect-error no types yet
} = require('lavamoat-tofu')
const { mergePolicy } = require('./mergePolicy')
const rootSlug = '$root$'
/**
* Symbols that look like globals but aren't; indexed by source type.
*/
const MODULE_REFS = /** @type {const} */ ({
module: ['arguments', 'import', 'export'],
script: ['arguments', 'require', 'module', 'exports'],
})
module.exports = { rootSlug, createModuleInspector, getDefaultPaths }
/**
* @param {ModuleInspectorOptions} opts
* @returns {ModuleInspector}
*/
function createModuleInspector(opts) {
const moduleIdToModuleRecord = new Map()
// "packageToModules" does not include builtin modules
const packageToModules = new Map()
const packageToGlobals = new Map()
/** @type {Map<string, string[]>} */
const packageToBuiltinImports = new Map()
const packageToNativeModules = new Map()
/** @type {Record<string, import('./schema').DebugInfo>} */
const debugInfo = {}
/** @type {ModuleInspector} */
const inspector = Object.assign(new EventEmitter(), {
/** @type {InspectModuleFn} */
inspectModule: (moduleRecord, opts2) => {
inspectModule(moduleRecord, { ...opts, ...opts2 })
},
/** @type {GeneratePolicyFn} */
generatePolicy: (opts2) => {
return generatePolicy({ ...opts, ...opts2 })
},
})
return inspector
/**
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {ModuleInspectorOptions} opts
*/
function inspectModule(
moduleRecord,
{ isBuiltin, includeDebugInfo = false }
) {
const { packageName, specifier, type } = moduleRecord
// record the module
moduleIdToModuleRecord.set(specifier, moduleRecord)
// call the correct analyzer for the module type
switch (type) {
case 'builtin': {
inspectBuiltinModule(moduleRecord, { includeDebugInfo })
return
}
case 'native': {
inspectNativeModule(moduleRecord, { includeDebugInfo })
return
}
case 'js': {
inspectJsModule(moduleRecord, { isBuiltin, includeDebugInfo })
return
}
default: {
const errMsg = `LavaMoat - unknown module type "${type}" for package "${packageName}" module "${specifier}"`
throw new Error(errMsg)
}
}
}
/**
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {Partial<ModuleInspectorOptions>} opts
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function inspectBuiltinModule(moduleRecord, opts) {
// builtins themselves do not require any configuration
// packages that import builtins need to add that to their configuration
}
/**
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {Partial<ModuleInspectorOptions>} opts
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function inspectNativeModule(moduleRecord, opts) {
// LavaMoat does attempt to sandbox native modules
// packages with native modules need to specify that in the policy file
const { packageName } = moduleRecord
if (!packageToNativeModules.has(packageName)) {
packageToNativeModules.set(packageName, [])
}
const packageNativeModules = packageToNativeModules.get(packageName)
packageNativeModules.push(moduleRecord)
}
/**
* @param {AST} ast
* @returns {ast is import('@babel/parser').ParseResult<import('@babel/types').File>}
*/
function isParsedAST(ast) {
return 'errors' in ast
}
/**
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {ModuleInspectorOptions} opts
*/
function inspectJsModule(
moduleRecord,
{ isBuiltin, includeDebugInfo = false }
) {
const { packageName, specifier } = moduleRecord
let moduleDebug
// record the module
moduleIdToModuleRecord.set(specifier, moduleRecord)
// initialize mapping from package to module
if (!packageToModules.has(packageName)) {
packageToModules.set(packageName, new Map())
}
const packageModules = packageToModules.get(packageName)
packageModules[specifier] = moduleRecord
// initialize module debug info
if (includeDebugInfo) {
moduleDebug = debugInfo[specifier] = /** @type {any} */ ({})
// append moduleRecord, ensure ast is not copied
const debugData = {
...moduleRecord,
}
delete debugData.ast
moduleDebug.moduleRecord = debugData
}
// skip for root modules (modules not from deps)
const isRootModule = packageName === rootSlug
if (isRootModule) {
return
}
// skip json files
const filename = moduleRecord.file || 'unknown'
const fileExtension = path.extname(filename)
if (!fileExtension.match(/^\.([cm]?js|ts)$/)) {
return
}
// get ast (parse or use cached)
/**
* @type {AST}
* @todo - Put this in `LavamoatModuleRecord` instead
*/
const ast =
moduleRecord.ast ||
parse(moduleRecord.content, {
// esm support
sourceType: 'unambiguous',
// someone must have been doing this
allowReturnOutsideFunction: true,
errorRecovery: true,
})
if (includeDebugInfo && isParsedAST(ast) && ast.errors.length) {
moduleDebug.parseErrors = ast.errors
}
// ensure ses compatibility
inspectForEnvironment(ast, moduleRecord, includeDebugInfo)
// get global usage
inspectForGlobals(ast, moduleRecord, packageName, includeDebugInfo)
// get builtin package usage
inspectForImports(
ast,
moduleRecord,
packageName,
isBuiltin,
includeDebugInfo
)
// ensure module ast is cleaned up
delete moduleRecord.ast
}
/**
* @param {AST} ast
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {boolean} includeDebugInfo
* @returns
*/
function inspectForEnvironment(ast, moduleRecord, includeDebugInfo) {
const { packageName } = moduleRecord
const compatWarnings = inspectSesCompat(ast)
/** @type {import('./schema').SesCompat} */
const { primordialMutations, strictModeViolations, dynamicRequires } =
compatWarnings
const hasResults =
primordialMutations.length > 0 ||
strictModeViolations.length > 0 ||
dynamicRequires.length > 0
if (!hasResults) {
return
}
if (includeDebugInfo) {
const moduleDebug = debugInfo[moduleRecord.specifier]
moduleDebug.sesCompat = {
// FIXME: I don't think this is needed, since it appears we overwrite all properties
...compatWarnings,
// fix serialization
primordialMutations: primordialMutations.map(({ node: { loc } }) => ({
node: { loc },
})),
strictModeViolations: strictModeViolations.map(({ node: { loc } }) => ({
node: { loc },
})),
dynamicRequires: dynamicRequires.map(({ node: { loc } }) => ({
node: { loc },
})),
}
} else {
// warn if non-compatible code found
if (inspector.listenerCount('compat-warning') > 0) {
inspector.emit('compat-warning', { moduleRecord, compatWarnings })
} else {
const samples = jsonStringify({
primordialMutations: primordialMutations.map(({ node }) =>
codeSampleFromAstNode(node, moduleRecord)
),
strictModeViolations: strictModeViolations.map(({ node }) =>
codeSampleFromAstNode(node, moduleRecord)
),
dynamicRequires: dynamicRequires.map(({ node }) =>
codeSampleFromAstNode(node, moduleRecord)
),
})
const errMsg = `Incompatible code detected in package "${packageName}" file "${moduleRecord.file}". Violations:\n${samples}`
console.warn(errMsg)
}
}
}
/**
* @param {AST} ast
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {string} packageName
* @param {boolean} includeDebugInfo
*/
function inspectForGlobals(ast, moduleRecord, packageName, includeDebugInfo) {
const moduleRefs = MODULE_REFS[ast.program.sourceType]
const globalObjPrototypeRefs = Object.getOwnPropertyNames(Object.prototype)
const foundGlobals = inspectGlobals(ast, {
// browserify commonjs scope
ignoredRefs: [...moduleRefs, ...globalObjPrototypeRefs],
// browser global refs + browserify global
globalRefs: ['globalThis', 'self', 'window', 'global'],
})
// skip if no results
if (!foundGlobals.size) {
return
}
// add debug info
if (includeDebugInfo) {
const moduleDebug = debugInfo[moduleRecord.specifier]
moduleDebug.globals = mapToObj(foundGlobals)
}
// agregate globals
if (!packageToGlobals.has(packageName)) {
packageToGlobals.set(packageName, [])
}
let packageGlobals = packageToGlobals.get(packageName)
packageGlobals = mergeGlobalsPolicy(packageGlobals, foundGlobals)
packageToGlobals.set(packageName, packageGlobals)
}
/**
* @param {AST} ast
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {string} packageName
* @param {(value: string) => boolean} isBuiltin
* @param {boolean} includeDebugInfo
* @returns
*/
function inspectForImports(
ast,
moduleRecord,
packageName,
isBuiltin,
includeDebugInfo
) {
// get all requested names that resolve to isBuiltin
const namesForBuiltins = Object.entries(moduleRecord.importMap)
.filter(([, resolvedName]) => isBuiltin(resolvedName))
.map(([requestedName]) => requestedName)
const esmModuleBuiltins = inspectEsmImports(ast, namesForBuiltins)
const { cjsImports: cjsModuleBuiltins } = inspectImports(
ast,
namesForBuiltins
)
if (cjsModuleBuiltins.length + esmModuleBuiltins.length === 0) {
return
}
// add debug info
if (includeDebugInfo) {
const moduleDebug = debugInfo[moduleRecord.specifier]
moduleDebug.builtin = [
...new Set([...esmModuleBuiltins, ...cjsModuleBuiltins]),
]
}
// aggregate package builtins
if (!packageToBuiltinImports.has(packageName)) {
packageToBuiltinImports.set(packageName, [])
}
let packageBuiltins = packageToBuiltinImports.get(packageName) ?? []
packageBuiltins = [
...new Set([
...packageBuiltins,
...cjsModuleBuiltins,
...esmModuleBuiltins,
]),
]
packageToBuiltinImports.set(packageName, packageBuiltins)
}
/**
* @type {GeneratePolicyFn}
*/
function generatePolicy({ policyOverride, includeDebugInfo = false }) {
/** @type {import('./schema').Resources} */
const resources = {}
/**
* @type {import('./schema').LavaMoatPolicyDebug
* | import('./schema').LavaMoatPolicy}
*/
const policy = { resources }
packageToModules.forEach((packageModules, packageName) => {
// the policy fields for each package
/** @type {import('./schema').ResourcePolicy['globals']} */
let globals
/** @type {import('./schema').ResourcePolicy['builtin']} */
let builtin
/** @type {import('./schema').ResourcePolicy['packages']} */
let packages
/** @type {import('./schema').ResourcePolicy['native']} */
let native
// skip for root modules (modules not from deps)
const isRootModule = packageName === rootSlug
if (isRootModule) {
return
}
// get dependencies, ignoring builtins
const packageDeps = aggregateDeps({
packageModules,
moduleIdToModuleRecord,
})
if (packageDeps.length) {
packages = Object.fromEntries(
packageDeps.map((depPackageName) => [depPackageName, true])
)
}
// get globals
if (packageToGlobals.has(packageName)) {
const globalMap = mapToObj(packageToGlobals.get(packageName))
// prefer "true" over "read" for clearer difference between
// read/write syntax highlighting
Object.keys(globalMap).forEach((key) => {
if (globalMap[key] === 'read') {
globalMap[key] = true
}
})
globals = globalMap
}
// get builtin imports
const builtinImports = packageToBuiltinImports.get(packageName)
if (builtinImports && builtinImports.length) {
/** @type {Record<string, boolean>} */
const importBuiltin = {}
const topmostApiCalls = /** @type {string[]} */ (
reduceToTopmostApiCallsFromStrings(builtinImports)
)
topmostApiCalls.forEach((path) => {
importBuiltin[path] = true
})
builtin = importBuiltin
}
// get native modules
native = packageToNativeModules.has(packageName)
// skip package policy if there are no settings needed
if (!packages && !globals && !builtin) {
return
}
// create minimal policy object
const packagePolicy = {}
if (packages) {
packagePolicy.packages = packages
}
if (globals) {
packagePolicy.globals = globals
}
if (builtin) {
packagePolicy.builtin = builtin
}
if (native) {
packagePolicy.native = native
}
// set policy for package
resources[packageName] = packagePolicy
})
// append serializeable debug info
if (includeDebugInfo) {
// this is here because we should be using semicolons :D
// prettier-ignore
;(/** @type {import('./schema').LavaMoatPolicyDebug} */(policy).debugInfo = debugInfo)
}
// merge override policy
const mergedPolicy = mergePolicy(policy, policyOverride)
return mergedPolicy
}
}
/**
* @param {{
* packageModules: Record<
* string,
* import('./moduleRecord').LavamoatModuleRecord
* >
* moduleIdToModuleRecord: Map<
* string,
* import('./moduleRecord').LavamoatModuleRecord
* >
* }} opts
* @returns
*/
function aggregateDeps({ packageModules, moduleIdToModuleRecord }) {
const deps = new Set()
// get all dep package from the "packageModules" collection of modules
Object.values(packageModules).forEach((moduleRecord) => {
Object.entries(moduleRecord.importMap).forEach(
([requestedName, specifier]) => {
// skip entries where resolution was skipped
if (!specifier) {
return
}
// get packageName from module record, or guess
const moduleRecord = moduleIdToModuleRecord.get(specifier)
if (moduleRecord) {
// builtin modules are ignored here, handled elsewhere
if (moduleRecord.type === 'builtin') {
return
}
deps.add(moduleRecord.packageName)
return
}
// moduleRecord missing, guess package name
const packageName = guessPackageName(requestedName)
deps.add(packageName)
}
)
// ensure the package is not listed as its own dependency
deps.delete(moduleRecord.packageName)
})
// return as array
const depsArray = Array.from(deps.values())
return depsArray
}
/**
* For when you encounter a `requestedName` that was not inspected, likely
* because resolution was skipped for that module
*
* @param {string} requestedName
* @returns {string}
*/
function guessPackageName(requestedName) {
const isNotPackageName =
requestedName.startsWith('/') || requestedName.startsWith('.')
if (isNotPackageName) {
return `<unknown:${requestedName}>`
}
// resolving is skipped so guess package name
const pathParts = requestedName.split('/')
const nameSpaced = requestedName.startsWith('@')
const packagePartCount = nameSpaced ? 2 : 1
const packageName = pathParts.slice(0, packagePartCount).join('/')
return packageName
}
/**
* @param {string} policyName
* @returns
*/
function getDefaultPaths(policyName) {
const policiesDir = 'lavamoat'
const policyDir = path.join(policiesDir, policyName)
return {
policiesDir,
policyDir,
primary: path.join(policyDir, 'policy.json'),
override: path.join(policyDir, 'policy-override.json'),
debug: path.join(policyDir, 'policy-debug.json'),
}
}
/**
* @callback GeneratePolicyFn
* @param {Partial<ModuleInspectorOptions> & {
* policyOverride?: import('./schema').LavaMoatPolicyOverrides
* }} opts
* @returns {import('./schema').LavaMoatPolicy
* | import('./schema').LavaMoatPolicyDebug}
*/
/**
* @callback InspectModuleFn
* @param {import('./moduleRecord').LavamoatModuleRecord} moduleRecord
* @param {Partial<ModuleInspectorOptions>} [opts]
*/
/**
* @typedef ModuleInspectorOptions
* @property {(value: string) => boolean} isBuiltin
* @property {boolean} [includeDebugInfo]
*/
/**
* @typedef ModuleInspectorMembers
* @property {GeneratePolicyFn} generatePolicy
* @property {InspectModuleFn} inspectModule
*/
/**
* @typedef {import('node:events').EventEmitter & ModuleInspectorMembers} ModuleInspector
*/
/**
* @typedef {import('@babel/parser').ParseResult<import('@babel/types').File>
* | import('@babel/types').File} AST
*/