Security: enable Jinja2 autoescaping in CLI template rendering (FHUB-259) - #986
Merged
Merged
Conversation
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.
Summary
Resolves the two high-severity CodeQL code-scanning alerts
py/jinja2/autoescape-falsein the CLI by enabling Jinja2 autoescaping wheretemplates are rendered.
Changes
cli/common/generate_item_yaml.py– passautoescape=select_autoescape()to the Jinja2
Environment. The item templates are*.yaml.j2, soautoescape resolves to
Falseat runtime and generateditem.yamloutput isunchanged; the change satisfies the scanner and is safe-by-default for any
future HTML/XML templates added to the loader.
cli/utils/helpers.py(render_jinja) – choose autoescape based on thetemplate file extension via
select_autoescape(enabled_extensions=("html","htm","xml")). This is thesubstantive fix:
render_jinjainjects raw source code / YAML into themarketplace's
python.htmlandyaml.htmlpages, which were previouslyrendered without escaping (the reported XSS vector). HTML/XML templates are now
escaped, while non-HTML templates (e.g.
conf.template->conf.py) continueto render unescaped.
Why extension-based (not a blanket
autoescape=True)render_jinjais used for both HTML pages (must escape) and non-HTML files likeconf.py(must not escape, or the generated Python config could be corrupted).Deriving autoescape from the template extension fixes the XSS without changing
non-HTML output.
Test plan
(
<script>-><script>).conf.template/ non-HTML output remains unescaped.