Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): treat a non-object safetensors header as corrupt instead of throwing

[._.]/ Adam Eivy8ab4cbea07a9
diff --git a/server/lib/hfCache.js b/server/lib/hfCache.js
index 61f17c4f45..96d4930f95 100644
--- a/server/lib/hfCache.js
+++ b/server/lib/hfCache.js
@@ -191,6 +191,13 @@ async function verifySafetensorsStructure(path, size) {
} catch {
return { ok: false, reason: 'unparseable-header' };
}
+ // A valid safetensors header is a JSON object. Anything else that happens
+ // to parse (null, an array, a bare number/string) is corrupt — and
+ // `Object.entries(null)` would throw, turning a repairable bad file into a
+ // 500 on the status/verify/repair endpoints.
+ if (!header || typeof header !== 'object' || Array.isArray(header)) {
+ return { ok: false, reason: 'unparseable-header' };
+ }
// The largest tensor end-offset (relative to the byte buffer after the
// header) is the minimum payload length the file must contain.
let maxEnd = 0;
diff --git a/server/lib/hfCache.test.js b/server/lib/hfCache.test.js
index e66cc50d71..f6830fc931 100644
--- a/server/lib/hfCache.test.js
+++ b/server/lib/hfCache.test.js
@@ -238,6 +238,24 @@ describe('verifyModelCache', () => {
expect(r.files[0].reason).toBe('bad-header-length');
});
+ it('flags a parseable-but-non-object header (e.g. JSON null) as bad without throwing', async () => {
+ // Header length points at valid JSON `null` — Object.entries(null) would
+ // throw, which must surface as status:'bad', not a 500.
+ const headerBuf = Buffer.from('null', 'utf8');
+ const lenBuf = Buffer.alloc(8);
+ lenBuf.writeBigUInt64LE(BigInt(headerBuf.length), 0);
+ const content = Buffer.concat([lenBuf, headerBuf, Buffer.alloc(8)]);
+ const { root } = buildContentCache({
+ repoId: 'org/nullheader',
+ files: { 'model.safetensors': { content } },
+ });
+ roots.push(root);
+ process.env.HF_HUB_CACHE = root;
+ const r = await verifyModelCache('org/nullheader');
+ expect(r.status).toBe('bad');
+ expect(r.files[0].reason).toBe('unparseable-header');
+ });
+
it('deep check passes when content sha256 matches the blob name', async () => {
const { root } = buildContentCache({
repoId: 'org/deepok',