From 839dc3e07f1b6c2408deadb95009bc37a9b031fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 4 Jun 2020 10:31:15 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20templates=20after=20jinja?= =?UTF-8?q?=20upgrade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current Jinja versions no longer allow modifying variables set outside of for loops inside them. So something like: {% set mark_active = True %} {% for item in items %} # do something {% if mark_active %} # do something more {% set mark_active = False %} {% endif %} {% endfor %} no longer works - mark_active will be reset on every iteration of the loop to its initial value False. The fix is to use namespaces to hold the modifiable values instead: {% set ns = namespace(mark_active = True) %} {% for item in items %} # do something {% if ns.mark_active %} # do something more {% set ns.mark_active = False %} {% endif %} {% endfor %} See also #1697 and https://jinja.palletsprojects.com/en/2.11.x/templates/#assignments The Jinja dependency upgrade thus introduces a breaking change for plugins as well. However, it was necessary for security reasons: older versions have a bug that could allow template authors to break out of the template sandbox. Not a huge risk in the context of OctoPrint (plugins already have a lot more power than jinja would give them), but still overdue to fix. --- src/octoprint/templates/dialogs/about.jinja2 | 8 ++++---- src/octoprint/templates/dialogs/settings.jinja2 | 8 ++++---- src/octoprint/templates/dialogs/usersettings.jinja2 | 8 ++++---- src/octoprint/templates/dialogs/wizard.jinja2 | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/octoprint/templates/dialogs/about.jinja2 b/src/octoprint/templates/dialogs/about.jinja2 index dc94bb2cb..af4d590d1 100644 --- a/src/octoprint/templates/dialogs/about.jinja2 +++ b/src/octoprint/templates/dialogs/about.jinja2 @@ -8,7 +8,7 @@
- {% set mark_active = True %} + {% set ns = namespace(mark_active = True) %} {% for key in templates.about.order %} {% set entry, data = templates.about.entries[key] %} {% if data is not none %} {% if "custom_bindings" not in data or data["custom_bindings"] %}{% endif %}
{% try "There was an error with the template {filename} at line number {lineno}: {exception}" %}{% include data.template ignore missing %}{% endtry %} diff --git a/src/octoprint/templates/dialogs/settings.jinja2 b/src/octoprint/templates/dialogs/settings.jinja2 index 2db70d4d9..1063b6809 100644 --- a/src/octoprint/templates/dialogs/settings.jinja2 +++ b/src/octoprint/templates/dialogs/settings.jinja2 @@ -8,7 +8,7 @@
- {% set mark_active = True %} + {% set ns = namespace(mark_active = True) %} {% for key in templates.settings.order %} {% set entry, data = templates.settings.entries[key] %} {% if data is not none %} {% if "custom_bindings" not in data or data["custom_bindings"] %}{% endif %}
{% try "There was an error with the template {filename} at line number {lineno}: {exception}" %}{% include data.template ignore missing %}{% endtry %} diff --git a/src/octoprint/templates/dialogs/usersettings.jinja2 b/src/octoprint/templates/dialogs/usersettings.jinja2 index d34da7b46..b6eeaef6a 100644 --- a/src/octoprint/templates/dialogs/usersettings.jinja2 +++ b/src/octoprint/templates/dialogs/usersettings.jinja2 @@ -5,7 +5,7 @@