@ai-ecoverse/slicc-kernel 1.195.30 → 1.195.31

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.
@@ -5161,7 +5161,7 @@ function satisfies(version, range2) {
5161
5161
  }
5162
5162
 
5163
5163
  // src/version.ts
5164
- var KERNEL_VERSION = true ? "1.195.30" : "0.0.0-dev";
5164
+ var KERNEL_VERSION = true ? "1.195.31" : "0.0.0-dev";
5165
5165
  var OS_RELEASE = "6.0.0-slicc";
5166
5166
  function procVersion() {
5167
5167
  return `Linux version ${OS_RELEASE} (slicc-kernel ${KERNEL_VERSION}) #1 SMP PREEMPT_DYNAMIC
@@ -5570,12 +5570,12 @@ function binfmtEntry(pkg, key, value2) {
5570
5570
  return { name: key, pkg, command: value2, preserveArgv0: false };
5571
5571
  }
5572
5572
  async function scanBinfmts(fs, modules, warn = () => {
5573
- }, readManifest = (path) => fs.readFile(path)) {
5573
+ }) {
5574
5574
  const found = /* @__PURE__ */ new Map();
5575
5575
  for (const pkg of await packagesIn(fs, modules)) {
5576
5576
  let manifest;
5577
5577
  try {
5578
- manifest = JSON.parse(await readManifest(`${pkg}/package.json`));
5578
+ manifest = JSON.parse(await fs.readFile(`${pkg}/package.json`));
5579
5579
  } catch {
5580
5580
  continue;
5581
5581
  }
@@ -9217,6 +9217,10 @@ var Integrity = class {
9217
9217
  const found = await this.real(path, asked, real2);
9218
9218
  if (found) await this.verify(found.name, bytes, caller, EXEC_OPS);
9219
9219
  }
9220
+ async checkManifest(path, real2, hash, caller) {
9221
+ const found = await this.real(path, void 0, real2);
9222
+ if (found) await this.checkHash(found.name, hash, caller, EXEC_OPS);
9223
+ }
9220
9224
  get readsChecked() {
9221
9225
  return this.sources.length > 0;
9222
9226
  }
@@ -9259,7 +9263,9 @@ var Integrity = class {
9259
9263
  return st?.isFile && ((st.mode ?? 0) & 73) !== 0 ? found : void 0;
9260
9264
  }
9261
9265
  async verify(name2, bytes, caller, ops) {
9262
- const actual = await sha2562(bytes);
9266
+ return this.checkHash(name2, await sha2562(bytes), caller, ops);
9267
+ }
9268
+ async checkHash(name2, actual, caller, ops) {
9263
9269
  const denial = { pid: caller.pid, uid: caller.uid, path: name2, actual };
9264
9270
  const tagged = caller.tag !== void 0 ? { tag: caller.tag } : {};
9265
9271
  let expected;
@@ -18217,6 +18223,11 @@ var SudoReads = class {
18217
18223
  access;
18218
18224
  reads = /* @__PURE__ */ new Map();
18219
18225
  names = /* @__PURE__ */ new Map();
18226
+ documents = /* @__PURE__ */ new Map();
18227
+ manifestFiles = /* @__PURE__ */ new Map();
18228
+ observed = /* @__PURE__ */ new Set();
18229
+ interpreters = [];
18230
+ chainSha256;
18220
18231
  operandsFromEnd = /* @__PURE__ */ new Map();
18221
18232
  snapshots;
18222
18233
  constructor(access2, snapshots = /* @__PURE__ */ new Map()) {
@@ -18235,16 +18246,41 @@ var SudoReads = class {
18235
18246
  const saved = this.snapshots.get(real2);
18236
18247
  return saved ? Promise.resolve(saved) : this.access.read(real2);
18237
18248
  }
18238
- async manifest(path, verify) {
18249
+ manifest(path, verify, raw2) {
18250
+ let found = this.documents.get(path);
18251
+ if (!found) {
18252
+ found = this.readManifest(path, verify, raw2);
18253
+ this.documents.set(path, found);
18254
+ }
18255
+ return found;
18256
+ }
18257
+ async readManifest(path, verify, raw2) {
18239
18258
  const real2 = await this.access.real(path);
18240
18259
  this.saw(path, real2);
18241
- const bytes = new Uint8Array(await this.raw(real2));
18242
- await verify(real2, bytes);
18243
- const read = await fingerprint(bytes);
18244
- const earlier = this.reads.get(real2);
18245
- const consistent = !earlier || (await earlier)?.hash === read.hash;
18246
- this.reads.set(real2, Promise.resolve(consistent ? read : void 0));
18247
- return new TextDecoder().decode(bytes);
18260
+ const { text, read } = await this.document(real2, raw2);
18261
+ this.observed.add(path);
18262
+ await verify(real2, read.hash);
18263
+ return text;
18264
+ }
18265
+ document(real2, raw2) {
18266
+ let found = this.manifestFiles.get(real2);
18267
+ if (!found) {
18268
+ const saved = this.snapshots.get(real2);
18269
+ found = (saved ? Promise.resolve(saved) : raw2(real2)).then(async (data) => {
18270
+ const bytes = new Uint8Array(data);
18271
+ return { text: new TextDecoder().decode(bytes), read: await fingerprint(bytes) };
18272
+ });
18273
+ this.manifestFiles.set(real2, found);
18274
+ const earlier = this.reads.get(real2);
18275
+ this.reads.set(
18276
+ real2,
18277
+ found.then(
18278
+ async ({ read }) => !earlier || (await earlier)?.hash === read.hash ? read : void 0,
18279
+ () => void 0
18280
+ )
18281
+ );
18282
+ }
18283
+ return found;
18248
18284
  }
18249
18285
  saw(path, real2) {
18250
18286
  const seen = this.names.get(path);
@@ -18258,10 +18294,15 @@ var SudoReads = class {
18258
18294
  this.saw(path, real2);
18259
18295
  return (await this.bytes(real2))?.head;
18260
18296
  }
18297
+ *inputs(paths) {
18298
+ const primary = new Set(paths);
18299
+ yield* primary;
18300
+ for (const path of this.observed) if (!primary.has(path)) yield path;
18301
+ }
18261
18302
  async bind(paths) {
18262
18303
  const hashes = {};
18263
18304
  const routes = {};
18264
- for (const path of new Set(paths)) {
18305
+ for (const path of this.inputs(paths)) {
18265
18306
  if (await this.access.trusted(path)) continue;
18266
18307
  const real2 = await this.access.real(path);
18267
18308
  routes[path] = real2;
@@ -18272,7 +18313,12 @@ var SudoReads = class {
18272
18313
  const scripts = [...new Set(this.operandsFromEnd.values())].filter(
18273
18314
  (path) => Object.hasOwn(routes, path)
18274
18315
  );
18275
- const binding = { hashes, routes, ...scripts.length ? { scripts } : {} };
18316
+ const binding = {
18317
+ hashes,
18318
+ routes,
18319
+ ...scripts.length ? { scripts } : {},
18320
+ ...this.chainSha256 ? { chainSha256: this.chainSha256 } : {}
18321
+ };
18276
18322
  if (!Object.keys(routes).some((path) => path !== routes[path])) return binding;
18277
18323
  const pairs = Object.keys(routes).sort().map((path) => [path, routes[path]]);
18278
18324
  const routeSha256 = await sha2562(new TextEncoder().encode(JSON.stringify(pairs)));
@@ -19339,8 +19385,14 @@ function changedFile(approved, now) {
19339
19385
  function pinned(rule2, hashes, routeSha256) {
19340
19386
  return rule2.sha256 === void 0 || rule2.routeSha256 === routeSha256 && changedFile(rule2.sha256, hashes) === void 0;
19341
19387
  }
19342
- function stampOf({ hashes, routeSha256 }) {
19343
- return routeSha256 ? { ...hashes, routeSha256 } : hashes;
19388
+ function proofOf({ routeSha256, chainSha256 }) {
19389
+ return { ...routeSha256 ? { routeSha256 } : {}, ...chainSha256 ? { chainSha256 } : {} };
19390
+ }
19391
+ function changedDecision(hashes, proof, now, path) {
19392
+ return changedFile(hashes, now.hashes) ?? (proof.routeSha256 !== now.routeSha256 || proof.chainSha256 !== now.chainSha256 ? path : void 0);
19393
+ }
19394
+ function stampOf(files) {
19395
+ return Object.keys(files.hashes).length ? { ...files.hashes, ...proofOf(files) } : files.hashes;
19344
19396
  }
19345
19397
  var REPLAY_ID = /^[!-~]{1,256}$/;
19346
19398
  var REPLAY_TAGS_MAX = 64;
@@ -19355,13 +19407,23 @@ function words(value2) {
19355
19407
  const bytes = value2.reduce((n, s) => n + s.length + 1, 0);
19356
19408
  return bytes <= REPLAY_BYTES_MAX && !value2.some((s) => s.includes("\0"));
19357
19409
  }
19410
+ var FACT_OVERHEAD = 66;
19411
+ function factsFit(files) {
19412
+ let budget = REPLAY_BYTES_MAX;
19413
+ for (const path of Object.keys(files))
19414
+ if ((budget -= storedSize(path) + FACT_OVERHEAD) < 0) return false;
19415
+ return true;
19416
+ }
19358
19417
  function filesOf(value2) {
19359
19418
  if (value2 === void 0) return {};
19360
- if (!Array.isArray(value2) || value2.length > OPERANDS_MAX) return void 0;
19419
+ if (!Array.isArray(value2) || value2.length * FACT_OVERHEAD > REPLAY_BYTES_MAX) return void 0;
19361
19420
  const files = {};
19421
+ let budget = REPLAY_BYTES_MAX;
19362
19422
  for (const file of value2) {
19363
19423
  const { path, sha256: hex5 } = file ?? {};
19364
- if (typeof path !== "string" || typeof hex5 !== "string") return void 0;
19424
+ if (typeof path !== "string" || !path.startsWith("/") || typeof hex5 !== "string" || !/^[0-9a-f]{64}$/.test(hex5))
19425
+ return void 0;
19426
+ if (path.length > budget || (budget -= storedSize(path) + FACT_OVERHEAD) < 0) return void 0;
19365
19427
  files[path] = hex5;
19366
19428
  }
19367
19429
  return files;
@@ -19384,9 +19446,9 @@ function replayOf(value2) {
19384
19446
  const { id: id2, user, uid, target, argv, path, cwd, stdin, tag, tags } = r;
19385
19447
  const rootOnly = flagsOf(r.rootOnly);
19386
19448
  const bound3 = filesOf(r.userWritable);
19387
- const { routeSha256 } = r;
19388
- if (routeSha256 !== void 0 && (typeof routeSha256 !== "string" || !/^[0-9a-f]{64}$/.test(routeSha256)))
19389
- return failed2("EINVAL", NOT_A_REQUEST);
19449
+ const { routeSha256, chainSha256 } = r;
19450
+ const digest2 = (v) => v === void 0 || typeof v === "string" && /^[0-9a-f]{64}$/.test(v);
19451
+ if (!digest2(routeSha256) || !digest2(chainSha256)) return failed2("EINVAL", NOT_A_REQUEST);
19390
19452
  const named = (s) => typeof s === "string" && s !== "";
19391
19453
  const absolute2 = (s) => typeof s === "string" && s.startsWith("/") && s.length <= PATH_MAX2 && !s.includes("\0");
19392
19454
  const valid3 = typeof id2 === "string" && REPLAY_ID.test(id2) && named(user) && named(target) && Number.isInteger(uid) && uid >= 0 && words(argv) && absolute2(path) && absolute2(cwd) && (stdin === "tty" || stdin === "none") && (tag === void 0 || typeof tag === "string") && (tags === void 0 || strings2(tags, REPLAY_TAGS_MAX)) && rootOnly !== void 0 && bound3 !== void 0;
@@ -19404,6 +19466,7 @@ function replayOf(value2) {
19404
19466
  rootOnly,
19405
19467
  bound: bound3,
19406
19468
  ...routeSha256 === void 0 ? {} : { routeSha256 },
19469
+ ...chainSha256 === void 0 ? {} : { chainSha256 },
19407
19470
  tags: [...new Set(all)]
19408
19471
  };
19409
19472
  }
@@ -19581,8 +19644,7 @@ var Sudo = class {
19581
19644
  }
19582
19645
  return true;
19583
19646
  }
19584
- async trustedThrough(path, destination) {
19585
- const { fs } = this.o;
19647
+ async trustedThrough(path, destination, fs = this.o.fs) {
19586
19648
  if (!heldByRoot(await fs.lstat("/").catch(() => void 0))) return false;
19587
19649
  const queue = path.split("/").filter(Boolean);
19588
19650
  const done4 = [];
@@ -19771,18 +19833,26 @@ var Sudo = class {
19771
19833
  }
19772
19834
  return void 0;
19773
19835
  }
19836
+ async missingBacking(hashes, except) {
19837
+ for (const path of Object.keys(hashes)) {
19838
+ if (path !== except && !(await this.o.fs.stat(path).catch(() => void 0))?.isFile)
19839
+ return path;
19840
+ }
19841
+ return void 0;
19842
+ }
19774
19843
  reads(snapshots) {
19775
- return new SudoReads(
19844
+ const reads = new SudoReads(
19776
19845
  {
19777
19846
  real: (at) => this.real(at),
19778
- trusted: (at) => this.trustedThrough(at),
19779
- read: (at) => this.o.fs.readFileBuffer(at)
19847
+ trusted: (at) => this.trustedThrough(at, void 0, fs),
19848
+ read: (at) => fs.readFileBuffer(at)
19780
19849
  },
19781
19850
  snapshots
19782
19851
  );
19852
+ const fs = this.o.planFs?.(reads) ?? this.o.fs;
19853
+ return reads;
19783
19854
  }
19784
- async bound(path, cwd, asked = path, pid) {
19785
- const reads = this.reads();
19855
+ async bound(path, cwd, asked = path, pid, reads = this.reads()) {
19786
19856
  const files = this.o.backing ? await this.o.backing(path, cwd, reads, pid) : [];
19787
19857
  const bound3 = files && await reads.bind([path, asked, ...files]);
19788
19858
  return bound3 && !Object.values(bound3.hashes).includes("") && (bound3.routes[asked] ?? path) === path ? bound3 : void 0;
@@ -19794,8 +19864,8 @@ var Sudo = class {
19794
19864
  if (bound3) return Object.keys(bound3.hashes).length > 0;
19795
19865
  return !await this.trusted(await this.existing(path));
19796
19866
  }
19797
- async locate(word, cwd) {
19798
- const { fs } = this.o;
19867
+ async locate(word, cwd, reads) {
19868
+ const fs = this.o.planFs?.(reads) ?? this.o.fs;
19799
19869
  const candidates = word.includes("/") ? [fs.resolvePath(cwd, word)] : SECURE_PATH.split(":").map((dir) => `${dir}/${word}`);
19800
19870
  for (const path of candidates) {
19801
19871
  const st = await fs.stat(path).catch(() => void 0);
@@ -19886,13 +19956,17 @@ var Sudo = class {
19886
19956
  `sudo: cannot run in ${r.cwd}: it isn't a directory ${self.name} can enter`
19887
19957
  );
