Skip to content

Commit 0da4cc8

Browse files
authored
Merge pull request #280 from CoreTrace/fix/273-page-composition
fix(templates): read each page as Django puts its blocks and includes together
2 parents abd028c + 9e954b2 commit 0da4cc8

6 files changed

Lines changed: 1020 additions & 54 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
instead of repeating its id (#270), and each result carries its confidence as the
2121
`confidence` property (#271). A flow reaching its sink inside a project function points at
2222
that sink as a related location, which the result's message links to (#271).
23+
- Django templates are also read as each page is put together (#273): a variable a
24+
child's `{% block %}` prints inside its parent's `<script>`, an `{% include %}` that ends
25+
the attribute value it stands in, or one of a name several files have outside HTML data,
26+
is reported as `xss` where it was not, as is the variable `block` named anywhere but in
27+
a bare `{{ block.super }}`. So is a template that includes Django's
28+
`django/forms/widgets/attrs.html` inside a tag, which prints attribute names, and a
29+
template including itself inside a tag or a script. A page beyond 8 MiB once put
30+
together, or a template with more than 64 pages, or 16 MiB in all, through names
31+
several files have, is not established and is listed in `Coverage.unread_templates`.
2332
- Plugin API, additive: `Plugin.rules` maps the rules a plugin reports to their
2433
`RuleDescription`, optional, so that third-party plugins describe their own rules;
2534
`Finding.related` lists `RelatedLocation`s (a span and a message). `Report.rules` carries the

‎docs/usage.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,25 @@ ends there and the browser would never run it. A template including one that end
213213
such a block, a tag or an end tag is not established either: the markup after the
214214
include continues it. A template extending another ends where that one ends. A comment
215215
hides what it holds only up to its first template tag, since an include there may close
216-
it and a condition around its `<!--` may leave it out. A `{% block %}` or
217-
`{% include %}` placed inside a parent's `<script>` is not covered yet (#273).
216+
it and a condition around its `<!--` may leave it out.
217+
218+
Each page is also put together as Django renders it, and read once more (#273): the
219+
templates it extends, each block taking the definition of the most derived one and `{{
220+
block.super }}` the next one up, and each template it includes in place. What a child's
221+
block prints inside its parent's `<script>`, or what an include puts inside an
222+
attribute, is read where it lands. `{{ block.super }}` is put in place only bare: the
223+
variable `block` named anywhere else, through a filter, a tag or an alias (`{% with
224+
parent=block %}{{ parent.super }}`), may still print the parent's markup, marked safe,
225+
and the template is not established. A name several files have, which Django resolves
226+
through loaders the analyzer does not read, counts as each of them: a page extending it
227+
must escape with each file, except those already in its chain, which Django leaves out,
228+
so that a template extending its own name extends another file of it; an include of it
229+
is accepted only in HTML data, never inside an attribute, a tag, a `<script>` or
230+
`<style>` block or a comment, and only when each file ends in HTML data, nothing of it
231+
left open. A template including itself is read the same way. A page beyond 8 MiB once
232+
put together, or a template with more than 64 pages, or 16 MiB in all, through the files
233+
the templates it extends may be, is not established, and is listed among the templates
234+
the analyzer could not read.
218235

219236
The analyzer never assumes escaping where it cannot read the template. That covers a
220237
template file it cannot read, a template named by an expression, a template found

‎src/coretrace_python/engine.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@
135135
SecurityModelRegistry,
136136
TaintAnalysis,
137137
TaintKind,
138-
escaped_templates,
139138
project_templates,
140139
registered_routes,
141140
request_processor,
141+
template_escaping,
142142
unread_renders,
143143
)
144144
from coretrace_python.taint.urls import flow_url
@@ -532,7 +532,8 @@ def analyze_project(
532532
for name in sorted(analysable):
533533
for symbol, registered in _routes_of(analysable[name]).items():
534534
routes.setdefault(symbol, registered)
535-
escaped = escaped_templates(root)
535+
escaping = template_escaping(root)
536+
escaped = escaping.escaped
536537
templates = project_templates(root)
537538
clearing = models.clearing(escaped)
538539
template_calls = models.template_calls(
@@ -678,7 +679,7 @@ def analyze_project(
678679
findings = [_sourced(finding, origins) for finding in findings]
679680
accepted = tuple(f for f in findings if policy.accepts(f))
680681
kept, suppressed = partition(apply_policy(policy, findings), _text_of(sources))
681-
unread = list(templates.unread)
682+
unread = [*templates.unread, *escaping.unread]
682683
for name in sorted(call_graphs):
683684
unread.extend(unread_renders(name, call_graphs[name], models, templates.names))
684685
return ProjectAnalysis(

‎src/coretrace_python/taint/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
escaped_templates,
4343
project_templates,
4444
request_processor,
45+
template_escaping,
4546
unread_renders,
4647
)
4748

@@ -85,5 +86,6 @@
8586
"propagate_taint",
8687
"registered_routes",
8788
"request_processor",
89+
"template_escaping",
8890
"unread_renders",
8991
]

0 commit comments

Comments
 (0)