Skip to content

Studio STT: only load safetensors weights for custom dictation models (RCE fix) - #7364

Merged
danielhanchen merged 2 commits into
mainfrom
fix/stt-pickle-rce
Jul 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/stt-pickle-rce

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Fixes an authenticated remote code execution path introduced with the local STT dictation feature (#7095). The STT sidecar accepts arbitrary Hugging Face owner/model repositories for custom dictation models, and when a repo shipped no safetensors it downloaded and loaded pytorch_model.bin through WhisperForConditionalGeneration.from_pretrained. PyTorch checkpoints are pickles that execute code during deserialization, and this path never ran the malware gate the normal model loader applies (evaluate_file_security in core/inference/worker.py, per utils/security/file_security.py). An authenticated client on a network-exposed Studio instance could therefore point dictation at a crafted Whisper-looking repo and run code as the backend user.

Fix

Restrict custom STT repositories to safetensors, in three layers so no single check is load-bearing:

  • _select_snapshot_files no longer falls back to pytorch_model.bin / pytorch_model.bin.index.json; a repo with no safetensors weights fails closed with an actionable error instead of downloading a pickle.
  • _snapshot_is_complete ignores cached pickle weights, so a previously cached pickle checkpoint is treated as incomplete and re-resolved (then rejected) rather than reused.
  • _build_model passes use_safetensors = True to from_pretrained, so even a stray cached pytorch_model.bin cannot be deserialized.

Safetensors is pure tensor data and cannot execute code, and trust_remote_code stays at its default False, so this closes the load-time RCE surface without depending on a network-time security scan.

No behavior change for built-in models

The five curated Whisper defaults (unsloth/whisper-tiny / -base / -small / -large-v3-turbo / -large-v3) each ship model.safetensors and no pytorch_model.bin, so default dictation is unaffected. Only custom repos that publish pickle weights and no safetensors are now rejected, with a message pointing to save_pretrained(safe_serialization=True).

Tests

  • test_snapshot_selection_rejects_pickle_only_weights: a repo with only pytorch_model.bin raises SttModelCompatibilityError.
  • test_pickle_checkpoint_snapshot_is_never_complete: sharded and single-file pickle snapshots read as incomplete; the safetensors equivalent stays complete.
  • test_load_uses_model_hub_cache_without_implicit_download: the weight load asserts use_safetensors=True.
  • Existing selection/download tests already asserted pytorch_model.bin is excluded when safetensors are present; those continue to pass.
  • python -m pytest tests/ -k "stt or dictation or whisper or transcribe": 379 passed, 1 skipped.

The STT sidecar accepts arbitrary Hugging Face owner/model repos for
custom dictation models and, when safetensors were absent, downloaded
and loaded pytorch_model.bin through WhisperForConditionalGeneration
.from_pretrained. PyTorch checkpoints are pickles that execute code
during deserialization, and this path does not run the malware gate the
normal model loader applies, so an authenticated client on an exposed
Studio instance could load a crafted Whisper-looking repo and run code
in the backend.

Restrict custom STT repos to safetensors: the snapshot selector no
longer falls back to pytorch_model.bin(.index.json), the cached-snapshot
completeness check ignores pickle weights, and the load forces
use_safetensors so a stray cached pickle still cannot execute. The five
curated Whisper defaults already ship safetensors only, so this changes
nothing for the built-in models.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

shards = set(weight_map.values())
if not all(isinstance(shard, str) and shard in siblings for shard in shards):

P1 Badge Reject non-safetensors shards referenced by the safe index

A custom repository can publish model.safetensors.index.json whose weight_map references existing pytorch_model-*.bin shards. This accepts and downloads those shards because only their presence is checked here. use_safetensors=True selects the safetensors index, but Transformers subsequently dispatches each indexed shard according to its own filename, so .bin entries take the PyTorch deserialization path. Require every weight_map value to end in .safetensors (and apply the same validation in _snapshot_is_complete) to keep the intended pickle-free invariant.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

A safetensors index (model.safetensors.index.json) is attacker-supplied
JSON and can name pytorch_model-*.bin shards in its weight_map.
Transformers dispatches shard loading per file by extension, so those
.bin shards still load through torch.load (pickle) even with
use_safetensors set. Require every weight_map value to end in
.safetensors in both the snapshot selector and the completeness check so
no pickle shard is downloaded or reused.
@danielhanchen

Copy link
Copy Markdown
Member Author

Good catch on the P1. Fixed in fe30963: a model.safetensors.index.json can name pytorch_model-*.bin shards in its weight_map, and Transformers dispatches each shard by its own filename, so those .bin entries would still take the pickle path even with use_safetensors=True. _select_snapshot_files now rejects any index whose weight_map has a value not ending in .safetensors (fails closed with an actionable error), and _snapshot_is_complete applies the same check so a cached mixed index is treated as incomplete and re-resolved rather than reused. Added two regression tests covering the safe-index-pointing-at-pickle-shards case.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: fe30963031

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 734cec9 into main Jul 23, 2026
33 of 38 checks passed
@danielhanchen
danielhanchen deleted the fix/stt-pickle-rce branch July 23, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant