-
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.
* lxml parser defaults codemod * generalize add arg to add args * add integration test * add docs * add namedtuple NewArg * fix summary, desc, and add test
- Loading branch information
1 parent
61beb53
commit 0790cc2
Showing
13 changed files
with
262 additions
and
38 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,16 @@ | ||
from core_codemods.lxml_safe_parser_defaults import LxmlSafeParserDefaults | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestLxmlSafeParserDefaults(BaseIntegrationTest): | ||
codemod = LxmlSafeParserDefaults | ||
code_path = "tests/samples/lxml_parser.py" | ||
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_line_change = "2" | ||
change_description = LxmlSafeParserDefaults.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
22 changes: 22 additions & 0 deletions
22
src/core_codemods/docs/pixee_python_safe-lxml-parser-defaults.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,22 @@ | ||
This codemod configures safe parameter values when initializing `lxml.etree.XMLParser`, `lxml.etree.ETCompatXMLParser`, | ||
`lxml.etree.XMLTreeBuilder`, or `lxml.etree.XMLPullParser`. If parameters `resolve_entities`, `no_network`, | ||
and `dtd_validation` are not set to safe values, your code may be vulnerable to entity expansion | ||
attacks and external entity (XXE) attacks. | ||
|
||
Parameters `no_network` and `dtd_validation` have safe default values of `True` and `False`, respectively, so this | ||
codemod will set each to the default safe value if your code has assigned either to an unsafe value. | ||
|
||
Parameter `resolve_entities` has an unsafe default value of `True`. This codemod will set `resolve_entities=False` if set to `True` or omitted. | ||
|
||
The changes look as follows: | ||
|
||
```diff | ||
import lxml | ||
|
||
- parser = lxml.etree.XMLParser() | ||
- parser = lxml.etree.XMLParser(resolve_entities=True) | ||
- parser = lxml.etree.XMLParser(resolve_entities=True, no_network=False, dtd_validation=True) | ||
+ parser = lxml.etree.XMLParser(resolve_entities=False) | ||
+ parser = lxml.etree.XMLParser(resolve_entities=False) | ||
+ parser = lxml.etree.XMLParser(resolve_entities=False, no_network=True, dtd_validation=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
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,43 @@ | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
from codemodder.codemods.api.helpers import NewArg | ||
|
||
|
||
class LxmlSafeParserDefaults(SemgrepCodemod): | ||
NAME = "safe-lxml-parser-defaults" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
SUMMARY = "Use safe defaults for lxml parsers" | ||
DESCRIPTION = "Replace lxml parser parameters with safe defaults" | ||
|
||
@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, | ||
[ | ||
NewArg(name="resolve_entities", value="False", add_if_missing=True), | ||
NewArg(name="no_network", value="True", add_if_missing=False), | ||
NewArg(name="dtd_validation", value="False", add_if_missing=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
Oops, something went wrong.