Skip to content

[1.11.0rc1] Autoescape false wrapper breaks macros #5099

@foosel

Description

@foosel

Problem

Discovered here: cp2004/OctoPrint-CameraStreamer-Control#9

Third party plugins not opted into autoescaping are wrapped inside a

{% autoescape false %}
...
{% endautoescape %}

block here:

if PluginFlags.AUTOESCAPE_ON not in plugin._plugin_info.flags and (
PluginFlags.AUTOESCAPE_OFF in plugin._plugin_info.flags
or (
not plugin._plugin_info.bundled
and not plugin.is_template_autoescaped()
)
):
loader = octoprint.util.jinja.PostProcessWrapperLoader(
loader,
lambda source: "{% autoescape false %}"
+ source
+ "{% endautoescape %}",
)

This somehow seems to break macros insides those third party plugins. E.g. this piece of jinja code:

{% import "macros.jinja2" as macros %}
// ...
<label class="control-label" for="{{ macros.id("mode") }}">Preferred Video Mode</label>

leads to an error like this:

2025-01-30 10:25:12,823 - octoprint.util.jinja - ERROR - Caught exception while compiling template tabs/control.jinja2 at line 14
Traceback (most recent call last):
  File "/home/gina/devel/OctoPrint/OctoPrint/src/octoprint/util/jinja.py", line 258, in _handle_body
    return caller()
           ^^^^^^^^
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/runtime.py", line 770, in __call__
    return self._invoke(arguments, autoescape)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/runtime.py", line 784, in _invoke
    rv = self._func(*arguments)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gina/devel/OctoPrint/OctoPrint/src/octoprint/templates/tabs/control.jinja2", line 89, in macro
    </div>
           
  File "/home/gina/devel/OctoPrint/OctoPrint-CameraStreamer-Control/octoprint_camerastreamer_control/templates/camerastreamer_control_webcam.jinja2", line 18, in root
  File "/home/gina/devel/OctoPrint/OctoPrint-CameraStreamer-Control/octoprint_camerastreamer_control/templates/modes/mjpg.jinja2", line 17, in root
    <p>
        
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/runtime.py", line 284, in call
    pass_arg = _PassArg.from_obj(__obj)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/utils.py", line 92, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/runtime.py", line 870, in __getattr__
    return self._fail_with_undefined_error()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gina/.pyenv/versions/3.11.2/envs/octoprint-py311/lib/python3.11/site-packages/jinja2/runtime.py", line 859, in _fail_with_undefined_error
    raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'jinja2.environment.TemplateModule object' has no attribute 'id'

For some reason, the id property is not available on the imported macros template.

Disabling autoescape and removing the wrapper makes things work again.

Solution

TBD, Investigating...

Metadata

Metadata

Assignees

Labels

approvedIssue has been approved by the bot or manually for further processingbugIssue describes a bugdoneDone but not yet released

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions