FIX_COMMIT=eaaaf8494dd5386634ae37d1d122212fdc315be5 VULNERABLE_PARENT=47949d3a0e1cf9248f2a3eb3cd0deb12ee37b9e9 VULN_HAS_GUARD=0 FIXED_HAS_GUARD=1 diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index 1d8074a3a6..a707da0432 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -1146,6 +1146,9 @@ class ProcessorMixin(PushToHubMixin): else: os.makedirs(chat_template_dir, exist_ok=True) template_filepath = os.path.join(chat_template_dir, f"{template_name}.jinja") + # template_name is an untrusted dict key; reject path traversal (CWE-22) + if Path(template_filepath).resolve().parent != Path(chat_template_dir).resolve(): + raise ValueError(f"Invalid chat template name: {template_name!r}") with open(template_filepath, "w", encoding="utf-8") as f: f.write(template) logger.info(f"chat template saved in {template_filepath}") diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index 853ead472b..76a36a2e7b 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -3314,6 +3314,9 @@ class PreTrainedTokenizerBase(PushToHubMixin): else: Path(chat_template_dir).mkdir(exist_ok=True) template_filepath = os.path.join(chat_template_dir, f"{template_name}.jinja") + # template_name is an untrusted dict key; reject path traversal (CWE-22) + if Path(template_filepath).resolve().parent != Path(chat_template_dir).resolve(): + raise ValueError(f"Invalid chat template name: {template_name!r}") with open(template_filepath, "w", encoding="utf-8") as f: f.write(template) logger.info(f"chat template saved in {template_filepath}")