-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Dan D'Avella <[email protected]>
- Loading branch information
1 parent
df11542
commit 9978745
Showing
10 changed files
with
212 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from core_codemods.enable_jinja2_autoescape import EnableJinja2Autoescape | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestEnableJinja2Autoescape(BaseIntegrationTest): | ||
codemod = EnableJinja2Autoescape | ||
code_path = "tests/samples/jinja2_autoescape.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
(2, "env = Environment(autoescape=True)\n"), | ||
(3, "env = Environment(autoescape=True)\n"), | ||
], | ||
) | ||
expected_diff = "--- \n+++ \n@@ -1,4 +1,4 @@\n from jinja2 import Environment\n \n-env = Environment()\n-env = Environment(autoescape=False)\n+env = Environment(autoescape=True)\n+env = Environment(autoescape=True)\n" | ||
expected_line_change = "3" | ||
num_changes = 2 | ||
change_description = EnableJinja2Autoescape.CHANGE_DESCRIPTION |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
src/core_codemods/docs/pixee_python_enable-jinja2-autoescape.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
This codemod enables autoescaping of HTML content in `jinja2`. Unfortunately, the jinja2 | ||
default behavior is to not autoescape when rendering templates, which makes your applications | ||
vulnerable to Cross-Site Scripting (XSS) attacks. | ||
|
||
Our codemod checks if you forgot to enable autoescape or if you explicitly disabled it. The change looks as follows: | ||
|
||
```diff | ||
from jinja2 import Environment | ||
|
||
- env = Environment() | ||
- env = Environment(autoescape=False, loader=some_loader) | ||
+ env = Environment(autoescape=True) | ||
+ env = Environment(autoescape=True, loader=some_loader) | ||
... | ||
``` | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
|
||
|
||
class EnableJinja2Autoescape(SemgrepCodemod): | ||
NAME = "enable-jinja2-autoescape" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW | ||
SUMMARY = "Enable jinja2 autoescape" | ||
DESCRIPTION = "Makes the `autoescape` parameter to jinja2.Environment be `True`." | ||
|
||
@classmethod | ||
def rule(cls): | ||
return """ | ||
rules: | ||
- patterns: | ||
- pattern: jinja2.Environment(...) | ||
- pattern-not: jinja2.Environment(..., autoescape=True, ...) | ||
- pattern-inside: | | ||
import jinja2 | ||
... | ||
""" | ||
|
||
def on_result_found(self, original_node, updated_node): | ||
new_args = self.replace_arg( | ||
original_node, "autoescape", "True", add_if_missing=True | ||
) | ||
return self.update_arg_target(updated_node, new_args) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from core_codemods.enable_jinja2_autoescape import EnableJinja2Autoescape | ||
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest | ||
|
||
|
||
class TestEnableJinja2Autoescape(BaseSemgrepCodemodTest): | ||
codemod = EnableJinja2Autoescape | ||
|
||
def test_rule_ids(self): | ||
assert self.codemod.NAME == "enable-jinja2-autoescape" | ||
|
||
def test_import(self, tmpdir): | ||
input_code = """import jinja2 | ||
env = jinja2.Environment() | ||
var = "hello" | ||
""" | ||
expexted_output = """import jinja2 | ||
env = jinja2.Environment(autoescape=True) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_import_with_arg(self, tmpdir): | ||
input_code = """import jinja2 | ||
env = jinja2.Environment(autoescape=False) | ||
var = "hello" | ||
""" | ||
expexted_output = """import jinja2 | ||
env = jinja2.Environment(autoescape=True) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_import_with_more_args(self, tmpdir): | ||
input_code = """import jinja2 | ||
env = jinja2.Environment(loader=some_loader, autoescape=False) | ||
var = "hello" | ||
""" | ||
expexted_output = """import jinja2 | ||
env = jinja2.Environment(loader=some_loader, autoescape=True) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_import_with_other_args(self, tmpdir): | ||
input_code = """import jinja2 | ||
env = jinja2.Environment(loader=some_loader) | ||
var = "hello" | ||
""" | ||
expexted_output = """import jinja2 | ||
env = jinja2.Environment(loader=some_loader, autoescape=True) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_from_import(self, tmpdir): | ||
input_code = """from jinja2 import Environment | ||
env = Environment() | ||
var = "hello" | ||
""" | ||
expexted_output = """from jinja2 import Environment | ||
env = Environment(autoescape=True) | ||
var = "hello" | ||
""" | ||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_import_alias(self, tmpdir): | ||
input_code = """import jinja2 as templating | ||
env = templating.Environment() | ||
var = "hello" | ||
""" | ||
expexted_output = """import jinja2 as templating | ||
env = templating.Environment(autoescape=True) | ||
var = "hello" | ||
""" | ||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
def test_autoescape_enabled(self, tmpdir): | ||
input_code = """import jinja2 | ||
env = jinja2.Environment(autoescape=True) | ||
var = "hello" | ||
""" | ||
expexted_output = input_code | ||
self.run_and_assert(tmpdir, input_code, expexted_output) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from jinja2 import Environment | ||
|
||
env = Environment() | ||
env = Environment(autoescape=False) |