Repository navigation
Closed
Labels
bugSomething isn't working
Description
Summary
With trust_remote_code=False (the default), several load, save and dequantize paths still import Python files from the model snapshot. Loading or re-saving an untrusted Hub repo can therefore run arbitrary code. Audited at a16977e.
Findings
1. GPTQModel.load() / from_pretrained() import auto_map classes when trust is off (Critical, CWE-94/829)
- Sink:
gptqmodel/utils/hf.py:1900-1923.prepare_remote_model_init_compat()readsconfig.auto_map["AutoModelForCausalLM"](falling back toAutoModelForVision2Seq, orAutoModelforlocateanything). It passes the value totransformers.dynamic_module_utils.get_class_from_dynamic_module(class_ref, model_path), which has no trust check of its own. - Reached from:
- Load:
loader.py:1789callsAutoConfig.from_pretrained(..., trust_remote_code=trust_remote_code), thenloader.py:1801callsprepare_remote_model_init_compat(model_local_path, config)without passing the flag. - Quantize: the same pattern at
loader.py:1307→1313.
- Load:
- Why the existing gate misses it: a config with a native
model_type(e.g.llama) plus anauto_mapentry loads as a plain built-in config and keepsauto_mapas an attribute. Model selection then picks a native QModel, so therequire_trust_remote_codegate (loader.py:1722) never fires, and the snapshot module is imported anyway. - Cross-repo variant: transformers'
repo--module.Classsyntax also letsclass_refpull code from a different Hub repo. - Related:
normalize_hf_config_compat(hf.py:1729-1735) also turns on remote-code compat patches when onlyauto_mapis present (has_remote_automap). - Verified: a standalone reproduction with transformers 5.22.2 imported the snapshot module.
2. Re-saving a loaded quantized model forces trust_remote_code=True (High, CWE-94)
- Sink:
gptqmodel/models/writer.py:2140-2147.get_model_with_quantize()callsAutoConfig.from_pretrained(model_id_or_path, trust_remote_code=True). - Reached from:
GPTQModel.load(untrusted)followed by.save()/save_quantized(), throughwriter.py:1861. This path runs for any non-EXL3 re-save, conversion, or push of a downloaded quant. - Trigger: an
auto_map.AutoConfigentry is ignored at load (trust=False) but executed at save. This is independent of finding 1. - Fix: use
self.trust_remote_code.
3. dequantize_model() hard-codes trust_remote_code=True (High, CWE-94)
- Sinks:
gptqmodel/utils/model_dequant.py:335and:354, on the compressed-tensors path. The format is chosen from checkpoint data:quant_method=compressed-tensorswithformat=pack-quantized, or any.weight_packedkey. - Reached from: the public
dequantize_model()andscripts/dequantize_model.py. The API has no trust parameter. - Fix: add
trust_remote_code: bool = Falseand pass it through.
4. Hardening
- Processor loading ignores the caller's trust flag:
base.py:746usesself.require_trust_remote_codeinstead of the user's value.- Several
load_processor()methods hard-codetrust_remote_code=True, for exampledeepseek_vl_v2.py:162,minicpmv.py:94,openvla.py:177andnemotron_omni.py:143. - Today these are protected only indirectly, by the loader's trust gate.
KimiK3Sourceruns snapshot code with no opt-in:utils/kimi_k3.py:24-57load_calibration_referenceexecsconfiguration_kimi_k3.py/modeling_kimi_linear.pyfrom the snapshot with no opt-in. It is reached only through the explicitKimiK3*Sourceresearch APIs, but those should requiretrust_remote_code=True.
Suggested fix
- Pass the user's
trust_remote_codeintoprepare_remote_model_init_compatandnormalize_hf_config_compat, and return early when it is False. - Never hard-code
True. - Add a regression test that loads, saves and dequantizes a checkpoint whose
auto_mappoints at a module that writes a marker file, with trust=False, and asserts the marker is absent.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
fix(security): gate checkpoint code imports on trust_remote_code (#3968…