Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): refuse a SHA-256 pin that is not a map instead of verifying nothing

Aashish28a1a8290734
diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.ts
index fd614c87..a6e0a017 100644
--- a/laya-ts/src/providers.ts
+++ b/laya-ts/src/providers.ts
@@ -37,6 +37,18 @@ function normaliseDigestPath(rel: string): string {
return norm;
}
+/**
+ * Refuse a pin that could never verify anything. A number, boolean, bare digest string or
+ * array reads back no artifact keys, so the loaders below would silently skip the integrity
+ * check they were asked to perform. `undefined`/`null` stays the documented "not asked for".
+ */
+function assertDigestMap(expected: unknown): void {
+ if (expected === undefined || expected === null) return;
+ if (typeof expected !== "object" || Array.isArray(expected)) {
+ throw new Error("laya-ts: expected_sha256 must be a mapping of artifact paths to digests");
+ }
+}
+
/** Verify `data` against expectedSha256[rel]; artifacts not listed are left unchecked. */
async function expectDigest(
rel: string,
@@ -304,6 +316,7 @@ export async function loadNodeBundle(
onProgress?: ((done: number, total: number, file: string) => void) | null;
},
): Promise<NodeBundle> {
+ assertDigestMap(opts?.expectedSha256);
const fs: typeof import("node:fs/promises") = await import("node:fs/promises");
const path: typeof import("node:path") = await import("node:path");
const os: typeof import("node:os") = await import("node:os");
@@ -443,6 +456,7 @@ export async function loadWebBundle(
onProgress?: ((done: number, total: number, file: string) => void) | null;
},
): Promise<WebBundle> {
+ assertDigestMap(opts?.expectedSha256);
const revision = resolveRevision(repoOrUrl, opts?.revision);
const base = baseUrlFor(repoOrUrl, opts?.subfolder ?? null, revision);
let reportedRevision = revision;
@@ -484,6 +498,7 @@ export async function createNodeProvider(
modelDir: string,
opts?: ProviderOptions,
): Promise<SessionProvider> {
+ assertDigestMap(opts?.expectedSha256);
const fs: typeof import("node:fs/promises") = await import("node:fs/promises");
const path: typeof import("node:path") = await import("node:path");
// Verify the artifacts and their digests BEFORE importing the native runtime. `onnxruntime-node`
@@ -593,6 +608,7 @@ export async function createWebProvider(
modelUrl: string,
opts?: ProviderOptions,
): Promise<SessionProvider> {
+ assertDigestMap(opts?.expectedSha256);
const spec = "onnxruntime-" + "web";
const ort: any = await import(/* @vite-ignore */ spec);
applyNumThreads(ort, opts?.numThreads);
diff --git a/laya-ts/tests/revision-pinning.test.ts b/laya-ts/tests/revision-pinning.test.ts
index 3992857a..10834eae 100644
--- a/laya-ts/tests/revision-pinning.test.ts
+++ b/laya-ts/tests/revision-pinning.test.ts
@@ -5,7 +5,7 @@ import * as os from "node:os";
import * as path from "node:path";
import { Agent } from "../src/agent.js";
import { Router } from "../src/router.js";
-import { PINNED_REVISIONS, createNodeProvider, loadNodeBundle, loadWebBundle, resolveRevision } from "../src/providers.js";
+import { PINNED_REVISIONS, createNodeProvider, createWebProvider, loadNodeBundle, loadWebBundle, resolveRevision } from "../src/providers.js";
const fakeProvider = () => ({
async runEncoder(_b: any) { return { lastHidden: [[[1, 0], [0, 1]]] }; },
@@ -164,6 +164,36 @@ describe("loadNodeBundle expectedSha256", () => {
expectedSha256: { "encoder.onnx": "0".repeat(64) },
})).rejects.toThrow(/SHA-256 mismatch/);
});
+
+ // A pin that is not a {artifact: digest} map yields no keys, so every loader would skip the
+ // integrity check it was handed and load tampered weights in silence. `laya/revisions.py`
+ // refuses the same shapes; the port now does too.
+ it("a pin that is not a map is refused instead of verifying nothing", async () => {
+ const dir = makeCheckpoint({ ...cfgFile, "model.bin": new TextEncoder().encode("weights") });
+ for (const notAMap of ["a".repeat(64), 5, 0, true, false, ["a".repeat(64)]]) {
+ await expect(loadNodeBundle(dir, { expectedSha256: notAMap as any }))
+ .rejects.toThrow(/expected_sha256 must be a mapping of artifact paths to digests/);
+ await expect(createNodeProvider(dir, { expectedSha256: notAMap as any }))
+ .rejects.toThrow(/expected_sha256 must be a mapping/);
+ await expect(loadWebBundle("https://example.com/m", { expectedSha256: notAMap as any }))
+ .rejects.toThrow(/expected_sha256 must be a mapping/);
+ await expect(createWebProvider("https://example.com/m", { expectedSha256: notAMap as any }))
+ .rejects.toThrow(/expected_sha256 must be a mapping/);
+ }
+ });
+
+ it("absent and empty pins keep their distinct meanings", async () => {
+ const dir = makeCheckpoint(cfgFile);
+ for (const notPinned of [undefined, null]) {
+ expect((await loadNodeBundle(dir, { expectedSha256: notPinned as any })).dir).toBe(dir);
+ }
+ const weights = new TextEncoder().encode("weights");
+ const withWeights = makeCheckpoint({ ...cfgFile, "model.bin": weights });
+ expect((await loadNodeBundle(withWeights, { expectedSha256: {} })).dir).toBe(withWeights);
+ expect((await loadNodeBundle(withWeights, {
+ expectedSha256: { "model.bin": sha256(weights) },
+ })).dir).toBe(withWeights);
+ });
});
describe("revision plumbing", () => {