Files
llmgw/internal/agent/weights/pickle.go
T
Ilker ManapandClaude Opus 5.5 4466d2d15a Weights that run code are refused
Python pickles (.bin, .pt, .pth, .ckpt, .pkl) run whatever they hold when
loaded, as the engine's user, on the node; that is how the malicious models
found on Hugging Face work. The node refuses such a source, or a mirror set
holding one, before fetching anything. A model added with one, or given
one, is refused, while one already in the catalog still loads and is
refused by its node with the same words. vLLM and SGLang downloading a
model by its id get --load-format safetensors. The mirror page marks the
file. allow_pickle: true on a model lets it through on purpose, carried to
the node in EngineProfile.allow_pickle.

BACKLOG M lists the rest: checksums against Hugging Face on every path,
its own scan, trusted publishers, engines without the internet, an eval
before an untrusted model serves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 23:17:22 +01:00

42 lines
1.4 KiB
Go

package weights
import (
"errors"
"fmt"
"path"
"strings"
)
// Weights in Python's pickle format are a program as much as a file: loading
// one runs whatever it says, as the engine's user, on the node. That is how
// the malicious models found on Hugging Face work, and nothing in the file's
// name or size says so. safetensors and GGUF are only tensors and are read,
// never run, so those are what a node fetches unless the catalog says
// otherwise for a model, with allow_pickle, on purpose.
// pickleExts are the extensions of PyTorch's pickle formats, and of pickle
// itself. A .bin beside safetensors is the same model in pickle form.
var pickleExts = []string{".bin", ".pt", ".pth", ".ckpt", ".pkl", ".pickle"}
// Pickled reports whether a file name or URL is in a pickle format.
func Pickled(name string) bool {
if i := strings.IndexAny(name, "?#"); i >= 0 && strings.Contains(name, "://") {
name = name[:i]
}
ext := strings.ToLower(path.Ext(name))
for _, e := range pickleExts {
if ext == e {
return true
}
}
return false
}
// ErrPickle is a source refused for being in a pickle format.
var ErrPickle = errors.New("weights in a pickle format run code when they are loaded")
func refusePickle(name string) error {
return fmt.Errorf("%w: %s is refused; use the model's safetensors or GGUF, or set allow_pickle: true "+
"on the model if it was checked and has no other form", ErrPickle, redact(name))
}