8000 fix(security): Render all jinja templates in immutable sandbox (#1441) · Smartappli/llama-cpp-python@561e880 · GitHub
[go: up one dir, main page]

Skip to content

Commit 561e880

Browse files
< 8000 img data-component="Avatar" class="pc-AvatarItem prc-AvatarStack-AvatarItem-nS1mt prc-Avatar-Avatar-ZRS-m" alt="CISC" width="20" height="20" style="--avatarSize-regular:20px" src="https://avatars.githubusercontent.com/u/1629204?v=4&size=40" data-testid="commit-stack-avatar" data-hovercard-url="/users/CISC/hovercard"/>abetlen
andauthored
fix(security): Render all jinja templates in immutable sandbox (abetlen#1441)
Chat templates are rendered with ImmutableSandboxedEnvironment in transformers so no need to do otherwise here. Co-authored-by: Andrei <abetlen@gmail.com>
1 parent b454f40 commit 561e880

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,7 @@ def hf_tokenizer_config_to_chat_formatter(
685685
assert isinstance(tokenizer_config["eos_token"], str)
686686
eos_token = tokenizer_config["eos_token"]
687687

688-
env = jinja2.Environment(
689-
loader=jinja2.BaseLoader(),
688+
env = ImmutableSandboxedEnvironment(
690689
trim_blocks=True,
691690
lstrip_blocks=True,
692691
).from_string(chat_template)
@@ -2603,7 +2602,10 @@ def __call__(
26032602
messages = [llama_types.ChatCompletionRequestSystemMessage(role="system", content=self.DEFAULT_SYSTEM_MESSAGE)] + messages
26042603

26052604
image_urls = self.get_image_urls(messages)
2606-
template = jinja2.Template(self.CHAT_FORMAT)
2605+
template = ImmutableSandboxedEnvironment(
2606+
trim_blocks=True,
2607+
lstrip_blocks=True,
2608+
).from_string(self.CHAT_FORMAT)
26072609
text = template.render(
26082610
messages=messages,
26092611
add_generation_prompt=True,
@@ -3242,8 +3244,7 @@ def chatml_function_calling(
32423244
"{% endfor %}"
32433245
"{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
32443246
)
3245-
template_renderer = jinja2.Environment(
3246-
loader=jinja2.BaseLoader(),
3247+
template_renderer = ImmutableSandboxedEnvironment(
32473248
autoescape=jinja2.select_autoescape(["html", "xml"]),
32483249
undefined=jinja2.StrictUndefined,
32493250
).from_string(function_calling_template)

0 commit comments

Comments
 (0)
0