Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja autoescape #50

Merged
merged 7 commits into from Sep 28, 2023
Merged

Jinja autoescape #50

merged 7 commits into from Sep 28, 2023

Conversation

clavedeluna
Copy link
Contributor

@clavedeluna clavedeluna commented Sep 27, 2023

Overview

Add a codemod that checks if jinja2.Environment enables autoescape

Description

  • autoescape, a jinja2 setting to guard against XSS attacks, is disabled by default (and can explicitly be disabled)
  • This PR ensures users enable autoescape
  • Due to confusion around jinja2's select_autoescape function (see bug report here), this codemod does not detect if autoescape is assigned to a callable, such as Environment(autoescape=select_autoescape()). this is the recommended way in the docs. We will cover that later.

@clavedeluna clavedeluna changed the base branch from fix-pre-commit to main September 27, 2023 14:38
@clavedeluna clavedeluna marked this pull request as ready for review September 27, 2023 14:45
@clavedeluna clavedeluna force-pushed the jinja-autoescape branch 2 times, most recently from b85c36f to f909c5a Compare September 27, 2023 14:58
Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. The main substantive comment is about the semgrep pattern, plus a few minor comments on the docs.

from jinja2 import Environment

- env = Environment()
- env = Environment(autoescape=False, loader=some-loader)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing but the - here and below is not valid Python syntax.

jinja2.Environment(...)
- pattern: |
jinja2.Environment(..., autoescape=False, ...)
- pattern-not: |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this pattern-not by itself be sufficient instead of enumerating the other cases above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh hmm good point let me check if tests pass with that

else:
new = arg
new_args.append(new)

if add_if_missing and not arg_added:
new = cst.Arg(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small thing but it seems like this Arg creation could be factored out into another method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, thought about that and forgot, will do!

return """
rules:
- patterns:
- pattern-either:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment below but I think this might be redundant with the pattern-not.

@clavedeluna clavedeluna merged commit 9978745 into main Sep 28, 2023
6 checks passed
@clavedeluna clavedeluna deleted the jinja-autoescape branch September 28, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants