/
util.js
730 lines (699 loc) · 19.4 KB
/
util.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// @ts-check
const {
parseForPolicy,
LavamoatModuleRecord,
generateKernel,
getDefaultPaths,
} = require('../src')
const mergeDeep = require('merge-deep')
const { runInContext, createContext } = require('node:vm')
const path = require('node:path')
const tmp = require('tmp-promise')
const stringify = require('json-stable-stringify')
const { applySourceTransforms } = require('../src/sourceTransforms.js')
module.exports = {
generateConfigFromFiles: generatePolicyFromFiles,
createScenarioFromScaffold,
runScenario,
createConfigForTest,
autoConfigForScenario,
prepareScenarioOnDisk,
convertOptsToArgs,
evaluateWithSourceUrl,
createHookedConsole,
fillInFileDetails,
functionToString,
runAndTestScenario,
}
/**
* @typedef {Partial<import('../src/parseForPolicy').ParseForPolicyOpts> & {
* files: import('./scenario').NormalizedScenarioJSFile[]
* }} GeneratePolicyFromFilesOpts
*/
/**
* @param {GeneratePolicyFromFilesOpts} opts
* @returns
*/
async function generatePolicyFromFiles({ files, ...opts }) {
const config = await parseForPolicy({
moduleSpecifier:
/** @type {import('./scenario').NormalizedScenarioJSFile} */ (
files.find((file) => file.entry)
).specifier,
resolveHook: (requestedName, parentAddress) => {
return /** @type {import('./scenario').NormalizedScenarioJSFile} */ (
files.find((file) => file.specifier === parentAddress)
).importMap[requestedName]
},
importHook: async (address) => {
return new LavamoatModuleRecord(
/** @type {import('./scenario').NormalizedBuiltin} */ (
files.find((file) => file.specifier === address)
)
)
},
isBuiltin: () => false,
includeDebugInfo: false,
...opts,
})
return config
}
/**
* Given an incomplete scenario definition, normalize it in preparation for
* running.
*
* @template [Result=unknown] Default is `unknown`
* @param {import('./scenario').Scenario} scenario
* @returns {import('./scenario').NormalizedScenario<{ value: string }>}
*/
function createScenarioFromScaffold({
name = 'template scenario',
expectedResult = {
value: 'this is module two',
},
testType = 'deepEqual',
checkPostRun = async (t, result, err, scenario) => {
if (err) {
await /** @type {import('./scenario').ScenarioCheckErrorFn<Result>} */ (
scenario.checkError
)(t, err, scenario)
} else {
// assumes `result` is not undefined
await /** @type {import('./scenario').ScenarioCheckResultFn<Result>} */ (
scenario.checkResult
)(t, /** @type {Result} */ (result), scenario)
}
},
checkError = async (t, err, scenario) => {
if (scenario.expectedFailure) {
t.truthy(err, `Scenario fails as expected: ${scenario.name} - ${err}`)
t.regex(
err.message,
/** @type {RegExp} */ (scenario.expectedFailureMessageRegex),
'Error message expects to match regex'
)
} else {
if (err) {
t.fail(`Unexpected error in scenario: ${scenario.name} - ${err}`)
throw err
}
}
},
checkResult = async (t, result, scenario) => {
if (scenario.testType === 'truthy') {
t.assert(
result,
`${scenario.name} - scenario gives expected truthy result`
)
} else if (scenario.testType === 'falsy') {
t.falsy(result, `${scenario.name} - scenario gives expected falsy result`)
} else {
t.deepEqual(
result,
scenario.expectedResult,
`${scenario.name} - scenario gives expected result`
)
}
},
expectedFailure = false,
expectedFailureMessageRegex = /[\s\S]*/,
files = {},
builtin = {},
context = {},
opts = { scuttleGlobalThis: {} },
config,
configOverride,
defineEntry,
defineOne,
defineTwo,
defineThree,
defaultPolicy = true,
dir,
...extraArgs
} = {}) {
function _defineEntry() {
// @ts-expect-error - does not exist
const testResult = require('one')
console.log(JSON.stringify(testResult, null, 2))
}
function _defineOne() {
// @ts-expect-error - does not exist
module.exports = require('two')
}
function _defineTwo() {
module.exports = {
value: 'this is module two',
}
}
function _defineThree() {
module.exports = {
value: 'this is module three',
}
}
const _files = fillInFileDetails({
'entry.js': {
content: `(${defineEntry || _defineEntry}).call(this)`,
packageName: '$root$',
importMap: {
one: 'node_modules/one/index.js',
two: 'node_modules/two/index.js',
three: 'node_modules/three/index.js',
},
entry: true,
},
'package.json': {
content: `${JSON.stringify(
{
name,
dependencies: {
one: '1.0.0',
two: '1.0.0',
three: '1.0.0',
},
devDependencies: {},
},
null,
2
)}`,
},
'node_modules/one/index.js': {
packageName: 'one',
content: `(${defineOne || _defineOne}).call(this)`,
importMap: {
two: 'node_modules/two/index.js',
three: 'node_modules/three/index.js',
},
},
'node_modules/one/package.json': {
content: `${JSON.stringify(
{
name: 'one',
dependencies: {
two: '1.0.0',
three: '1.0.0',
},
},
null,
2
)}`,
},
'node_modules/two/index.js': {
packageName: 'two',
content: `(${defineTwo || _defineTwo}).call(this)`,
importMap: {
three: 'node_modules/three/index.js',
},
},
'node_modules/two/package.json': {
content: `${JSON.stringify(
{
name: 'two',
dependencies: {
three: '1.0.0',
},
},
null,
2
)}`,
},
'node_modules/three/index.js': {
packageName: 'three',
content: `(${defineThree || _defineThree}).call(this)`,
importMap: {
one: 'node_modules/one/index.js',
},
},
'node_modules/three/package.json': {
content: `${JSON.stringify(
{
name: 'three',
dependencies: {
one: '1.0.0',
},
},
null,
2
)}`,
},
...files,
})
let _config
if (defaultPolicy) {
_config = mergeDeep(
{
resources: {
one: {
packages: {
two: true,
three: true,
},
},
two: {
packages: {
three: true,
},
},
},
},
config
)
} else {
_config = config
}
const _configOverride = mergeDeep(
{
resources: {
one: {
packages: {
five: true,
},
},
},
},
configOverride
)
return {
...extraArgs,
name: name,
checkPostRun,
checkResult,
checkError,
testType,
builtin,
expectedResult,
expectedFailure,
expectedFailureMessageRegex,
entries: ['entry.js'],
files: _files,
config: /** @type {import('../src/schema').LavaMoatPolicy} */ (_config),
configOverride:
/** @type {import('../src/schema').LavaMoatPolicyOverrides} */ (
_configOverride
),
context,
opts,
dir,
}
}
function createHookedConsole() {
let hasResolved = false
/** @type {(arg: any) => void} */
let resolve
const firstLogEventPromise = new Promise((_resolve) => {
resolve = _resolve
})
/** @param {string} message */
const hookedLog = (message) => {
if (hasResolved) {
throw new Error(`console.log called multiple times. got "${message}"`)
}
hasResolved = true
// run result through serialization boundary. this ensures these tests:
// - work across a serialization boundary
// - return simple objects non wrapped by membranes
let result
try {
result = JSON.parse(message)
} catch (err) {
throw new Error(`LavaMoat - failed to parse test output:\n${message}`)
}
resolve(result)
}
const hookedConsole = { ...console, log: hookedLog }
return {
firstLogEventPromise,
hookedConsole,
}
}
/**
* Options for {@link PlatformRunScenario}
*
* @template [Result=unknown] Default is `unknown`
* @typedef PlatformRunScenarioOpts
* @property {import('./scenario').NormalizedScenario<Result>} scenario
* @property {boolean} [runWithPrecompiledModules]
* @property {(...args: any[]) => void} [log]
*/
/**
* Scenario runner function provided by the consuming platform.
*
* Called by {@link runAndTestScenario}
*
* @template [Result=unknown] Default is `unknown`
* @callback PlatformRunScenario
* @param {PlatformRunScenarioOpts<Result>} opts
* @returns {Promise<Result>}
*/
/**
* Run the given scenario.
*
* The `scenario` itself should be passed thru `createScenarioFromScaffold` to
* normalize it.
*
* @template [Result=unknown] Default is `unknown`
* @type {PlatformRunScenario<Result>}
*/
async function runScenario({ scenario, runWithPrecompiledModules = false }) {
const {
entries,
files,
config,
configOverride,
builtin,
kernelArgs = {},
opts = {},
beforeCreateKernel = () => {},
} = scenario
const lavamoatConfig = mergeDeep(config, configOverride)
const kernelSrc = generateKernel(opts)
const { hookedConsole, firstLogEventPromise } = createHookedConsole()
Object.assign(scenario.context, { console: hookedConsole })
const {
result: createKernel,
vmGlobalThis,
vmContext,
vmFeralFunction,
} = evaluateWithSourceUrl(
'LavaMoat/core-test/kernel',
kernelSrc,
scenario.context
)
// root global for test realm
scenario.globalThis = vmGlobalThis
scenario.vmContext = vmContext
// call hook before kernel is created
beforeCreateKernel(scenario)
// create kernel
const kernel = createKernel({
runWithPrecompiledModules,
lavamoatConfig,
/** @param {string} id */
loadModuleData: (id) => {
if (id in builtin) {
return moduleDataForBuiltin(builtin, id)
}
const moduleRecord = files[id]
/**
* @type {any}
* @todo Need a type for "module data"
*/
const moduleData = {
id: moduleRecord.specifier,
package: moduleRecord.packageName,
type: moduleRecord.type,
file: moduleRecord.file,
deps: moduleRecord.importMap,
moduleInitializer: moduleRecord.moduleInitializer,
}
// append the source or prepare the precompiledInitializer
const intializerSource = `(function(exports, require, module, __filename, __dirname){\n${applySourceTransforms(
moduleRecord.content
)}\n})`
if (runWithPrecompiledModules) {
moduleData.precompiledInitializer = vmFeralFunction(`
with (this.scopeTerminator) {
with (this.globalThis) {
return function() {
'use strict';
return ${intializerSource}
};
}
}
`)
} else {
moduleData.source = intializerSource
}
return moduleData
},
/**
* @param {string} id
* @param {string} relative
* @returns {string}
*/
getRelativeModuleId: (id, relative) => {
return (
/** @type {import('./scenario').NormalizedScenarioJSFile} */ (files[id])
.importMap[relative] || relative
)
},
prepareModuleInitializerArgs,
...kernelArgs,
})
entries.forEach((id) => kernel.internalRequire(id))
const testResult = await firstLogEventPromise
return testResult
}
/**
* The subset of the `fs/promises` module that is used by
* `prepareScenarioOnDisk`.
*
* @typedef FsPromiseApi
* @property {(
* dir: string,
* opts?: import('node:fs').MakeDirectoryOptions & { recursive: true }
* ) => Promise<string | undefined>} mkdir
* @property {(filepath: string, data: any) => Promise<void>} writeFile
*/
/**
* Prepares a scenario on disk by writing files based on the provided scenario
* object.
*
* @param {Object} options - The options for preparing the scenario.
* @param {FsPromiseApi} [options.fs] - The file system module to use (default:
* `node:fs/promises`).
* @param {import('./scenario').Scenario} options.scenario - The scenario object
* containing the files to write.
* @param {string} [options.policyName='policies'] - The name of the policy
* directory (default: 'policies'). Default is `'policies'`
* @param {string} [options.projectDir] - The project directory path.
* @returns {Promise<{ projectDir: string; policyDir: string }>} - An object
* containing the project directory path and the policy directory path.
*/
async function prepareScenarioOnDisk({
fs = require('node:fs/promises'),
scenario,
policyName = 'policies',
projectDir,
}) {
if (projectDir === undefined) {
;({ path: projectDir } = await tmp.dir())
}
const filesToWrite = Object.values(scenario.files ?? {})
if (!scenario.opts?.writeAutoPolicy) {
const defaultPaths = getDefaultPaths(policyName)
const primaryPath =
typeof scenario.opts?.policyPath === 'string'
? scenario.opts.policyPath
: defaultPaths.primary
filesToWrite.push({
file: primaryPath,
content: stringify(scenario.config),
})
if (scenario.configOverride) {
const overridePath =
typeof scenario.opts?.policyOverridePath === 'string'
? scenario.opts.policyOverridePath
: defaultPaths.override
filesToWrite.push({
file: overridePath,
content: stringify(scenario.configOverride),
})
}
}
await Promise.all(
filesToWrite.map(async (file) => {
const fullPath = path.join(
/** @type {string} */ (projectDir),
/** @type {import('./scenario').NormalizedScenarioJSFile} */ (file).file
)
const dirname = path.dirname(fullPath)
await fs.mkdir(dirname, { recursive: true })
await fs.writeFile(fullPath, file.content)
})
)
return {
projectDir,
policyDir: path.join(projectDir, `/lavamoat/${policyName}/`),
}
}
/**
* @param {Record<string, import('./scenario').ScenarioFile>} files
* @returns
*/
function fillInFileDetails(files) {
Object.entries(files).forEach(([file, fileObj]) => {
fileObj.file = fileObj.file || file
if (path.extname(file) === '.js') {
// parse as LavamoatModuleRecord
fileObj.specifier = fileObj.file || file
fileObj.type = fileObj.type || 'js'
fileObj.entry = Boolean(fileObj.entry)
}
})
return files
}
/**
* @param {Record<string, any>} builtinObj
* @param {string} name
* @returns {any}
* @todo Type the module data
*/
function moduleDataForBuiltin(builtinObj, name) {
return {
id: name,
file: name,
package: name,
type: 'builtin',
/** @type {import('./scenario').ScenarioFile['moduleInitializer']} */
moduleInitializer: (_, _2, module) => {
module.exports = builtinObj[name]
},
}
}
/**
* @param {((id: string) => unknown) & { resolve: (id: string) => string }} requireRelativeWithContext
* @param {any} moduleObj
* @param {any} moduleData
* @returns
*/
function prepareModuleInitializerArgs(
requireRelativeWithContext,
moduleObj,
moduleData
) {
/** @type {NodeJS.Require} */
const require = /** @type {any} */ (requireRelativeWithContext)
const module = moduleObj
const exports = moduleObj.exports
const __filename = moduleData.file
const __dirname = path.dirname(__filename)
require.resolve = /** @type {any} */ (
() => {
throw new Error(
'require.resolve not implemented in lavamoat-core test harness'
)
}
)
return [exports, require, module, __filename, __dirname]
}
/**
* @param {string} filename
* @param {string} content
* @param {object} context
*/
function evaluateWithSourceUrl(filename, content, context) {
const vmContext = createContext()
const vmGlobalThis = runInContext('this', vmContext)
const vmFeralFunction = vmGlobalThis.Function
Object.defineProperties(
vmGlobalThis,
Object.getOwnPropertyDescriptors(context)
)
// circular ref (used when globalThis is not present)
if (!vmGlobalThis.globalThis) {
vmGlobalThis.globalThis = vmGlobalThis
}
// Since the browserify test uses this vm util as a browser env simulation,
// creating actual dom nodes that can leak the real global object is not possible,
// therefore there is no way to access the real global object otherwise, but since we
// have to (for the scuttling tests) - we intentionally export this util func to solve this:
vmGlobalThis.getTrueGlobalThisForTestsOnly = () => vmGlobalThis
// perform eval
let result
try {
result = runInContext(`${content}\n//# sourceURL=${filename}`, vmContext)
} catch (e) {
console.log(/** @type {Error} */ (e).stack)
throw e
}
// pull out test result value from context (not always used)
return { result, vmGlobalThis, vmContext, vmFeralFunction }
}
/**
* Create a LavaMoat policy config for a given test function
*
* @param {() => any} testFn - Test function which will be wrapped and used as
* the module contents; otherwise a string of the module contents. ESM modules
* must use a string, since `import` and `export` are only valid at the top
* level.
* @param {Partial<GeneratePolicyFromFilesOpts>} [opts]
* @returns {Promise<import('../src/schema').LavaMoatPolicy>}
*/
async function createConfigForTest(testFn, opts = {}) {
/** @type {import('./scenario').NormalizedScenarioJSFile[]} */
const files = [
{
type: 'js',
specifier: './entry.js',
file: './entry.js',
packageName: '$root$',
importMap: {
test: './node_modules/test/index.js',
},
content: 'require("test")',
entry: true,
},
{
// non-entry
type: 'js',
specifier: './node_modules/test/index.js',
file: './node_modules/test/index.js',
packageName: 'test',
importMap: {},
content: typeof testFn === 'function' ? `(${testFn})()` : `${testFn}`,
},
]
const policy = await generatePolicyFromFiles({ files, ...opts })
return policy
}
/**
* @param {object} opts
* @param {import('./scenario').Scenario} opts.scenario
* @param {Partial<GeneratePolicyFromFilesOpts>} [opts.opts]
*/
async function autoConfigForScenario({ scenario, opts = {} }) {
const files = /** @type {import('./scenario').NormalizedScenarioJSFile[]} */ (
Object.values(scenario.files ?? {})
)
const policy = await generatePolicyFromFiles({ files, ...opts })
scenario.config = policy
}
/**
* @param {object} opts
* @param {import('./scenario').NormalizedScenario} opts.scenario
*/
function convertOptsToArgs({ scenario }) {
const { entries } = scenario
if (entries.length !== 1) {
throw new Error('LavaMoat - invalid entries')
}
const firstEntry = entries[0]
return [firstEntry]
}
/**
* @param {string} func
* @returns {unknown}
*/
function functionToString(func) {
return `(${func}).call(this)`
}
/**
* @template [Result=unknown] Default is `unknown`
* @param {import('ava').ExecutionContext} t
* @param {import('./scenario').NormalizedScenario<Result>} scenario
* @param {PlatformRunScenario<Result>} platformRunScenario
* @returns {Promise<Result | undefined>}
*/
async function runAndTestScenario(t, scenario, platformRunScenario) {
/** @type {Result | undefined} */
let result
/** @type {Error | undefined} */
let err
try {
result = await platformRunScenario({ scenario, log: t.log.bind(t) })
} catch (e) {
err = /** @type {Error} */ (e)
}
await scenario.checkPostRun(t, result, err, scenario)
return result
}