Repository navigation
fix: clear render_to_string output only for templates shown to escape - #131
Merged
Merged
Conversation
# Conflicts: # CHANGELOG.md # docs/usage.md
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.
Closes #130. This is the second model fix that comes before the stricter guard rule from #98. It is independent of #129; the two only touch the same
CHANGELOGspot, and I'll mergemainin to resolve that once one of them lands.The three pygoat findings, checked
introduction/views.py:290,304and318were reported asxssonHttpResponse(render_to_string('Lab/AUTH/auth_success.html', {...})).{{ username }},{{ name }},{{ userid }}and{{ err_msg }}in HTML text only, with no|safeand no{% autoescape off %}.introduction/base.html, which loads onlystaticand prints no context variable.DjangoTemplateswith default options, so autoescaping is on.HttpResponse.All three are false positives.
Change
Escaping is modelled only where the engine can establish it.
taint.templates.escaped_templates(root)reads the project's templates, found undertemplatesdirectories by the path Django uses. It returns those that escape everything they print. A template qualifies only if all of these hold:extends/includeclosure is found;|safe,|safeseqor{% autoescape off %};<script>/<style>, event handlers,style/srcdoc, a URL attribute not fixed by a relative orhttp(s)://host/prefix, unquoted attributes, attribute or tag names;autoescapetoFalse.TemplateRender(symbol, position=0, keyword="template_name")model declaresdjango.template.loader.render_to_string. When a call writes the template name as a constant and that template is established, its result losesHTMLtaint. Other kinds are unaffected.|safeinvalidates cached results.DIRSentry not namedtemplates, shipped by an installed package, rendered throughget_template, or a Flask Jinja2 template. Single-file checks read no templates. The usage guide explains this, and a regression test covers theDIRScase.Verification
pytest: 1028 passed.ruff check src testsandmypyare clean.pytest -m regressionunder Python 3.12: 19 passed. Only the pygoat snapshot changed, and only by those threexssfindings.{{ ...|safe }}are not;Lab/XSS/xss.htmlmentions|safeonly in prose, as{query|safe}inside<code>, so it is correctly established.test_architectureforbids parsers outside the frontend, so template names are compared as call sites record constants (repr) rather than parsed.