Skip to content

Commit d3e9a64

Browse files
authored
Merge pull request #189 from CoreTrace/fix/template-filter-overrides
fix: respect project template filter overrides before reporting built-ins
2 parents 0aca784 + 4570ce8 commit d3e9a64

5 files changed

Lines changed: 660 additions & 23 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- A `TEMPLATES` assigned inside a function or a class no longer counts as Django settings: only a module-level assignment can activate the `request` context processor, and only a mention of the module's `TEMPLATES` elsewhere leaves that activation uncertain. A function-local `TEMPLATES`, called or not, used to enable the implicit request source, upgrading a template filter finding to exploitable (#184).
88
- A template block that renders `{{ block.super }}` keeps the parent block's content, so the filters the parent applies stay fed by the render context, with the parent template as the place of the call; a block without it still replaces the parent's whole block, and a chain of templates keeps exactly the parents each level renders. The parent's filters used to drop out of the exploitable flow as soon as a child defined the block (#185).
9+
- A project tag library registering a filter of a built-in's name replaces the built-in in every Django template loading it before applying the filter, as Django's parser does: `{% load custom %}{{ bio|striptags }}` calls the project's `striptags` when `app/templatetags/custom.py` registers one, so it is no longer a reachable or exploitable call to `django.template.defaultfilters.striptags`; the project's function is ordinary code, analysed as any other. A load affects only the filters after it, a later load overrides an earlier one, `{% load striptags from custom %}` replaces only what it names, and Django's own libraries change nothing. The analyzer reads the registrations on `register`, a `django.template.Library` however it is imported, in decorator and call form, at module level or under `if`, `try` or a function. Where it cannot tell what a library registers — no such library file, one it cannot read or parse, a name not written as a constant, `register` passed elsewhere or filled through `filters=` or `register.filters`, or two apps giving the name and registering different filters — a filter of that name applied under the load is neither the built-in nor certainly replaced: no call is claimed, and the load counts among the templates the analyzer could not read (`app/templates/app/profile.html:1 loads 'custom', not read`), so an advisory naming the filter stays `under_investigation` in the VEX document. The settings' `OPTIONS["libraries"]` and `OPTIONS["builtins"]` are not read (#183).
910

1011
### Plugins
1112

‎docs/plugins.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ when the models make it one. `ctx.call_graph(module)` gives each function's call
260260
(`sites(function)`) and the symbols it reads, called or not (`reads(function)`: reading
261261
`request.form['name']` reads `python.flask.request.form`), each where it first reads it.
262262
`ctx.templates` gives the templates found under the project's `templates` directories:
263-
their `names`, the filters they apply as `calls` (each a `FilterCall(symbol, span)`, the
264-
function behind the filter and where the template applies it), and where they name a
265-
template the analyzer cannot read (`unread`).
263+
their `names`, the filters of Django's own libraries they apply as `calls` (each a
264+
`FilterCall(symbol, span)`, the function behind the filter and where the template
265+
applies it; a filter a project tag library the template loads replaces is no call),
266+
and where they name a template the analyzer cannot read, or load a library it cannot
267+
read while applying a filter of that library's name (`unread`).
266268
The shipped `vulnerable-dependency`, `reachable-vulnerability` and `dependency-policy`
267269
plugins are of this kind.
268270

‎docs/usage.md‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,28 @@ Django. Most filters are functions of the same name in `django.template.defaultf
372372
are `<name>_filter` there, and the filters of `i18n`, `l10n`, `tz` and `humanize` are in
373373
their library's module (`timezone` is `django.templatetags.tz.do_timezone`).
374374

375+
A project tag library, a module of a `templatetags` directory, that registers a filter
376+
of that name replaces the built-in in every template loading it, as Django's parser
377+
does: `{% load custom %}{{ bio|striptags }}` calls the project's function when
378+
`custom.py` registers a `striptags` filter, which is no call to the built-in; the
379+
project's function is ordinary code, whose own calls are analysed as any other. A
380+
load affects only the filters applied after it in the file, a later load overrides an
381+
earlier one, `{% load striptags from custom %}` replaces only the filters it names, and
382+
Django's own libraries (`static`, `i18n`, `l10n`, `tz`, `cache`, `humanize`) change
383+
nothing. The analyzer reads the registrations on the library's `register`, a
384+
`django.template.Library` however it is imported: `@register.filter`,
385+
`@register.filter("name")`, `@register.filter(name="name", is_safe=True)`,
386+
`register.filter("name", function)` and `register.filter(function)`, at module level or
387+
under `if`, `try` or a function. Where it cannot tell what a library registers, the
388+
filter is neither the built-in nor certainly replaced: a library found in no
389+
`templatetags` directory (one an installed package ships, or that the settings'
390+
`OPTIONS["libraries"]` or `OPTIONS["builtins"]` name, which are not read), a file it
391+
cannot read or parse, a name given by a variable or unpacked arguments, `register`
392+
passed to another function, filled through `filters=` or `register.filters`, or bound
393+
to anything but a `Library`, or two apps giving the same library name and registering
394+
different filters. The analyzer then claims no call, and counts the template among those
395+
it could not read (below) when a filter of the library's name is applied under the load.
396+
375397
Attacker input in the render context makes the filter exploitable at the render call:
376398
`render(request, "app/profile.html", {"bio": request.POST["bio"]})` with
377399
`{{ bio|striptags }}`. The analyzer links a context value to a filter only where it is
@@ -506,10 +528,14 @@ A template the analyzer cannot read may apply any filter. The project names one
506528
`render`, `render_to_string`, `TemplateResponse`, an `{% include %}` or an `{% extends %}`
507529
names a template by an expression, or by a name found under no `templates` directory,
508530
including one an installed package ships; a template file that cannot be read counts
509-
too. An advisory whose entry points include a template filter then stays
510-
`under_investigation`, and the notes list where the project names each such template
511-
(`app.views:12 names a template by an expression`). A template named elsewhere, such as
512-
a class-based view's `template_name`, is not checked.
531+
too, and so does a template loading a tag library the analyzer cannot read, or find,
532+
or finds in several apps registering different filters, where it then applies a filter
533+
of a built-in's name, since the library may replace it. An advisory whose entry points
534+
include a template filter then stays `under_investigation`, and the notes list where
535+
the project names each such template (`app.views:12 names a template by an
536+
expression`, `app/templates/app/profile.html:1 loads 'crispy_forms_tags', not found`).
537+
A template named elsewhere, such as a class-based view's `template_name`, is not
538+
checked.
513539

514540
`not_affected` needs the lock-file check because the analyzer reads the project's code,
515541
not the code of installed packages. A framework calling the vulnerable function on the

0 commit comments

Comments
 (0)