Merge branch '3.1.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents bbd5bce, 6e7b0fa · Browse files · 3 files·+31 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−12 removed
- tests:+6 added−5 removed
- other:+25 added−7 removed
Provenance
- Commit
11550f9- Tree
402d977- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
bbd5bce- Parent 2
6e7b0fa
3 files changed+31 added−12 removed
modified:src/jinja2/filters.py+17 added-5 removed
248 unmodified lines249 250 yield from value.items()251 252 253-_space_re = re.compile(r"\s", flags=re.ASCII)253+# Check for characters that would move the parser state from key to value.254+# https://html.spec.whatwg.org/#attribute-name-state255+_attr_key_re = re.compile(r"[\s/>=]", flags=re.ASCII)256 257 258 @pass_eval_context259 def do_xmlattr(260 eval_ctx: "EvalContext", d: t.Mapping[str, t.Any], autospace: bool = True261 ) -> str:262 """Create an SGML/XML attribute string based on the items in a dict.263 262- If any key contains a space, this fails with a ``ValueError``. Values that263- are neither ``none`` nor ``undefined`` are automatically escaped.264+ **Values** that are neither ``none`` nor ``undefined`` are automatically265+ escaped, safely allowing untrusted user input.266 267+ User input should not be used as **keys** to this filter. If any key268+ contains a space, ``/`` solidus, ``>`` greater-than sign, or ``=`` equals269+ sign, this fails with a ``ValueError``. Regardless of this, user input270+ should never be used as keys to this filter, or must be separately validated271+ first.272+273 .. sourcecode:: html+jinja274 275 <ul{{ {'class': 'my_list', 'missing': none,276 'id': 'list-%d'|format(variable)}|xmlattr }}>10 unmodified lines287 288 As you can see it automatically prepends a space in front of the item289 if the filter returned something unless the second parameter is false.290 291+ .. versionchanged:: 3.1.4292+ Keys with ``/`` solidus, ``>`` greater-than sign, or ``=`` equals sign293+ are not allowed.294+295 .. versionchanged:: 3.1.3296 Keys with spaces are not allowed.297 """298 items = []1 unmodified line300 for key, value in d.items():301 if value is None or isinstance(value, Undefined):302 continue303 292- if _space_re.search(key) is not None:293- raise ValueError(f"Spaces are not allowed in attributes: '{key}'")304+ if _attr_key_re.search(key) is not None:305+ raise ValueError(f"Invalid character in attribute name: {key!r}")306 307 items.append(f'{escape(key)}="{escape(value)}"')308 309 rv = " ".join(items)1557 unmodified linesmodified:tests/test_filters.py+6 added-5 removed
472 unmodified lines473 assert 'foo="42"' in out474 assert 'bar="23"' in out475 assert 'blub:blub="<?>"' in out476 477- def test_xmlattr_key_with_spaces(self, env):478- with pytest.raises(ValueError, match="Spaces are not allowed"):479- env.from_string(480- "{{ {'src=1 onerror=alert(1)': 'my_class'}|xmlattr }}"481- ).render()477+ @pytest.mark.parametrize("sep", ("\t", "\n", "\f", " ", "/", ">", "="))478+ def test_xmlattr_key_invalid(self, env: Environment, sep: str) -> None:479+ with pytest.raises(ValueError, match="Invalid character"):480+ env.from_string("{{ {key: 'my_class'}|xmlattr }}").render(481+ key=f"class{sep}onclick=alert(1)"482+ )483 484 def test_sort1(self, env):485 tmpl = env.from_string("{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}")486 assert tmpl.render() == "[1, 2, 3]|[3, 2, 1]"396 unmodified linesmodified:CHANGES.rst+8 added-2 removed
12 unmodified lines13 14 Version 3.1.415 -------------16 17-Unreleased17+Released 2024-05-0518 19+- The ``xmlattr`` filter does not allow keys with ``/`` solidus, ``>``20+ greater-than sign, or ``=`` equals sign, in addition to disallowing spaces.21+ Regardless of any validation done by Jinja, user input should never be used22+ as keys to this filter, or must be separately validated first.23+ :ghsa:`h75v-3vvj-5mfj`24 25+26 Version 3.1.327 -------------28 29 Released 2024-01-1030 31 - Fix compiler error when checking if required blocks in parent templates are32 empty. :pr:`1858`27-- ``xmlattr`` filter does not allow keys with spaces. GHSA-h5c8-rqwp-cp9533+- ``xmlattr`` filter does not allow keys with spaces. :ghsa:`h5c8-rqwp-cp95`34 - Make error messages stemming from invalid nesting of ``{% trans %}`` blocks35 more helpful. :pr:`1918`36 37 973 unmodified lines