@exodus/stasis-core 1.0.0-beta.1 → 1.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/stasis-core.js +38 -28
- package/package.json +8 -1
- package/src/add.js +325 -0
- package/src/brotli.js +15 -16
- package/src/bundle-util.js +66 -0
- package/src/bundle.js +99 -88
- package/src/config.js +78 -104
- package/src/extract.js +159 -0
- package/src/fs.js +123 -313
- package/src/hooks.js +129 -449
- package/src/loader.js +1 -3
- package/src/lockfile.js +82 -62
- package/src/prune.js +37 -152
- package/src/shard.js +60 -0
- package/src/state-util.js +4 -26
- package/src/state.js +579 -1128
- package/src/util.js +545 -97
package/src/hooks.js
CHANGED
|
@@ -8,33 +8,17 @@ import assert from 'node:assert/strict'
|
|
|
8
8
|
|
|
9
9
|
import { State } from './state.js'
|
|
10
10
|
import { installFsHooks } from './fs.js'
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
import {
|
|
12
|
+
NATIVE_BUILD_FORMATS as NATIVE_SOURCE_FORMATS,
|
|
13
|
+
NODE_FORMATS as NODEJS_FORMATS,
|
|
14
|
+
RESOURCE_FORMATS,
|
|
15
|
+
SOURCE_LANGUAGE_FORMATS as NON_NODE_SOURCE_LANGUAGES,
|
|
16
|
+
STAT_FORMATS,
|
|
17
|
+
} from './util.js'
|
|
18
|
+
|
|
19
|
+
// Snapshot off the namespace so `--fs` can't redirect stasis's own capture/shard reads.
|
|
17
20
|
const { mkdtempSync, opendirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } = fs
|
|
18
21
|
|
|
19
|
-
// Warning: only covers code imports, not file reads
|
|
20
|
-
|
|
21
|
-
// Formats Node's module loader recognizes (and either executes, like `module`/
|
|
22
|
-
// `commonjs`/typescript variants, or imports as data, like `json`). The runtime
|
|
23
|
-
// loader treats this as the trust boundary -- a file served from the bundle whose
|
|
24
|
-
// attested format is outside this allowlist is refused at load time (and earlier
|
|
25
|
-
// at resolve, where applicable) with a contextual message. Adding a tag here
|
|
26
|
-
// means "Node can handle it"; everything else (resource/asset content,
|
|
27
|
-
// source-language bundles, an unknown string from a tampered or newer bundle)
|
|
28
|
-
// fails closed.
|
|
29
|
-
const NODEJS_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
|
|
30
|
-
|
|
31
|
-
// Per-kind messages for the few categories we recognize; everything else gets a
|
|
32
|
-
// generic "unrecognized format" message. Kept separate from NODEJS_FORMATS so
|
|
33
|
-
// the allowlist stays the single trust gate and the message is just UX.
|
|
34
|
-
const NON_NODE_SOURCE_LANGUAGES = new Set(['solidity', 'php', 'bash', 'rust'])
|
|
35
|
-
const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
|
|
36
|
-
// The executable CommonJS formats (plain CJS + TS-CJS). Named like the sets above so
|
|
37
|
-
// "which formats are CJS" lives in one place; the require-repair below keys off it.
|
|
38
22
|
const CJS_FORMATS = new Set(['commonjs', 'commonjs-typescript'])
|
|
39
23
|
|
|
40
24
|
function refuseNonNodeFormat(format, url) {
|
|
@@ -44,19 +28,38 @@ function refuseNonNodeFormat(format, url) {
|
|
|
44
28
|
`produced for external analysis, not for 'stasis run --bundle=load' (${url})`
|
|
45
29
|
)
|
|
46
30
|
}
|
|
31
|
+
if (NATIVE_SOURCE_FORMATS.has(format)) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`[stasis] cannot execute a '${format}' file: native build inputs ` +
|
|
34
|
+
`(podspec/gradle/Java/Kotlin/ObjC++/...) are attested for the CocoaPods/Gradle ` +
|
|
35
|
+
`toolchain, not executable by Node (${url})`
|
|
36
|
+
)
|
|
37
|
+
}
|
|
47
38
|
if (RESOURCE_FORMATS.has(format)) {
|
|
48
39
|
throw new Error(
|
|
49
40
|
`[stasis] cannot import a resource file ('${format}'): asset content is not ` +
|
|
50
41
|
`executable JavaScript. Read it with fs (or your bundler's asset loader) instead (${url})`
|
|
51
42
|
)
|
|
52
43
|
}
|
|
44
|
+
if (format === 'patch') {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`[stasis] cannot execute a 'patch' file: a unified diff is applied by a patch step ` +
|
|
47
|
+
`(pnpm patchedDependencies, patch-package, a build script), not run by Node (${url})`
|
|
48
|
+
)
|
|
49
|
+
}
|
|
53
50
|
if (format === 'directory') {
|
|
54
51
|
throw new Error(
|
|
55
52
|
`[stasis] cannot import a directory listing ('directory'): a captured ` +
|
|
56
53
|
`fs.readdirSync result is not executable JavaScript. Read it with fs.readdirSync instead (${url})`
|
|
57
54
|
)
|
|
58
55
|
}
|
|
59
|
-
|
|
56
|
+
if (STAT_FORMATS.has(format)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`[stasis] cannot import '${url}': the bundle carries only a payload-free stat record ` +
|
|
59
|
+
`('${format}', an fs.lstatSync/statSync capture attesting existence and kind) -- ` +
|
|
60
|
+
`not the file's content. Capture a run that reads or imports it to bundle its bytes.`
|
|
61
|
+
)
|
|
62
|
+
}
|
|
60
63
|
throw new Error(
|
|
61
64
|
`[stasis] cannot execute '${url}': attested format '${format}' is not a Node loader ` +
|
|
62
65
|
`format. The bundle may be tampered, produced by a newer stasis, or built for a ` +
|
|
@@ -64,14 +67,7 @@ function refuseNonNodeFormat(format, url) {
|
|
|
64
67
|
)
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
// node:59666
|
|
68
|
-
// into served CommonJS source, that restores the `require.cache` and
|
|
69
|
-
// `require.extensions` the ESM->CJS translator's re-invented require omits.
|
|
70
|
-
// `require` here is the module wrapper's own parameter; node:module's CJS
|
|
71
|
-
// loader owns the real `Module._cache` / `Module._extensions`, so pointing the
|
|
72
|
-
// two properties at those makes a bundle-served module's require match a
|
|
73
|
-
// disk-loaded one's shape. Guarded (typeof/try) so a require without the usual
|
|
74
|
-
// shape, or a frozen builtins object, degrades to a no-op rather than throwing.
|
|
70
|
+
// node:59666: restores require.cache/require.extensions the ESM->CJS translator's require omits.
|
|
75
71
|
const CJS_REQUIRE_REPAIR =
|
|
76
72
|
"try{if(typeof require==='function'&&!require.cache){" +
|
|
77
73
|
"const __stasis_m=require('node:module');" +
|
|
@@ -79,22 +75,11 @@ const CJS_REQUIRE_REPAIR =
|
|
|
79
75
|
'require.cache=__stasis_m.Module._cache;' +
|
|
80
76
|
'if(!require.extensions)require.extensions=__stasis_m.Module._extensions;}}}catch{}'
|
|
81
77
|
|
|
82
|
-
// Sticky scanners for the pieces of a Directive Prologue: a run of whitespace and
|
|
83
|
-
// line/block comments, a single string literal, and the run of horizontal space and
|
|
84
|
-
// SAME-LINE block comments that may trail a directive before its terminator. Sticky (`y`)
|
|
85
|
-
// + lastIndex so the prologue is walked in place, never by re-slicing the source each step.
|
|
86
78
|
const WS_COMMENTS = /(?:\s|\/\/[^\n]*|\/\*[\s\S]*?\*\/)*/uy
|
|
87
79
|
const STRING_LITERAL = /(['"])(?:[^\\]|\\.)*?\1/uy
|
|
88
|
-
//
|
|
89
|
-
// the trailing run stops at one (it's handled as a line boundary below); this matches only
|
|
90
|
-
// single-line block comments (comment body has no line terminators).
|
|
80
|
+
// Matches only single-line block comments: a newline-spanning one counts as a line boundary for ASI.
|
|
91
81
|
const TRAILING_SAME_LINE = /(?:[ \t]|\/\*(?:[^*\n\r\u2028\u2029]|\*(?!\/))*\*\/)*/uy
|
|
92
|
-
//
|
|
93
|
-
// break following a candidate directive string is one of these, the string heads a
|
|
94
|
-
// multi-line expression (`'a,b'\n.split(...)`, `'x'\ninstanceof Y`), not a directive
|
|
95
|
-
// (ASI does not insert a semicolon before them), so it must NOT be skipped. Punctuators
|
|
96
|
-
// are matched by first char; the two keyword operators need a `\b` so `in`/`instanceof`
|
|
97
|
-
// don't swallow identifiers like `index`/`instances`.
|
|
82
|
+
// A directive string followed by one of these heads a multi-line expression, not a directive (ASI gotcha).
|
|
98
83
|
const EXPR_CONTINUATION = new Set(['.', '[', '(', '`', '+', '-', '*', '/', '%', ',', '?', ':', '=', '<', '>', '&', '|', '^'])
|
|
99
84
|
const KEYWORD_CONTINUATION = /(?:instanceof|in)\b/uy
|
|
100
85
|
function skipSticky(re, source, i) {
|
|
@@ -102,9 +87,6 @@ function skipSticky(re, source, i) {
|
|
|
102
87
|
re.exec(source)
|
|
103
88
|
return re.lastIndex
|
|
104
89
|
}
|
|
105
|
-
// At a line boundary at `pos` (a line terminator or a newline-spanning block comment), does
|
|
106
|
-
// the next significant token begin a NEW statement -- so the preceding string is a complete
|
|
107
|
-
// directive -- rather than continue the expression?
|
|
108
90
|
function newStatementAfterBreak(source, pos) {
|
|
109
91
|
const afterBreak = skipSticky(WS_COMMENTS, source, pos)
|
|
110
92
|
if (afterBreak >= source.length) return true
|
|
@@ -113,23 +95,8 @@ function newStatementAfterBreak(source, pos) {
|
|
|
113
95
|
return !KEYWORD_CONTINUATION.test(source)
|
|
114
96
|
}
|
|
115
97
|
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
// Prologue -- so a `'use strict'` directive keeps strict mode. The prelude is a `try{...}`
|
|
119
|
-
// statement: inserting it ahead of a directive would demote that directive to a plain
|
|
120
|
-
// expression (dropping the module to sloppy mode), so we insert AFTER the prologue --
|
|
121
|
-
// shebang, then whitespace, comments, and string-literal directive STATEMENTS. A leading
|
|
122
|
-
// ';' makes the insertion self-terminating so it can never fuse `'use strict'try{` into a
|
|
123
|
-
// SyntaxError.
|
|
124
|
-
//
|
|
125
|
-
// A leading string counts as a directive only when a real statement boundary follows it:
|
|
126
|
-
// `;`, `}`, a line comment, EOF, or a LINE boundary -- a line terminator OR a block comment
|
|
127
|
-
// containing one -- whose next token does not continue the expression. So a module that
|
|
128
|
-
// opens with a string EXPRESSION (`'x'.toUpperCase()`, `'a' + b`, `'a,b'` newline-then-
|
|
129
|
-
// `.split()`, `'x'` newline-then-`instanceof Y`) is left intact instead of being split
|
|
130
|
-
// mid-expression (a SyntaxError, or silently wrong bytes), while a real directive followed
|
|
131
|
-
// by a multi-line banner comment still keeps strict mode. Distinguishing the two is the ASI
|
|
132
|
-
// problem, so this is a small scan rather than one regex.
|
|
98
|
+
// Insert CJS_REQUIRE_REPAIR AFTER the Directive Prologue with a leading ';', so a `'use strict'`
|
|
99
|
+
// stays strict and a leading string EXPRESSION isn't split mid-expression.
|
|
133
100
|
function repairCjsRequire(source) {
|
|
134
101
|
if (typeof source !== 'string') return source
|
|
135
102
|
let i = 0
|
|
@@ -140,92 +107,47 @@ function repairCjsRequire(source) {
|
|
|
140
107
|
for (;;) {
|
|
141
108
|
const afterWs = skipSticky(WS_COMMENTS, source, i)
|
|
142
109
|
STRING_LITERAL.lastIndex = afterWs
|
|
143
|
-
if (STRING_LITERAL.exec(source) === null) break
|
|
110
|
+
if (STRING_LITERAL.exec(source) === null) break
|
|
144
111
|
const afterStr = STRING_LITERAL.lastIndex
|
|
145
112
|
const afterTrail = skipSticky(TRAILING_SAME_LINE, source, afterStr)
|
|
146
113
|
const c = source[afterTrail]
|
|
147
114
|
let isDirective
|
|
148
115
|
if (c === undefined || c === ';' || c === '}') isDirective = true
|
|
149
|
-
else if (c === '/' && source[afterTrail + 1] === '/') isDirective = true
|
|
150
|
-
// A `/*` here is a newline-spanning block comment
|
|
151
|
-
// above); like a bare line break it terminates the directive when a new statement follows.
|
|
116
|
+
else if (c === '/' && source[afterTrail + 1] === '/') isDirective = true
|
|
117
|
+
// A `/*` here is a newline-spanning block comment: like a line break it can end the directive.
|
|
152
118
|
else if (c === '/' && source[afterTrail + 1] === '*') isDirective = newStatementAfterBreak(source, afterTrail)
|
|
153
119
|
else if (c === '\n' || c === '\r' || c === '\u2028' || c === '\u2029') isDirective = newStatementAfterBreak(source, afterTrail)
|
|
154
|
-
else isDirective = false
|
|
120
|
+
else isDirective = false
|
|
155
121
|
if (!isDirective) break
|
|
156
|
-
i = afterStr
|
|
122
|
+
i = afterStr
|
|
157
123
|
}
|
|
158
124
|
return `${source.slice(0, i)};${CJS_REQUIRE_REPAIR}${source.slice(i)}`
|
|
159
125
|
}
|
|
160
126
|
|
|
161
127
|
let state
|
|
162
|
-
// The shard dir
|
|
163
|
-
// is exported to descendants via env (each capturing child signs its shard with it); the PUBLIC
|
|
164
|
-
// key (createdShardPub, a KeyObject) is what the root verifies every shard's signature against,
|
|
165
|
-
// and its base64url form (createdShardPubId) prefixes each shard's filename. Tracked so the root
|
|
166
|
-
// merges from / removes ONLY a dir it minted, and accepts ONLY shards that carry a valid
|
|
167
|
-
// signature under the keypair it minted -- never an inherited/attacker dir or a foreign-signed file.
|
|
128
|
+
// The root merges/removes ONLY a shard dir it minted and accepts ONLY shards signed under its own key.
|
|
168
129
|
let createdShardDir
|
|
169
130
|
let createdShardPub
|
|
170
131
|
let createdShardPubId
|
|
171
|
-
// True once the
|
|
172
|
-
// beforeExit/exit firings and re-flushes late-observed resolution edges; see its comment).
|
|
173
|
-
// Consulted by the load hook's fail-loud assert: a module whose BYTES first load after a
|
|
174
|
-
// write may miss the final artifact entirely, so it must crash the capture rather than
|
|
175
|
-
// silently ship unattested. Late resolution EDGES of already-loaded modules, by contrast,
|
|
176
|
-
// are what the re-flush exists to pick up.
|
|
132
|
+
// True once artifacts are written; the load hook asserts on it so a late first load can't ship unattested.
|
|
177
133
|
let saved = false
|
|
178
|
-
// Serialized shard text a child last wrote, so
|
|
179
|
-
// re-write instead of dropping a second (equal) shard file for the root to merge.
|
|
134
|
+
// Serialized shard text a child last wrote, so a second flush skips an identical re-write.
|
|
180
135
|
let lastShardWritten
|
|
181
|
-
// Set when stasis's own capture/verification rejects something
|
|
182
|
-
// addFile/addImport below). Distinct from "the run exited non-zero": a clean capture is
|
|
183
|
-
// allowed to exit non-zero for its own reasons and still be persisted.
|
|
136
|
+
// Set when stasis's own capture/verification rejects something; when set, nothing is written.
|
|
184
137
|
let aborted = false
|
|
185
|
-
// >0 while Node's
|
|
186
|
-
// CommonJS goes through the public fs.readFileSync the --fs hook patches. The module is
|
|
187
|
-
// already captured as code via addFile, so the --fs capture hook skips reads taken
|
|
188
|
-
// while this is set: it records the program's OWN fs reads, not Node's module loading.
|
|
138
|
+
// >0 while Node's loader reads a module's source; the --fs hook skips reads taken then.
|
|
189
139
|
let loadingModule = 0
|
|
190
|
-
// True until the first non-builtin load hook fires
|
|
191
|
-
// `!state` heuristic for entry detection, which broke once initState could
|
|
192
|
-
// be triggered eagerly by plugins.js via ensureStateForLoader (state set
|
|
193
|
-
// before the entry's load).
|
|
140
|
+
// True until the first non-builtin load hook fires (entry detection).
|
|
194
141
|
let entryUnseen = true
|
|
195
142
|
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
// tree (the one the CLI launches, or a direct `node --import`). It is unset there, so the
|
|
199
|
-
// root claims the slot with its own pid and is allowed to write. A process that ends up
|
|
200
|
-
// running this loader as a child -- child_process.fork() inherits our `--import` via
|
|
201
|
-
// process.execArgv automatically; a manual `spawn(node, ['--import', <loader>, ...])` does so
|
|
202
|
-
// explicitly -- inherits EXODUS_STASIS_PID through the environment but runs under a different
|
|
203
|
-
// pid. On that mismatch we suppress the bundle/lockfile write for ANY child, fork or not --
|
|
204
|
-
// the root owns the artifact, and a second writer to the same path would race it. We
|
|
205
|
-
// additionally relax the entry-point check for a FORKED child specifically (mismatch AND an
|
|
206
|
-
// IPC channel, which fork() always creates and a non-fork child lacks): its main module is a
|
|
207
|
-
// fork target, not a declared CLI entry, whereas a `node --import` child names its own entry.
|
|
208
|
-
// getFile() still fails closed on anything unattested. (A `stasis run` nested inside an
|
|
209
|
-
// already-running stasis process inherits the pid and is thus treated as a child too -- it
|
|
210
|
-
// won't write; run stasis at a single level.) Resolved once, before any user code runs.
|
|
143
|
+
// EXODUS_STASIS_PID = the root stasis's pid; on mismatch we're a child and must not write the
|
|
144
|
+
// bundle/lockfile (root owns the artifact; a 2nd writer races). A forked child also relaxes assertEntry.
|
|
211
145
|
const OWN_PID = String(process.pid)
|
|
212
146
|
if (!process.env.EXODUS_STASIS_PID) process.env.EXODUS_STASIS_PID = OWN_PID
|
|
213
147
|
const isChildProcess = process.env.EXODUS_STASIS_PID !== OWN_PID
|
|
214
148
|
const forkedChild = isChildProcess && process.channel !== undefined
|
|
215
149
|
|
|
216
|
-
//
|
|
217
|
-
// as `preloadRoot` so state.write()'s backfill statically captures stasis-core's
|
|
218
|
-
// internal edges -- the (state.js -> config.js)-shape relative imports that
|
|
219
|
-
// were loaded by Node BEFORE install() registered our hooks and that the live
|
|
220
|
-
// resolve hook therefore never observed.
|
|
221
|
-
//
|
|
222
|
-
// `findPackageJSON` is wrapped because bundlers that ship this file as part
|
|
223
|
-
// of a captured bundle don't always have it mocked / wired up -- e.g. a
|
|
224
|
-
// metro-built bundle replaces node:module with its own shim where the API
|
|
225
|
-
// is absent. Returning undefined on failure makes the State constructor's
|
|
226
|
-
// `if (preloadRoot !== undefined)` skip cleanly; the backfill that consumes
|
|
227
|
-
// preloadRoot is a best-effort feature and falls through gracefully when
|
|
228
|
-
// it's unset.
|
|
150
|
+
// stasis-core's own package root, so State's backfill captures edges loaded before our hooks registered.
|
|
229
151
|
const PRELOAD_ROOT = (() => {
|
|
230
152
|
try {
|
|
231
153
|
return dirname(findPackageJSON(import.meta.url))
|
|
@@ -234,44 +156,9 @@ const PRELOAD_ROOT = (() => {
|
|
|
234
156
|
}
|
|
235
157
|
})()
|
|
236
158
|
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
//
|
|
240
|
-
// the transformer requires per file. But a child must never write the real lockfile/bundle
|
|
241
|
-
// (the root owns it; two writers race -- see save()). So each capturing child instead writes
|
|
242
|
-
// a per-pid SHARD -- its shardSnapshot(), a lockfile-shaped record of what it captured -- into
|
|
243
|
-
// a shared dir the ROOT creates and exports as EXODUS_STASIS_SHARD_DIR (inherited through the
|
|
244
|
-
// env by every descendant). Just before the root writes, it merges every shard back into its
|
|
245
|
-
// State (re-reading each file's bytes from disk), so observations only a child made are still
|
|
246
|
-
// attested. Best-effort throughout: a missing/partial shard (a child killed mid-write) or an
|
|
247
|
-
// unreadable dir is skipped, never fatal -- the root's own capture is unaffected.
|
|
248
|
-
//
|
|
249
|
-
// OPT-IN: gated behind --child-process (config.childProcess). The shard dir is a
|
|
250
|
-
// process-coordination channel, not part of normal capture, so it exists only when a user
|
|
251
|
-
// explicitly enables cross-process capture (e.g. for Metro). A default run never creates it.
|
|
252
|
-
//
|
|
253
|
-
// TRUST (defense in depth against shard-channel injection):
|
|
254
|
-
// - the channel only exists under the --child-process opt-in;
|
|
255
|
-
// - the dir is minted in the OS tmpdir via mkdtemp, which creates it mode 0700 (owner-only), so
|
|
256
|
-
// a different-uid process can't read its contents; trust does NOT rest on the dir being secret
|
|
257
|
-
// (its path travels to children in env) -- it rests on the signature gate below;
|
|
258
|
-
// - the root ALWAYS mints its own dir + a per-build ed25519 keypair, overwriting any
|
|
259
|
-
// inherited/attacker-set EXODUS_STASIS_SHARD_DIR/_KEY, and merges from / removes ONLY
|
|
260
|
-
// that dir. Only the PRIVATE key reaches descendants (via env); each child SIGNS its shard
|
|
261
|
-
// with it, and the root verifies every shard against its own PUBLIC key, accepting only a
|
|
262
|
-
// valid signature. The filename carries the public key (non-secret) purely to pre-filter;
|
|
263
|
-
// trust comes from the signature, NOT the filename -- a file signed by some other keypair,
|
|
264
|
-
// or unsigned, is rejected even if it's named with the right public-key prefix;
|
|
265
|
-
// - shards carry NO bytes -- mergeShard re-reads each file from disk and re-hashes -- so a shard
|
|
266
|
-
// can never inject forged file CONTENT (bytes); it can only over-attest a file already on disk.
|
|
267
|
-
// Because the dir holds only public-keyed, signed shards, a peer who can read the (owner-only)
|
|
268
|
-
// dir but NOT this process's env cannot forge one. The residual is broader than a /proc snooper:
|
|
269
|
-
// the private key rides in the environment, so ANY code the build itself executes -- a postinstall
|
|
270
|
-
// script, codegen, a transitive dependency -- inherits it and can mint a valid shard. And a valid
|
|
271
|
-
// shard does more than over-attest bytes: mergeShard replays its resolution edges via addImport
|
|
272
|
-
// WITHOUT re-deriving them on disk, so a key-holder can also forge resolution edges (and formats).
|
|
273
|
-
// All of that is within the build's own trust domain -- that code already runs with the build's
|
|
274
|
-
// privileges -- which is the line this opt-in deliberately draws.
|
|
159
|
+
// Child-process capture forwarding (shards), opt-in via --child-process: a capturing child writes a
|
|
160
|
+
// signed per-pid shard the root verifies against its own public key before merging. Trust rests on
|
|
161
|
+
// that signature, not the dir being secret; shards carry NO bytes (the root re-reads from disk).
|
|
275
162
|
|
|
276
163
|
function shardForwardingEnabled() {
|
|
277
164
|
return Boolean(state) && state.config.childProcess && (state.config.writeLockfile || state.config.writeBundle)
|
|
@@ -283,48 +170,29 @@ function writeChildShard() {
|
|
|
283
170
|
const keyB64 = process.env.EXODUS_STASIS_SHARD_KEY
|
|
284
171
|
if (!dir || !keyB64) return
|
|
285
172
|
try {
|
|
286
|
-
// Sign the snapshot
|
|
287
|
-
//
|
|
288
|
-
|
|
289
|
-
// the parsed object, makes verification independent of JSON key ordering and robust against a
|
|
290
|
-
// future top-level `signature` lockfile key. The PUBLIC key (base64url) names the file --
|
|
291
|
-
// <base64url(public)>-<pid>-<rand>.json: the public prefix lets the root cheaply pre-filter to
|
|
292
|
-
// its own build; the pid + random suffix keep workers from colliding even if the OS recycles an
|
|
293
|
-
// exited worker's pid within one build. Write to a dot-prefixed temp then rename: the merge
|
|
294
|
-
// filter ignores the temp, so the root never reads a half-written shard.
|
|
295
|
-
const shard = state.shardSnapshot() // serialized lockfile string
|
|
296
|
-
// save() re-fires on exit after the beforeExit flush (late-observed resolution edges;
|
|
297
|
-
// see save's comment). An unchanged snapshot means nothing new to forward -- skip the
|
|
298
|
-
// duplicate shard file rather than make the root merge the same content twice.
|
|
173
|
+
// Sign the exact snapshot string, never a re-stringify, so verify is independent of JSON key
|
|
174
|
+
// order; temp + rename so the merge never reads a half-written shard.
|
|
175
|
+
const shard = state.shardSnapshot()
|
|
299
176
|
if (shard === lastShardWritten) return
|
|
300
177
|
const privateKey = createPrivateKey({ key: Buffer.from(keyB64, 'base64'), format: 'der', type: 'pkcs8' })
|
|
301
178
|
const pubId = createPublicKey(privateKey).export({ format: 'jwk' }).x
|
|
302
179
|
const signature = sign(null, Buffer.from(shard), privateKey).toString('base64url')
|
|
303
|
-
const unique = `${OWN_PID}-${randomBytes(6).toString('hex')}`
|
|
180
|
+
const unique = `${OWN_PID}-${randomBytes(6).toString('hex')}`
|
|
304
181
|
const tmpPath = join(dir, `.${pubId}-${unique}.tmp`)
|
|
305
182
|
writeFileSync(tmpPath, JSON.stringify({ shard, signature }))
|
|
306
183
|
renameSync(tmpPath, join(dir, `${pubId}-${unique}.json`))
|
|
307
184
|
lastShardWritten = shard
|
|
308
185
|
} catch {
|
|
309
|
-
//
|
|
310
|
-
// its own capture, and a later frozen run fails closed on anything genuinely missing.
|
|
186
|
+
// Best-effort: a child that can't snapshot/sign/write just doesn't contribute.
|
|
311
187
|
}
|
|
312
188
|
}
|
|
313
189
|
|
|
314
|
-
//
|
|
315
|
-
// of shards can't OOM the root. Generous -- a real toolchain shard is a small lockfile.
|
|
190
|
+
// Cap shard count + size so a hostile/runaway set can't OOM the root.
|
|
316
191
|
const MAX_SHARDS = 4096
|
|
317
192
|
const MAX_SHARD_BYTES = 64 * 1024 * 1024
|
|
318
193
|
|
|
319
|
-
// Merge
|
|
320
|
-
//
|
|
321
|
-
// envelope; verify the shard's exact bytes against our own PUBLIC key (createdShardPub -- never a
|
|
322
|
-
// key derived from the filename) and merge only those verified bytes. So a file that's unsigned,
|
|
323
|
-
// foreign-signed, or tampered after signing (e.g. dropped by a non-descendant that doesn't hold
|
|
324
|
-
// the private key) is ignored even if it's named with our prefix. The caller (save) removes `dir`
|
|
325
|
-
// afterward in a finally, so a partial read here never leaves it behind. We iterate with opendir
|
|
326
|
-
// (not readdirSync) so the listing is streamed, never materialized -- MEMORY stays bounded even for
|
|
327
|
-
// a dir stuffed with entries -- and stop parsing/verifying after MAX_SHARDS matching shards.
|
|
194
|
+
// Merge children's shards from `dir`: verify each envelope against our own PUBLIC key, not one from
|
|
195
|
+
// the filename. opendir streams the listing so memory stays bounded.
|
|
328
196
|
function mergeChildShards(dir) {
|
|
329
197
|
let handle
|
|
330
198
|
try { handle = opendirSync(dir) } catch { return }
|
|
@@ -338,13 +206,13 @@ function mergeChildShards(dir) {
|
|
|
338
206
|
if (++count > MAX_SHARDS) break
|
|
339
207
|
const path = join(dir, name)
|
|
340
208
|
try {
|
|
341
|
-
if (statSync(path).size > MAX_SHARD_BYTES) continue
|
|
209
|
+
if (statSync(path).size > MAX_SHARD_BYTES) continue
|
|
342
210
|
const { shard, signature } = JSON.parse(readFileSync(path, 'utf-8'))
|
|
343
|
-
if (typeof shard !== 'string' || typeof signature !== 'string') continue
|
|
211
|
+
if (typeof shard !== 'string' || typeof signature !== 'string') continue
|
|
344
212
|
if (!verify(null, Buffer.from(shard), createdShardPub, Buffer.from(signature, 'base64url'))) continue
|
|
345
|
-
state.mergeShard(shard)
|
|
213
|
+
state.mergeShard(shard)
|
|
346
214
|
} catch {
|
|
347
|
-
// A malformed/partial shard
|
|
215
|
+
// A malformed/partial shard is skipped; the rest merge.
|
|
348
216
|
}
|
|
349
217
|
}
|
|
350
218
|
} finally {
|
|
@@ -352,32 +220,16 @@ function mergeChildShards(dir) {
|
|
|
352
220
|
}
|
|
353
221
|
}
|
|
354
222
|
|
|
355
|
-
//
|
|
356
|
-
//
|
|
357
|
-
// and under 'async' their async (callback + fs.promises) counterparts -- to capture them into the
|
|
358
|
-
// bundle (bundle=add|replace) or serve them from it (bundle=load). Driven off the resolved Config
|
|
359
|
-
// (state.config.fs) rather than the raw env var, so the mode can also come from stasis.config.json
|
|
360
|
-
// -- which isn't read until State is constructed. Called from initState (which fires on the first
|
|
361
|
-
// load/resolve hook, before any user code runs), AFTER the lib's own fs snapshots (fs.js /
|
|
362
|
-
// state.js / state-util.js, all taken at module-eval) and after registerHooks -- so the patch
|
|
363
|
-
// never redirects stasis's own reads, and only when the user opted in. `state` is read lazily
|
|
364
|
-
// (per call); installFsHooks is internally idempotent. A capture-side conflict (a file read twice
|
|
365
|
-
// with diverging bytes) taints the run the same way an addFile/addImport rejection does, so
|
|
366
|
-
// nothing inconsistent is written.
|
|
223
|
+
// Install the --fs reader patches. Must run AFTER stasis's own fs snapshots so they never redirect
|
|
224
|
+
// stasis's own reads.
|
|
367
225
|
function installFsHooksFromConfig() {
|
|
368
226
|
const fsMode = state.config.fs
|
|
369
227
|
if (!fsMode) return
|
|
370
228
|
installFsHooks({
|
|
371
|
-
// 'async' adds the async readers; 'sync' (the only other valid value) is sync-only.
|
|
372
229
|
async: fsMode === 'async',
|
|
373
230
|
getState: () => state,
|
|
374
|
-
// True while Node's default loader is reading a module's source: the --fs hook
|
|
375
|
-
// skips those so it captures the program's explicit reads, not module loading.
|
|
376
231
|
isLoadingModule: () => loadingModule > 0,
|
|
377
|
-
//
|
|
378
|
-
// taints the run like an addFile/addImport rejection -- nothing is written.
|
|
379
|
-
// Warn on the first taint so the discard isn't silent (the user's reads still
|
|
380
|
-
// succeed, so the run otherwise exits 0 with no bundle/lockfile and no clue).
|
|
232
|
+
// Warn on the first taint so the silent discard (reads succeed, exit 0, no artifact) has a clue.
|
|
381
233
|
markAborted: (err) => {
|
|
382
234
|
if (!aborted) console.warn(`[stasis] --fs: capture aborted, no bundle/lockfile will be written -- ${err?.message ?? err}`)
|
|
383
235
|
aborted = true
|
|
@@ -388,27 +240,10 @@ function installFsHooksFromConfig() {
|
|
|
388
240
|
function initState(root) {
|
|
389
241
|
state = new State(root, { preload: true, preloadRoot: PRELOAD_ROOT })
|
|
390
242
|
|
|
391
|
-
// Install the --fs reader patches now that Config is resolved (it carries the fs mode,
|
|
392
|
-
// possibly from stasis.config.json). Before any child can fork and before user code runs.
|
|
393
243
|
installFsHooksFromConfig()
|
|
394
244
|
|
|
395
|
-
// Root + opt-in
|
|
396
|
-
//
|
|
397
|
-
// tmpdir via mkdtemp (mode 0700, owner-only) -- NOT under the project/write-target tree, where a
|
|
398
|
-
// dir would pollute an `--fs` readdir of the root and, on a SIGKILL that skips our exit cleanup,
|
|
399
|
-
// litter the repo (a later `--fs` run could even re-capture it). tmpdir keeps the scaffolding out
|
|
400
|
-
// of the captured tree and lets the OS reap it if we're hard-killed. The location is not a secret
|
|
401
|
-
// (its path travels to children in env); forgery is stopped by the signature, and the 0700 mode
|
|
402
|
-
// keeps a different-uid peer from reading the dir's contents. ALWAYS mint our own dir + per-build
|
|
403
|
-
// ed25519 keypair, overwriting any inherited/attacker-set EXODUS_STASIS_SHARD_DIR/_KEY, so the
|
|
404
|
-
// root only ever merges from -- and rm's -- a dir it created, and only shards it can
|
|
405
|
-
// authenticate. The PRIVATE key is exported so descendants inherit it and sign their shards; the
|
|
406
|
-
// root keeps the PUBLIC key to verify. The dir is removed on every exit path (see `save`). Only
|
|
407
|
-
// the root mints (children are isChildProcess); a non-opt-in or non-capturing run creates
|
|
408
|
-
// nothing, so children no-op. NB: the dir lives under os.tmpdir(), so capture integrity assumes a
|
|
409
|
-
// trustworthy TMPDIR -- a TMPDIR an attacker controls (or a non-sticky shared one) reopens the
|
|
410
|
-
// classic tmpdir symlink race; that is outside the threat model (we defend a dir READER, not a
|
|
411
|
-
// TMPDIR controller), and /tmp's sticky bit defeats it in the normal case.
|
|
245
|
+
// Root + opt-in only, before any child can fork: ALWAYS mint our own dir (OS tmpdir, mkdtemp 0700)
|
|
246
|
+
// and keypair, overwriting any inherited/attacker-set env, so we merge only shards we can verify.
|
|
412
247
|
if (!isChildProcess && shardForwardingEnabled()) {
|
|
413
248
|
try {
|
|
414
249
|
const { publicKey, privateKey } = generateKeyPairSync('ed25519')
|
|
@@ -416,18 +251,11 @@ function initState(root) {
|
|
|
416
251
|
createdShardPub = publicKey
|
|
417
252
|
createdShardPubId = publicKey.export({ format: 'jwk' }).x // base64url(raw public key)
|
|
418
253
|
process.env.EXODUS_STASIS_SHARD_DIR = createdShardDir
|
|
419
|
-
//
|
|
420
|
-
// shards, so reading the dir (without this env) is not enough to forge one. These two vars
|
|
421
|
-
// ride process.env, so EVERY transitive descendant inherits them -- a child that itself
|
|
422
|
-
// forks/spawns a subprocess (or re-invokes a stasis bin) propagates the channel onward, and
|
|
423
|
-
// those grandchildren contribute shards too. Do NOT scrub them anywhere downstream: that would
|
|
424
|
-
// sever capture for whole subtrees the build legitimately spawns.
|
|
254
|
+
// Rides process.env so every transitive descendant inherits; do NOT scrub it downstream.
|
|
425
255
|
process.env.EXODUS_STASIS_SHARD_KEY = privateKey.export({ type: 'pkcs8', format: 'der' }).toString('base64')
|
|
426
256
|
} catch {
|
|
427
|
-
//
|
|
428
|
-
//
|
|
429
|
-
// clear both so descendants don't fall back to a channel we never minted or validated. If
|
|
430
|
-
// mkdtemp already created the dir before a later step threw, remove the (empty) dir too.
|
|
257
|
+
// Best-effort: clear any inherited (possibly attacker-set) dir/key so descendants don't fall
|
|
258
|
+
// back to a channel we never minted.
|
|
431
259
|
if (createdShardDir) { try { rmSync(createdShardDir, { recursive: true, force: true }) } catch { /* best-effort */ } }
|
|
432
260
|
createdShardDir = undefined
|
|
433
261
|
createdShardPub = undefined
|
|
@@ -436,83 +264,56 @@ function initState(root) {
|
|
|
436
264
|
delete process.env.EXODUS_STASIS_SHARD_KEY
|
|
437
265
|
}
|
|
438
266
|
} else if (!isChildProcess) {
|
|
439
|
-
// Non-capturing root
|
|
440
|
-
// dir, so shed any inherited (possibly attacker-set) EXODUS_STASIS_SHARD_DIR/_KEY rather than
|
|
441
|
-
// leave them in our env. A non-capturing root's descendants can't capture either (they inherit
|
|
442
|
-
// our non-writing config), so this severs no legitimate channel -- it just drops dead weight.
|
|
267
|
+
// Non-capturing root: shed any inherited (possibly attacker-set) shard env.
|
|
443
268
|
delete process.env.EXODUS_STASIS_SHARD_DIR
|
|
444
269
|
delete process.env.EXODUS_STASIS_SHARD_KEY
|
|
445
270
|
}
|
|
446
271
|
|
|
447
|
-
// Persist
|
|
448
|
-
//
|
|
449
|
-
// its own reasons -- a server running its own SIGINT shutdown and exiting 130, or any
|
|
450
|
-
// script that returns non-zero -- must still persist what it captured. The hazard we
|
|
451
|
-
// guard is narrower: addFile records a file's hash (which lock=add/replace then
|
|
452
|
-
// serializes) *before* a later check -- the frozen bundle's byte/format/resolution
|
|
453
|
-
// check, or a lock=add conflict against an existing lockfile -- can reject it. Writing
|
|
454
|
-
// that would bake the rejected drift into the lockfile/bundle, so a subsequent
|
|
455
|
-
// lock=frozen run would accept it. So we skip the write only when `aborted` is set, which
|
|
456
|
-
// the hooks set whenever addFile/addImport throws -- catching a stasis rejection even if
|
|
457
|
-
// user code swallows it (try/catch around a dynamic import) and exits 0.
|
|
458
|
-
//
|
|
459
|
-
// An abort that does NOT originate in addFile/addImport -- an uncaught module-not-found
|
|
460
|
-
// (thrown by Node's resolver), or a user error after a partial-but-clean capture -- does
|
|
461
|
-
// not taint, so the files captured so far ARE still written. That is intentional and
|
|
462
|
-
// safe: those recorded hashes are accurate (nothing drifted is baked in), and a later
|
|
463
|
-
// lock=frozen run fails closed on anything missing. Unhandled signals (SIGINT/SIGTERM
|
|
464
|
-
// with no handler) bypass exit hooks entirely; servers own that behavior, we don't touch it.
|
|
465
|
-
//
|
|
466
|
-
// save() runs on EVERY beforeExit/exit firing rather than latching after the first.
|
|
467
|
-
// These handlers were registered at initState -- before any user code ran -- so they fire
|
|
468
|
-
// FIRST on each event, and a require() issued by a LATER handler on the same event
|
|
469
|
-
// resolves AFTER our write. CLIs commonly log a summary at exit through a lazy require
|
|
470
|
-
// (Babel lazy interop: @react-native-community/cli-tools' logger require()s chalk on
|
|
471
|
-
// its first call): the target's bytes are usually attested via other importers, so a
|
|
472
|
-
// one-shot save silently dropped only that resolution EDGE, surfacing at bundle=load
|
|
473
|
-
// as MODULE_NOT_FOUND for a package the bundle carries. Re-running is cheap -- write()
|
|
474
|
-
// compares each artifact's serialized text and skips unchanged outputs, and the child
|
|
475
|
-
// shard path keeps its own last-written compare -- so the exit firing re-flushes
|
|
476
|
-
// whatever the beforeExit firing missed. Requires issued by a user 'exit' handler
|
|
477
|
-
// AFTER ours are the residual gap: 'exit' is the last stop, nothing later can flush.
|
|
272
|
+
// Persist on exit unless `aborted` -- deliberately NOT gated on exit code, so a clean capture
|
|
273
|
+
// exiting non-zero still persists. Must stay fully synchronous for `exit` to host it.
|
|
478
274
|
const save = () => {
|
|
479
|
-
|
|
480
|
-
// child
|
|
481
|
-
// would race it. Instead it forwards what it captured via a shard the root merges below.
|
|
482
|
-
// writeChildShard no-ops unless the channel is on AND a shard dir was inherited: a child of a
|
|
483
|
-
// non---child-process run (no dir minted) contributes nothing, while a child under
|
|
484
|
-
// --child-process (forked or a spawned `node --import` loader) inherits the dir+key and does.
|
|
275
|
+
const writing = state.config.writeLockfile || state.config.writeBundle
|
|
276
|
+
// A child forwards a shard instead of writing the artifact (root owns it; a 2nd writer races).
|
|
485
277
|
if (isChildProcess) {
|
|
486
|
-
|
|
487
|
-
// same fail-stance the root takes when `aborted`. The root re-reads + noupserts every shard
|
|
488
|
-
// so a partial one couldn't taint it, but there's no value in shipping a rejected capture.
|
|
489
|
-
if (!aborted) writeChildShard()
|
|
278
|
+
if (writing && !aborted) writeChildShard()
|
|
490
279
|
return
|
|
491
280
|
}
|
|
492
281
|
try {
|
|
493
|
-
// A stasis rejection (addFile/addImport threw -> aborted) must NOT write -- that would bake
|
|
494
|
-
// the rejected drift into the lockfile/bundle, so a later frozen run would accept it. We
|
|
495
|
-
// still tear down the shard dir in the finally below.
|
|
496
282
|
if (aborted) return
|
|
497
|
-
// Root: fold in every forked child's shard before writing, so files/edges only a child
|
|
498
|
-
// observed (e.g. a Metro worker's babel.config.js + preset/plugins) are attested too. Only
|
|
499
|
-
// when we minted a shard dir (opt-in + capturing); merges from that dir alone. On a
|
|
500
|
-
// re-flush after the dir was already removed below, the merge no-ops (opendir fails).
|
|
501
283
|
if (createdShardDir) mergeChildShards(createdShardDir)
|
|
284
|
+
// write() self-gates the artifact writes, but also VERIFIES in read-only frozen modes, so it
|
|
285
|
+
// must run in every mode.
|
|
502
286
|
state.write()
|
|
503
|
-
|
|
287
|
+
// Latch only in write modes: a read-only run persists nothing, so a later first load (e.g. a
|
|
288
|
+
// require() from another exit handler) is still attested and must not trip the load hook.
|
|
289
|
+
if (writing) saved = true
|
|
504
290
|
} finally {
|
|
505
|
-
// Always remove the shard dir we minted -- clean exit, abort, or a throwing write. This is
|
|
506
|
-
// the single owner of that cleanup (mergeChildShards no longer self-removes). A SIGKILL
|
|
507
|
-
// bypasses exit hooks, but the dir is in the OS tmpdir, so the OS reaps it.
|
|
508
291
|
if (createdShardDir) {
|
|
509
292
|
try { rmSync(createdShardDir, { recursive: true, force: true }) } catch { /* best-effort */ }
|
|
510
293
|
}
|
|
511
294
|
}
|
|
512
295
|
}
|
|
513
296
|
|
|
514
|
-
|
|
297
|
+
// `exit` alone, NOT the beforeExit/exit pair: it runs after every beforeExit handler, so one flush
|
|
298
|
+
// there sees strictly more. A process that never reaches `exit` writes nothing.
|
|
515
299
|
process.on('exit', save)
|
|
300
|
+
|
|
301
|
+
// config.shardSignalFlush (opt-in): flush the shard when a capturing CHILD is ended by SIGTERM
|
|
302
|
+
// (jest-worker's forceExit bypasses `exit`), then re-deliver the signal -- unless user code owns
|
|
303
|
+
// SIGTERM, then only flush. `userOwnedSigterm` is snapshotted at registration: a `once` that
|
|
304
|
+
// already ran is invisible to listenerCount. Must fit forceExit's 500ms window.
|
|
305
|
+
if (isChildProcess && shardForwardingEnabled() && state.config.shardSignalFlush) {
|
|
306
|
+
const userOwnedSigterm = process.listenerCount('SIGTERM') > 0
|
|
307
|
+
process.once('SIGTERM', () => {
|
|
308
|
+
try {
|
|
309
|
+
save()
|
|
310
|
+
} finally {
|
|
311
|
+
if (!userOwnedSigterm && process.listenerCount('SIGTERM') === 0) {
|
|
312
|
+
process.kill(process.pid, 'SIGTERM')
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
}
|
|
516
317
|
}
|
|
517
318
|
|
|
518
319
|
function load(url, context, nextLoad) {
|
|
@@ -525,54 +326,23 @@ function load(url, context, nextLoad) {
|
|
|
525
326
|
}
|
|
526
327
|
|
|
527
328
|
if (state && state.config.loadBundle) {
|
|
528
|
-
// node_modules scope only
|
|
529
|
-
// sources, including a workspace package symlinked into node_modules (its real path is
|
|
530
|
-
// not under node_modules, so inNodeModules() is false) -- is read from disk via Node's
|
|
531
|
-
// default loader. Gate on the canonical classification, not a raw '/node_modules/'
|
|
532
|
-
// substring: otherwise such a symlink URL would be routed to state.getFile and crash,
|
|
533
|
-
// since the file is a source deliberately omitted from the node_modules-scope bundle.
|
|
329
|
+
// node_modules scope serves only nm deps; gate on inNodeModules(), not a raw substring (symlinks).
|
|
534
330
|
if (!state.config.full && !state.inNodeModules(url)) {
|
|
535
331
|
return nextLoad(url, context)
|
|
536
332
|
}
|
|
537
333
|
const { source, format } = state.getFile(url)
|
|
538
|
-
//
|
|
539
|
-
//
|
|
540
|
-
// populate format for the load hook). Only cross-check when the chain provided one.
|
|
541
|
-
// The `formats` map this value comes from is attested against the lockfile at State
|
|
542
|
-
// construction (#mergeBundleMetadata) when a lockfile is loaded -- so a tampered
|
|
543
|
-
// bundle can't flip module<->commonjs (or a .js file to module-typescript, which
|
|
544
|
-
// would make Node strip type-argument-shaped syntax like `f<string>('x')`) for a
|
|
545
|
-
// hash-valid file. Without a lockfile the bundle is self-authoritative for formats
|
|
546
|
-
// just as it is for bytes (see getFile's hash carve-out).
|
|
334
|
+
// Cross-check format only when the resolve chain set it; the formats map is itself attested
|
|
335
|
+
// against the lockfile at construction.
|
|
547
336
|
if (context.format != null) assert.equal(format, context.format)
|
|
548
|
-
// Trust gate:
|
|
549
|
-
// can actually execute. Resource assets (`resource`, `resource:base64`),
|
|
550
|
-
// source-language bundles (solidity/php/bash/rust), and any unknown string
|
|
551
|
-
// a tampered or forward-incompatible bundle might smuggle through all fail
|
|
552
|
-
// closed here with a contextual message -- never as an opaque assertion.
|
|
553
|
-
// *-typescript formats are in the allowlist: Node's own translators strip
|
|
554
|
-
// the types after this hook returns.
|
|
337
|
+
// Trust gate: only serve a file whose attested format Node can execute; everything else fails closed.
|
|
555
338
|
if (!NODEJS_FORMATS.has(format)) refuseNonNodeFormat(format, url)
|
|
556
|
-
// node:59666
|
|
557
|
-
// module, Node's ESM->CJS translator (loadCJSModule) hands the module a
|
|
558
|
-
// *re-invented* require() that sets only `.resolve`/`.main` and omits
|
|
559
|
-
// `.cache` and `.extensions` -- unlike the require a disk-loaded CJS module
|
|
560
|
-
// gets (makeRequireFunction, which sets all four). Packages that read those
|
|
561
|
-
// (import-fresh, and anything walking require.cache / require.extensions)
|
|
562
|
-
// then throw `Cannot read properties of undefined` or misbehave -- but only
|
|
563
|
-
// under bundle=load, because that's the only mode that serves CJS via a
|
|
564
|
-
// source override. Repair the two missing properties from node:module
|
|
565
|
-
// before user code runs. Confined to executable CJS formats; ESM has no
|
|
566
|
-
// require and resources are not executed.
|
|
339
|
+
// node:59666: a load-hook-supplied CJS source gets a re-invented require() missing .cache/.extensions.
|
|
567
340
|
if (CJS_FORMATS.has(format)) {
|
|
568
341
|
return { source: repairCjsRequire(source), format, shortCircuit: true }
|
|
569
342
|
}
|
|
570
343
|
return { source, format, shortCircuit: true }
|
|
571
344
|
}
|
|
572
345
|
|
|
573
|
-
// nextLoad runs Node's default loader, which reads a CJS module's source through
|
|
574
|
-
// the public fs.readFileSync the --fs hook patches. Mark the window so that hook
|
|
575
|
-
// doesn't re-capture the module as a filesystem read (it's recorded as code below).
|
|
576
346
|
loadingModule++
|
|
577
347
|
let result
|
|
578
348
|
try {
|
|
@@ -587,13 +357,6 @@ function load(url, context, nextLoad) {
|
|
|
587
357
|
// Node's CJS loader may return source=null and read from disk itself; capture it for the bundle
|
|
588
358
|
if (source == null && format === 'commonjs') source = readFileSync(fileURLToPath(url))
|
|
589
359
|
|
|
590
|
-
// Entry detection: the FIRST non-builtin load through this hook is the
|
|
591
|
-
// entry. `entryUnseen` (set true on every state init) flips to false after
|
|
592
|
-
// the first load fires here. The pre-existing heuristic was `!state` --
|
|
593
|
-
// tight while initState was lazy, but a plugin-driven `ensureStateForLoader`
|
|
594
|
-
// path now eager-inits from cwd before the first load, so `!state` would
|
|
595
|
-
// miss the entry. The flag lives module-local rather than on State because
|
|
596
|
-
// it's strictly a hook-firing-order signal.
|
|
597
360
|
const isEntry = entryUnseen
|
|
598
361
|
entryUnseen = false
|
|
599
362
|
if (!state) {
|
|
@@ -603,10 +366,7 @@ function load(url, context, nextLoad) {
|
|
|
603
366
|
}
|
|
604
367
|
|
|
605
368
|
assert.equal(saved, false)
|
|
606
|
-
// A
|
|
607
|
-
// conflict against an existing lockfile, ... -- means stasis rejected this capture. Flag
|
|
608
|
-
// it so save() persists nothing, even if user code swallows the rejection (a try/catch
|
|
609
|
-
// around a dynamic import) and the process then exits cleanly.
|
|
369
|
+
// A rejected capture must never be written, even if user code swallows the throw.
|
|
610
370
|
try {
|
|
611
371
|
state.addFile(url, { source, format, isEntry })
|
|
612
372
|
} catch (err) {
|
|
@@ -625,53 +385,37 @@ function resolve(specifier, context, nextResolve) {
|
|
|
625
385
|
}
|
|
626
386
|
|
|
627
387
|
if (context.importAttributes !== undefined) {
|
|
628
|
-
const expectedAttrs = Object.create(null) //
|
|
388
|
+
const expectedAttrs = Object.create(null) // saving not supported yet; enforce expected attrs
|
|
629
389
|
if (extname(specifier) === '.json' && context.importAttributes?.type) expectedAttrs.type = 'json'
|
|
630
390
|
assert.deepStrictEqual(context.importAttributes, expectedAttrs)
|
|
631
391
|
}
|
|
632
392
|
|
|
633
393
|
const { parentURL, conditions, importAttributes } = context
|
|
634
394
|
|
|
635
|
-
//
|
|
636
|
-
// disk, so state must be initialised before nextResolve runs. node_modules scope reads
|
|
637
|
-
// non-nm sources (including the entry) from disk, so state is initialised later in load().
|
|
395
|
+
// Full-scope load serves the entry from the bundle, so state must exist before nextResolve.
|
|
638
396
|
if (!state && !parentURL && process.env.EXODUS_STASIS_BUNDLE === 'load'
|
|
639
397
|
&& process.env.EXODUS_STASIS_SCOPE !== 'node_modules') {
|
|
640
398
|
initState(process.cwd())
|
|
641
399
|
}
|
|
642
400
|
|
|
643
401
|
if (state && state.config.loadBundle) {
|
|
644
|
-
//
|
|
645
|
-
// between non-nm sources then resolve to local files, and bare specifiers resolve via
|
|
646
|
-
// the on-disk node_modules layout -- but the load() hook still serves the resulting
|
|
647
|
-
// node_modules target from the bundle, so package contents need not be on disk. A
|
|
648
|
-
// workspace source linked into node_modules is a non-nm parent (its canonical path is
|
|
649
|
-
// outside node_modules), so it too is deferred -- hence inNodeModules() rather than a
|
|
650
|
-
// raw '/node_modules/' substring on parentURL.
|
|
402
|
+
// nm scope defers non-nm parents to Node's resolver; gate on inNodeModules() to cover symlinks.
|
|
651
403
|
if (!state.config.full && !(parentURL && state.inNodeModules(parentURL))) {
|
|
652
404
|
return nextResolve(specifier)
|
|
653
405
|
}
|
|
654
|
-
//
|
|
655
|
-
//
|
|
656
|
-
// ESM-to-CJS translator require() targets both arrive that way) or a bare entry
|
|
657
|
-
// specifier we anchor to cwd -- either way, skip nextResolve so the entry file
|
|
658
|
-
// doesn't need to exist on disk.
|
|
406
|
+
// A bare specifier with a parent resolves from the recorded import map; otherwise skip
|
|
407
|
+
// nextResolve so the entry needn't be on disk.
|
|
659
408
|
if (parentURL && !specifier.startsWith('file:')) {
|
|
660
409
|
const { url, format } = state.getImport(parentURL, specifier, { conditions, importAttributes })
|
|
661
|
-
//
|
|
662
|
-
// tampered/asset cases load() catches, just earlier and with the parent
|
|
663
|
-
// module's URL in the stack trace for easier diagnosis. The load gate
|
|
664
|
-
// remains the load-bearing one (a sibling resolver could deliver a target
|
|
665
|
-
// that bypasses this branch); we throw here as a friendlier failure point.
|
|
410
|
+
// Friendlier error at resolve time; the load gate is the load-bearing one.
|
|
666
411
|
if (format != null && !NODEJS_FORMATS.has(format)) refuseNonNodeFormat(format, url)
|
|
667
412
|
return { url, format, importAttributes: undefined, shortCircuit: true }
|
|
668
413
|
}
|
|
669
414
|
const url = specifier.startsWith('file:')
|
|
670
415
|
? specifier
|
|
671
416
|
: pathToFileURL(resolvePath(process.cwd(), specifier)).toString()
|
|
672
|
-
// A forked child's main module
|
|
673
|
-
//
|
|
674
|
-
// rejects anything the bundle doesn't attest. The root run stays strict.
|
|
417
|
+
// A forked child's main module isn't a declared CLI entry, so skip assertEntry (getFile still
|
|
418
|
+
// fails closed on anything unattested).
|
|
675
419
|
if (!parentURL && state.config.full && !forkedChild) state.assertEntry(url)
|
|
676
420
|
const format = state.getFormat(url)
|
|
677
421
|
if (format != null && !NODEJS_FORMATS.has(format)) refuseNonNodeFormat(format, url)
|
|
@@ -683,8 +427,6 @@ function resolve(specifier, context, nextResolve) {
|
|
|
683
427
|
|
|
684
428
|
assert.equal(res.importAttributes, undefined) // unsupported yet
|
|
685
429
|
if (parentURL) assert.ok(state)
|
|
686
|
-
// As in load(): a rejected resolution (frozen redirect mismatch, conflict, ...) must keep
|
|
687
|
-
// the captured state from being written, swallowed or not.
|
|
688
430
|
if (state) {
|
|
689
431
|
try {
|
|
690
432
|
state.addImport(parentURL, specifier, url, { conditions, format, importAttributes })
|
|
@@ -698,57 +440,28 @@ function resolve(specifier, context, nextResolve) {
|
|
|
698
440
|
|
|
699
441
|
let installed = false
|
|
700
442
|
|
|
701
|
-
//
|
|
702
|
-
// running under the stasis loader" even before the lazy initState fires --
|
|
703
|
-
// a bundler plugin constructed during webpack's config-parse can run
|
|
704
|
-
// AHEAD of the first user-file load hook, leaving State.preload null.
|
|
705
|
-
// resolvePluginState reads this to force-init State from cwd in that
|
|
706
|
-
// window so the plugin inherits the loader's config rather than erroring
|
|
707
|
-
// on the default lock mode.
|
|
443
|
+
// Exported so plugins.js can detect "under the stasis loader" before the lazy initState fires.
|
|
708
444
|
export function isLoaderInstalled() {
|
|
709
445
|
return installed
|
|
710
446
|
}
|
|
711
447
|
|
|
712
|
-
//
|
|
713
|
-
// yet. Used by plugins.js's resolvePluginState to ensure State.preload is
|
|
714
|
-
// available when the plugin constructor runs ahead of the load hook. A
|
|
715
|
-
// subsequent first-load through the load hook detects "state already set"
|
|
716
|
-
// and skips its own lazy initState, but still marks isEntry correctly via
|
|
717
|
-
// the entryUnseen flag.
|
|
448
|
+
// For plugins.js when a plugin constructor runs ahead of the load hook.
|
|
718
449
|
export function ensureStateForLoader() {
|
|
719
450
|
if (state) return
|
|
720
451
|
initState(process.cwd())
|
|
721
452
|
}
|
|
722
453
|
|
|
723
|
-
//
|
|
724
|
-
// (loader.js here; the @exodus/stasis package's loader and --mock loader) so a
|
|
725
|
-
// host can evaluate this lib -- snapshotting node:fs/path/etc. for stasis's own
|
|
726
|
-
// use -- and compose extra setup (e.g. the tooling package's --mock side-effect
|
|
727
|
-
// denials) via static import order BEFORE the hooks are registered, with no
|
|
728
|
-
// env-var coordination. Idempotent.
|
|
454
|
+
// Separate from the --import entry points so a host can compose setup before the hooks register.
|
|
729
455
|
export function install() {
|
|
730
456
|
if (installed) return
|
|
731
457
|
installed = true
|
|
732
458
|
registerHooks({ load, resolve })
|
|
733
459
|
patchCjsResolution()
|
|
734
|
-
//
|
|
735
|
-
// stasis.config.json, which isn't read until State is constructed, so installation is
|
|
736
|
-
// deferred to initState() (see installFsHooksFromConfig) -- which still runs before any
|
|
737
|
-
// user code. install() only registers the module hooks the fs patch composes on top of.
|
|
460
|
+
// The --fs reader patches are deferred to initState (their mode may come from stasis.config.json).
|
|
738
461
|
}
|
|
739
462
|
|
|
740
|
-
// registerHooks
|
|
741
|
-
//
|
|
742
|
-
// builds when it evaluates a CJS module: that require resolves through
|
|
743
|
-
// Module._resolveFilename against disk before any hook runs. So a CJS module we
|
|
744
|
-
// serve from the bundle (`import chalk from 'chalk'`, chalk being CommonJS) has
|
|
745
|
-
// its nested require()s resolved on disk -- and once node_modules is pruned or
|
|
746
|
-
// shipped-without, `require('escape-string-regexp')` throws MODULE_NOT_FOUND
|
|
747
|
-
// before our hooks can serve it, i.e. the bundle isn't self-contained for a CJS
|
|
748
|
-
// dependency graph. Shim _resolveFilename in load mode to resolve such a target
|
|
749
|
-
// from the bundle's recorded import map and return its (possibly non-existent on
|
|
750
|
-
// disk) path; the load() hook then serves the bytes. For an edge the bundle
|
|
751
|
-
// doesn't record, or outside load mode, fall back to Node's native resolution.
|
|
463
|
+
// registerHooks doesn't intercept the require() the ESM->CJS translator builds for a CJS module (it
|
|
464
|
+
// resolves through Module._resolveFilename against disk), so shim that to resolve from the import map.
|
|
752
465
|
function patchCjsResolution() {
|
|
753
466
|
const original = Module._resolveFilename
|
|
754
467
|
Module._resolveFilename = function (request, parent, ...rest) {
|
|
@@ -756,48 +469,23 @@ function patchCjsResolution() {
|
|
|
756
469
|
if (loadMode && typeof request === 'string' && !isBuiltin(request)) {
|
|
757
470
|
if (typeof parent?.filename === 'string') {
|
|
758
471
|
const parentURL = pathToFileURL(parent.filename).toString()
|
|
759
|
-
// Same scope gate the resolve hook applies (see above): node_modules scope
|
|
760
|
-
// only serves dependency parents from the bundle; workspace/app code is left
|
|
761
|
-
// to Node's native resolution (read from disk). Without this the shim would
|
|
762
|
-
// silently take over CJS resolution for app files too.
|
|
763
472
|
if (state.config.full || state.inNodeModules(parentURL)) {
|
|
764
473
|
const resolved = state.resolveBundled(parentURL, request)
|
|
765
474
|
if (resolved !== undefined) return resolved
|
|
766
475
|
}
|
|
767
476
|
} else if (isAbsolute(request)) {
|
|
768
|
-
// NO parent,
|
|
769
|
-
//
|
|
770
|
-
//
|
|
771
|
-
//
|
|
772
|
-
// (the 'commonjs-sync' translator, reached whenever a require() pulls in an ESM graph
|
|
773
|
-
// that imports a CJS dep; also createCJSNoSourceModuleWrap). resolveForCJSWithHooks
|
|
774
|
-
// then bypasses the resolve hook entirely, so this shim is the ONLY interception
|
|
775
|
-
// point. An absolute path can only arrive here from a prior successful resolution
|
|
776
|
-
// (ours, from the bundle -- or Node's own), so when the bundle attests that exact
|
|
777
|
-
// FILE under the same scope gate, the resolution is the identity (returned in
|
|
778
|
-
// path.resolve()d form, matching native's normalized cache-key contract). Falling
|
|
779
|
-
// through to native would stat a file the bundle carries but disk (pruned /
|
|
780
|
-
// never-shipped node_modules) may not -- MODULE_NOT_FOUND on a servable path.
|
|
781
|
-
// getFsStat === 'file' (not mere presence) keeps a --fs-captured directory
|
|
782
|
-
// LISTING from hijacking native directory->main/index resolution. This widens
|
|
783
|
-
// parentless reachability to any attested in-scope file when disk lacks the path;
|
|
784
|
-
// that is bounded by attestation and the load hook's gates (getFile +
|
|
785
|
-
// NODEJS_FORMATS), which remain the load-bearing checks.
|
|
786
|
-
// Node >=24.18 forwards pre-resolved context for hook-resolved modules, masking the
|
|
787
|
-
// common case; on 24.14-24.17 (our supported floor) every commonjs-sync evaluation
|
|
788
|
-
// re-resolves through here, and the no-parent shape survives on >=24.18 too (the
|
|
789
|
-
// deferred-resolution branch and createCJSNoSourceModuleWrap still take it).
|
|
477
|
+
// NO parent, absolute request: Node's ESM<->CJS interop re-enters the CJS loader with hooks
|
|
478
|
+
// SKIPPED, so this shim is the only interception point. hasFsFileContent (not a dir/stat
|
|
479
|
+
// record) keeps a listing or payload-free stat from hijacking resolution. Node 24.14-24.17
|
|
480
|
+
// re-resolve here every commonjs-sync eval; the no-parent shape survives on >=24.18 too.
|
|
790
481
|
const url = pathToFileURL(request).toString()
|
|
791
|
-
if ((state.config.full || state.inNodeModules(url)) && state.
|
|
482
|
+
if ((state.config.full || state.inNodeModules(url)) && state.hasFsFileContent(url)) {
|
|
792
483
|
return resolvePath(request)
|
|
793
484
|
}
|
|
794
485
|
}
|
|
795
486
|
}
|
|
796
487
|
const resolved = original.call(this, request, parent, ...rest)
|
|
797
|
-
// Capture: require.resolve()
|
|
798
|
-
// resolve through native Module._resolveFilename WITHOUT firing the resolve
|
|
799
|
-
// hook, so addImport never sees them. Observe the resolution here; write()'s
|
|
800
|
-
// backfill records the ones the hook missed (deduped, in-bundle targets only).
|
|
488
|
+
// Capture: require.resolve() and some CJS require()s never fire the resolve hook.
|
|
801
489
|
if (state && !loadMode && typeof request === 'string'
|
|
802
490
|
&& !isBuiltin(request) && typeof parent?.filename === 'string') {
|
|
803
491
|
state.observeResolution(pathToFileURL(parent.filename).toString(), request, pathToFileURL(resolved).toString())
|
|
@@ -805,16 +493,8 @@ function patchCjsResolution() {
|
|
|
805
493
|
return resolved
|
|
806
494
|
}
|
|
807
495
|
|
|
808
|
-
// Capture: Module._load's fast path
|
|
809
|
-
//
|
|
810
|
-
// _resolveFilename -- so a SECOND sibling importer's require() of a target the first
|
|
811
|
-
// sibling already loaded never reaches the observe shim above, and the edge is
|
|
812
|
-
// recorded only under the first importer (under-recording). _load runs for EVERY
|
|
813
|
-
// require() including cache hits, so force the resolution here to observe the true
|
|
814
|
-
// per-importer edge; _resolveFilename is itself cached, so this is cheap, and a
|
|
815
|
-
// resolution error is left for the original _load to raise (behavior unchanged).
|
|
816
|
-
// We call the PROPERTY `Module._resolveFilename` (our shim above), not a captured
|
|
817
|
-
// reference, so the call routes through observeResolution.
|
|
496
|
+
// Capture: Module._load's cache fast path can skip _resolveFilename, so a second require() of the
|
|
497
|
+
// same target goes unrecorded. Force it through the PROPERTY (our shim) to observe the edge.
|
|
818
498
|
const originalLoad = Module._load
|
|
819
499
|
Module._load = function (request, parent, ...rest) {
|
|
820
500
|
if (state && !state.config.loadBundle && typeof request === 'string'
|