Prevent path traversal via sharded-checkpoint index filenames - #47996
Open
jaideeppyne wants to merge 1 commit into
Open
Prevent path traversal via sharded-checkpoint index filenames#47996jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
get_checkpoint_shard_files reads shard filenames from the model-supplied index (model.safetensors.index.json / pytorch_model.bin.index.json) and joins each one to the model directory with os.path.join and no validation. Those weight_map values are attacker-controlled: a value containing ../ escapes the directory and an absolute value is honored verbatim, so loading an untrusted local model directory opens/reads arbitrary files (and fully exfiltrates the tensor contents of any safetensors file the process can read). Reject any shard filename that is not a plain filename located next to the index. Legitimate index files always reference sibling shards, so this does not affect normal loading. Fixes huggingface#47176.
Contributor
CI recapDashboard: View test results in Grafana |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #47176.
Diagnosis
When
from_pretrainedloads a sharded checkpoint,get_checkpoint_shard_files(src/transformers/utils/hub.py) reads the shard filenames straight from the model-supplied index (model.safetensors.index.json/pytorch_model.bin.index.json) and joins each to the model directory withos.path.joinand no validation:The
weight_mapvalues are arbitrary strings from the checkpoint.os.path.joindoes not neutralize.., and an absolute value discards the directory prefix entirely, so a malicious index escapes the model directory. The returned paths then flow intosafe_open(...)/torch.load(...), so a plainAutoModel.from_pretrained("/untrusted/model/dir"):weight_map = {"w": "/etc/passwd"}), andweight_map = {"stolen": "../victim/private.safetensors"}.This is the "load a model someone gave me" threat model (a downloaded repo, a shared directory on a multi-tenant host). No auth required beyond getting the victim to load the directory.
Fix
Reject any shard filename that isn't a plain filename sitting next to the index, before it is joined:
Placed right after the
weight_mapvalues are read, so it guards both the local-directory and the Hub-download paths. Legitimate index files are always produced with sibling shard filenames (model-00001-of-0000N.safetensors), confirmed against the sharded-save code inmodeling_utils.py, so normal loading is unaffected.Tests
tests/utils/test_hub_utils.py::GetCheckpointShardFilesTest:../,sub/dir/...,.., and absolute (/etc/passwd)weight_mapvalues raiseValueError.Verified locally: the traversal test fails on
main(ValueError not raised) and passes with the fix.ruff check/ruff formatclean on the changed lines; the diff is a single guard plus the test.