19888
19958
  }
19889
- const located = await this.locate(word, r.cwd);
19890
- if (!located) return failed2("ENOENT", `sudo: ${word}: command not found`);
19959
+ const reads = this.reads();
19960
+ const located = await this.locate(word, r.cwd, reads);
19961
+ if (!located) {
19962
+ const gone = await this.missingBacking(r.bound, r.path);
19963
+ return gone ? failed2("EPERM", requestedText(gone)) : failed2("ENOENT", `sudo: ${word}: command not found`);
19964
+ }
19891
19965
  const { path, asked } = located;
19892
19966
  if (path !== r.path) return failed2("EPERM", `sudo: ${word} now runs ${path}, not ${r.path}`);
19893
- const bound3 = await this.bound(path, r.cwd, asked);
19894
- if (!bound3) return failed2("ENOENT", `sudo: ${word}: command not found`);
19895
- const changed2 = changedFile(r.bound, bound3.hashes) ?? (r.routeSha256 !== bound3.routeSha256 ? path : void 0);
19967
+ const bound3 = await this.bound(path, r.cwd, asked, void 0, reads);
19968
+ if (!bound3) return failed2("EPERM", requestedText(await this.missingBacking(r.bound) ?? path));
19969
+ const changed2 = changedDecision(r.bound, r, bound3, path);
19896
19970
  if (changed2 !== void 0) return failed2("EPERM", requestedText(changed2));
