fix(core): add more sanitization to templates#36612
fix(core): add more sanitization to templates#36612Eugene Yurtsev (eyurtsev) merged 1 commit intomasterfrom
Conversation
Merging this PR will not alter performance
|
There was a problem hiding this comment.
Pull request overview
This PR tightens template sanitization for prompt templates, focusing on preventing f-string template injection vectors (attribute access/indexing and nested replacement fields in format specs) and ensuring prompt template types validate their embedded string templates during construction/deserialization.
Changes:
- Added
validate_f_string_template()to centralize f-string template validation and integrated it intoget_template_variables()andcheck_valid_template(). - Added validation for
ImagePromptTemplateandDictPromptTemplateto reject unsafe variable expressions in template string values (including via deserialization). - Expanded unit test coverage for rejecting nested replacement fields in format specs and attribute access patterns across prompt template types.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| libs/core/langchain_core/prompts/string.py | Centralizes and strengthens f-string template parsing/validation, including nested replacement field blocking. |
| libs/core/langchain_core/prompts/image.py | Adds validation pass over string template values to reject unsafe variable expressions. |
| libs/core/langchain_core/prompts/dict.py | Adds a Pydantic model validator to ensure dict templates contain only safe variables. |
| libs/core/tests/unit_tests/prompts/test_string.py | Adds tests covering nested replacement fields and safe format specifiers. |
| libs/core/tests/unit_tests/prompts/test_image.py | Adds tests ensuring ImagePromptTemplate rejects attribute access (including via deserialization). |
| libs/core/tests/unit_tests/prompts/test_dict.py | Adds tests ensuring DictPromptTemplate and PromptTemplate reject attribute access (including via deserialization/load paths). |
| libs/core/tests/unit_tests/prompts/test_chat.py | Adds test ensuring nested replacement fields are rejected inside image URL templates in chat prompts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for value in template.values(): | ||
| if isinstance(value, str): | ||
| get_template_variables(value, template_format) |
There was a problem hiding this comment.
ImagePromptTemplate.__init__ now calls template.values() before Pydantic validation. If template is not a dict (e.g., malformed payload during deserialization), this will raise an AttributeError rather than a clean validation error. Consider guarding with isinstance(template, dict) (or moving this check into a @model_validator(mode="after") so it runs after field validation) to keep error handling consistent and avoid unexpected exceptions.
| for value in template.values(): | |
| if isinstance(value, str): | |
| get_template_variables(value, template_format) | |
| if isinstance(template, dict): | |
| for value in template.values(): | |
| if isinstance(value, str): | |
| get_template_variables(value, template_format) |
add more sanitization to templates