-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
177 lines (156 loc) · 5.05 KB
/
index.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
/**
* Provides Lavamoat policy generation facilities via {@link generatePolicy}
*
* @packageDocumentation
*/
import { loadCompartmentMapForArchive } from '@endo/compartment-mapper'
import assert from 'node:assert'
import nodeFs from 'node:fs'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { DEFAULT_POLICY_DEBUG_PATH, DEFAULT_POLICY_PATH } from '../constants.js'
import { importHook } from '../import-hook.js'
import { moduleTransforms } from '../module-transforms.js'
import { defaultReadPowers, makeReadPowers } from '../power.js'
import { isFsAPI, writeJson } from '../util.js'
import { PolicyGenerator } from './policy-generator.js'
const { fromEntries, entries } = Object
/**
* Generates a LavaMoat debug policy from a given entry point using
* `@endo/compartment-mapper`
*
* @overload
* @param {string | URL} entrypointPath
* @param {import('./types.js').GeneratePolicyOptions & { debug: true }} opts
* @returns {Promise<import('lavamoat-core').LavaMoatPolicyDebug>}
* @public
*/
/**
* Generates a LavaMoat policy from a given entry point using
* `@endo/compartment-mapper`
*
* @overload
* @param {string | URL} entrypointPath
* @param {import('./types.js').GeneratePolicyOptions} [opts]
* @returns {Promise<import('lavamoat-core').LavaMoatPolicy>}
* @public
*/
/**
* Generates a LavaMoat policy or debug policy from a given entry point using
* `@endo/compartment-mapper`
*
* @param {string | URL} entrypointPath
* @param {import('./types.js').GeneratePolicyOptions} [opts]
* @returns {Promise<import('lavamoat-core').LavaMoatPolicy>}
* @public
*/
export async function generatePolicy(
entrypointPath,
{
readPowers = defaultReadPowers,
debug = false,
policyOverride,
...archiveOpts
} = {}
) {
readPowers = isFsAPI(readPowers) ? makeReadPowers(readPowers) : readPowers
const { compartmentMap, sources, renames } = await loadCompartmentMap(
entrypointPath,
{
...archiveOpts,
readPowers,
}
)
/** @type {import('./types.js').PolicyGeneratorOptions} */
const baseOpts = { readPowers, policyOverride }
// this weird thing is to make TS happy about the overload
const opts = debug ? { debug: true, ...baseOpts } : baseOpts
return await PolicyGenerator.generatePolicy(
compartmentMap,
sources,
renames,
opts
)
}
/**
* Generates a LavaMoat policy or debug policy from a given entry point using
* `@endo/compartment-mapper`
*
* @param {string | URL} entrypointPath
* @param {import('./types.js').GenerateAndWritePolicyOptions} [opts]
* @returns {Promise<import('lavamoat-core').LavaMoatPolicy>}
* @public
*/
export async function generateAndWritePolicy(entrypointPath, opts = {}) {
const {
policyDebugPath = DEFAULT_POLICY_DEBUG_PATH,
policyPath = DEFAULT_POLICY_PATH,
fs = nodeFs,
...generateOpts
} = opts
assert(path.isAbsolute(policyPath), 'policyPath must be an absolute path')
assert(
path.isAbsolute(policyDebugPath),
'policyDebugPath must be an absolute path'
)
await Promise.resolve()
/** @type {import('lavamoat-core').LavaMoatPolicy | undefined} */
let policy
// if the debug flag was true, then the result of generatePolicy
// will be a LavaMoatPolicyDebug. we will write that entire thing to the debug policy,
// then extract everything except the `debugInfo` prop, and write _that_ to the actual policy
if (generateOpts.debug) {
const debugPolicy = await generatePolicy(entrypointPath, {
...generateOpts,
debug: true,
})
await writeJson(policyDebugPath, debugPolicy, { fs })
// do not attempt to use the `delete` keyword with typescript. you have been warned!
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { debugInfo, ...corePolicy } =
/** @type {import('lavamoat-core').LavaMoatPolicyDebug} */ (debugPolicy)
policy = corePolicy
}
policy = policy ?? (await generatePolicy(entrypointPath, generateOpts))
// XXX: do we need to merge policies here?
await writeJson(policyPath, policy, { fs })
return policy
}
/**
* Loads compartment map and associated sources.
*
* @param {string | URL} entrypointPath
* @param {import('./types.js').LoadCompartmentMapOptions} opts
* @internal
*/
export async function loadCompartmentMap(
entrypointPath,
{ readPowers = defaultReadPowers, ...archiveOpts } = {}
) {
const moduleLocation =
entrypointPath instanceof URL
? `${entrypointPath}`
: `${pathToFileURL(entrypointPath)}`
const { archiveCompartmentMap, archiveSources, compartmentRenames } =
await loadCompartmentMapForArchive({
dev: true,
...archiveOpts,
readPowers,
moduleLocation,
importHook,
moduleTransforms,
fallbackLanguageForExtension: {
node: 'bytes',
},
})
// `compartmentRenames` is a mapping of filepath to compartment name;
// we need the reverse mapping.
const renames = fromEntries(
entries(compartmentRenames).map(([filepath, id]) => [id, filepath])
)
return {
compartmentMap: archiveCompartmentMap,
sources: archiveSources,
renames,
}
}