SKY-D213 · Security in code
Hugging Face trust_remote_code runs repository code
A Hugging Face model or dataset can ship its own Python files next to its weights. With trust_remote_code=True, transformers, datasets and the libraries built on them download those files and import them, so code from the model's repository runs inside your process. SKY-D213 reports every call that switches this on with a literal True.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
Whoever can push to that repository (its owner, or someone holding a stolen token) can change modeling_*.py, and your next start runs the new code with your server's permissions, files and secrets. Without a pinned revision the code can change between two runs while nothing changes in your own repository.
What Skylos flags
from transformers import AutoModelForCausalLM, AutoTokenizer MODEL_ID = "acme-labs/support-chat-7b" def load_chat_model(): tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True) return tokenizer, modelWhat Skylos leaves alone
import argparse from transformers import AutoModelForCausalLM, AutoTokenizer def main(): parser = argparse.ArgumentParser(description="Chat with a local model.") parser.add_argument("model_id") parser.add_argument("--trust-remote-code", action="store_true") args = parser.parse_args() tokenizer = AutoTokenizer.from_pretrained(args.model_id) model = AutoModelForCausalLM.from_pretrained( args.model_id, trust_remote_code=args.trust_remote_code ) return tokenizer, model if __name__ == "__main__": main()Why Skylos leaves it alone
The flag is off unless the person running the script passes --trust-remote-code. Skylos reports only the literal True, where the code always runs the repository's files; a value chosen at run time is the caller's decision. The same file still gets SKY-D345 for the missing revision=.
How Skylos decides
- Looks at every call in a Python file, whatever the function is called:
from_pretrained(),pipeline(),load_dataset(), LangChain'sHuggingFaceEmbeddings()and so on. - Reports the call when it passes
trust_remote_code=Trueas a keyword, or passes a literal dictionary that holds"trust_remote_code": True, for examplemodel_kwargs={"trust_remote_code": True}. - Skips the call when the value is
Falseor anything other than the literalTrue: a variable, a setting, or a**optionsdictionary built elsewhere. - Lowers the finding to MEDIUM when
revision=in the same call is a literal 40-character hex commit SHA (main,master,latest,refs/heads/…andrefs/pr/…do not count), or when the model reference (the first argument, orpretrained_model_name_or_path=,model_name_or_path=,model=,model_name=orpath=) is a string literal starting with./,../,/or~. Otherwise it is HIGH. - Reports one finding on the line where the call starts. A missing or branch-name revision on a Hugging Face download is a separate rule, SKY-D345.
How to fix it
If transformers supports the architecture itself, drop the flag: the library then builds the model from its own installed code. Pin revision= to a commit SHA so the files cannot change under you. If the model really needs its own code, read that code at one commit and pin the literal SHA in the call; Skylos then reports MEDIUM, which you can silence with the reason.
from transformers import AutoModelForCausalLM, AutoTokenizer MODEL_ID = "acme-labs/support-chat-7b"Added: MODEL_REVISION = "4f2c9e1d7a6b5c3e2f1a0b9c8d7e6f5a4b3c2d1e" def load_chat_model():Removed: tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)Removed: model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)Added: tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, revision=MODEL_REVISION)Added: model = AutoModelForCausalLM.from_pretrained(MODEL_ID, revision=MODEL_REVISION) return tokenizer, modelWhen it can be wrong
- A model whose code you have reviewed and pinned to one commit is still reported (as MEDIUM with a literal SHA, HIGH with a SHA in a constant). Skylos cannot tell a reviewed commit from an unreviewed one.
- A flag that is always on but passes through a variable or a
**optionsdictionary is not reported. This is a gap, not a sign the call is safe.
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 reviewed call on the line where the call starts and say why. A bare
# noqadoes not hide a HIGH security finding.Python model = AutoModel.from_pretrained( # skylos: ignore[SKY-D213] reviewed at 4f2c9e1 MODEL_ID, revision=MODEL_REVISION, trust_remote_code=True)Accept the calls you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D213"]turns the rule off for the whole project, including calls added later.