Skip to content

Commit 56a7246

Browse files
sispdavidism
authored andcommitted
fix f-string syntax error in code generation
1 parent 48b0687 commit 56a7246

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGES.rst

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Unreleased
88
- The sandboxed environment handles indirect calls to ``str.format``, such as
99
by passing a stored reference to a filter that calls its argument.
1010
:ghsa:`q2x7-8rv6-6q7h`
11+
- Escape template name before formatting it into error messages, to avoid
12+
issues with names that contain f-string syntax.
13+
:issue:`1792`, :ghsa:`gmj6-6f8f-6699`
1114
- Sandbox does not allow ``clear`` and ``pop`` on known mutable sequence
1215
types. :issue:`2032`
1316
- Calling sync ``render`` for an async template uses ``asyncio.run``.

src/jinja2/compiler.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,14 @@ def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None:
11411141
)
11421142
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
11431143
self.indent()
1144+
# The position will contain the template name, and will be formatted
1145+
# into a string that will be compiled into an f-string. Curly braces
1146+
# in the name must be replaced with escapes so that they will not be
1147+
# executed as part of the f-string.
1148+
position = self.position(node).replace("{", "{{").replace("}", "}}")
11441149
message = (
11451150
"the template {included_template.__name__!r}"
1146-
f" (imported on {self.position(node)})"
1151+
f" (imported on {position})"
11471152
f" does not export the requested name {name!r}"
11481153
)
11491154
self.writeline(

tests/test_compile.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22
import re
33

4+
import pytest
5+
6+
from jinja2 import UndefinedError
47
from jinja2.environment import Environment
58
from jinja2.loaders import DictLoader
69

@@ -87,3 +90,19 @@ def test_block_set_vars_unpacking_deterministic(tmp_path):
8790
content,
8891
)[:10]
8992
assert found == expect
93+
94+
95+
def test_undefined_import_curly_name():
96+
env = Environment(
97+
loader=DictLoader(
98+
{
99+
"{bad}": "{% from 'macro' import m %}{{ m() }}",
100+
"macro": "",
101+
}
102+
)
103+
)
104+
105+
# Must not raise `NameError: 'bad' is not defined`, as that would indicate
106+
# that `{bad}` is being interpreted as an f-string. It must be escaped.
107+
with pytest.raises(UndefinedError):
108+
env.get_template("{bad}").render()

0 commit comments

Comments
 (0)