Skip to content

Commit

Permalink
fix import lxml to import lxml.etree
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 6, 2023
1 parent 4cd522c commit 6cc4453
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion integration_tests/test_lxml_safe_parser_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class TestLxmlSafeParserDefaults(BaseIntegrationTest):
original_code, expected_new_code = original_and_expected_from_code_path(
code_path, [(1, "parser = lxml.etree.XMLParser(resolve_entities=False)\n")]
)
expected_diff = "--- \n+++ \n@@ -1,2 +1,2 @@\n import lxml\n-parser = lxml.etree.XMLParser()\n+parser = lxml.etree.XMLParser(resolve_entities=False)\n"
expected_diff = "--- \n+++ \n@@ -1,2 +1,2 @@\n import lxml.etree\n-parser = lxml.etree.XMLParser()\n+parser = lxml.etree.XMLParser(resolve_entities=False)\n"
expected_line_change = "2"
change_description = LxmlSafeParserDefaults.CHANGE_DESCRIPTION
2 changes: 1 addition & 1 deletion integration_tests/test_lxml_safe_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestLxmlSafeParsing(BaseIntegrationTest):
),
],
)
expected_diff = '--- \n+++ \n@@ -1,3 +1,3 @@\n import lxml\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_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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Parameter `resolve_entities` has an unsafe default value of `True`. This codemod
The changes look as follows:

```diff
import lxml
import lxml.etree

- parser = lxml.etree.XMLParser()
- parser = lxml.etree.XMLParser(resolve_entities=True)
Expand Down
2 changes: 1 addition & 1 deletion src/core_codemods/docs/pixee_python_safe-lxml-parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ attacks and external entity (XXE) attacks.
The changes look as follows:

```diff
import lxml
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))
Expand Down
2 changes: 1 addition & 1 deletion src/core_codemods/lxml_safe_parser_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def rule(cls):
- pattern: XMLTreeBuilder
- pattern: XMLPullParser
- pattern-inside: |
import lxml
import lxml.etree
...
"""

Expand Down
6 changes: 3 additions & 3 deletions src/core_codemods/lxml_safe_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def rule(cls):
- pattern: parse
- pattern: fromstring
- pattern-inside: |
import lxml
import lxml.etree
...
- patterns:
- pattern: lxml.etree.$FUNC(..., parser=None, ...)
Expand All @@ -37,13 +37,13 @@ def rule(cls):
- pattern: parse
- pattern: fromstring
- pattern-inside: |
import lxml
import lxml.etree
...
"""

def on_result_found(self, original_node, updated_node):
self.remove_unused_import(original_node)
self.add_needed_import("lxml")
self.add_needed_import("lxml.etree")
safe_parser = "lxml.etree.XMLParser(resolve_entities=False)"
new_args = self.replace_args(
original_node,
Expand Down
8 changes: 4 additions & 4 deletions tests/codemods/test_lxml_safe_parameter_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def test_name(self):

@each_class
def test_import(self, tmpdir, klass):
input_code = f"""import lxml
input_code = f"""import lxml.etree
parser = lxml.etree.{klass}()
var = "hello"
"""
expexted_output = f"""import lxml
expexted_output = f"""import lxml.etree
parser = lxml.etree.{klass}(resolve_entities=False)
var = "hello"
Expand Down Expand Up @@ -104,11 +104,11 @@ def test_import_alias(self, tmpdir, klass):
)
@each_class
def test_verify_variations(self, tmpdir, klass, input_args, expected_args):
input_code = f"""import lxml
input_code = f"""import lxml.etree
parser = lxml.etree.{klass}({input_args})
var = "hello"
"""
expexted_output = f"""import lxml
expexted_output = f"""import lxml.etree
parser = lxml.etree.{klass}({expected_args})
var = "hello"
"""
Expand Down
14 changes: 7 additions & 7 deletions tests/codemods/test_lxml_safe_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def test_name(self):

@each_func
def test_import(self, tmpdir, func):
input_code = f"""import lxml
input_code = f"""import lxml.etree
lxml.etree.{func}("path_to_file")
var = "hello"
"""
expexted_output = f"""import lxml
expexted_output = f"""import lxml.etree
lxml.etree.{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))
var = "hello"
Expand All @@ -34,7 +34,7 @@ def test_from_import(self, tmpdir, func):
var = "hello"
"""
expexted_output = f"""from lxml.etree import {func}
import lxml
import lxml.etree
{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))
var = "hello"
Expand All @@ -50,7 +50,7 @@ def test_from_import_module(self, tmpdir, func):
var = "hello"
"""
expexted_output = f"""from lxml import etree
import lxml
import lxml.etree
etree.{func}("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))
var = "hello"
Expand All @@ -66,7 +66,7 @@ def test_import_alias(self, tmpdir, func):
var = "hello"
"""
expexted_output = f"""from lxml.etree import {func} as func
import lxml
import lxml.etree
func("path_to_file", parser=lxml.etree.XMLParser(resolve_entities=False))
var = "hello"
Expand Down Expand Up @@ -98,11 +98,11 @@ def test_import_alias(self, tmpdir, func):
)
@each_func
def test_verify_variations(self, tmpdir, func, input_args, expected_args):
input_code = f"""import lxml
input_code = f"""import lxml.etree
lxml.etree.{func}({input_args})
var = "hello"
"""
expexted_output = f"""import lxml
expexted_output = f"""import lxml.etree
lxml.etree.{func}({expected_args})
var = "hello"
"""
Expand Down

0 comments on commit 6cc4453

Please sign in to comment.