-
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.
* semgrep from core codemod to allow list of rules * new harden pyyaml semgrep codemod
- Loading branch information
1 parent
1033d88
commit efbbc77
Showing
9 changed files
with
200 additions
and
23 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
14 changes: 11 additions & 3 deletions
14
src/core_codemods/semgrep/semgrep_django_secure_set_cookie.py
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,11 +1,19 @@ | ||
from codemodder.codemods.base_codemod import ToolRule | ||
from core_codemods.defectdojo.semgrep.django_secure_set_cookie import ( | ||
DjangoSecureSetCookie, | ||
) | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id | ||
|
||
SemgrepDjangoSecureSetCookie = SemgrepCodemod.from_core_codemod( | ||
name="django-secure-set-cookie", | ||
other=DjangoSecureSetCookie, | ||
rule_id="python.django.security.audit.secure-cookies.django-secure-set-cookie", | ||
rule_name="django-secure-set-cookie", | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.django.security.audit.secure-cookies.django-secure-set-cookie" | ||
), | ||
name="django-secure-set-cookie", | ||
url=semgrep_url_from_id(rule_id), | ||
) | ||
], | ||
) |
14 changes: 11 additions & 3 deletions
14
src/core_codemods/semgrep/semgrep_enable_jinja2_autoescape.py
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,9 +1,17 @@ | ||
from codemodder.codemods.base_codemod import ToolRule | ||
from core_codemods.enable_jinja2_autoescape import EnableJinja2Autoescape | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id | ||
|
||
SemgrepEnableJinja2Autoescape = SemgrepCodemod.from_core_codemod( | ||
name="enable-jinja2-autoescape", | ||
other=EnableJinja2Autoescape, | ||
rule_id="python.flask.security.xss.audit.direct-use-of-jinja2.direct-use-of-jinja2", | ||
rule_name="direct-use-of-jinja2", | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.flask.security.xss.audit.direct-use-of-jinja2.direct-use-of-jinja2" | ||
), | ||
name="direct-use-of-jinja2", | ||
url=semgrep_url_from_id(rule_id), | ||
) | ||
], | ||
) |
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,24 @@ | ||
from codemodder.codemods.base_codemod import ToolRule | ||
from core_codemods.harden_pyyaml import HardenPyyaml | ||
from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id | ||
|
||
SemgrepHardenPyyaml = SemgrepCodemod.from_core_codemod( | ||
name="harden-pyyaml", | ||
other=HardenPyyaml, | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.lang.security.deserialization.avoid-pyyaml-load.avoid-pyyaml-load" | ||
), | ||
name=" avoid-pyyaml-load", | ||
url=semgrep_url_from_id(rule_id), | ||
), | ||
ToolRule( | ||
id=( | ||
rule_id := "python.django.security.audit.avoid-insecure-deserialization.avoid-insecure-deserialization" | ||
), | ||
name="avoid-insecure-deserialization", | ||
url=semgrep_url_from_id(rule_id), | ||
), | ||
], | ||
) |
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,14 +1,22 @@ | ||
from codemodder.codemods.base_codemod import ToolRule | ||
from codemodder.codemods.libcst_transformer import LibcstTransformerPipeline | ||
from core_codemods.jwt_decode_verify import ( | ||
JwtDecodeVerify, | ||
JwtDecodeVerifySASTTransformer, | ||
) | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id | ||
|
||
SemgrepJwtDecodeVerify = SemgrepCodemod.from_core_codemod( | ||
name="jwt-decode-verify", | ||
other=JwtDecodeVerify, | ||
rule_id="python.jwt.security.unverified-jwt-decode.unverified-jwt-decode", | ||
rule_name="unverified-jwt-decode", | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.jwt.security.unverified-jwt-decode.unverified-jwt-decode" | ||
), | ||
name="unverified-jwt-decode", | ||
url=semgrep_url_from_id(rule_id), | ||
) | ||
], | ||
transformer=LibcstTransformerPipeline(JwtDecodeVerifySASTTransformer), | ||
) |
14 changes: 11 additions & 3 deletions
14
src/core_codemods/semgrep/semgrep_subprocess_shell_false.py
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,9 +1,17 @@ | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from codemodder.codemods.base_codemod import ToolRule | ||
from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id | ||
from core_codemods.subprocess_shell_false import SubprocessShellFalse | ||
|
||
SemgrepSubprocessShellFalse = SemgrepCodemod.from_core_codemod( | ||
name="subprocess-shell-false", | ||
other=SubprocessShellFalse, | ||
rule_id="python.lang.security.audit.subprocess-shell-true.subprocess-shell-true", | ||
rule_name="subprocess-shell-true", | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.lang.security.audit.subprocess-shell-true.subprocess-shell-true" | ||
), | ||
name="subprocess-shell-true", | ||
url=semgrep_url_from_id(rule_id), | ||
) | ||
], | ||
) |
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,124 @@ | ||
import json | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from core_codemods.semgrep.semgrep_harden_pyyaml import SemgrepHardenPyyaml | ||
|
||
|
||
class TestSemgrepHardenPyyaml(BaseSASTCodemodTest): | ||
codemod = SemgrepHardenPyyaml | ||
tool = "semgrep" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "harden-pyyaml" | ||
|
||
def test_pyyaml(self, tmpdir): | ||
input_code = """\ | ||
import yaml | ||
data = b'!!python/object/apply:subprocess.Popen \\n- ls' | ||
deserialized_data = yaml.load(data, Loader=yaml.Loader) | ||
""" | ||
expected_output = """\ | ||
import yaml | ||
data = b'!!python/object/apply:subprocess.Popen \\n- ls' | ||
deserialized_data = yaml.load(data, Loader=yaml.SafeLoader) | ||
""" | ||
|
||
results = { | ||
"runs": [ | ||
{ | ||
"results": [ | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "123"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 56, | ||
"endLine": 3, | ||
"snippet": { | ||
"text": "deserialized_data = yaml.load(data, Loader=yaml.Loader)" | ||
}, | ||
"startColumn": 21, | ||
"startLine": 3, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "Detected a possible YAML deserialization vulnerability. `yaml.unsafe_load`, `yaml.Loader`, `yaml.CLoader`, and `yaml.UnsafeLoader` are all known to be unsafe methods of deserializing YAML. An attacker with control over the YAML input could create special YAML input that allows the attacker to run arbitrary Python code. This would allow the attacker to steal files, download and install malware, or otherwise take over the machine. Use `yaml.safe_load` or `yaml.SafeLoader` instead." | ||
}, | ||
"properties": {}, | ||
"ruleId": "python.lang.security.deserialization.avoid-pyyaml-load.avoid-pyyaml-load", | ||
} | ||
] | ||
} | ||
] | ||
} | ||
self.run_and_assert( | ||
tmpdir, | ||
input_code, | ||
expected_output, | ||
results=json.dumps(results), | ||
) | ||
|
||
def test_pyyaml_django(self, tmpdir): | ||
input_code = """\ | ||
import yaml | ||
def index(request): | ||
cookie = request.cookies.get('cookie') | ||
return "Hey there! {}!".format(yaml.load(cookie)) | ||
""" | ||
expected_output = """\ | ||
import yaml | ||
def index(request): | ||
cookie = request.cookies.get('cookie') | ||
return "Hey there! {}!".format(yaml.load(cookie, Loader=yaml.SafeLoader)) | ||
""" | ||
|
||
results = { | ||
"runs": [ | ||
{ | ||
"results": [ | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "123"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 53, | ||
"endLine": 5, | ||
"snippet": { | ||
"text": ' return "Hey there! {}!".format(yaml.load(cookie))' | ||
}, | ||
"startColumn": 36, | ||
"startLine": 5, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "Avoid using insecure deserialization library, backed by `pickle`, `_pickle`, `cpickle`, `dill`, `shelve`, or `yaml`, which are known to lead to remote code execution vulnerabilities." | ||
}, | ||
"properties": {}, | ||
"ruleId": "python.django.security.audit.avoid-insecure-deserialization.avoid-insecure-deserialization", | ||
} | ||
] | ||
} | ||
] | ||
} | ||
self.run_and_assert( | ||
tmpdir, | ||
input_code, | ||
expected_output, | ||
results=json.dumps(results), | ||
) |