Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#13573 closed (fixed)

Cached template loader can return incorrect template

Reported by: lamby Owned by: nobody
Component: Template system Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


The cached.Loader's find_template method constructs a cache key based solely on the requested template name and does not include the template_dirs. This results in find_template returning the incorrect template where a template with the same template_name was previously loaded and cached from a different set of template_dirs. That is to say, the function is overly non-injective.

For example, the following snippets return the same Template. when they should return a different template for templates/foo/file.html and templates/bar/file.html respectfully:

from django.template import loader
a = loader.find_template('file.html', ('templates/foo',))
b = loader.find_template('file.html', ('templates/bar',))

Patch attached.

Attachments (1)

36-include-template_dirs-in-template-caching-hash.diff (1.2 KB) - added by lamby 5 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 5 years ago by russellm

  • milestone set to 1.3
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [13295]) Fixed #13573 -- Corrected problem with template caching when template directories are provided. Thanks to lamby for the report.

comment:3 Changed 5 years ago by Alex

Calling hash() here seems like a really bad idea. I'm just waiting for someone to provide a collision...

comment:4 Changed 5 years ago by russellm

(In [13299]) Refs #13573 -- Modified the key technique added in r13295 to be more robust against potential key collisions while keeping key names human-readable. Thanks to Alex for being finicky.

comment:5 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top