-
Notifications
You must be signed in to change notification settings - Fork 10
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
New codemod: safe-lxml-parsing
#64
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 core_codemods.lxml_safe_parsing import LxmlSafeParsing | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestLxmlSafeParsing(BaseIntegrationTest): | ||
codemod = LxmlSafeParsing | ||
code_path = "tests/samples/lxml_parsing.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
( | ||
1, | ||
'lxml.etree.parse("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))\n', | ||
), | ||
( | ||
2, | ||
'lxml.etree.fromstring("xml_str", parser=lxml.etree.XMLParser(resolve_entities=False))\n', | ||
), | ||
], | ||
) | ||
expected_diff = '--- \n+++ \n@@ -1,3 +1,3 @@\n import lxml.etree\n-lxml.etree.parse("path_to_file")\n-lxml.etree.fromstring("xml_str")\n+lxml.etree.parse("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))\n+lxml.etree.fromstring("xml_str", parser=lxml.etree.XMLParser(resolve_entities=False))\n' | ||
expected_line_change = "2" | ||
num_changes = 2 | ||
change_description = LxmlSafeParsing.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,14 @@ | ||
This codemod sets the `parser` parameter in calls to `lxml.etree.parse` and `lxml.etree.fromstring` | ||
if omitted or set to `None` (the default value). Unfortunately, the default `parser=None` means `lxml` | ||
will rely on an unsafe parser, making your code potentially vulnerable to entity expansion | ||
attacks and external entity (XXE) attacks. | ||
|
||
The changes look as follows: | ||
|
||
```diff | ||
import lxml.etree | ||
- lxml.etree.parse("path_to_file") | ||
- lxml.etree.fromstring("xml_str") | ||
+ lxml.etree.parse("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False)) | ||
+ lxml.etree.fromstring("xml_str", parser=lxml.etree.XMLParser(resolve_entities=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
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,52 @@ | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
from codemodder.codemods.api.helpers import NewArg | ||
|
||
|
||
class LxmlSafeParsing(SemgrepCodemod): | ||
NAME = "safe-lxml-parsing" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
SUMMARY = "Use safe parsers in lxml parsing functions" | ||
DESCRIPTION = ( | ||
"Call `lxml.etree.parse` and `lxml.etree.fromstring` with a safe parser" | ||
) | ||
|
||
@classmethod | ||
def rule(cls): | ||
return """ | ||
rules: | ||
- pattern-either: | ||
- patterns: | ||
- pattern: lxml.etree.$FUNC(...) | ||
- pattern-not: lxml.etree.$FUNC(...,parser=..., ...) | ||
- metavariable-pattern: | ||
metavariable: $FUNC | ||
patterns: | ||
- pattern-either: | ||
- pattern: parse | ||
- pattern: fromstring | ||
- pattern-inside: | | ||
import lxml.etree | ||
... | ||
- patterns: | ||
- pattern: lxml.etree.$FUNC(..., parser=None, ...) | ||
- metavariable-pattern: | ||
metavariable: $FUNC | ||
patterns: | ||
- pattern-either: | ||
- pattern: parse | ||
- pattern: fromstring | ||
- pattern-inside: | | ||
import lxml.etree | ||
... | ||
""" | ||
|
||
def on_result_found(self, original_node, updated_node): | ||
self.remove_unused_import(original_node) | ||
self.add_needed_import("lxml.etree") | ||
safe_parser = "lxml.etree.XMLParser(resolve_entities=False)" | ||
new_args = self.replace_args( | ||
original_node, | ||
[NewArg(name="parser", value=safe_parser, 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,109 @@ | ||
import pytest | ||
from core_codemods.lxml_safe_parsing import LxmlSafeParsing | ||
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest | ||
|
||
each_func = pytest.mark.parametrize("func", ["parse", "fromstring"]) | ||
|
||
|
||
class TestLxmlSafeParsing(BaseSemgrepCodemodTest): | ||
codemod = LxmlSafeParsing | ||
|
||
def test_name(self): | ||
assert self.codemod.name() == "safe-lxml-parsing" | ||
|
||
@each_func | ||
def test_import(self, tmpdir, func): | ||
input_code = f"""import lxml.etree | ||
|
||
lxml.etree.{func}("path_to_file") | ||
var = "hello" | ||
""" | ||
expexted_output = f"""import lxml.etree | ||
|
||
lxml.etree.{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False)) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@each_func | ||
def test_from_import(self, tmpdir, func): | ||
input_code = f"""from lxml.etree import {func} | ||
|
||
{func}("path_to_file") | ||
var = "hello" | ||
""" | ||
expexted_output = f"""from lxml.etree import {func} | ||
import lxml.etree | ||
|
||
{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False)) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@each_func | ||
def test_from_import_module(self, tmpdir, func): | ||
input_code = f"""from lxml import etree | ||
|
||
etree.{func}("path_to_file") | ||
var = "hello" | ||
""" | ||
expexted_output = f"""from lxml import etree | ||
import lxml.etree | ||
|
||
etree.{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False)) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@each_func | ||
def test_import_alias(self, tmpdir, func): | ||
input_code = f"""from lxml.etree import {func} as func | ||
|
||
func("path_to_file") | ||
var = "hello" | ||
""" | ||
expexted_output = f"""from lxml.etree import {func} as func | ||
import lxml.etree | ||
|
||
func("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False)) | ||
var = "hello" | ||
""" | ||
|
||
self.run_and_assert(tmpdir, input_code, expexted_output) | ||
|
||
@pytest.mark.parametrize( | ||
"input_args,expected_args", | ||
[ | ||
( | ||
"'str', parser=None", | ||
"'str', parser=lxml.etree.XMLParser(resolve_entities=False)", | ||
), | ||
( | ||
"source, base_url='url', parser=None", | ||
"source, base_url='url', parser=lxml.etree.XMLParser(resolve_entities=False)", | ||
), | ||
( | ||
"source, parser=lxml.etree.XMLParser(resolve_entities=False)", | ||
"source, parser=lxml.etree.XMLParser(resolve_entities=False)", | ||
), | ||
# This case would be changed by `safe-lxml-parser-defaults` codemod | ||
( | ||
"source, parser=lxml.etree.XMLParser()", | ||
"source, parser=lxml.etree.XMLParser()", | ||
), | ||
], | ||
) | ||
@each_func | ||
def test_verify_variations(self, tmpdir, func, input_args, expected_args): | ||
input_code = f"""import lxml.etree | ||
lxml.etree.{func}({input_args}) | ||
var = "hello" | ||
""" | ||
expexted_output = f"""import lxml.etree | ||
lxml.etree.{func}({expected_args}) | ||
var = "hello" | ||
""" | ||
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import lxml | ||
import lxml.etree | ||
parser = lxml.etree.XMLParser() |
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,3 @@ | ||
import lxml.etree | ||
lxml.etree.parse("path_to_file") | ||
lxml.etree.fromstring("xml_str") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️