Skip to content

Commit 034ecaf

Browse files
committed
fix: never assume an unreadable template file escapes
escaped_templates read a template file it could not open as empty text, and an empty template escapes everything it renders. Such a file now yields no escaping verdict, like a template the engine cannot find, so the flow stays xss and the templates needing it are not established either.
1 parent 4c2d62a commit 034ecaf

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Precision
6+
7+
- A Django template file the analyzer cannot read, because of its permissions, no longer counts as escaping everything it renders: it read the file as empty text, and an empty template escapes. The flow through `render_to_string` stays `xss`, and a template extending or including the unreadable one is not established to escape either, as for a template the analyzer cannot find. An unreadable file was already listed among the templates the project names but the run could not read.
8+
39
## 0.17.0 (2026-09-28)
410

511
### Plugins

‎docs/usage.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ It establishes escaping only when all of the following hold:
156156
- No Python file of the project sets `autoescape` to `False`.
157157

158158
The analyzer never assumes escaping where it cannot read the template. That covers a
159-
template named by an expression, a template found through a `DIRS` entry not named
160-
`templates`, one shipped by an installed package, one rendered through `get_template`,
161-
and Flask's Jinja2 templates. The flow is then reported as before. A single-file check
159+
template file it cannot read, a template named by an expression, a template found
160+
through a `DIRS` entry not named `templates`, one shipped by an installed package, one
161+
rendered through `get_template`, and Flask's Jinja2 templates. The flow is then reported as before. A single-file check
162162
reads no template.
163163

164164
### Dangerous API usage

‎src/coretrace_python/taint/templates.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
1616
Templates are found under the project's ``templates`` directories, by their path below
1717
one. A template the engine cannot find — named by an expression, under a ``DIRS`` entry
18-
of another name, shipped by an installed package — is never assumed to escape.
18+
of another name, shipped by an installed package — or cannot read is never assumed to
19+
escape.
1920
2021
A template applying a filter of Django's own libraries calls the function behind it
2122
whenever it is rendered, though no Python call names that function: ``{{ bio|striptags }}``
@@ -149,7 +150,7 @@ def escaped_templates(root: Path) -> frozenset[str]:
149150
local: dict[str, bool] = {}
150151
requires: dict[str, set[str]] = {}
151152
for name, paths in found.items():
152-
verdicts = [_inspect(_read(path)) for path in paths]
153+
verdicts = [_verdict(path) for path in paths]
153154
local[name] = all(safe for safe, _ in verdicts)
154155
requires[name] = {needed for _, needs in verdicts for needed in needs}
155156
escaped = {name for name, safe in local.items() if safe}
@@ -171,6 +172,14 @@ def _read(path: Path) -> str:
171172
return _text(path) or ""
172173

173174

175+
def _verdict(path: Path) -> tuple[bool, set[str]]:
176+
"""Whether one template file escapes all it prints, and what it needs: a file the
177+
engine cannot read is not established to escape, like a template it cannot find."""
178+
179+
text = _text(path)
180+
return (False, set()) if text is None else _inspect(text)
181+
182+
174183
def _text(path: Path) -> str | None:
175184
"""The text of ``path``, or None when it cannot be read."""
176185

0 commit comments

Comments
 (0)