SKY-D265 · Security in code
Unsafe ML model deserialization
Many model formats are Python pickles: PyTorch .pt and .pth checkpoints, joblib and scikit-learn models, NumPy files saved with objects, Keras models. Loading a pickle can run any code its author put in the file. SKY-D265 reports model-loading calls that can unpickle, and rates them CRITICAL when the file comes from a download or from outside input.
- Languages
- Python
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
- Finding type
- review
Why it matters
A checkpoint from a model hub, or one a user uploads, can carry a payload that runs a shell command the moment torch.load() opens it, with the permissions of your training job or inference server. Model files are rarely reviewed like code, so this is an easy way in.
What Skylos flags
import torchfrom huggingface_hub import hf_hub_download REPO_ID = "acme-labs/ticket-classifier"REVISION = "9b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c" def load_classifier(net): weights = hf_hub_download(REPO_ID, "classifier.pt", revision=REVISION) net.load_state_dict(torch.load(weights)) return net.eval()What Skylos leaves alone
import torchfrom huggingface_hub import hf_hub_download REPO_ID = "acme-labs/ticket-classifier"REVISION = "9b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c" def load_classifier(net): weights = hf_hub_download(REPO_ID, "classifier.pt", revision=REVISION) net.load_state_dict(torch.load(weights, weights_only=True)) return net.eval()Why Skylos leaves it alone
The file is still a downloaded pickle, but weights_only=True makes PyTorch use a restricted unpickler that accepts only tensors, containers and plain values, so the file cannot call arbitrary functions. Skylos skips torch.load() when this keyword is the literal True.
How Skylos decides
- Looks at Python calls and resolves import aliases, so
np.load,from tensorflow import kerasandfrom joblib import loadare recognised. - Reports
torch.load()unless it passesweights_only=Trueas a literal;numpy.load()only withallow_pickle=True; everyjoblib.load(); Kerasload_model()fromkeras.models,tensorflow.keras.modelsortf.keras.models; andpickle.load()when an argument contains a string ending in a model suffix (.pt,.pth,.ckpt,.bin,.h5,.keras,.pkl,.pickle,.joblib), for examplepickle.load(open("model.pkl", "rb")). - Rates the finding CRITICAL when an argument comes from outside: a variable assigned from
hf_hub_download(),snapshot_download(),requests.get()/post(),urllib.request.urlretrieve()orwget.download()(or such a call inside the argument); a string that starts withhttp://,https://,s3://,gs://orhf://; or tainted data: a function parameter,request.*orinput(). Otherwise it is HIGH. - Reports one finding per call, on the line where the call starts.
pickle.load()andpickle.loads()in general belong to SKY-D204 and SKY-D205, so apickle.load()of a model file can get both rules.
How to fix it
Ship and load weights in the safetensors format, which holds only tensor data and runs no code. When you must read a PyTorch checkpoint, pass weights_only=True and keep PyTorch at 2.6 or later: earlier versions had a bypass of that option (CVE-2025-32434). Load joblib, pickle and Keras files only from storage you control.
Removed: import torchfrom huggingface_hub import hf_hub_downloadAdded: from safetensors.torch import load_file REPO_ID = "acme-labs/ticket-classifier"REVISION = "9b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c" def load_classifier(net):Removed: weights = hf_hub_download(REPO_ID, "classifier.pt", revision=REVISION)Removed: net.load_state_dict(torch.load(weights))Added: weights = hf_hub_download(REPO_ID, "classifier.safetensors", revision=REVISION)Added: net.load_state_dict(load_file(weights)) return net.eval()When it can be wrong
- A helper such as
def load(path): return torch.load(path)is CRITICAL, because function parameters count as untrusted, even when every caller passes a file you built yourself. - PyTorch 2.6 and later already default to
weights_only=True, but Skylos cannot see the installed version and asks for the keyword. Kerasload_model()is reported for every file format and whatever itssafe_mode=setting. weights_only=Trueis trusted as written; Skylos does not check that the installed PyTorch has the fix for CVE-2025-32434.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Silence one call that reads only your own artefacts and say why. A bare
# noqadoes not hide a HIGH or CRITICAL security finding.Python model = joblib.load(MODEL_PATH) # skylos: ignore[SKY-D265] built by our CI jobAccept the loaders you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.