-
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.
- Loading branch information
1 parent
0c2a8c8
commit c6478bf
Showing
7 changed files
with
180 additions
and
4 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, | ||
[ | ||
(1, "env = Environment(autoescape=True)\n"), | ||
(2, "env = Environment(autoescape=True)\n"), | ||
], | ||
) | ||
expected_diff = "--- \n+++ \n@@ -1,3 +1,3 @@\n from jinja2 import Environment\n-env = Environment()\n-env = Environment(autoescape=False)\n+env = Environment(autoescape=True)\n+env = Environment(autoescape=True)\n" | ||
expected_line_change = "2" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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-either: | ||
- pattern: | | ||
jinja2.Environment() | ||
- pattern: | | ||
jinja2.Environment(...) | ||
- pattern: | | ||
jinja2.Environment(..., autoescape=False, ...) | ||
- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import pytest | ||
from codemodder.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.RULE_IDS == ["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) |
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