fix: prevent context pollution#213
Merged
fsbraun merged 4 commits intodjango-cms:masterfrom Mar 12, 2026
Merged
Conversation
Contributor
Reviewer's GuideReworks snippet rendering to use a context stack push instead of direct updates so that the calling template context is not mutated, and adds a regression test verifying that snippet rendering does not pollute the template context or leak stack frames. Sequence diagram for snippet rendering with context.push to prevent pollutionsequenceDiagram
participant TemplateEngine
participant SnippetRenderer
participant Context
participant DjangoTemplate as DjangoTemplateSystem
TemplateEngine->>SnippetRenderer: get_content_render(context, instance)
SnippetRenderer->>Context: push({object: instance})
activate Context
alt instance has template
SnippetRenderer->>Context: update({html: mark_safe(instance.html)})
SnippetRenderer->>DjangoTemplate: render_to_string(instance.template, context.flatten())
DjangoTemplate-->>SnippetRenderer: content
else instance has no template
SnippetRenderer->>DjangoTemplate: Template(instance.html)
DjangoTemplate-->>SnippetRenderer: compiled_template
SnippetRenderer->>compiled_template: render(context)
compiled_template-->>SnippetRenderer: content
end
SnippetRenderer-->>TemplateEngine: return content
Context-->>Context: automatic pop on with block exit
deactivate Context
TemplateEngine->>Context: read outer keys
Context-->>TemplateEngine: unchanged outer context (no pollution)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Inside the
with context.push({"object": instance}):block you still callcontext.update({"html": ...}); consider passing"html"into the samepushcall for consistency and to keep all temporary keys clearly scoped to the pushed context. - The new test relies on
len(context.dicts)to detect leaks; adding a short comment there explaining why checking the stack length is important will make the intent clearer to future maintainers.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Inside the `with context.push({"object": instance}):` block you still call `context.update({"html": ...})`; consider passing `"html"` into the same `push` call for consistency and to keep all temporary keys clearly scoped to the pushed context.
- The new test relies on `len(context.dicts)` to detect leaks; adding a short comment there explaining why checking the stack length is important will make the intent clearer to future maintainers.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Use single context.push() and add comments explaining the context stack length assertion.
fsbraun
reviewed
Mar 11, 2026
tests/test_templatetags.py
Outdated
| context = Context({"object": og_object}) | ||
| initial_stack_len = len(context.dicts) | ||
|
|
||
| template_to_render = Template('{% load snippet_tags %}{% snippet_fragment "pollution_test" %}') |
Member
There was a problem hiding this comment.
Suggested change
| template_to_render = Template('{% load snippet_tags %}{% snippet_fragment "pollution_test" %}') | |
| template_to_render = Template('{% load snippet_tags %}{% snippet_fragment "pollution_test" %} "{{ object }}"') |
If you render out {{ object }} you'll directly see what the template engine puts there. Should be This shouldn't change.
This way you do not have to rely on the render method to not push context itself.
fsbraun
requested changes
Mar 11, 2026
Member
fsbraun
left a comment
There was a problem hiding this comment.
I just have a tiny suggestion to improve test clarity
fsbraun
reviewed
Mar 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
I replaced
context.update()withcontext.push()in order to avoid the template context being polluted after rendering the snippet. I also added a simple test case to check that the bug has been fixed and avoid regressions.Related resources
Checklist
masterDiscord to find a “pr review buddy” who is
going to review my pull request.
Summary by Sourcery
Prevent snippet template rendering from leaking variables into the outer template context and add regression coverage.
Bug Fixes:
Tests: