space-data-module-sdk 0.10.4 → 0.11.0

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.
@@ -27,6 +27,8 @@ import {
27
27
  resolveModuleSignaturePolicy,
28
28
  verifyModuleArtifact,
29
29
  } from "../bundle/signing.js";
30
+ import { readBoundManifestBytes } from "../bundle/codeManifest.js";
31
+ import { decodePlgManifest } from "../manifest/plgCodec.js";
30
32
  import { attachHostDispatch, inspectModule } from "./isomorphicLoaderCore.js";
31
33
  import { assertArtifactRuntimeTarget } from "./runtimeTargetGate.js";
32
34
  import { DEFAULT_HOSTCALL_IMPORT_MODULE } from "./abi.js";
@@ -67,8 +69,9 @@ async function createWasmEdgeCommandHarness(options = {}) {
67
69
  // Signed/published artifacts carry an appended publication record
68
70
  // collection that WasmEdge rejects ("malformed section id"). Strip to the
69
71
  // canonical module payload; if anything was stripped, launch WasmEdge from
70
- // a temp copy of the stripped bytes (signature verification, when
71
- // requested, already ran against the full artifact in loadModule()).
72
+ // a temp copy of the stripped bytes. Under a signature policy loadModule()
73
+ // passes, as wasmSource, its own private copy of the bytes it verified, so
74
+ // these are those bytes, not a second read of the caller's file.
72
75
  const loadableBytes = toLoadableWasmBytes(wasmBytes);
73
76
  const inspection = await inspectModule(loadableBytes);
74
77
 
@@ -270,7 +273,58 @@ export async function loadModule(options = {}) {
270
273
  if (isBrowser) {
271
274
  return createBrowserModuleHarness(options);
272
275
  }
276
+ const owned = { verifiedCopy: null };
277
+ let harness;
278
+ try {
279
+ harness = await loadServerModule(options, signaturePolicy, owned);
280
+ } catch (error) {
281
+ await owned.verifiedCopy?.remove();
282
+ throw error;
283
+ }
284
+ const verifiedCopy = owned.verifiedCopy;
285
+ if (!verifiedCopy) {
286
+ return harness;
287
+ }
288
+ // The verified copy lives as long as the harness that runs it.
289
+ const destroy = typeof harness.destroy === "function" ? harness.destroy.bind(harness) : null;
290
+ harness.verifiedArtifactPath = verifiedCopy.path;
291
+ harness.destroy = async (...args) => {
292
+ try {
293
+ return destroy ? await destroy(...args) : undefined;
294
+ } finally {
295
+ await verifiedCopy.remove();
296
+ }
297
+ };
298
+ return harness;
299
+ }
273
300
 
301
+ // The verified bytes, written to a new private temporary directory
302
+ // (owner-only), for the harness to read instead of the caller's path.
303
+ async function writeVerifiedArtifactCopy(bytes) {
304
+ const [{ mkdtemp, writeFile, rm }, osModule, pathModule] = await Promise.all([
305
+ import("node:fs/promises"),
306
+ import("node:os"),
307
+ import("node:path"),
308
+ ]);
309
+ const os = osModule.default ?? osModule;
310
+ const path = pathModule.default ?? pathModule;
311
+ const dir = await mkdtemp(path.join(os.tmpdir(), "sdm-verified-"));
312
+ const filePath = path.join(dir, "module.wasm");
313
+ try {
314
+ await writeFile(filePath, bytes, { mode: 0o600, flag: "wx" });
315
+ } catch (error) {
316
+ await rm(dir, { recursive: true, force: true });
317
+ throw error;
318
+ }
319
+ return {
320
+ path: filePath,
321
+ async remove() {
322
+ await rm(dir, { recursive: true, force: true });
323
+ },
324
+ };
325
+ }
326
+
327
+ async function loadServerModule(options, signaturePolicy, owned) {
274
328
  const { createModuleHarness } = await import("../testing/moduleHarness.js");
275
329
  const source = options.wasmSource;
276
330
  if (typeof source !== "string") {
@@ -304,18 +358,40 @@ export async function loadModule(options = {}) {
304
358
  let artifactBytes = null;
305
359
  let wasmModule = null;
306
360
  let embeddedManifest = null;
361
+ // Under a signature policy every runtime runs the bytes that were VERIFIED:
362
+ // they are written to a private temporary file and that file, never the
363
+ // caller's path, is what the harness reads and launches. Reading the path
364
+ // again after the check would let a file swapped in between run unverified.
365
+ let launchSource = source;
307
366
  if (needsBytes) {
308
367
  const { readFile } = await import("node:fs/promises");
309
368
  artifactBytes = new Uint8Array(await readFile(source));
369
+ let verifiedSignature = false;
310
370
  if (signaturePolicy) {
311
- await verifyModuleArtifact(artifactBytes, signaturePolicy);
371
+ const verification = await verifyModuleArtifact(artifactBytes, signaturePolicy);
372
+ verifiedSignature =
373
+ signaturePolicy.requireSignature === true && verification?.verified === true;
374
+ owned.verifiedCopy = await writeVerifiedArtifactCopy(artifactBytes);
375
+ launchSource = owned.verifiedCopy.path;
312
376
  }
313
377
  if (runtimeKind === "wasmedge") {
314
- const { locateEmbeddedPlgManifest } = await import(
315
- "../compliance/index.js"
316
- );
317
- embeddedManifest =
318
- locateEmbeddedPlgManifest(artifactBytes)?.decoded ?? null;
378
+ // The declaration comes from the SIGNED code (readBoundManifestBytes),
379
+ // never from the trailer's manifest entry, which the signature does not
380
+ // cover; a trailer copy that differs from the code's is refused by name.
381
+ // The code is run in this process to ask it ONLY when its signature
382
+ // was verified under a policy that requires one; otherwise the manifest
383
+ // exports are answered from the bytes alone or not at all.
384
+ const manifestBytes = await readBoundManifestBytes(artifactBytes, {
385
+ verifiedSignature,
386
+ });
387
+ embeddedManifest = null;
388
+ if (manifestBytes) {
389
+ try {
390
+ embeddedManifest = decodePlgManifest(manifestBytes);
391
+ } catch {
392
+ // An undecodable manifest is not a declaration.
393
+ }
394
+ }
319
395
  }
320
396
  if (needsCompiledModule) {
321
397
  wasmModule = await WebAssembly.compile(toLoadableWasmBytes(artifactBytes));
@@ -346,7 +422,7 @@ export async function loadModule(options = {}) {
346
422
  (!options.wasmEdgeRunnerBinary || options.surface === "command" || useRunner)
347
423
  ) {
348
424
  return attachHostDispatch(
349
- await createWasmEdgeCommandHarness(options),
425
+ await createWasmEdgeCommandHarness({ ...options, wasmSource: launchSource }),
350
426
  options.host ?? null,
351
427
  );
352
428
  }
@@ -356,7 +432,7 @@ export async function loadModule(options = {}) {
356
432
  await createModuleHarness({
357
433
  runtime: {
358
434
  kind: "wasmedge",
359
- wasmPath: source,
435
+ wasmPath: launchSource,
360
436
  wasmEdgeBinary: options.wasmEdgeBinary,
361
437
  wasmEdgeRunnerBinary: options.wasmEdgeRunnerBinary,
362
438
  enableThreads: options.enableThreads,
@@ -377,7 +453,9 @@ export async function loadModule(options = {}) {
377
453
  runtime: {
378
454
  kind: runtimeKind,
379
455
  command: options.command ?? runtimeKind,
380
- args: options.args ?? [source],
456
+ // Caller-given args are the caller's: the loader cannot know what
457
+ // they read. The default runs the verified copy.
458
+ args: options.args ?? [launchSource],
381
459
  env: options.env,
382
460
  cwd: options.cwd,
383
461
  hostProfile: options.hostProfile,