@exodus/stasis-core 1.0.0-beta.0 → 1.0.0-beta.10
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 +78 -44
- package/package.json +14 -4
- package/src/add.js +317 -0
- package/src/artifact-util.js +652 -0
- package/src/brotli.js +15 -16
- package/src/bundle-util.js +379 -0
- package/src/bundle.js +217 -161
- package/src/config.js +203 -282
- package/src/extract.js +151 -0
- package/src/fs.js +224 -530
- package/src/hooks.js +327 -527
- package/src/host.js +45 -0
- package/src/loader.js +1 -3
- package/src/lockfile.js +67 -97
- package/src/prune.js +90 -272
- package/src/shard.js +68 -0
- package/src/state-util.js +13 -31
- package/src/state.js +1028 -1561
- package/src/util.js +299 -161
package/src/extract.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
3
|
+
import { brotliDecompressSync } from 'node:zlib'
|
|
4
|
+
|
|
5
|
+
import { Bundle } from './bundle.js'
|
|
6
|
+
import { Lockfile } from './lockfile.js'
|
|
7
|
+
import { sha512integrity } from './state-util.js'
|
|
8
|
+
import { EXECUTE_BITS, canObserveExecuteBits, moduleFileKey, moduleInfo } from './util.js'
|
|
9
|
+
|
|
10
|
+
const FILE_LOCK = 'stasis.lock.json'
|
|
11
|
+
|
|
12
|
+
// Buffer.from silently drops invalid chars, so a tampered `resource:base64` could hash-spoof; require a fixed point.
|
|
13
|
+
function assertCanonicalBase64(content, file) {
|
|
14
|
+
const buf = Buffer.from(content, 'base64')
|
|
15
|
+
if (buf.toString('base64') !== content) {
|
|
16
|
+
throw new Error(`extract: non-canonical base64 in resource:base64 content for ${file}`)
|
|
17
|
+
}
|
|
18
|
+
return buf
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function lockfileFromBundle(bundle) {
|
|
22
|
+
const modules = new Map()
|
|
23
|
+
for (const [dir, { files, ...info }] of bundle.modules) {
|
|
24
|
+
const hashed = Object.create(null)
|
|
25
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
26
|
+
// Hash the raw on-disk bytes, so decode 'resource:base64' back first.
|
|
27
|
+
const file = moduleFileKey(dir, rel)
|
|
28
|
+
hashed[rel] = sha512integrity(bundle.formats.get(file) === 'resource:base64' ? assertCanonicalBase64(content, file) : content)
|
|
29
|
+
}
|
|
30
|
+
modules.set(dir, moduleInfo({ ...info, files: hashed }))
|
|
31
|
+
}
|
|
32
|
+
// Carry imports+formats across, else the derived lockfile is bytes-only and a later frozen run skips those cross-checks.
|
|
33
|
+
return new Lockfile({
|
|
34
|
+
config: bundle.config,
|
|
35
|
+
entries: bundle.entries,
|
|
36
|
+
modules,
|
|
37
|
+
imports: bundle.imports,
|
|
38
|
+
formats: bundle.formats,
|
|
39
|
+
// Same file set as the bundle, so its executable list transfers as-is.
|
|
40
|
+
executable: bundle.executable,
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function extractCommand({ cwd = process.cwd(), bundleFile, output, logLabel = 'stasis-core' } = {}) {
|
|
45
|
+
if (!bundleFile) throw new Error('extract: a bundle file is required')
|
|
46
|
+
const bundleAbs = resolve(cwd, bundleFile)
|
|
47
|
+
if (!existsSync(bundleAbs)) throw new Error(`extract: bundle file not found: ${bundleAbs}`)
|
|
48
|
+
|
|
49
|
+
let bundle
|
|
50
|
+
try {
|
|
51
|
+
bundle = Bundle.parse(brotliDecompressSync(readFileSync(bundleAbs)).toString('utf-8'))
|
|
52
|
+
} catch (cause) {
|
|
53
|
+
// Bundle.parse throws message-less asserts; wrap so the user learns which file failed.
|
|
54
|
+
throw new Error(`extract: not a valid stasis bundle: ${bundleAbs}`, { cause })
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// v0 bundles have null metadata, so no lockfile can be derived (serialize requires name+version).
|
|
58
|
+
const withLockfile = bundle.version === Bundle.VERSION
|
|
59
|
+
|
|
60
|
+
const outDir = resolve(cwd, output ?? '.')
|
|
61
|
+
const outPrefix = outDir === sep ? outDir : `${outDir}${sep}`
|
|
62
|
+
const lockAbs = join(outDir, FILE_LOCK)
|
|
63
|
+
|
|
64
|
+
// Plan-first: every target is validated inside outDir before any write; walk modules, not the last-wins
|
|
65
|
+
// `sources` getter. Containment is lexical -- symlinks are followed, so untrusted bundles need a fresh dir (doc/extract.md).
|
|
66
|
+
const writes = []
|
|
67
|
+
const targets = new Set()
|
|
68
|
+
let executables = 0
|
|
69
|
+
for (const [dir, { files }] of bundle.modules) {
|
|
70
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
71
|
+
// A `directory` capture is a listing at the dir's own path, not a file to write (its children recreate the dir).
|
|
72
|
+
const key = moduleFileKey(dir, rel)
|
|
73
|
+
if (bundle.formats.get(key) === 'directory') continue
|
|
74
|
+
// The WRITE path keeps the plain join, so a non-`directory` rel === '' fails the canonical-path
|
|
75
|
+
// check instead of writing at the bucket root.
|
|
76
|
+
const file = dir === '.' ? rel : `${dir}/${rel}`
|
|
77
|
+
if (typeof content !== 'string') throw new Error(`extract: bundle file content is not a string: ${file}`)
|
|
78
|
+
const abs = resolve(outDir, file)
|
|
79
|
+
const relToOut = relative(outDir, abs)
|
|
80
|
+
// The `..` test is `../`-aware, NOT a bare startsWith('..') -- that would reject a legit `..foo` filename.
|
|
81
|
+
if (relToOut === '..' || relToOut.startsWith(`..${sep}`) || isAbsolute(relToOut) || !abs.startsWith(outPrefix)) {
|
|
82
|
+
throw new Error(`extract: bundle path escapes output dir: ${file}`)
|
|
83
|
+
}
|
|
84
|
+
// A non-canonical path changes under the resolve+relative round-trip; writing it desyncs tree from lockfile.
|
|
85
|
+
if (relToOut !== file) throw new Error(`extract: non-canonical bundle path: ${file}`)
|
|
86
|
+
if (targets.has(abs)) throw new Error(`extract: duplicate bundle path: ${file}`)
|
|
87
|
+
if (withLockfile && abs === lockAbs) {
|
|
88
|
+
throw new Error(`extract: bundle contains ${FILE_LOCK}, which would collide with the derived lockfile`)
|
|
89
|
+
}
|
|
90
|
+
targets.add(abs)
|
|
91
|
+
// Executability rides the plan tuple, so the chmod can only ever reach a file we actually wrote.
|
|
92
|
+
const exec = bundle.executable.has(key)
|
|
93
|
+
if (exec) executables += 1
|
|
94
|
+
writes.push([abs, bundle.formats.get(key) === 'resource:base64' ? assertCanonicalBase64(content, file) : content, exec])
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// A planned file must not also be a parent directory of another, else the writes throw halfway through.
|
|
98
|
+
for (const [abs] of writes) {
|
|
99
|
+
for (let d = dirname(abs); d.length > outDir.length; d = dirname(d)) {
|
|
100
|
+
if (targets.has(d)) {
|
|
101
|
+
throw new Error(`extract: bundle path used as both file and directory: ${relative(outDir, d)}`)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const lockText = withLockfile ? lockfileFromBundle(bundle).serialize() : null
|
|
106
|
+
|
|
107
|
+
// Only the execute bit is attested, so only it is touched -- rw bits stay as written (never widen a 0600 target)
|
|
108
|
+
// and setuid/setgid/sticky are cleared. Applied to every written file; skipped for v0, which attests no modes.
|
|
109
|
+
const withModes = withLockfile && canObserveExecuteBits()
|
|
110
|
+
let chmodFailures = 0
|
|
111
|
+
const applyMode = (abs, exec) => {
|
|
112
|
+
let stats
|
|
113
|
+
try {
|
|
114
|
+
stats = lstatSync(abs)
|
|
115
|
+
} catch {
|
|
116
|
+
chmodFailures += 1
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
// Never chmod through a symlink: +x on a link's target could make an arbitrary file outside outDir runnable.
|
|
120
|
+
if (stats.isSymbolicLink()) return
|
|
121
|
+
const perms = stats.mode & 0o7777
|
|
122
|
+
const rwx = perms & 0o777
|
|
123
|
+
// Execute wherever readable, with owner-execute as a floor so an aggressive umask can't defeat it.
|
|
124
|
+
const want = exec ? rwx | ((rwx & 0o444) >> 2) | 0o100 : rwx & ~EXECUTE_BITS
|
|
125
|
+
if (want === perms) return
|
|
126
|
+
// Tolerated per file: a mode-less filesystem (vfat/exFAT/CIFS) accepts the write and rejects the chmod.
|
|
127
|
+
try {
|
|
128
|
+
chmodSync(abs, want)
|
|
129
|
+
} catch {
|
|
130
|
+
chmodFailures += 1
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const [abs, content, exec] of writes) {
|
|
135
|
+
mkdirSync(dirname(abs), { recursive: true })
|
|
136
|
+
writeFileSync(abs, content)
|
|
137
|
+
if (withModes) applyMode(abs, exec)
|
|
138
|
+
}
|
|
139
|
+
mkdirSync(outDir, { recursive: true }) // for an empty bundle, where no write created it
|
|
140
|
+
if (withLockfile) writeFileSync(lockAbs, lockText)
|
|
141
|
+
|
|
142
|
+
const execNote = executables > 0 ? ` (${executables} executable)` : ''
|
|
143
|
+
console.warn(`[${logLabel}] Extracted ${writes.length} file(s)${execNote}${withLockfile ? ` and ${FILE_LOCK}` : ''} to ${outDir}`)
|
|
144
|
+
if (chmodFailures > 0) {
|
|
145
|
+
console.warn(`[${logLabel}] Warning: could not set file modes on ${chmodFailures} file(s) (the filesystem may not support them); contents were written`)
|
|
146
|
+
}
|
|
147
|
+
if (!withLockfile) {
|
|
148
|
+
console.warn(`[${logLabel}] Warning: legacy v0 bundle records no package name/version, so ${FILE_LOCK} can not be restored and was not written`)
|
|
149
|
+
}
|
|
150
|
+
return { dir: outDir, files: writes.length, executable: executables }
|
|
151
|
+
}
|