python: add hf-trust-remote-code rule (HuggingFace trust_remote_code=True RCE)#83
Open
DevamShah wants to merge 2 commits into
Open
python: add hf-trust-remote-code rule (HuggingFace trust_remote_code=True RCE)#83DevamShah wants to merge 2 commits into
DevamShah wants to merge 2 commits into
Conversation
…True RCE) Signed-off-by: Devam Shah <devamshah91@gmail.com>
…True RCE) Signed-off-by: Devam Shah <devamshah91@gmail.com>
|
|
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.
Add
hf-trust-remote-coderule: arbitrary code execution via Hugging Facetrust_remote_code=TrueSummary
Adds a Python rule,
hf-trust-remote-code, that flags loading Hugging Facetransformers/datasetsassets withtrust_remote_code=True, the highest-impact untracked remote-code-execution vector in the ML-supply-chain category this repo already covers (pickle, joblib, keras, ONNX, torch).Problem / motivation
trust_remote_code=Trueinstructstransformers/datasetsto download and execute arbitrary Python shipped inside a model or dataset repository (modeling_*.py,configuration_*.py, dataset loading scripts) at load time. A malicious, typosquatted, or compromised Hub repository therefore achieves remote code execution on the loading host, the same trust boundary as the pickle deserialization rules already in this directory, but reached through an explicit, greppable keyword argument rather than an opaque serialized blob.The repository covers pickle-based RCE across PyTorch, NumPy, Pandas, TensorFlow, Keras, ONNX, and scikit/joblib, yet has no rule for
trust_remote_code, which is the most common code-execution switch in modern LLM and dataset loading code. Hugging Face's own documentation warns to "take extra precaution when loading a custom model" and to pin a reviewedrevision, but provides no static enforcement. This rule closes that gap.Change
python/hf-trust-remote-code.yaml— new rule following the existing ML-supply-chain rule conventions (category: security,subcategory: [vuln],severity: ERROR,impact: HIGH,confidence: MEDIUM,>-multiline message, backticked identifiers,languages: [python]).python/hf-trust-remote-code.py— test fixture with 9 true positives (ruleid:) and 9 true negatives (ok:), ordered simple-to-complex per CONTRIBUTING.README.md— regenerated python rules table (python rules_table_generator.py); single-row insertion.Detection covers
pattern-either:$OBJ.from_pretrained(..., trust_remote_code=True, ...)— coversAutoModel*,AutoTokenizer,AutoConfig,AutoFeatureExtractor,AutoProcessor, and model-specific classes uniformly.pipeline(...)andtransformers.pipeline(...).load_dataset(...)anddatasets.load_dataset(...).False-positive controls (2+ matchers): every call shape has a paired
pattern-not: ... trust_remote_code=False, and the rule matches the literalTrue(not a bare metavariable). Combined with Semgrep constant propagation this flags provably-true values, ignores explicitFalse, and does not fire on omitted arguments, the safe default.Security rationale
trust_remote_code=Trueis the canonical in-code manifestation.revision(HF's recommended mitigation) does not remove the risk and is intentionally still flagged for review.Testing / validation
Run from a clean clone with the rule and fixture in place (
semgrep 1.159.0):README table regenerated with the repo's own
python rules_table_generator.py; diff is a single inserted row, alphabetically placed betweenautomatic-memory-pinningandlxml-in-pandas.References used: Hugging Face custom-models documentation and the Trail of Bits ML supply-chain writeup, per CONTRIBUTING's reference guidance.
🤖 Generated with Claude Code