fix(template-transform): use base64 encoding for Jinja2 templates to fix #26818#30223
Conversation
When Jinja2 templates containing special characters (quotes, newlines) were embedded directly in the generated Python script using triple-quoted strings, the script would break. This fix encodes the template as base64 before embedding, then decodes it at runtime. Root cause: Templates were inserted via string replacement into a Python script like `jinja2.Template('''...''')`. If the template contained `'''`, the generated script had a syntax error. Solution: Add serialize_code() method to base64-encode templates, then override assemble_runner_script() in Jinja2TemplateTransformer to use the encoded template with runtime decoding. This specifically affects Jinja2 templates - Python3 and JavaScript transformers insert user code at the top level (not inside strings), so they don't have this vulnerability. Fixes langgenius#26818 Summary of ChangesHello @devbyteai, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug where Jinja2 templates failed to render correctly when they contained special characters, particularly within HTML textarea elements. The core problem stemmed from templates being directly embedded as string literals in the generated Python script, leading to syntax errors. The solution involves implementing base64 encoding for the template content, ensuring that any characters can be safely embedded and decoded at runtime. This change significantly improves the robustness of Jinja2 template processing, preventing unexpected failures and enhancing the reliability of template-based functionalities. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively resolves the issue with special characters in Jinja2 templates by implementing base64 encoding. The approach is sound, adding a serialize_code method and correctly overriding assemble_runner_script for the Jinja2TemplateTransformer. The changes are well-contained and do not impact other template transformers. I appreciate the thoroughness of the new regression tests, which cover various edge cases and ensure the bug will not reappear. The code quality is high, and I have just one minor suggestion to improve the generated runner script.
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #26818 where textarea elements in Jinja2 templates weren't displaying pre-filled values correctly. The root cause was that template code containing special characters (quotes, newlines) was directly embedded into generated Python scripts using triple-quoted strings, causing syntax errors. The fix applies base64 encoding to templates before embedding them in scripts, matching the approach already used for input data.
Key Changes:
- Added
serialize_code()method to theTemplateTransformerbase class for base64 encoding template code - Overrode
assemble_runner_script()inJinja2TemplateTransformerto encode templates as base64 before embedding - Updated existing tests and added comprehensive regression tests covering special characters and HTML textarea scenarios
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
api/core/helper/code_executor/template_transformer.py | Added serialize_code() utility method for base64 encoding template code |
api/core/helper/code_executor/jinja2/jinja2_transformer.py | Implemented base64 encoding for Jinja2 templates via overridden assemble_runner_script() method and updated runner script template |
api/tests/integration_tests/workflow/nodes/code_executor/test_code_jinja2.py | Updated existing tests to use new base64 placeholder and added three new regression tests |
api/tests/test_containers_integration_tests/workflow/nodes/code_executor/test_code_jinja2.py | Updated existing tests to use new base64 placeholder and added one regression test |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
Fixes #26818 - Textarea pre-fill values in Template node were not displaying correctly when using Jinja2 variables.
Problem
When creating HTML forms in Template node with Jinja2 variables:
<input value="{{ task.get('Task ID', '') }}"/>- WORKED<textarea>{{ task.get('Issues', 'No issues') }}</textarea>- DIDN'T WORKRoot Cause Analysis
The Jinja2 template was embedded directly into the generated Python script using triple-quoted strings:
If the template contained
''', single quotes, double quotes, or multiline content (common in textareas), the generated Python script would have a syntax error.Interestingly, the inputs were already safely handled via base64 encoding:
But the template code was inserted via direct string replacement without any encoding protection.
Solution
Apply the same base64 encoding pattern to templates that is already used for inputs:
Added
serialize_code()method toTemplateTransformerbase class for base64 encoding template codeOverride
assemble_runner_script()inJinja2TemplateTransformerto encode template as base64 before embedding in script and decode at runtimeBefore (breaks with special chars):
After (works with any content):
Scope & Impact Assessment
Jinja2 templates only: I verified that Python3 and JavaScript transformers are NOT affected. They insert user code at the top level of the script (not inside a string literal), so special characters in user code are handled correctly by the respective language parsers.
Backward compatible: No changes to external APIs or workflow behavior. The fix is purely internal to how templates are embedded in generated runner scripts.
Testing
I added comprehensive regression tests to ensure this issue doesn't recur:
test_jinja2_template_with_special_charactersTests a template containing all the problematic characters: triple quotes
''', single quotes, double quotes, and multiline content. This template would have broken the old implementation. The test verifies that HTML output is correctly rendered with all special characters intact.test_jinja2_template_with_html_textarea_prefillDirect reproduction of issue #26818 - an HTML textarea element with Jinja2 variable content containing newlines and quotes. Verifies the exact scenario from the bug report works correctly.
test_jinja2_assemble_runner_script_encodes_templateUnit test verifying that
assemble_runner_script()correctly base64-encodes the template. Asserts that the encoded template appears in the script and the raw template does NOT appear (proving encoding is working).Updated existing tests
test_jinja2andtest_jinja2_get_runner_scriptwere updated to use the new_template_b64_placeholderpattern with proper base64 encoding.Files Changed
api/core/helper/code_executor/template_transformer.py- Addedserialize_code()method for base64 encodingapi/core/helper/code_executor/jinja2/jinja2_transformer.py- Overrideassemble_runner_script()to use base64 encodingapi/tests/integration_tests/.../test_code_jinja2.py- Updated tests + added regression testsapi/tests/test_containers_integration_tests/.../test_code_jinja2.py- Updated tests + added regression tests