Skip to content

fix(security): gate checkpoint code imports on trust_remote_code (#3968) - #3981

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3968
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3968

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3968

TL;DR

trust_remote_code=False no longer executes checkpoint Python on load, save or dequantize. auto_map, quant_method=compressed-tensors and the re-save path are treated as checkpoint data, not consent. Net: 28 files, +434/-52 (204 lines are the new regression test and docs).

1. The problem

A checkpoint could carry a native model_type (so it loads as a built-in config and a native QModel) plus an auto_map entry. The existing require_trust_remote_code gate never fired because the native class was selected, but the snapshot module was still imported:

Mechanism Before (origin/main 93d3ae7)
Load-time auto_map import prepare_remote_model_init_compat() called get_class_from_dynamic_module(class_ref, model_path) with no trust check (gptqmodel/utils/hf.py:1900-1923); reached from loader.py:1789/1801 and 1307/1313
auto_map compat patches normalize_hf_config_compat() enabled remote-code shims whenever auto_map existed (gptqmodel/utils/hf.py:1729-1735)
Re-save get_model_with_quantize() called AutoConfig.from_pretrained(..., trust_remote_code=True) (gptqmodel/models/writer.py:2140-2147)
Dequantize compressed-tensors path hard-coded True (gptqmodel/utils/model_dequant.py:335, 354)
Processors base.py:746 used self.require_trust_remote_code; several load_processor() overrides hard-coded True
Kimi K3 load_calibration_reference() execed snapshot files with no opt-in (gptqmodel/utils/kimi_k3.py:24-57)

2. The new flow

flowchart TD
    A["GPTQModel.load / from_quantized"] --> B["resolve_trust_remote_code(model_local_path)"]
    B --> C["AutoConfig.from_pretrained(trust_remote_code=trust_remote_code)"]
    C --> D["normalize_hf_config_compat(config, trust_remote_code)"]
    D --> E{"prepare_remote_model_init_compat:<br/>trust_remote_code?"}
    E -- "False" --> F["return before auto_map import"]
    E -- "True" --> G["get_class_from_dynamic_module(class_ref, model_path)"]
    G --> H["remote model init compat patches"]
    F --> I["native QModel / built-in config"]
    H --> I
    E -. "fail-closed path<br/>auto_map is data only" .-> F
    style E fill:#ffe0b2,stroke:#e65100
    style F fill:#ffcdd2,stroke:#b71c1c
Loading

Save:

flowchart TD
    A["save() / save_quantized()"] --> B{"self.load_quantized_model?"}
    B -- "yes" --> C["get_model_with_quantize(qcfg, model_local_path)"]
    C --> D["AutoConfig.from_pretrained(trust_remote_code=self.trust_remote_code)"]
    D --> E{"trust_remote_code?"}
    E -- "False" --> F["built-in config; auto_map AutoConfig not imported"]
    E -- "True" --> G["auto_map AutoConfig imported"]
    B -- "no" --> H["write saved config and shards"]
    F --> H
    G --> H
    style E fill:#ffe0b2,stroke:#e65100
    style F fill:#ffcdd2,stroke:#b71c1c
Loading

Dequantize:

flowchart TD
    A["dequantize_model(..., trust_remote_code=False)"] --> B["detect_format(model_path, config)"]
    B --> C{"format == compressed-pack?"}
    C -- "no" --> D["shard converters"]
    C -- "yes" --> E["_prepare_compressed_tensors_context(trust_remote_code)"]
    E --> F["_discover_compressed_tensors_module_schemes(trust_remote_code)"]
    F --> G["AutoConfig.from_pretrained(trust_remote_code)"]
    G --> H{"trust_remote_code?"}
    H -- "False" --> I["built-in config; auto_map not imported"]
    H -- "True" --> J["auto_map AutoConfig imported"]
    I --> K["loader.from_config(trust_remote_code=False)"]
    J --> L["loader.from_config(trust_remote_code=True)"]
    style H fill:#ffe0b2,stroke:#e65100
    style I fill:#ffcdd2,stroke:#b71c1c
Loading

3. The fix

  • prepare_remote_model_init_compat(..., *, trust_remote_code=False) returns before get_class_from_dynamic_module() unless trust is on. This also covers transformers' cross-repo repo--module.Class form.
  • normalize_hf_config_compat() returns after the RoPE backfill when trust is off; auto_map alone no longer enables remote-code compat patches.
  • Both loader call sites pass the caller's flag (gptqmodel/models/loader.py:1313, 1803).
  • get_model_with_quantize() uses self.trust_remote_code (gptqmodel/models/writer.py:2140-2147).
  • dequantize_model(..., trust_remote_code=False) threads the flag through _prepare_compressed_tensors_context() / _discover_compressed_tensors_module_schemes() (gptqmodel/utils/model_dequant.py:1766). scripts/dequantize_model.py exposes --trust-remote-code.
  • BaseQModel.__init__() and every load_processor() override use self.trust_remote_code (gptqmodel/models/base.py:746).
  • KimiK3Source / load_calibration_reference() require trust_remote_code=True (gptqmodel/utils/kimi_k3.py:24, 92); KimiK31Source does the same.

4. Before/after evidence

Regression checkpoint: native model_type=llama + auto_map.AutoConfig and auto_map.AutoModelForCausalLM modules that write EXECUTED into the checkpoint directory. trust_remote_code=False throughout.

Unmodified origin/main (93d3ae7, clean worktree /tmp/gptq-base-3968a with only the new test copied in):

$ CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
    /opt/venvs/cu130t/bin/python -m pytest tests/test_trust_remote_code_isolation.py \
    -q -k "does_not_import"
FAILED test_load_does_not_import_auto_map_without_trust
E   AssertionError: load imported checkpoint code with trust_remote_code=False
FAILED test_save_does_not_import_auto_map_config_without_trust
E   AssertionError: save imported checkpoint code with trust_remote_code=False
FAILED test_dequantize_does_not_import_auto_map_config_without_trust
=========== 3 failed, 2 deselected, 4 warnings in 5.42s ===========

This branch (771a45e2):

$ CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
    /opt/venvs/cu130t/bin/python -m pytest tests/test_trust_remote_code_isolation.py -q
======================== 5 passed, 2 warnings in 6.19s =========================

$ HIP_VISIBLE_DEVICES=0 PYTHONPATH=$PWD \
    /opt/venvs/rocm101t/bin/python -m pytest tests/test_trust_remote_code_isolation.py -q
======================== 5 passed, 2 warnings in 6.62s =========================

The five tests cover load, save, dequantize (marker absent with trust off) plus opt-in import for the loader and dequantize paths (marker present with trust_remote_code=True).

5. Related tests

CUDA (cu130t), branch:

$ pytest tests/test_hf_config_compat.py tests/test_locateanything_support.py \
    tests/test_kimi_k3_support.py tests/test_kimi_k3_core_format.py \
    tests/test_kimi_k31.py tests/test_model_dequant_fp8.py -q
6 failed, 122 passed, 14 skipped, 1 error

The same six failures and one teardown error reproduce on unmodified origin/main with the identical selection (6 failed, 121 passed, 14 skipped, 1 error): test_normalize_hf_config_compat_restores_legacy_cache_length_helpers, test_resolve_trust_remote_code_overrides_when_native_support_exists, three test_generic_wrapper_crops_padded_output_and_preserves_input_dtype cases, test_ignored_layers_are_honored_by_non_fp8_converters, and the test_locateanything_prepare_dataset_uses_remote_processor teardown error. They are pre-existing and unrelated to this diff.

ROCm (rocm101t), branch:

$ HIP_VISIBLE_DEVICES=0 pytest tests/test_hf_config_compat.py tests/test_kimi_k3_support.py \
    tests/test_kimi_k3_core_format.py tests/test_kimi_k31.py \
    tests/test_model_dequant_fp8.py tests/test_dequant_launch_config.py -q
5 failed, 129 passed, 13 skipped

Same pre-existing failure names as above. tests/test_kimi_k3_support.py (16 passed), tests/test_kimi_k31.py (10 passed), and tests/test_dequant_launch_config.py (14 passed) are clean.

6. Documentation

The committed reference page is docs/trust-remote-code-isolation.md, linked from the inference section of README.md. It carries the same three diagrams and the fail-closed contract.

7. Hardware and limitation

  • CUDA: GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae (RTX 4090), /opt/venvs/cu130t/bin/python (torch 2.16.0.dev+cu130, Python 3.14t).
  • ROCm: HIP_VISIBLE_DEVICES=0 (R9700), /opt/venvs/rocm101t/bin/python (torch 2.16.0.dev+rocm10.1).
  • Limitation: compressed_tensors is not installed in either venv, so the dequantize regression injects the dependency boundary (_get_compressed_tensors_dependencies) while using the real transformers AutoConfig / model classes. The trust gate, the auto_map import sink, and the from_config call are the shipped ones; only the compressed-tensors plugin objects are fakes. The CLI flag is verified with --help.

trust_remote_code=False was bypassed on load, save and dequantize because
auto_map data, the compressed-tensors format, and the writer's re-save path
all imported Python from the snapshot.

- prepare_remote_model_init_compat() and normalize_hf_config_compat() now
  require the caller's trust_remote_code; auto_map alone no longer enables
  remote-code imports or compat patches. Loader call sites forward the flag.
- get_model_with_quantize() uses self.trust_remote_code instead of True.
- dequantize_model() takes trust_remote_code=False and threads it through
  the compressed-tensors discovery path; the CLI exposes it.
- Processor loads use self.trust_remote_code; Kimi K3 reference loading
  requires trust_remote_code=True.

Adds a checkpoint-based regression test whose auto_map modules write a
marker file; the marker must stay absent after load, save and dequantize.
@Qubitium
Qubitium merged commit e62ab99 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3968 branch November 14, 2026 11:47
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.

[Security] trust_remote_code=False is bypassed: load, save and dequantize import checkpoint code (CWE-94)

1 participant