-
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
dfdbba0
commit 3b35355
Showing
4 changed files
with
178 additions
and
2 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
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,42 @@ | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
|
||
|
||
class LxmlSafeParserDefaults(SemgrepCodemod): | ||
NAME = "safe-lxml-parser-defaults" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW | ||
SUMMARY = "Enable all security checks in `lxml.etree.XMLParser` call." | ||
DESCRIPTION = "...........TODO" | ||
|
||
@classmethod | ||
def rule(cls): | ||
return """ | ||
rules: | ||
- patterns: | ||
- pattern: lxml.etree.$CLASS(...) | ||
- pattern-not: lxml.etree.$CLASS(..., resolve_entities=False, ...) | ||
- pattern-not: lxml.etree.$CLASS(..., no_network=True, ..., resolve_entities=False, ...) | ||
- pattern-not: lxml.etree.$CLASS(..., dtd_validation=False, ..., resolve_entities=False, ...) | ||
- metavariable-pattern: | ||
metavariable: $CLASS | ||
patterns: | ||
- pattern-either: | ||
- pattern: XMLParser | ||
- pattern: ETCompatXMLParser | ||
- pattern: XMLTreeBuilder | ||
- pattern: XMLPullParser | ||
- pattern-inside: | | ||
import lxml | ||
... | ||
""" | ||
|
||
def on_result_found(self, original_node, updated_node): | ||
new_args = self.replace_args( | ||
original_node, | ||
[ | ||
("resolve_entities", "False", True), | ||
("no_network", "True", False), | ||
("dtd_validation", "False", False), | ||
], | ||
) | ||
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,100 @@ | ||
import pytest | ||
from core_codemods.lxml_safe_parser_defaults import LxmlSafeParserDefaults | ||
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest | ||
|
||
each_class = pytest.mark.parametrize( | ||
"klass", ["XMLParser", "ETCompatXMLParser", "XMLTreeBuilder", "XMLPullParser"] | ||
) | ||
|
||
|
||
class TestJwtDecodeVerify(BaseSemgrepCodemodTest): | ||
codemod = LxmlSafeParserDefaults | ||
|
||
def test_name(self): | ||
assert self.codemod.name() == "safe-lxml-parser-defaults" | ||
|
||
@each_class | ||
def test_import(self, tmpdir, klass): | ||
input_code = f"""import lxml | ||
parser = lxml.etree.{klass}() | ||
var = "hello" | ||
""" | ||
expexted_output = f"""import lxml | ||
parser = lxml.etree.{klass}(resolve_entities=False) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@each_class | ||
def test_from_import(self, tmpdir, klass): | ||
input_code = f"""from lxml.etree import {klass} | ||
parser = {klass}() | ||
var = "hello" | ||
""" | ||
expexted_output = f"""from lxml.etree import {klass} | ||
parser = {klass}(resolve_entities=False) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@each_class | ||
def test_import_alias(self, tmpdir, klass): | ||
input_code = f"""from lxml.etree import {klass} as xmlklass | ||
parser = xmlklass() | ||
var = "hello" | ||
""" | ||
expexted_output = f"""from lxml.etree import {klass} as xmlklass | ||
parser = xmlklass(resolve_entities=False) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@pytest.mark.parametrize( | ||
"input_args,expected_args", | ||
[ | ||
( | ||
"resolve_entities=True", | ||
"resolve_entities=False", | ||
), | ||
( | ||
"resolve_entities=False", | ||
"resolve_entities=False", | ||
), | ||
( | ||
"""resolve_entities=True, no_network=False, dtd_validation=True""", | ||
"""resolve_entities=False, no_network=True, dtd_validation=False""", | ||
), | ||
( | ||
"""dtd_validation=True""", | ||
"""dtd_validation=False, resolve_entities=False""", | ||
), | ||
( | ||
"""no_network=False""", | ||
"""no_network=True, resolve_entities=False""", | ||
), | ||
( | ||
"""no_network=True""", | ||
"""no_network=True, resolve_entities=False""", | ||
), | ||
], | ||
) | ||
@each_class | ||
def test_verify_variations(self, tmpdir, klass, input_args, expected_args): | ||
input_code = f"""import lxml | ||
parser = lxml.etree.{klass}({input_args}) | ||
var = "hello" | ||
""" | ||
expexted_output = f"""import lxml | ||
parser = lxml.etree.{klass}({expected_args}) | ||
var = "hello" | ||
""" | ||
self.run_and_assert(tmpdir, input_code, expexted_output) |