19897
19971
  const now = await this.rootOnly(r.cwd, args);
19898
19972
  const flipped = Object.keys(r.rootOnly).find((at) => r.rootOnly[at] && now[at] !== true);
@@ -19934,8 +20008,9 @@ var Sudo = class {
19934
20008
  `sudo: cannot run in ${call.cwd}: it isn't a directory you can enter`
19935
20009
  );
19936
20010
  }
19937
- const located = await this.locate(word, cwd);
19938
- const bound3 = located && await this.bound(located.path, cwd, located.asked, caller.pid);
20011
+ const reads = this.reads();
20012
+ const located = await this.locate(word, cwd, reads);
20013
+ const bound3 = located && await this.bound(located.path, cwd, located.asked, caller.pid, reads);
19939
20014
  if (!located || !bound3) return failed2("ENOENT", `sudo: ${word}: command not found`);
19940
20015
  const { path, asked } = located;
19941
20016
  const argv = [path, ...args];
@@ -19958,13 +20033,13 @@ var Sudo = class {
19958
20033
  }
19959
20034
  if (parsed3.nonInteractive) return failed2("EPERM", "sudo: a password is required");
19960
20035
  return this.pendingFor(cred.ruid, async () => {
19961
- const routes = bound3.routeSha256 ? { routeSha256: bound3.routeSha256 } : {};
20036
+ const proof = proofOf(bound3);
19962
20037
  const answer2 = await this.ask(s, {
19963
20038
  argv: parsed3.command,
19964
20039
  path,
19965
20040
  cwd,
19966
20041
  bound: bound3.hashes,
19967
- ...routes
20042
+ ...proof
19968
20043
  });
19969
20044
  if ("error" in answer2) return answer2;
19970
20045
  if (await this.canonical(call.cwd) !== cwd || !await this.reachable(cred, cwd)) {
@@ -19972,14 +20047,17 @@ var Sudo = class {
19972
20047
  }
19973
20048
  const moved3 = await this.moved(bound3);
19974
20049
  if (moved3 !== void 0) return failed2("EPERM", changedText(moved3));
20050
+ if (answer2.answer === "once" && !answer2.person) return grant();
19975
20051
  const now = await this.bound(path, cwd, asked, caller.pid);
19976
- if (!now) return failed2("ENOENT", `sudo: ${word}: command not found`);
20052
+ if (!now)
20053
+ return failed2("EPERM", changedText(await this.missingBacking(bound3.hashes) ?? path));
20054
+ const changed2 = changedDecision(bound3.hashes, bound3, now, path);
20055
+ if (changed2 !== void 0) return failed2("EPERM", changedText(changed2));
19977
20056
  const shown2 = recordable(cwd, [...parsed3.command, ...Object.keys(bound3.hashes)]);
19978
20057
  const result = answer2.answer === "once" ? grant() : await this.always(
19979
20058
  self,
19980
20059
  target,
19981
- { cwd, argv, bound: bound3.hashes, ...routes },
19982
- now,
20060
+ { cwd, argv, bound: bound3.hashes, ...proof },
19983
20061
  shown2,
19984
20062
  grant
19985
20063
  );
@@ -20029,11 +20107,8 @@ var Sudo = class {
20029
20107
  `sudo: ${self.name} : TTY=${tty} ; PWD=${quoteWord(cwd)} ; USER=${target.name} ; TIMESTAMP=${type} ; COMMAND=${command}`
20030
20108
  );
20031
20109
  }
20032
- async always(self, target, line, now, shown2, grant) {
20033
- const { cwd, argv, bound: bound3 } = line;
20034
- if (!shown2 || !recordable(cwd, argv)) return { ...grant(), notice: RAN_ONCE };
20035
- const changed2 = changedFile(bound3, now.hashes) ?? (line.routeSha256 !== now.routeSha256 ? argv[0] : void 0);
20036
- if (changed2 !== void 0) return failed2("EPERM", changedText(changed2));
20110
+ async always(self, target, line, shown2, grant) {
20111
+ if (!shown2 || !recordable(line.cwd, line.argv)) return { ...grant(), notice: RAN_ONCE };
20037
20112
  if (!await this.remember(self, target, line)) {
20038
20113
  return failed2("EIO", "sudo: the rule could not be recorded");
20039
20114
  }
@@ -20068,6 +20143,8 @@ ${lines3}
20068
20143
  async ask(s, asked) {
20069
20144
  const { caller, self, target, signal: requester } = s;
20070
20145
  const { argv, path, cwd } = asked;
20146
+ if (!factsFit(asked.bound))
20147
+ return failed2("E2BIG", "sudo: too many user-writable file facts for an approval request");
20071
20148
  const { approve } = this.o;
20072
20149
  if (!approve || requester.aborted) return failed2("EACCES", DENIED2);
20073
20150
  const createdAt = Date.now();
@@ -20090,7 +20167,7 @@ ${lines3}
20090
20167
  stdin: caller.stdin,
20091
20168
  rootOnly: asked.validate ? {} : await this.rootOnly(cwd, argv.slice(1)),
20092
20169
  ...userWritable(asked.bound),
20093
- ...asked.routeSha256 ? { routeSha256: asked.routeSha256 } : {},
20170
+ ...proofOf(asked),
20094
20171
  ...asked.validate ? { validate: true } : {},
20095
20172
  timestamp: true
20096
20173
  };
@@ -36691,6 +36768,12 @@ function addTimes(into, pid, t) {
36691
36768
  function locksForPackages(options) {
36692
36769
  return new PackageLocks(options.packageLocks ?? META_DB, options.leaseImage ? null : void 0);
36693
36770
  }
36771
+ var CatalogCache = class {
36772
+ commands = /* @__PURE__ */ new Map();
36773
+ own = /* @__PURE__ */ new Map();
36774
+ rules = /* @__PURE__ */ new Map();
36775
+ fs;
36776
+ };
36694
36777
  var Launcher = class {
36695
36778
  emscripten;
36696
36779
  admit;
@@ -36717,9 +36800,8 @@ var Launcher = class {
36717
36800
  appended;
36718
36801
  reportedRoots = /* @__PURE__ */ new Set();
36719
36802
  env;
36720
- catalogs = /* @__PURE__ */ new Map();
36721
- ownCatalogs = /* @__PURE__ */ new Map();
36722
- binfmts = /* @__PURE__ */ new Map();
36803
+ catalog = new CatalogCache();
36804
+ planCatalogs = /* @__PURE__ */ new WeakMap();
36723
36805
  scopes;
36724
36806
  unwatchScopes = () => void 0;
36725
36807
  warned = /* @__PURE__ */ new Set();
@@ -36997,6 +37079,7 @@ var Launcher = class {
36997
37079
  env: () => this.env,
36998
37080
  pnpmHome: this.pnpmHome,
36999
37081
  backing: (path, cwd, reads, pid) => this.backing(path, cwd, reads, pid),
37082
+ planFs: (reads) => this.sudoFs(reads),
37000
37083
  approve: options.approve,
37001
37084
  timeoutMs: options.sudoTimeoutMs,
37002
37085
  now: sudoClock ? () => sudoClock.now() : void 0
@@ -37070,14 +37153,44 @@ var Launcher = class {
37070
37153
  for (const listener of [...this.traitsListeners]) listener();
37071
37154
  return true;
37072
37155
  }
37073
- commands(scope = "") {
37074
- let found = this.catalogs.get(scope);
37156
+ catalogOf(reads) {
37157
+ if (!reads) return this.catalog;
37158
+ let cache = this.planCatalogs.get(reads);
37159
+ if (!cache) {
37160
+ cache = new CatalogCache();
37161
+ this.planCatalogs.set(reads, cache);
37162
+ }
37163
+ return cache;
37164
+ }
37165
+ scanFs(reads) {
37166
+ if (!reads) return this.scanned;
37167
+ return Object.assign(Object.create(this.scanned), {
37168
+ readFile: (path) => reads.manifest(
37169
+ path,
37170
+ async (real2, hash) => {
37171
+ await this.integrity?.checkManifest(path, real2, hash, KERNEL_CALLER);
37172
+ },
37173
+ (real2) => this.base.readFileBuffer(real2)
37174
+ )
37175
+ });
37176
+ }
37177
+ sudoFs(reads) {
37178
+ const cache = this.catalogOf(reads);
37179
+ return cache.fs ??= this.processView(
37180
+ this.commandFs(this.base, reads),
37181
+ this.nodes,
37182
+ this.unlinked
37183
+ );
37184
+ }
37185
+ commands(scope = "", reads) {
37186
+ const cache = this.catalogOf(reads).commands;
37187
+ let found = cache.get(scope);
37075
37188
  if (!found) {
37076
37189
  found = this.swaps.settled().then(() => this.trustedAppended()).then(async (appended2) => {
37077
37190
  const tiers = await this.tiers(scope, appended2);
37078
37191
  const refused5 = [];
37079
37192
  const commands = await scanCommands(
37080
- this.scanned,
37193
+ this.scanFs(reads),
37081
37194
  tiers,
37082
37195
  tiers.length - appended2.length,
37083
37196
  void 0,
@@ -37089,7 +37202,7 @@ var Launcher = class {
37089
37202
  }).then(
37090
37203
  (commands) => this.refusals(commands).set(SUDO_COMMAND.name, SUDO_COMMAND).set(VISUDO_COMMAND.name, VISUDO_COMMAND).set(CRONTAB_COMMAND.name, CRONTAB_COMMAND).set(SEALED_COMMAND.name, SEALED_COMMAND)
37091
37204
  );
37092
- this.catalogs.set(scope, found);
37205
+ cache.set(scope, found);
37093
37206
  }
37094
37207
  return found;
37095
37208
  }
@@ -37104,25 +37217,26 @@ var Launcher = class {
37104
37217
  }
37105
37218
  return commands;
37106
37219
  }
37107
- ownCommands(scope) {
37108
- let found = this.ownCatalogs.get(scope);
37220
+ ownCommands(scope, reads) {
37221
+ const cache = this.catalogOf(reads).own;
37222
+ let found = cache.get(scope);
37109
37223
  if (!found) {
37110
- found = this.ownCatalog(scope);
37111
- this.ownCatalogs.set(scope, found);
37224
+ found = this.ownCatalog(scope, reads);
37225
+ cache.set(scope, found);
37112
37226
  }
37113
37227
  return found;
37114
37228
  }
37115
- async ownCatalog(scope) {
37229
+ async ownCatalog(scope, reads) {
37116
37230
  const roots = await pnpmGlobalRoots(this.base, scope);
37117
37231
  const own = await scanCommands(
37118
- this.scanned,
37232
+ this.scanFs(reads),
37119
37233
  roots,
37120
37234
  void 0,
37121
37235
  void 0,
37122
37236
  void 0,
37123
37237
  new Set(roots)
37124
37238
  );
37125
- const system = await this.commands();
37239
+ const system = await this.commands("", reads);
37126
37240
  return new Set(
37127
37241
  [...own.values()].filter((command) => !command.dependency || !system.has(command.name)).map((command) => command.name)
37128
37242
  );
@@ -37134,28 +37248,20 @@ var Launcher = class {
37134
37248
  }
37135
37249
  interpreters(scope = "", reads) {
37136
37250
  if (this.leaseImage) return Promise.resolve([]);
37137
- if (reads) return this.scanInterpreters(scope, reads);
37138
- let found = this.binfmts.get(scope);
37251
+ const cache = this.catalogOf(reads).rules;
37252
+ let found = cache.get(scope);
37139
37253
  if (!found) {
37140
- found = this.scanInterpreters(scope);
37141
- this.binfmts.set(scope, found);
37254
+ found = this.scanInterpreters(scope, reads);
37255
+ cache.set(scope, found);
37142
37256
  }
37143
37257
  return found;
37144
37258
  }
37145
37259
  async scanInterpreters(scope, reads) {
37146
- const read = reads && ((path) => reads.manifest(path, async (real2, bytes) => {
37147
- await this.integrity?.checkSnapshot(path, real2, bytes, KERNEL_CALLER);
37148
- }));
37149
- return scanBinfmts(
37150
- this.scanned,
37151
- await this.roots(scope),
37152
- (message3) => {
37153
- if (this.warned.has(message3)) return;
37154
- this.warned.add(message3);
37155
- console.warn(message3);
37156
- },
37157
- read
37158
- );
37260
+ return scanBinfmts(this.scanFs(reads), await this.roots(scope), (message3) => {
37261
+ if (this.warned.has(message3)) return;
37262
+ this.warned.add(message3);
37263
+ console.warn(message3);
37264
+ });
37159
37265
  }
37160
37266
  async tiers(scope = "", appended2) {
37161
37267
  const system = [
@@ -37213,9 +37319,7 @@ var Launcher = class {
37213
37319
  );
37214
37320
  }
37215
37321
  resetCatalogs() {
37216
- this.catalogs.clear();
37217
- this.ownCatalogs.clear();
37218
- this.binfmts.clear();
37322
+ this.catalog = new CatalogCache();
37219
37323
  }
37220
37324
  userScopes() {
37221
37325
  this.scopes ??= this.users.list().then(
@@ -37238,27 +37342,27 @@ var Launcher = class {
37238
37342
  );
37239
37343
  return this.scopes;
37240
37344
  }
37241
- async userCommandDir(dir) {
37345
+ async userCommandDir(dir, reads) {
37242
37346
  const scope = (await this.userScopes()).byBin.get(dir);
37243
- return scope === void 0 ? void 0 : this.ownCommands(scope);
37347
+ return scope === void 0 ? void 0 : this.ownCommands(scope, reads);
37244
37348
  }
37245
37349
  async scopeOf(uid) {
37246
37350
  return (await this.userScopes()).byUid.get(uid) ?? "";
37247
37351
  }
37248
- async commandAt(file, scope) {
37352
+ async commandAt(file, scope, reads) {
37249
37353
  const system = COMMAND2.exec(file)?.[1];
37250
- if (system !== void 0) return (await this.commands()).get(system);
37251
- if (!file.includes("/")) return (await this.commands(scope)).get(file);
37354
+ if (system !== void 0) return (await this.commands("", reads)).get(system);
37355
+ if (!file.includes("/")) return (await this.commands(scope, reads)).get(file);
37252
37356
  const slash = file.lastIndexOf("/");
37253
37357
  const owner = (await this.userScopes()).byBin.get(file.slice(0, slash));
37254
- if (owner === void 0 || !(await this.ownCommands(owner)).has(file.slice(slash + 1))) {
37358
+ if (owner === void 0 || !(await this.ownCommands(owner, reads)).has(file.slice(slash + 1))) {
37255
37359
  return void 0;
37256
37360
  }
37257
- return (await this.commands(owner)).get(file.slice(slash + 1));
37361
+ return (await this.commands(owner, reads)).get(file.slice(slash + 1));
37258
37362
  }
37259
37363
  async resolve(file, argv0, cwd, scope = "", reads) {
37260
37364
  if (!file.includes("/") || COMMAND2.test(file)) {
37261
- const command = await this.commandAt(file, scope);
37365
+ const command = await this.commandAt(file, scope, reads);
37262
37366
  return command && !command.script ? targetOf(command) : void 0;
37263
37367
  }
37264
37368
  const path = this.fs.resolvePath(cwd, file);
@@ -37268,15 +37372,15 @@ var Launcher = class {
37268
37372
  if (linked2 !== void 0) {
37269
37373
  const found = await this.resolve(`/bin/${linked2}`, argv0, cwd, "", reads);
37270
37374
  const target = found && { ...found, ...asked };
37271
- const byPath = !COMMAND2.test(path) && (await this.commands()).get(linked2)?.argv0Path === true;
37375
+ const byPath = !COMMAND2.test(path) && (await this.commands("", reads)).get(linked2)?.argv0Path === true;
37272
37376
  return target && byPath ? { ...target, argv0: path } : target;
37273
37377
  }
37274
- const own = await this.ownOrRefused(glue, baseName3(file), scope);
37378
+ const own = await this.ownOrRefused(glue, baseName3(file), scope, reads);
37275
37379
  if (own) return own.script ? void 0 : { ...targetOf(own), ...asked };
37276
37380
  const head = await this.head(glue, reads);
37277
37381
  if (!head) return void 0;
37278
37382
  if (isWasm(head)) {
37279
- const owner2 = await this.wasmOwner(glue, baseName3(file).replace(/\.wasm$/, ""), scope);
37383
+ const owner2 = await this.wasmOwner(glue, baseName3(file).replace(/\.wasm$/, ""), scope, reads);
37280
37384
  if (owner2) return { ...targetOf(owner2), ...asked };
37281
37385
  return {
37282
37386
  abi: "wasi",
@@ -37292,18 +37396,18 @@ var Launcher = class {
37292
37396
  if (await this.base.exists(wasm)) {
37293
37397
  return { abi: "emscripten", glue, wasm, argv0: baseName3(argv0 || file), ...asked };
37294
37398
  }
37295
- const owner = await this.glueOwner(glue, baseName3(file), scope);
37399
+ const owner = await this.glueOwner(glue, baseName3(file), scope, reads);
37296
37400
  if (!owner) {
37297
- const shim = await this.shimOwner(glue, baseName3(file), scope);
37401
+ const shim = await this.shimOwner(glue, baseName3(file), scope, reads);
37298
37402
  return shim && { ...targetOf(shim), ...asked, shim: glue };
37299
37403
  }
37300
37404
  return { ...targetOf(owner), ...asked, ...owner.glue === glue ? {} : { copy: glue } };
37301
37405
  }
37302
- async ownOrRefused(path, name2, scope) {
37303
- return await this.commandAt(path, scope) ?? await this.refusedAt(path, name2, scope);
37406
+ async ownOrRefused(path, name2, scope, reads) {
37407
+ return await this.commandAt(path, scope, reads) ?? await this.refusedAt(path, name2, scope, reads);
37304
37408
  }
37305
- async refusedAt(path, name2, scope) {
37306
- const refused5 = this.refusedOf.get(await this.commands(scope)) ?? [];
37409
+ async refusedAt(path, name2, scope, reads) {
37410
+ const refused5 = this.refusedOf.get(await this.commands(scope, reads)) ?? [];
37307
37411
  if (refused5.length === 0) return void 0;
37308
37412
  const st = await this.base.stat(path).catch(() => void 0);
37309
37413
  if (!st?.isFile) return void 0;
@@ -37332,8 +37436,8 @@ var Launcher = class {
37332
37436
  }
37333
37437
  return void 0;
37334
37438
  }
37335
- async shimOwner(script, name2, scope) {
37336
- const command = (await this.commands(scope)).get(name2);
37439
+ async shimOwner(script, name2, scope, reads) {
37440
+ const command = (await this.commands(scope, reads)).get(name2);
37337
37441
  if (!command || command.script || command.origin?.script) return void 0;
37338
37442
  const target = await this.shimTarget(script);
37339
37443
  if (target === void 0) return void 0;
@@ -37369,8 +37473,8 @@ var Launcher = class {
37369
37473
  }
37370
37474
  return void 0;
37371
37475
  }
37372
- async glueOwner(glue, name2, scope) {
37373
- const commands = [...(await this.commands(scope)).values()];
37476
+ async glueOwner(glue, name2, scope, reads) {
37477
+ const commands = [...(await this.commands(scope, reads)).values()];
37374
37478
  const owners = commands.filter((c) => c.abi === "emscripten" && !c.script && c.glue === glue);
37375
37479
  if (owners.length > 0) return owners.find((c) => c.name === name2) ?? owners[0];
37376
37480
  const leaf = baseName3(glue);
@@ -37395,11 +37499,11 @@ var Launcher = class {
37395
37499
  this.sameFiles.set(`${a}|${b}`, { stamp, same: same2 });
37396
37500
  return same2;
37397
37501
  }
37398
- async wasmOwner(wasm, name2, scope) {
37502
+ async wasmOwner(wasm, name2, scope, reads) {
37399
37503
  const leaf = baseName3(wasm);
37400
37504
  const real2 = await physicalPathAsync(this.base, wasm);
37401
37505
  const owners = [];
37402
- for (const command of (await this.commands(scope)).values()) {
37506
+ for (const command of (await this.commands(scope, reads)).values()) {
37403
37507
  if (command.abi !== "wasi" || command.script || baseName3(command.wasm) !== leaf) continue;
37404
37508
  if (command.wasm === wasm || await physicalPathAsync(this.base, command.wasm) === real2)
37405
37509
  owners.push(command);
@@ -37422,15 +37526,15 @@ var Launcher = class {
37422
37526
  async scriptCommand(file, cwd, scope, reads) {
37423
37527
  const direct = !file.includes("/") || COMMAND2.test(file);
37424
37528
  const at = direct ? file : await this.followCommand(this.fs.resolvePath(cwd, file), reads);
37425
- const command = await this.commandAt(at, scope);
37529
+ const command = await this.commandAt(at, scope, reads);
37426
37530
  return command?.script ? command : void 0;
37427
37531
  }
37428
- async mayRead(reader, path) {
37532
+ async mayRead(reader, path, reads) {
37429
37533
  if (!reader || !this.dac) return true;
37430
- return (await access(this.fs, reader, path, R_OK)).denied === void 0;
37534
+ return (await access(reads ? this.sudoFs(reads) : this.fs, reader, path, R_OK)).denied === void 0;
37431
37535
  }
37432
37536
  async binfmtOf(path, head, scope, reader, reads) {
37433
- const shown2 = await this.mayRead(reader, path) ? head : void 0;
37537
+ const shown2 = await this.mayRead(reader, path, reads) ? head : void 0;
37434
37538
  return binfmtFor(await this.interpreters(scope, reads), path, shown2);
37435
37539
  }
37436
37540
  async interpreterWords(head, script, who) {
@@ -37468,6 +37572,7 @@ var Launcher = class {
37468
37572
  const passed = [...args, command ? script : file, ...words3.kept, ...argv.slice(1)];
37469
37573
  if (!interp) return void 0;
37470
37574
  who.reads?.operandsFromEnd.set(passed.length - args.length, script);
37575
+ who.reads?.interpreters.push([script, ...words3.words, String(words3.kept.length)]);
37471
37576
  const found = await this.resolve(interp, interp, cwd, scope, who.reads);
37472
37577
  if (!found) {
37473
37578
  const next3 = depth < MAX_INTERPRETERS ? await this.scriptCommand(interp, cwd, scope, who.reads) : void 0;
@@ -37529,13 +37634,19 @@ var Launcher = class {
37529
37634
  const given = typeof argv0 === "string" ? { argv0 } : {};
37530
37635
  const who = { scope, reader, reads, ...given };
37531
37636
  const script = await this.interpreted(file, argv, cwd, who, command);
37637
+ if (reads && script)
37638
+ reads.chainSha256 = await sha2562(
37639
+ new TextEncoder().encode(
37640
+ JSON.stringify([reads.interpreters, script.binfmts, script.target])
37641
+ )
37642
+ );
37532
37643
  return { planned: script && { ...script, script: true }, ...examined };
37533
37644
  }
37534
- async mayExec(cred, req, { planned, examined }) {
37645
+ async mayExec(cred, req, { planned, examined }, reads) {
37535
37646
  const { dac } = this;
37536
37647
  if (!dac) return;
37537
37648
  const g = {
37538
- fs: this.fs,
37649
+ fs: reads ? this.sudoFs(reads) : this.fs,
37539
37650
  cred,
37540
37651
  pid: req.pid,
37541
37652
  dac,
@@ -37562,10 +37673,11 @@ var Launcher = class {
37562
37673
  throw err;
37563
37674
  }
37564
37675
  }
37565
- planOrSearch(req, ctx) {
37676
+ async planOrSearch(req, ctx) {
37677
+ const reads = ctx.grant?.files && await this.scriptReads(ctx.grant.files, ctx.cred, ctx.ppid);
37566
37678
  return planOrSearch(req, {
37567
- listed: (file) => this.commandAt(file, ctx.scope),
37568
- plan: (candidate, catalogOnly) => this.planOne(candidate, ctx, catalogOnly),
37679
+ listed: (file) => this.commandAt(file, ctx.scope, reads),
37680
+ plan: (candidate, catalogOnly) => this.planOne(candidate, ctx, catalogOnly, reads),
37569
37681
  searchable: (dir) => this.execDirectory(
37570
37682
  dir.startsWith("/") ? dir : `${req.cwd}/${dir}`,
37571
37683
  this.execGuard(ctx.cred, ctx.ppid),
@@ -37573,22 +37685,26 @@ var Launcher = class {
37573
37685
  )
37574
37686
  });
37575
37687
  }
37576
- async planOne(req, { ppid, cred, scope, grant }, catalogOnly = false) {
37688
+ async planOne(req, { ppid, cred, scope, grant }, catalogOnly = false, prepared) {
37577
37689
  const refused5 = (err) => {
37578
37690
  throw codeOf9(err) === "EACCES" ? new SpawnError("EACCES") : err;
37579
37691
  };
37580
37692
  const file = await this.execFile(req.file, req.cwd, this.execGuard(cred, ppid), ppid).catch(
37581
37693
  refused5
37582
37694
  );
37583
- const reads = grant?.files && await this.scriptReads(grant.files, cred, ppid);
37695
+ const reads = prepared ?? (grant?.files && await this.scriptReads(grant.files, cred, ppid));
37584
37696
  const plan = await this.plan(file, req.argv, req.cwd, scope, {
37585
37697
  argv0: req.argv0,
37586
37698
  catalogOnly,
37587
37699
  reader: cred,
37588
37700
  reads
37589
37701
  });
37590
- await this.mayExec(cred, { ...req, file, pid: ppid }, plan).catch(refused5);
37702
+ await this.mayExec(cred, { ...req, file, pid: ppid }, plan, reads).catch(refused5);
37591
37703
  if (plan.planned) return reads ? { ...plan.planned, reads } : plan.planned;
37704
+ if (grant?.files) {
37705
+ const gone = await this.sudoer.missingBacking(grant.files.hashes) ?? file;
37706
+ throw new ChangedError(changedText(gone), gone);
37707
+ }
37592
37708
  if (catalogOnly) throw new SpawnError("ENOENT");
37593
37709
  throw new SpawnError(await this.unrunnable(file, req.cwd, scope, cred));
37594
37710
  }
@@ -37733,6 +37849,7 @@ var Launcher = class {
37733
37849
  const now = await planned.reads.bind([file, grant.asked ?? file, ...files]);
37734
37850
  refuseChanged(approved.hashes, now.hashes);
37735
37851
  refuseChanged(approved.routes, now.routes);
37852
+ if (now.chainSha256 !== approved.chainSha256) throw new ChangedError(changedText(file), file);
37736
37853
  return { pinned: approved };
37737
37854
  }
37738
37855
  approvedChain(planned, file, cwd, grant) {
@@ -37746,7 +37863,8 @@ var Launcher = class {
37746
37863
  async scriptReads(files, cred, pid) {
37747
37864
  await this.checkRoutes(files, false);
37748
37865
  const snapshots = /* @__PURE__ */ new Map();
37749
- const guard2 = { fs: this.fs, dac: this.dac, cred, pid, links: cred.euid };
37866
+ const reads = this.sudoer.reads(snapshots);
37867
+ const guard2 = { fs: this.sudoFs(reads), dac: this.dac, cred, pid, links: cred.euid };
37750
37868
  for (const path of files.scripts ?? []) {
37751
37869
  const real2 = files.routes[path];
37752
37870
  const walked2 = await need(guard2, "open", path, R_OK, { directory: false });
@@ -37754,7 +37872,7 @@ var Launcher = class {
37754
37872
  if (!snapshots.has(real2))
37755
37873
  snapshots.set(real2, new Uint8Array(await this.noLinksFs.readFileBuffer(real2)));
37756
37874
  }
37757
- return this.sudoer.reads(snapshots);
37875
+ return reads;
37758
37876
  }
37759
37877
  scriptArgs(planned, files, fds) {
37760
37878
  const reads = planned.reads;
@@ -39760,14 +39878,14 @@ var Launcher = class {
39760
39878
  const watched = this.watchers.wrap(mounted);
39761
39879
  return this.owners ? this.freshModules.wrap(watched) : watched;
39762
39880
  }
39763
- commandFs(root) {
39881
+ commandFs(root, reads) {
39764
39882
  return withCommandDirs(
39765
39883
  withEtcFiles(root, /* @__PURE__ */ new Map([[HOSTNAME_PATH, () => `${this.hostname}
39766
39884
  `]])),
39767
- async () => this.commandNames(await this.commands()),
39768
- (dir) => this.userCommandDir(dir),
39885
+ async () => this.commandNames(await this.commands("", reads)),
39886
+ (dir) => this.userCommandDir(dir, reads),
39769
39887
  this.owners,
39770
- (path) => this.entryTarget(path)
39888
+ (path) => this.entryTarget(path, reads)
39771
39889
  );
39772
39890
  }
39773
39891
  named = /* @__PURE__ */ new WeakMap();
@@ -39779,13 +39897,13 @@ var Launcher = class {
39779
39897
  }
39780
39898
  return names2;
39781
39899
  }
39782
- async entryTarget(path) {
39900
+ async entryTarget(path, reads) {
39783
39901
  const system = COMMAND2.exec(path)?.[1];
39784
39902
  const slash = path.lastIndexOf("/");
39785
39903
  const scopes = await this.userScopes();
39786
39904
  const scope = system === void 0 ? scopes.byBin.get(path.slice(0, slash)) : "";
39787
39905
  const name2 = system ?? path.slice(slash + 1);
39788
- const command = scope === void 0 ? void 0 : (await this.commands(scope)).get(name2);
39906
+ const command = scope === void 0 ? void 0 : (await this.commands(scope, reads)).get(name2);
39789
39907
  const owner = scope ? scopes.owners?.get(scope) : void 0;
39790
39908
  const target = command && (command.shows ?? command.script ?? command.wasm);
39791
39909
  return target === void 0 ? void 0 : { path: target, ...owner ? { owner } : {} };
@@ -39807,6 +39925,9 @@ var Launcher = class {
39807
39925
  nodes.changed = (path) => this.changes.publish(path);
39808
39926
  nodes.movedAway = (from, to) => this.moved(from, to, nodes);
39809
39927
  nodes.dirtied = (delta) => this.changes.dirtied(delta);
39928
+ return this.processView(fs, nodes, unlinked);
39929
+ }
39930
+ processView(fs, nodes, unlinked) {
39810
39931
  const kept = keepingOpen(fs, nodes, (path) => this.changes.publish(path));
39811
39932
  return this.textBusy.view(this.swaps.wrap(unlinked.wrap(kept)));
39812
39933
  }