Skip to content

Commit 88bcf03

Browse files
authored
Merge pull request #241 from microsoft/copilot/hot-bug-fix for security concern.
fix: address supply-chain RCE via trust_remote_code and torch.load
2 parents e0e9d99 + 3e96997 commit 88bcf03

5 files changed

Lines changed: 32 additions & 5 deletions

File tree

DOCUMENT.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ llm_lingua = PromptCompressor(
278278

279279
- **model_name** (str): Name of the small language model from Huggingface, use "microsoft/llmlingua-2-xlm-roberta-large-meetingbank" or "microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank" for LLMLingua-2. Defaults to "NousResearch/Llama-2-7b-hf".
280280
- **device_map** (str): The computing environment. Options include 'cuda', 'cpu', 'mps', 'balanced', 'balanced_low_0', 'auto'. Default is 'cuda'.
281-
- **model_config** (dict, optional): Configuration for the Huggingface model. Defaults to {}.
281+
- **model_config** (dict, optional): Configuration for the Huggingface model. Defaults to {}. Supports `trust_remote_code` (defaults to `False` for security; see [Security Considerations](#security-considerations)).
282282
- **open_api_config** (dict, optional): Configuration for OpenAI Embedding in coarse-level prompt compression. Defaults to {}.
283283
- **use_llmlingua2** (bool, optional): Whether to use llmlingua-2 for prompt compression. Defaults is False.
284284

@@ -417,3 +417,28 @@ recovered_response = llm_lingua.recover(
417417
#### Response
418418

419419
- **recovered_response** (str): The recovered response, integrating the original prompt's context.
420+
421+
## Security Considerations
422+
423+
### `trust_remote_code`
424+
425+
By default, LLMLingua sets `trust_remote_code=False` when loading models from the Hugging Face Hub. This prevents the automatic execution of arbitrary Python code shipped within a model repository, which could be exploited in a supply-chain attack.
426+
427+
If you are using a model that requires custom code (e.g., certain Jina embedding models), you can explicitly opt in by passing `trust_remote_code=True` in `model_config`:
428+
429+
```python
430+
llm_lingua = PromptCompressor(
431+
model_name="your-model-name",
432+
model_config={"trust_remote_code": True},
433+
)
434+
```
435+
436+
> **⚠️ Warning:** Only enable `trust_remote_code` for models you trust. A compromised or malicious model repository could execute arbitrary code on your machine when this option is enabled.
437+
438+
### `torch.load` and `weights_only`
439+
440+
The experiment scripts under `experiments/llmlingua2/` use `torch.load` with `weights_only=True` to prevent arbitrary code execution via Python pickle deserialization. If you are loading your own `.pt` files in custom training or data pipelines, ensure you also use `weights_only=True` unless you fully trust the source of the file:
441+
442+
```python
443+
data = torch.load(path, weights_only=True)
444+
```

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ llm_lingua = PromptCompressor("microsoft/phi-2")
182182
llm_lingua = PromptCompressor("TheBloke/Llama-2-7b-Chat-GPTQ", model_config={"revision": "main"})
183183
```
184184

185+
> **🔒 Security Note:** `trust_remote_code` now defaults to `False`. If your model requires custom remote code, opt in explicitly via `model_config={"trust_remote_code": True}`. See [Security Considerations](./DOCUMENT.md#security-considerations) for details.
186+
185187
To try **LongLLMLingua** in your scenarios, you can use
186188

187189
```python

experiments/llmlingua2/data_collection/filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
args = parser.parse_args()
2222

23-
res_pt = torch.load(args.load_path)
23+
res_pt = torch.load(args.load_path, weights_only=True)
2424

2525
## filtering
2626
variation_rate_list = res_pt["variation_rate"]

experiments/llmlingua2/model_training/train_roberta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test(model, eval_dataloader):
165165

166166

167167
device = "cuda" if cuda.is_available() else "cpu"
168-
data = torch.load(args.data_path)
168+
data = torch.load(args.data_path, weights_only=True)
169169

170170
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
171171
model = AutoModelForTokenClassification.from_pretrained(

llmlingua/prompt_compressor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def init_llmlingua2(
118118
def load_model(
119119
self, model_name: str, device_map: str = "cuda", model_config: dict = {}
120120
):
121-
trust_remote_code = model_config.get("trust_remote_code", True)
121+
trust_remote_code = model_config.get("trust_remote_code", False)
122122
if "trust_remote_code" not in model_config:
123123
model_config["trust_remote_code"] = trust_remote_code
124124
config = AutoConfig.from_pretrained(model_name, **model_config)
@@ -1987,7 +1987,7 @@ def cos_sim(a, b):
19871987
if self.retrieval_model is None or self.retrieval_model_name != rank_method:
19881988
model = (
19891989
AutoModel.from_pretrained(
1990-
"jinaai/jina-embeddings-v2-base-en", trust_remote_code=True
1990+
"jinaai/jina-embeddings-v2-base-en", trust_remote_code=False
19911991
)
19921992
.eval()
19931993
.to(self.device)

0 commit comments

Comments
 (0)