Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(code-executor): preserve `{{inputs}}` literals in user code (substitute placeholder before code insertion) (#43438)

Quan Kaiyieb0b4fd94c3d
diff --git a/api/core/helper/code_executor/template_transformer.py b/api/core/helper/code_executor/template_transformer.py
index 501f460ba95..ab21af03950 100644
--- a/api/core/helper/code_executor/template_transformer.py
+++ b/api/core/helper/code_executor/template_transformer.py
@@ -109,9 +109,13 @@ class TemplateTransformer(ABC):
def assemble_runner_script(cls, code: str, inputs: Mapping[str, Any]) -> str:
# assemble runner script
script = cls.get_runner_script()
- script = script.replace(cls._code_placeholder, code)
+ # Substitute the inputs placeholder before inserting the user code:
+ # a post-insertion substitution would also rewrite `{{inputs}}` literals
+ # inside the user's source (see #43435). The code placeholder is a single
+ # top-level slot, so substituting inputs first is always safe.
inputs_str = cls.serialize_inputs(inputs)
script = script.replace(cls._inputs_placeholder, inputs_str)
+ script = script.replace(cls._code_placeholder, code)
return script
@classmethod
diff --git a/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py b/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
index 1166cb8892b..99bd369c142 100644
--- a/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
+++ b/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
@@ -10,3 +10,16 @@ def test_get_runner_script():
code_lines = code.splitlines()
# Check that the first lines of script are exactly the same as code
assert script_lines[: len(code_lines)] == code_lines
+
+
+def test_inputs_placeholder_inside_user_code_is_preserved():
+ # User code contains a string literal with `{{inputs}}` — it must survive
+ # the runner assembly untouched (regression for #43435: the inputs
+ # substitution used to run after code insertion and corrupted the literal).
+ code = "def main():\n return {'literal': '{{inputs}}', 'inputs': {}}"
+ inputs = {"arg1": "hello"}
+ script = Python3TemplateTransformer.assemble_runner_script(code, inputs)
+ assert "return {'literal': '{{inputs}}', 'inputs': {}}" in script
+ # The real placeholder in the runner preamble is still substituted once.
+ assert script.count("b64decode('") == 1
+ assert "b64decode('" in script