-
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.
* do not short circuit on entire tre * new semgrep use defusdxml * update docs * fix description
- Loading branch information
1 parent
0105c7d
commit c5b471c
Showing
6 changed files
with
136 additions
and
6 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
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,9 @@ | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from core_codemods.use_defused_xml import UseDefusedXml | ||
|
||
SemgrepUseDefusedXml = SemgrepCodemod.from_import_modifier_codemod( | ||
name="use-defusedxml", | ||
other=UseDefusedXml, | ||
rule_id="python.lang.security.use-defused-xml-parse.use-defused-xml-parse", | ||
rule_name="use-defused-xml-parse", | ||
) |
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,72 @@ | ||
import json | ||
|
||
import mock | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from codemodder.dependency import DefusedXML | ||
from core_codemods.semgrep.semgrep_use_defused_xml import SemgrepUseDefusedXml | ||
|
||
|
||
class TestSemgrepUseDefusedXml(BaseSASTCodemodTest): | ||
codemod = SemgrepUseDefusedXml | ||
tool = "semgrep" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "use-defusedxml" | ||
|
||
@mock.patch("codemodder.codemods.api.FileContext.add_dependency") | ||
def test_etree_parse(self, add_dependency, tmpdir): | ||
original_code = """\ | ||
from xml.etree.ElementTree import parse | ||
et = parse(user_input) | ||
""" | ||
|
||
new_code = """\ | ||
import defusedxml.ElementTree | ||
et = defusedxml.ElementTree.parse(user_input) | ||
""" | ||
|
||
results = { | ||
"runs": [ | ||
{ | ||
"results": [ | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "123"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 23, | ||
"endLine": 3, | ||
"snippet": { | ||
"text": "et = parse(user_input)" | ||
}, | ||
"startColumn": 6, | ||
"startLine": 3, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": 'The native Python `xml` library is vulnerable to XML External Entity (XXE) attacks. These attacks can leak confidential data and "XML bombs" can cause denial of service. Do not use this library to parse untrusted input. Instead the Python documentation recommends using `defusedxml`.' | ||
}, | ||
"ruleId": "python.lang.security.use-defused-xml-parse.use-defused-xml-parse", | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
self.run_and_assert( | ||
tmpdir, | ||
original_code, | ||
new_code, | ||
results=json.dumps(results), | ||
) | ||
add_dependency.assert_called_once_with(DefusedXML) |