Skip to content

Commit 2b809da

Browse files
committed
fix: [Application] The application node uses {{}} to reference variables, resulting in a parsing failure.
1 parent 8285ee1 commit 2b809da

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

apps/common/init/init_template.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎虎
5+
@file: init_jinja.py
6+
@date:2025/12/1 17:16
7+
@desc:
8+
"""
9+
from typing import Any
10+
11+
from jinja2.sandbox import SandboxedEnvironment
12+
from langchain_core.prompts.string import DEFAULT_FORMATTER_MAPPING, _HAS_JINJA2
13+
14+
15+
def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
16+
"""Format a template using jinja2.
17+
18+
*Security warning*:
19+
As of LangChain 0.0.329, this method uses Jinja2's
20+
SandboxedEnvironment by default. However, this sand-boxing should
21+
be treated as a best-effort approach rather than a guarantee of security.
22+
Do not accept jinja2 templates from untrusted sources as they may lead
23+
to arbitrary Python code execution.
24+
25+
https://jinja.palletsprojects.com/en/3.1.x/sandbox/
26+
27+
Args:
28+
template: The template string.
29+
**kwargs: The variables to format the template with.
30+
31+
Returns:
32+
The formatted string.
33+
34+
Raises:
35+
ImportError: If jinja2 is not installed.
36+
"""
37+
if not _HAS_JINJA2:
38+
msg = (
39+
"jinja2 not installed, which is needed to use the jinja2_formatter. "
40+
"Please install it with `pip install jinja2`."
41+
"Please be cautious when using jinja2 templates. "
42+
"Do not expand jinja2 templates using unverified or user-controlled "
43+
"inputs as that can result in arbitrary Python code execution."
44+
)
45+
raise ImportError(msg)
46+
47+
# Use a restricted sandbox that blocks ALL attribute/method access
48+
# Only simple variable lookups like {{variable}} are allowed
49+
# Attribute access like {{variable.attr}} or {{variable.method()}} is blocked
50+
return SandboxedEnvironment().from_string(template).render(**kwargs)
51+
52+
53+
def run():
54+
DEFAULT_FORMATTER_MAPPING['jinja2'] = jinja2_formatter

apps/ops/celery/signal_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
import logging
44
import os
5-
5+
from common.init import init_template
66
from celery import subtask
77
from celery.signals import (
88
worker_ready, worker_shutdown, after_setup_logger, task_revoked, task_prerun
@@ -31,6 +31,7 @@ def on_app_ready(sender=None, headers=None, **kwargs):
3131
logger.debug("Periodic task [{}] is disabled!".format(task))
3232
continue
3333
subtask(task).delay()
34+
init_template.run()
3435

3536

3637
def delete_files(directory):

apps/smartdoc/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
from application.urls import urlpatterns as application_urlpatterns
2525
from common.cache_data.static_resource_cache import get_index_html
26-
from common.constants.cache_code_constants import CacheCodeConstants
26+
from common.init import init_template
2727
from common.init.init_doc import init_doc
2828
from common.response.result import Result
29-
from common.util.cache_util import get_cache
3029
from smartdoc import settings
3130
from smartdoc.conf import PROJECT_DIR
3231

@@ -72,3 +71,4 @@ def page_not_found(request, exception):
7271

7372
handler404 = page_not_found
7473
init_doc(urlpatterns, application_urlpatterns)
74+
init_template.run()

0 commit comments

Comments
 (0)