Information disclosure

HIGH
django/django
Commit: 34bd3ed944bf
Affected: 5.1.x before this commit
2026-04-05 14:27 UTC

Description

The commit fixes an information disclosure vulnerability where accessing PartialTemplate.source could leak internal Django file paths via a warning stack trace. The change switches the warning to skip internal Django file prefixes (skip_file_prefixes), so the emitted warning does not reveal internal paths and instead points to the application caller context.

Proof of Concept

PoC (illustrative reproduction): 1) Enable a Django project in DEBUG mode and render a template that exercises a partial/template inclusion that would trigger PartialTemplate.source. 2) Access the PartialTemplate.source attribute to trigger the warning (as done by Django's template engine when debugging is enabled). 3) Observe the RuntimeWarning emitted and note the filename associated with the warning in the traceback or warning output. Before this patch, the filename would often point to an internal Django path (e.g., .../django/template/...); with the patch, the warning should skip internal prefixes and instead point to the application's caller file. Python/Pseudo-code (illustrative; depends on Django internals): import warnings from django.conf import settings import django settings.configure(DEBUG=True, TEMPLATES=[{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[], 'APP_DIRS': True}]) django.setup() # Trigger the PartialTemplate.source warning via internal API (the exact access pattern may vary by Django version) # This is representative of how a developer might cause the warning during template debugging from django.template import engines template = engines['django'].from_string("{% include 'partials/foo.html' %}") partial = template.nodelist[0].template # depends on internal structure with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') _ = partial.source # accessing source triggers the warning under DEBUG for warn in w: print("Warning:", warn.message) print("Origin filename:", getattr(warn, 'filename', '<unknown>')) Notes: - The crucial aspect demonstrated is that, prior to this patch, the stack trace or warning location could reveal internal Django paths. The patch adds skip_file_prefixes so internal paths are not exposed. - The exact code path to reach PartialTemplate.source may vary between Django versions; the test suite for this commit demonstrates the intended behavior by asserting the caller filename is the test file rather than an internal Django file.

Commit Details

Author: farhan

Date: 2025-09-04 12:05 UTC

Message:

Refs #36559, #35667 -- Used skip_file_prefixes in PartialTemplate.source warning.

Triage Assessment

Vulnerability Type: Information disclosure

Confidence: HIGH

Reasoning:

The patch updates a warning to use skip_file_prefixes, preventing internal Django paths from being exposed in PartialTemplate.source warnings. This mitigates information disclosure via stack traces/file paths. The tests adjust expectations accordingly, indicating a security-oriented change rather than a pure refactor.

Verification Assessment

Vulnerability Type: Information disclosure

Confidence: HIGH

Affected Versions: 5.1.x before this commit

Code Diff

diff --git a/django/template/base.py b/django/template/base.py index 37e7243d5c9b..b87291746b68 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -52,10 +52,12 @@ import inspect import logging +import os import re import warnings from enum import Enum +import django from django.template.context import BaseContext from django.utils.formats import localize from django.utils.html import conditional_escape @@ -327,7 +329,7 @@ def source(self): "PartialTemplate.source is only available when template " "debugging is enabled.", RuntimeWarning, - stacklevel=2, + skip_file_prefixes=(os.path.dirname(django.__file__),), ) return self.find_partial_source(template.source) diff --git a/tests/template_tests/test_partials.py b/tests/template_tests/test_partials.py index 9762436fdc42..984f0441c204 100644 --- a/tests/template_tests/test_partials.py +++ b/tests/template_tests/test_partials.py @@ -144,8 +144,9 @@ def test_template_source_warning(self): RuntimeWarning, "PartialTemplate.source is only available when template " "debugging is enabled.", - ): + ) as ctx: self.assertEqual(partial.template.source, "") + self.assertEqual(ctx.filename, __file__) class RobustPartialHandlingTests(TestCase):
← Back to Alerts View on GitHub →