Merge branch 'stable'
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 6aeab5d, 01d0bab · Browse files · 5 files·+42 added−236 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+42 added−236 removed
- tests:+10 added−0 removed
- other:+32 added−236 removed
Provenance
- Commit
220e67a- Tree
ed959a7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
6aeab5d- Parent 2
01d0bab
5 files changed+42 added−236 removed
modified:src/jinja2/filters.py+15 added-20 removed
4 unmodified lines5 import re6 import typing7 import typing as t8 from collections import abc9+from inspect import getattr_static10 from itertools import chain11 from itertools import groupby12 13 from markupsafe import escape1397 unmodified lines1411 @pass_environment1412 def do_attr(1413 environment: "Environment", obj: t.Any, name: str1414 ) -> t.Union[Undefined, t.Any]:1414- """Get an attribute of an object. ``foo|attr("bar")`` works like1415- ``foo.bar`` just that always an attribute is returned and items are not1416- looked up.1415+ """Get an attribute of an object. ``foo|attr("bar")`` works like1416+ ``foo.bar``, but returns undefined instead of falling back to ``foo["bar"]``1417+ if the attribute doesn't exist.1418 1419 See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.1420 """1421+ # Environment.getattr will fall back to obj[name] if obj.name doesn't exist.1422+ # But we want to call env.getattr to get behavior such as sandboxing.1423+ # Determine if the attr exists first, so we know the fallback won't trigger.1424 try:1421- name = str(name)1422- except UnicodeError:1423- pass1424- else:1425- try:1426- value = getattr(obj, name)1427- except AttributeError:1428- pass1429- else:1430- if environment.sandboxed:1431- environment = t.cast("SandboxedEnvironment", environment)1425+ # This avoids executing properties/descriptors, but misses __getattr__1426+ # and __getattribute__ dynamic attrs.1427+ getattr_static(obj, name)1428+ except AttributeError:1429+ # This finds dynamic attrs, and we know it's not a descriptor at this point.1430+ if not hasattr(obj, name):1431+ return environment.undefined(obj=obj, name=name)1432 1433- if not environment.is_safe_attribute(obj, name, value):1434- return environment.unsafe_undefined(obj, name)1433+ return environment.getattr(obj, name)1434 1436- return value1435 1438- return environment.undefined(obj=obj, name=name)1439-1440-1436 @typing.overload1437 def sync_do_map(1438 context: "Context",1439 value: t.Iterable[t.Any],434 unmodified linesmodified:tests/test_security.py+10 added
188 unmodified lines189 )190 191 with pytest.raises(SecurityError):192 t.render()193+194+ def test_attr_filter(self) -> None:195+ env = SandboxedEnvironment()196+ t = env.from_string(197+ """{{ "{0.__call__.__builtins__[__import__]}"198+ | attr("format")(not_here) }}"""199+ )200+201+ with pytest.raises(SecurityError):202+ t.render()modified:CHANGES.rst+9 added
9 unmodified lines10 :pr:`1793`11 - Use ``flit_core`` instead of ``setuptools`` as build backend.12 13 14+Version 3.1.615+-------------16+17+Released 2025-03-0518+19+- The ``|attr`` filter does not bypass the environment's attribute lookup,20+ allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`21+22+23 Version 3.1.524 -------------25 26 Released 2024-12-211042 unmodified linesdeleted:CONTRIBUTING.rst-216 removed
1-How to contribute to Jinja2-==========================3-4-Thank you for considering contributing to Jinja!5-6-7-Support questions8------------------9-10-Please don't use the issue tracker for this. The issue tracker is a11-tool to address bugs and feature requests in Jinja itself. Use one of12-the following resources for questions about using Jinja or issues with13-your own code:14-15-- The ``#get-help`` channel on our Discord chat:16- https://discord.gg/pallets17-- The mailing list flask@python.org for long term discussion or larger18- issues.19-- Ask on `Stack Overflow`_. Search with Google first using:20- ``site:stackoverflow.com jinja {search term, exception message, etc.}``21-22-.. _Stack Overflow: https://stackoverflow.com/questions/tagged/jinja?tab=Frequent23-24-25-Reporting issues26-----------------27-28-Include the following information in your post:29-30-- Describe what you expected to happen.31-- If possible, include a `minimal reproducible example`_ to help us32- identify the issue. This also helps check that the issue is not with33- your own code.34-- Describe what actually happened. Include the full traceback if there35- was an exception.36-- List your Python and Jinja versions. If possible, check if this37- issue is already fixed in the latest releases or the latest code in38- the repository.39-40-.. _minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example41-42-43-Submitting patches44-------------------45-46-If there is not an open issue for what you want to submit, prefer47-opening one for discussion before working on a PR. You can work on any48-issue that doesn't have an open PR linked to it or a maintainer assigned49-to it. These show up in the sidebar. No need to ask if you can work on50-an issue that interests you.51-52-Include the following in your patch:53-54-- Use `Black`_ to format your code. This and other tools will run55- automatically if you install `pre-commit`_ using the instructions56- below.57-- Include tests if your patch adds or changes code. Make sure the test58- fails without your patch.59-- Update any relevant docs pages and docstrings. Docs pages and60- docstrings should be wrapped at 72 characters.61-- Add an entry in ``CHANGES.rst``. Use the same style as other62- entries. Also include ``.. versionchanged::`` inline changelogs in63- relevant docstrings.64-65-.. _Black: https://black.readthedocs.io66-.. _pre-commit: https://pre-commit.com67-68-69-First time setup70-~~~~~~~~~~~~~~~~71-72-- Download and install the `latest version of git`_.73-- Configure git with your `username`_ and `email`_.74-75- .. code-block:: text76-77- $ git config --global user.name 'your name'78- $ git config --global user.email 'your email'79-80-- Make sure you have a `GitHub account`_.81-- Fork Jinja to your GitHub account by clicking the `Fork`_ button.82-- `Clone`_ the main repository locally.83-84- .. code-block:: text85-86- $ git clone https://github.com/pallets/jinja87- $ cd jinja88-89-- Add your fork as a remote to push your work to. Replace90- ``{username}`` with your username. This names the remote "fork", the91- default Pallets remote is "origin".92-93- .. code-block:: text94-95- $ git remote add fork https://github.com/{username}/jinja96-97-- Create a virtualenv.98-99- .. code-block:: text100-101- $ python3 -m venv env102- $ . env/bin/activate103-104- On Windows, activating is different.105-106- .. code-block:: text107-108- > env\Scripts\activate109-110-- Install the development dependencies, then install Jinja in editable111- mode.112-113- .. code-block:: text114-115- $ pip install -r requirements/dev.txt && pip install -e .116-117-- Install the pre-commit hooks.118-119- .. code-block:: text120-121- $ pre-commit install122-123-.. _latest version of git: https://git-scm.com/downloads124-.. _username: https://docs.github.com/en/github/using-git/setting-your-username-in-git125-.. _email: https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address126-.. _GitHub account: https://github.com/join127-.. _Fork: https://github.com/pallets/jinja/fork128-.. _Clone: https://docs.github.com/en/github/getting-started-with-github/fork-a-repo#step-2-create-a-local-clone-of-your-fork129-130-131-Start coding132-~~~~~~~~~~~~133-134-- Create a branch to identify the issue you would like to work on. If135- you're submitting a bug or documentation fix, branch off of the136- latest ".x" branch.137-138- .. code-block:: text139-140- $ git fetch origin141- $ git checkout -b your-branch-name origin/3.0.x142-143- If you're submitting a feature addition or change, branch off of the144- "main" branch.145-146- .. code-block:: text147-148- $ git fetch origin149- $ git checkout -b your-branch-name origin/main150-151-- Using your favorite editor, make your changes,152- `committing as you go`_.153-- Include tests that cover any code changes you make. Make sure the154- test fails without your patch. Run the tests as described below.155-- Push your commits to your fork on GitHub and156- `create a pull request`_. Link to the issue being addressed with157- ``fixes #123`` in the pull request.158-159- .. code-block:: text160-161- $ git push --set-upstream fork your-branch-name162-163-.. _committing as you go: https://dont-be-afraid-to-commit.readthedocs.io/en/latest/git/commandlinegit.html#commit-your-changes164-.. _create a pull request: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request165-166-167-Running the tests168-~~~~~~~~~~~~~~~~~169-170-Run the basic test suite with pytest.171-172-.. code-block:: text173-174- $ pytest175-176-This runs the tests for the current environment, which is usually177-sufficient. CI will run the full suite when you submit your pull178-request. You can run the full test suite with tox if you don't want to179-wait.180-181-.. code-block:: text182-183- $ tox184-185-186-Running test coverage187-~~~~~~~~~~~~~~~~~~~~~188-189-Generating a report of lines that do not have test coverage can indicate190-where to start contributing. Run ``pytest`` using ``coverage`` and191-generate a report.192-193-.. code-block:: text194-195- $ pip install coverage196- $ coverage run -m pytest197- $ coverage html198-199-Open ``htmlcov/index.html`` in your browser to explore the report.200-201-Read more about `coverage <https://coverage.readthedocs.io>`__.202-203-204-Building the docs205-~~~~~~~~~~~~~~~~~206-207-Build the docs in the ``docs`` directory using Sphinx.208-209-.. code-block:: text210-211- $ cd docs212- $ make html213-214-Open ``_build/html/index.html`` in your browser to view the docs.215-216-Read more about `Sphinx <https://www.sphinx-doc.org/en/stable/>`__.modified:README.md+8 added
45 unmodified lines46474849505152535455565745 unmodified linesallow the maintainers to devote more time to the projects, [pleasedonate today][].
[please donate today]: https://palletsprojects.com/donate
## Contributing
See our [detailed contributing documentation][contrib] for many ways tocontribute, including reporting issues, requesting features, asking or answeringquestions, and making PRs.
[contrib]: https://palletsprojects.com/contributing/