fix(model): admit safetensors header lengths that resemble pickle markers (#5918)
A safetensors length prefix such as 640 encodes as 80 02 00 ..., which the
provider read as a pickle PROTO marker and refused before the safetensors
parser ran. The parser that owns the length prefix now makes the call: a
pickle-like prefix is safetensors only when byte 8 is '{' and the declared
length is within the safetensors header ceiling. Real pickles stay refused
and are never deserialized.
Fixes #5895
This commit is contained in:
1 parent
af73951405
commit
87657c6297
4 files changed
+94
-9
No files matched your search
@@ -42,6 +42,7 @@ import { readSafetensorsHeader } from "./SafetensorsHeader.js";
|
||||
import {
|
||||
ModelFormatFailure,
|
||||
rejectModel,
|
||||
rejectPickle,
|
||||
MODEL_REPORT_BYTES,
|
||||
modelCapacityRemediation,
|
||||
modelCapacityReportedLimits,
|
||||
@@ -274,14 +275,7 @@ export class ModelArtifactProvider implements ModelArtifactPort {
|
||||
);
|
||||
const reader = new ModelHeaderReader(file, Number(initial.size), signal);
|
||||
const first = await reader.take(4);
|
||||
if (
|
||||
(first[0] === 0x80 && (first[1] ?? 0) >= 2 && (first[1] ?? 0) <= 5) ||
|
||||
first.equals(Buffer.from([0x50, 0x4b, 3, 4]))
|
||||
)
|
||||
throw new ModelFormatFailure(
|
||||
"unsupported",
|
||||
"PyTorch pickle/ZIP model deserialization is unsupported; supply GGUF or safetensors instead",
|
||||
);
|
||||
if (first.equals(Buffer.from([0x50, 0x4b, 3, 4]))) rejectPickle();
|
||||
const format = first.equals(Buffer.from("GGUF")) ? "gguf" : "safetensors";
|
||||
let partial: ModelArtifactPartial | undefined;
|
||||
if (format === "gguf") {
|
||||
|
||||
@@ -35,6 +35,13 @@ export function rejectModel(message: string): never {
|
||||
throw new ModelFormatFailure("format", message);
|
||||
}
|
||||
|
||||
export function rejectPickle(): never {
|
||||
throw new ModelFormatFailure(
|
||||
"unsupported",
|
||||
"PyTorch pickle/ZIP model deserialization is unsupported; supply GGUF or safetensors instead",
|
||||
);
|
||||
}
|
||||
|
||||
// Shared header ceiling for GGUF and Safetensors. Safetensors counts declared
|
||||
// JSON bytes after the eight-byte length prefix; GGUF counts the complete
|
||||
// header. Retained rows and the serialized report have separate allowances.
|
||||
|
||||
@@ -7,9 +7,11 @@ import type {
|
||||
} from "../domain/model/modelArtifact.js";
|
||||
import { ModelHeaderReader } from "./ModelHeaderReader.js";
|
||||
import {
|
||||
MODEL_HEADER_BYTES,
|
||||
ModelFormatFailure,
|
||||
ModelReportBudget,
|
||||
rejectModel,
|
||||
rejectPickle,
|
||||
} from "./ModelFormat.js";
|
||||
import { safetensorsBits } from "./ModelTensorTypes.js";
|
||||
|
||||
@@ -154,11 +156,19 @@ export const readSafetensorsHeader = async (
|
||||
tensors: ModelTensor[];
|
||||
}> => {
|
||||
const budget = new ModelReportBudget();
|
||||
// Lengths such as 640 (80 02 ...) also begin like a pickle PROTO opcode.
|
||||
const protocol = first[1] ?? 0;
|
||||
const pickle = first[0] === 0x80 && protocol >= 2 && protocol <= 5;
|
||||
if (pickle && reader.size < 9) rejectPickle();
|
||||
const second = await reader.take(4);
|
||||
reader.excludeSafetensorsLengthPrefix();
|
||||
const length =
|
||||
BigInt(first.readUInt32LE()) + (BigInt(second.readUInt32LE()) << 32n);
|
||||
if (length === 0n || (await reader.take(1))[0] !== 0x7b)
|
||||
const brace = length === 0n ? undefined : (await reader.take(1))[0];
|
||||
// A brace after an admissible length is safetensors, even if truncated.
|
||||
if (pickle && (brace !== 0x7b || length > BigInt(MODEL_HEADER_BYTES)))
|
||||
rejectPickle();
|
||||
if (brace !== 0x7b)
|
||||
rejectModel("File does not contain a GGUF or safetensors header");
|
||||
if (length > BigInt(Number.MAX_SAFE_INTEGER))
|
||||
throw new ModelFormatFailure(
|
||||
|
||||
Reference in new issue
Block a user