Skip to content

armgit / bla

Public Imported from GitHub
0

🛡️ BLA (Blind LLM Agents) — Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

fix: reject numeric header tokens before JSON rounding

Codexbfbefb0adb16
diff --git a/README.md b/README.md
index 072d9ad..95b0df6 100644
--- a/README.md
+++ b/README.md
@@ -306,7 +306,11 @@ and emits an 8-byte-aligned header. Its deliberately conservative supported dtyp
subset is `BOOL`, `U8`, `I8`, `U16`, `I16`, `F16`, `BF16`, `U32`, `I32`, `F32`, `U64`,
`I64`, and `F64`. Other dtypes are reported as unsupported; this is not a claim that
those models are invalid. Headers above 100 MB and integers beyond JavaScript's
-safe range are refused rather than silently rounded.
+safe range are refused rather than silently rounded. Numeric tokens anywhere in
+headers must use integer notation; fractional or exponent notation is unsupported,
+even if its mathematical value is integral. This lexical check prevents JSON
+parsing from silently rounding malformed dimensions/offsets into accepted values.
+Numbers inside string metadata remain unchanged.
These are structural and byte-preservation checks, **not** official-loader
validation, model inference, or evidence of effectiveness against REA. The
diff --git a/src/shields/formats/model.ts b/src/shields/formats/model.ts
index 98c49f1..4bb15e2 100644
--- a/src/shields/formats/model.ts
+++ b/src/shields/formats/model.ts
@@ -48,13 +48,21 @@ export function validateSafetensorsBuffer(bytes: Buffer): {
header = JSON.parse(text);
// JSON.parse otherwise silently loses duplicate fields, including tensor names.
const stack: Array<Set<string> | null> = [];
- const tokens = /"(?:[^"\\]|\\.)*"|[{}\[\]]/g;
+ const tokens = /"(?:[^"\\]|\\.)*"|[{}\[\]]|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
for (const token of text.matchAll(tokens)) {
const value = token[0];
if (value === "{") stack.push(new Set());
else if (value === "[") stack.push(null);
else if (value === "}" || value === "]") stack.pop();
- else {
+ else if (!value.startsWith('"')) {
+ // JSON.parse rounds before Number.isSafeInteger can inspect the value.
+ // The supported header subset requires integer lexical tokens, not
+ // fractions/exponents even when they would round to an integer.
+ if (!/^-?\d+$/.test(value) || BigInt(value) > BigInt(Number.MAX_SAFE_INTEGER) ||
+ BigInt(value) < BigInt(Number.MIN_SAFE_INTEGER)) {
+ fail("unsupported numeric token: require safe integer notation");
+ }
+ } else {
const decoded = JSON.parse(value) as string;
if (/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(decoded)) {
fail("invalid Unicode string");
diff --git a/tests/safetensors-integrity.test.ts b/tests/safetensors-integrity.test.ts
index f5ed13f..1274889 100644
--- a/tests/safetensors-integrity.test.ts
+++ b/tests/safetensors-integrity.test.ts
@@ -9,7 +9,7 @@ const header = {
weights: { dtype: "F32", shape: [2], data_offsets: [0, 8] },
bias: { dtype: "I16", shape: [2], data_offsets: [8, 12] },
empty: { dtype: "F32", shape: [0], data_offsets: [12, 12] },
- __metadata__: { description: "trained weights — 测试", "bla.shield.signature": "keep-original", "enclave.manifest.0": "keep-this-too" },
+ __metadata__: { description: "trained weights — 测试; 1.0000000000000001 1e999 9007199254740993", "bla.shield.signature": "keep-original", "enclave.manifest.0": "keep-this-too" },
};
const data = Buffer.alloc(12);
data.writeFloatLE(1.25, 0);
@@ -73,6 +73,17 @@ describe("Safetensors format and tensor preservation (not inference)", () => {
expect(inspectModelBuffer(bytes).validModel).toBe(false);
expect(bytes).toEqual(snapshot);
});
+ it.each(["1.0000000000000001", "1.5", "1e0", "1e-999", "1e999", "9007199254740993"])(
+ "rejects unsupported numeric token %s before rounded values can be accepted", token => {
+ const bytes = rawHeader(`{"x":{"dtype":"F32","shape":[${token}],"data_offsets":[0,4]}}`, data.subarray(0, 4));
+ expect(() => protectSafetensorsBuffer(bytes)).toThrow(/unsupported numeric token/);
+ expect(inspectModelBuffer(bytes).validModel).toBe(false);
+ },
+ );
+ it("also rejects fractional offset notation that rounds to an integer", () => {
+ const bytes = rawHeader('{"x":{"dtype":"F32","shape":[1],"data_offsets":[0,4.0000000000000001]}}', data.subarray(0, 4));
+ expect(() => protectSafetensorsBuffer(bytes)).toThrow(/unsupported numeric token/);
+ });
it("explicitly reports unsupported dtypes rather than rewriting them", () => {
const model = buildSafetensorsBuffer({ x: { dtype: "F8_E4M3", shape: [12], data_offsets: [0, 12] } }, data);
expect(() => protectSafetensorsBuffer(model)).toThrow(/unsupported tensor dtype/);