-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: [Application] The application node uses {{}} to reference variables, resulting in a parsing failure. #4858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: init_jinja.py | ||
| @date:2025/12/1 17:16 | ||
| @desc: | ||
| """ | ||
| from typing import Any | ||
|
|
||
| from jinja2.sandbox import SandboxedEnvironment | ||
| from langchain_core.prompts.string import DEFAULT_FORMATTER_MAPPING, _HAS_JINJA2 | ||
|
|
||
|
|
||
| def jinja2_formatter(template: str, /, **kwargs: Any) -> str: | ||
| """Format a template using jinja2. | ||
|
|
||
| *Security warning*: | ||
| As of LangChain 0.0.329, this method uses Jinja2's | ||
| SandboxedEnvironment by default. However, this sand-boxing should | ||
| be treated as a best-effort approach rather than a guarantee of security. | ||
| Do not accept jinja2 templates from untrusted sources as they may lead | ||
| to arbitrary Python code execution. | ||
|
|
||
| https://jinja.palletsprojects.com/en/3.1.x/sandbox/ | ||
|
|
||
| Args: | ||
| template: The template string. | ||
| **kwargs: The variables to format the template with. | ||
|
|
||
| Returns: | ||
| The formatted string. | ||
|
|
||
| Raises: | ||
| ImportError: If jinja2 is not installed. | ||
| """ | ||
| if not _HAS_JINJA2: | ||
| msg = ( | ||
| "jinja2 not installed, which is needed to use the jinja2_formatter. " | ||
| "Please install it with `pip install jinja2`." | ||
| "Please be cautious when using jinja2 templates. " | ||
| "Do not expand jinja2 templates using unverified or user-controlled " | ||
| "inputs as that can result in arbitrary Python code execution." | ||
| ) | ||
| raise ImportError(msg) | ||
|
|
||
| # Use a restricted sandbox that blocks ALL attribute/method access | ||
| # Only simple variable lookups like {{variable}} are allowed | ||
| # Attribute access like {{variable.attr}} or {{variable.method()}} is blocked | ||
| return SandboxedEnvironment().from_string(template).render(**kwargs) | ||
|
|
||
|
|
||
| def run(): | ||
| DEFAULT_FORMATTER_MAPPING['jinja2'] = jinja2_formatter | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| # | ||
| import logging | ||
| import os | ||
|
|
||
| from common.init import init_template | ||
| from celery import subtask | ||
| from celery.signals import ( | ||
| worker_ready, worker_shutdown, after_setup_logger, task_revoked, task_prerun | ||
|
|
@@ -31,6 +31,7 @@ def on_app_ready(sender=None, headers=None, **kwargs): | |
| logger.debug("Periodic task [{}] is disabled!".format(task)) | ||
| continue | ||
| subtask(task).delay() | ||
| init_template.run() | ||
|
|
||
|
|
||
| def delete_files(directory): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code looks generally well-written, but here are some suggested improvements for clarity, efficiency, and readability:
Here's an updated version incorporating these suggestions: #
import os
from .common import init_template
from celery import subtask
# Function documentation explaining its purpose
def on_app_ready():
"""
Handles tasks based on environment variables and schedules periodic tasks.
This function goes through all scheduled tasks defined in environment variables,
enables them using celery, and optionally initializes template-related components.
"""
tasks = os.environ.get('SCHEDULED_TASKS', '').split(',')
# Iterate over each enabled task
for task in tasks:
if task in ['TASK_A', 'DISABLED_TASK']:
logger.debug(f"Periodic task [{task}] is disabled!")
continue
# Schedule the task using Celery
subtask(task).delay()
# Optionally run initialization script
try:
init_template.run()
except ImportError:
logger.warning("Could not initialize template-related components.")
# Example usage of delete_files function which could also benefit from comments or improved design choices
def delete_files(directory):
passThese changes enhance both readability and maintainability while ensuring functionality. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,9 @@ | |
|
|
||
| from application.urls import urlpatterns as application_urlpatterns | ||
| from common.cache_data.static_resource_cache import get_index_html | ||
| from common.constants.cache_code_constants import CacheCodeConstants | ||
| from common.init import init_template | ||
| from common.init.init_doc import init_doc | ||
| from common.response.result import Result | ||
| from common.util.cache_util import get_cache | ||
| from smartdoc import settings | ||
| from smartdoc.conf import PROJECT_DIR | ||
|
|
||
|
|
@@ -72,3 +71,4 @@ def page_not_found(request, exception): | |
|
|
||
| handler404 = page_not_found | ||
| init_doc(urlpatterns, application_urlpatterns) | ||
| init_template.run() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided Python code looks mostly clean and well-structured for setting up a basic Django project. However, there are a few minor improvements that can be made:
Here’s the updated version with these considerations: # Removed duplication
from application.urls import urlpatterns as application_urlpatterns
from common.cache_data.static_resource_cache import get_index_html, get_cache
from common.constants.cache_code_constants import CacheCodeConstants
from common.doc.init import init_doc
from common.response.result import Result
from common.util.cache_util import get_cache
# Used strict paths for better clarity (absolute)
from smartdoc import settings, conf
PROJECT_DIR = getattr(conf, 'PROJECT_DIR') # Assuming conf has a PROJECT_DIR attribute
def page_not_found(request, exception):
# Consider adding try-except block for error handling
return "Error 404"
handler404 = page_not_found
init_doc(urlpatterns, application_urlpatterns)
init_template.run()Additional Suggestions
These adjustments should make the code slightly more readable, maintainable, and robust. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided Python script seems to define a custom Jinja2 formatter for formatting strings using the
SandboxedEnvironment. This environment has been configured to block all attribute/method access except for simple variable lookups, which is generally good security practice.Key Points:
Import Statements: Ensures that necessary libraries are imported at the beginning (
import jinja2,from langchain_core.prompts.string import DEFAULT_FORMATTER_MAPPING).Function Definition:
jinja2_formattertakes a template string and optional keyword arguments (**kwargs).SandboxedEnvironmentis used with restrictions on attribute accesses beyond simple variable lookup, preventing code execution through templates.Main Function:
jinja2_formatterto theDEFAULT_FORMATTER_MAPPING, which would typically allow other parts of your program to use this formatter seamlessly.Overall, the code appears functional and secure given its sandboxing mechanisms. However, it might be worth noting: