From 236585a74e973ddac77b8d34abfed58ec3a7175d Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Sun, 15 Nov 2026 16:28:17 +0700 Subject: [PATCH] fix(gguf): fail closed instead of replacing a non-GGUF file with synthetic metadata protectGgufBuffer synthesised a brand-new container whenever the GGUF header did not parse. For a file that only carries a .gguf extension (detector.ts falls back to the extension, so any file reaches this path) the caller's bytes were discarded and replaced with decoy metadata, while protect reported success. PR #2 raised this and was closed on the understanding that preserving existing KV entries fixed it; that fix covers well-formed headers only. - throw InvalidGgufInputError instead of synthesising - probe doctor with a minimal valid v3 header rather than an empty buffer - cover the non-GGUF, empty and tensor-less cases in tests --- src/setup/doctor.ts | 7 ++++- src/shields/formats/model.ts | 40 +++++++++++++++++------- tests/deep-vertical-shields.test.ts | 5 ++- tests/gguf-structural-validation.test.ts | 28 ++++++++++++++++- 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/setup/doctor.ts b/src/setup/doctor.ts index 9c8626d..eb9aa0a 100644 --- a/src/setup/doctor.ts +++ b/src/setup/doctor.ts @@ -336,7 +336,12 @@ function checkEngines(sqliteOverride?: boolean): DoctorDiagnosticReport["engines intensity: "stealth", seed: "doctor-probe", }); - const ggufRes = protectGgufBuffer(Buffer.alloc(0), { + // An empty buffer is not a valid probe anymore: protectGgufBuffer fails + // closed on non-GGUF input. Probe with a minimal valid v3 header instead. + const emptyGguf = Buffer.alloc(24, 0); + emptyGguf.write("GGUF", 0, "ascii"); + emptyGguf.writeUInt32LE(3, 4); + const ggufRes = protectGgufBuffer(emptyGguf, { intensity: "stealth", seed: "doctor-probe", }); diff --git a/src/shields/formats/model.ts b/src/shields/formats/model.ts index d537876..6c32496 100644 --- a/src/shields/formats/model.ts +++ b/src/shields/formats/model.ts @@ -410,6 +410,14 @@ function parseExistingGgufHeader(bytes: Buffer): { }; } +/** Input that is not a parseable GGUF container. Never replaced with synthetic weights. */ +export class InvalidGgufInputError extends Error { + constructor(reason: string) { + super(`Invalid or unsupported GGUF input: ${reason}`); + this.name = "InvalidGgufInputError"; + } +} + /** * Builds or protects a GGUF v2/v3 (`.gguf`) model binary against REA's `GgufHeader.ts`: * 1. Preserves all existing GGUF metadata KV entries and trailing tensor records. @@ -437,13 +445,23 @@ export function protectGgufBuffer( const deception = buildCanaryAndDiagnosticPayloads(prng, intensity); const parsed = parseExistingGgufHeader(bytes); - const existingKvSlice = parsed ? bytes.subarray(24, parsed.kvEndOffset) : Buffer.alloc(0); - const trailingTensorSlice = parsed ? bytes.subarray(parsed.kvEndOffset) : Buffer.alloc(0); + if (parsed === null) { + // Fail closed like the safetensors / SQLite / archive shields. Synthesising + // a fresh container here discarded the caller's bytes entirely and returned + // a valid-looking GGUF with no weights. A `.gguf` extension alone reaches + // this path (detector.ts falls back to the extension), so it is reachable + // with any file the user is asked to "protect". + throw new InvalidGgufInputError( + "input is not a parseable GGUF v2/v3 container; refusing to replace it with synthetic metadata", + ); + } + const existingKvSlice = bytes.subarray(24, parsed.kvEndOffset); + const trailingTensorSlice = bytes.subarray(parsed.kvEndOffset); const kvEntries: Buffer[] = []; // KV 1: general.description (STRING = 8) — only add if not already present in input - if (!parsed || !parsed.existingKeys.has("general.description")) { + if (!parsed.existingKeys.has("general.description")) { const key = encodeGgufString("general.description"); const type = Buffer.alloc(4); type.writeUInt32LE(8, 0); @@ -454,7 +472,7 @@ export function protectGgufBuffer( } // KV 2: enclave.delegation.manifest (STRING = 8) - if (!parsed || !parsed.existingKeys.has("enclave.delegation.manifest")) { + if (!parsed.existingKeys.has("enclave.delegation.manifest")) { const key = encodeGgufString("enclave.delegation.manifest"); const type = Buffer.alloc(4); type.writeUInt32LE(8, 0); @@ -467,7 +485,7 @@ export function protectGgufBuffer( // KV canary / diagnostic STRING metadata (under enclave.* hierarchy) deception.canary.syntheticCanaryTokens.forEach((tok, idx) => { const keyName = `enclave.canary.token.${idx}`; - if (!parsed || !parsed.existingKeys.has(keyName)) { + if (!parsed.existingKeys.has(keyName)) { const key = encodeGgufString(keyName); const type = Buffer.alloc(4); type.writeUInt32LE(8, 0); @@ -476,7 +494,7 @@ export function protectGgufBuffer( }); deception.canary.ambiguousCredentialUrls.forEach((url, idx) => { const keyName = `enclave.canary.transport.${idx}`; - if (!parsed || !parsed.existingKeys.has(keyName)) { + if (!parsed.existingKeys.has(keyName)) { const key = encodeGgufString(keyName); const type = Buffer.alloc(4); type.writeUInt32LE(8, 0); @@ -485,7 +503,7 @@ export function protectGgufBuffer( }); deception.diagnostics.diagnosticsInjected.forEach((d, idx) => { const keyName = `enclave.diagnostic.${idx}`; - if (!parsed || !parsed.existingKeys.has(keyName)) { + if (!parsed.existingKeys.has(keyName)) { const key = encodeGgufString(keyName); const type = Buffer.alloc(4); type.writeUInt32LE(8, 0); @@ -498,7 +516,7 @@ export function protectGgufBuffer( // KV nesting: enclave.nesting.breaker — 35 nested ARRAY frames (> MODEL_METADATA_NESTING = 32) const nestingDepth = REA_INTERNAL_LIMITS.MODEL_MAX_METADATA_NESTING + 3; - if (!parsed || !parsed.existingKeys.has("enclave.nesting.breaker")) { + if (!parsed.existingKeys.has("enclave.nesting.breaker")) { const key = encodeGgufString("enclave.nesting.breaker"); const outerType = Buffer.alloc(4); outerType.writeUInt32LE(9, 0); // KV value type = 9 (ARRAY) @@ -533,10 +551,10 @@ export function protectGgufBuffer( const header = Buffer.alloc(24); header.write("GGUF", 0, "ascii"); - header.writeUInt32LE(parsed ? parsed.version : 3, 4); - header.writeBigUInt64LE(parsed ? parsed.tensorCount : 0n, 8); + header.writeUInt32LE(parsed.version, 4); + header.writeBigUInt64LE(parsed.tensorCount, 8); header.writeBigUInt64LE( - (parsed ? parsed.kvCount : 0n) + BigInt(kvEntries.length), + parsed.kvCount + BigInt(kvEntries.length), 16, ); diff --git a/tests/deep-vertical-shields.test.ts b/tests/deep-vertical-shields.test.ts index 702f678..c8dedec 100644 --- a/tests/deep-vertical-shields.test.ts +++ b/tests/deep-vertical-shields.test.ts @@ -180,7 +180,10 @@ describe("Deep Vertical Shields (Runtime Deception, Electron Fuses, SQLite, AI M expect(afterSt.blaProtected).toBe(true); // GGUF v3 model shield - const protGguf = protectGgufBuffer(Buffer.alloc(0), { + const emptyGguf = Buffer.alloc(24, 0); + emptyGguf.write("GGUF", 0, "ascii"); + emptyGguf.writeUInt32LE(3, 4); + const protGguf = protectGgufBuffer(emptyGguf, { intensity: "bankrupt", seed: "gguf-seed", }); diff --git a/tests/gguf-structural-validation.test.ts b/tests/gguf-structural-validation.test.ts index 1110f85..e35e2c9 100644 --- a/tests/gguf-structural-validation.test.ts +++ b/tests/gguf-structural-validation.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { inspectModelBuffer } from "../src/shields/formats/model.js"; +import { protectGgufBuffer, inspectModelBuffer } from "../src/shields/formats/model.js"; import { parseGgufLayout } from "../src/shields/formats/gguf-layout.js"; const ALIGNMENT = 32; @@ -90,4 +90,30 @@ describe("GGUF inspection does not trust the magic alone", () => { expect(inspection.validModel).toBe(false); expect(inspection.blaProtected).toBe(true); }); + + it("refuses to replace a non-GGUF file with synthetic metadata", () => { + // A `.gguf` extension alone routes a file here, so the protection entry + // point must fail closed instead of emitting a container with no weights. + expect(() => + protectGgufBuffer(Buffer.from("not a model at all"), { + intensity: "balanced", + seed: "gguf-fail-closed", + }), + ).toThrow(/not a parseable GGUF/); + expect(() => + protectGgufBuffer(Buffer.alloc(0), { + intensity: "balanced", + seed: "gguf-fail-closed", + }), + ).toThrow(/not a parseable GGUF/); + }); + + it("still protects a well-formed GGUF with no tensors", () => { + const protectedModel = protectGgufBuffer(gguf({ withTensor: false }), { + intensity: "balanced", + seed: "gguf-fail-closed", + }); + expect(protectedModel.buffer.subarray(0, 4).toString("ascii")).toBe("GGUF"); + expect(protectedModel.buffer.length).toBeGreaterThan(24); + }); }); \ No newline at end of file