-
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
Codemod Lxml parser defaults #61
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
323b320
lxml parser defaults codemod
clavedeluna f78ac36
generalize add arg to add args
clavedeluna 8507fcf
add integration test
clavedeluna 4a6b721
add docs
clavedeluna 71cc68a
add namedtuple NewArg
clavedeluna 6ac3925
fix summary, desc, and add test
clavedeluna 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
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.
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.
I like this, I think it makes sense.