-
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.
Showing
4 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from core_codemods.semgrep.api import SemgrepCodemod, ToolRule, semgrep_url_from_id | ||
from core_codemods.url_sandbox import UrlSandbox | ||
|
||
SemgrepUrlSandbox = SemgrepCodemod.from_core_codemod( | ||
name="url-sandbox", | ||
other=UrlSandbox, | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.django.security.injection.ssrf.ssrf-injection-requests.ssrf-injection-requests" | ||
), | ||
name="ssrf-injection-requests", | ||
url=semgrep_url_from_id(rule_id), | ||
), | ||
ToolRule( | ||
id=( | ||
rule_id := "python.flask.security.injection.ssrf-requests.ssrf-requests" | ||
), | ||
name="ssrf-requests", | ||
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,109 @@ | ||
import json | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from core_codemods.semgrep.semgrep_url_sandbox import SemgrepUrlSandbox | ||
|
||
|
||
class TestSemgrepUrlSandbox(BaseSASTCodemodTest): | ||
codemod = SemgrepUrlSandbox | ||
tool = "semgrep" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "url-sandbox" | ||
|
||
def test_url_sandbox(self, tmpdir): | ||
original_code = """\ | ||
import requests | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
@app.route("/example") | ||
def example(): | ||
url = request.args["url"] | ||
requests.get(url) | ||
""" | ||
|
||
new_code = """\ | ||
from flask import Flask, request | ||
from security import safe_requests | ||
app = Flask(__name__) | ||
@app.route("/example") | ||
def example(): | ||
url = request.args["url"] | ||
safe_requests.get(url) | ||
""" | ||
|
||
results = { | ||
"runs": [ | ||
{ | ||
"results": [ | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "370059975f"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 22, | ||
"endLine": 10, | ||
"snippet": { | ||
"text": ' url = request.args["url"]\n requests.get(url)' | ||
}, | ||
"startColumn": 5, | ||
"startLine": 9, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "Data from request object is passed to a new server-side request. This could lead to a server-side request forgery (SSRF). To mitigate, ensure that schemes and hosts are validated against an allowlist, do not forward the response to the user, and ensure proper authentication and transport-layer security in the proxied request. See https://owasp.org/www-community/attacks/Server_Side_Request_Forgery to learn more about SSRF vulnerabilities." | ||
}, | ||
"properties": {}, | ||
"ruleId": "python.django.security.injection.ssrf.ssrf-injection-requests.ssrf-injection-requests", | ||
}, | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "cd899"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 22, | ||
"endLine": 10, | ||
"snippet": { | ||
"text": " requests.get(url)" | ||
}, | ||
"startColumn": 5, | ||
"startLine": 10, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "Data from request object is passed to a new server-side request. This could lead to a server-side request forgery (SSRF). To mitigate, ensure that schemes and hosts are validated against an allowlist, do not forward the response to the user, and ensure proper authentication and transport-layer security in the proxied request." | ||
}, | ||
"properties": {}, | ||
"ruleId": "python.flask.security.injection.ssrf-requests.ssrf-requests", | ||
}, | ||
], | ||
} | ||
] | ||
} | ||
|
||
self.run_and_assert( | ||
tmpdir, | ||
original_code, | ||
new_code, | ||
results=json.dumps(results), | ||
) |