[PR #25753] fix: remote code execution in email endpoints #31189

Closed
opened 2026-02-21 20:48:59 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/25753

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #25752 by providing three templating options

  • unsafe: default jinja behavior. Most flexible, but vulnerable to rce
  • [default] sandbox: uses https://jinja.palletsprojects.com/en/stable/sandbox/ to intercept unsafe operation. basic ops that are considered "safe", such as arithmetic operations, can still be executed
  • disabled: no templating at all

Screenshots

Under sandbox mode, when sending mail with body

{% for cls in ''.__class__.__mro__[1].__subclasses__() %}{% if cls.__name__=="Popen" %}{{ cls(["ls"], shell=True, stdout=-1).communicate() }}{% endif %}{% endfor %}

An exception is thrown

Traceback (most recent call last):
  File "/app/api/tasks/mail_enterprise_task.py", line 36, in send_enterprise_email_task
    html_content = _render_template_with_strategy(body, substitutions)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/tasks/mail_enterprise_task.py", line 22, in _render_template_with_strategy
    return tmpl.render(substitutions)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
  File "/app/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/app/api/.venv/lib/python3.12/site-packages/jinja2/sandbox.py", line 322, in getattr
    return obj[attribute]
           ~~~^^^^^^^^^^^
jinja2.exceptions.SecurityError: access to attribute '__class__' of 'str' object is unsafe.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/25753 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary Fixes #25752 by providing three templating options - **unsafe**: default jinja behavior. Most flexible, but vulnerable to rce - **[default] sandbox**: uses https://jinja.palletsprojects.com/en/stable/sandbox/ to intercept unsafe operation. basic ops that are considered "safe", such as arithmetic operations, can still be executed - **disabled**: no templating at all ## Screenshots Under sandbox mode, when sending mail with body ``` {% for cls in ''.__class__.__mro__[1].__subclasses__() %}{% if cls.__name__=="Popen" %}{{ cls(["ls"], shell=True, stdout=-1).communicate() }}{% endif %}{% endfor %} ``` An exception is thrown ``` Traceback (most recent call last): File "/app/api/tasks/mail_enterprise_task.py", line 36, in send_enterprise_email_task html_content = _render_template_with_strategy(body, substitutions) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/tasks/mail_enterprise_task.py", line 22, in _render_template_with_strategy return tmpl.render(substitutions) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1295, in render self.environment.handle_exception() File "/app/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception raise rewrite_traceback_stack(source=source) File "<template>", line 1, in top-level template code File "/app/api/.venv/lib/python3.12/site-packages/jinja2/sandbox.py", line 322, in getattr return obj[attribute] ~~~^^^^^^^^^^^ jinja2.exceptions.SecurityError: access to attribute '__class__' of 'str' object is unsafe. ``` ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:48:59 -05:00
yindo closed this issue 2026-02-21 20:49:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31189