-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: Add Server-side Request Forgery sinks #8275
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b23e28a
add Server-side Request Forgery sinks
haby0 be40b54
add test
haby0 40feb1f
Python: SPURIOUS results for httpx
RasmusWL 56901ea
Python: Make new SSRF sink modules private
RasmusWL 7d6d8be
Python: Fix httpx modeling
RasmusWL c65839b
Python: improve urllib3 modeling
RasmusWL 02a97b0
Python: Move `urllib` and `urllib2` to be part of stdlib modeling
RasmusWL 866e615
Python: Add PyPI links in qldocs
RasmusWL 75bc532
Python: Avoid `toString` usage :O
RasmusWL d86284b
Python: Update frameworks.rst
RasmusWL e47f726
Python: Add change-note
RasmusWL f620e25
Merge branch 'main' into py/add-ssrf-sinks
RasmusWL 7e6666b
Merge branch 'main' into py/add-ssrf-sinks
haby0 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 hidden or 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 hidden or 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,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Added new SSRF sinks for `httpx`, `pycurl`, `urllib`, `urllib2`, `urllib3`, and `libtaxii`. This improvement was [submitted by @haby0](https://github.com/github/codeql/pull/8275). |
This file contains hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `httpx` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/httpx/ | ||
* - https://www.python-httpx.org/ | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `httpx` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/httpx/ | ||
* - https://www.python-httpx.org/ | ||
*/ | ||
private module HttpxModel { | ||
private class RequestCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
string methodName; | ||
|
||
RequestCall() { | ||
methodName in [HTTP::httpVerbLower(), "request", "stream"] and | ||
this = API::moduleImport("httpx").getMember(methodName).getACall() | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { | ||
result = this.getArgByName("url") | ||
or | ||
if methodName in ["request", "stream"] | ||
then result = this.getArg(1) | ||
else result = this.getArg(0) | ||
} | ||
|
||
override string getFramework() { result = "httpx" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
|
||
/** | ||
* Provides models for the `httpx.[Async]Client` class | ||
* | ||
* See https://www.python-httpx.org/async/ | ||
*/ | ||
module Client { | ||
/** Get a reference to the `httpx.Client` or `httpx.AsyncClient` class. */ | ||
private API::Node classRef() { | ||
result = API::moduleImport("httpx").getMember(["Client", "AsyncClient"]) | ||
} | ||
|
||
/** Get a reference to an `httpx.Client` or `httpx.AsyncClient` instance. */ | ||
private API::Node instance() { result = classRef().getReturn() } | ||
|
||
/** A method call on a Client that sends off a request */ | ||
private class OutgoingRequestCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
string methodName; | ||
|
||
OutgoingRequestCall() { | ||
methodName in [HTTP::httpVerbLower(), "request", "stream"] and | ||
this = instance().getMember(methodName).getACall() | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { | ||
result = this.getArgByName("url") | ||
or | ||
if methodName in ["request", "stream"] | ||
then result = this.getArg(1) | ||
else result = this.getArg(0) | ||
} | ||
|
||
override string getFramework() { result = "httpx.[Async]Client" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,42 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `libtaxii` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/libtaxii/ | ||
* - https://github.com/TAXIIProject/libtaxii | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `libtaxii` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/libtaxii/ | ||
* - https://github.com/TAXIIProject/libtaxii | ||
*/ | ||
private module Libtaxii { | ||
/** | ||
* A call to `libtaxii.common.parse`. | ||
* When the `allow_url` parameter value is set to `True`, there is an SSRF vulnerability.. | ||
*/ | ||
private class ParseCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
ParseCall() { | ||
this = API::moduleImport("libtaxii").getMember("common").getMember("parse").getACall() and | ||
this.getArgByName("allow_url").getALocalSource().asExpr() = any(True t) | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { result in [this.getArg(0), this.getArgByName("s")] } | ||
|
||
override string getFramework() { result = "libtaxii.common.parse" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
} |
This file contains hidden or 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,59 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `pycurl` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/pycurl/ | ||
* - https://pycurl.io/docs/latest/ | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `pycurl` PyPI package. | ||
* | ||
* See | ||
* - https://pypi.org/project/pycurl/ | ||
* - https://pycurl.io/docs/latest/ | ||
*/ | ||
private module Pycurl { | ||
/** | ||
* Provides models for the `pycurl.Curl` class | ||
* | ||
* See https://pycurl.io/docs/latest/curl.html. | ||
*/ | ||
module Curl { | ||
/** Gets a reference to the `pycurl.Curl` class. */ | ||
private API::Node classRef() { result = API::moduleImport("pycurl").getMember("Curl") } | ||
|
||
/** Gets a reference to an instance of `pycurl.Curl`. */ | ||
private API::Node instance() { result = classRef().getReturn() } | ||
|
||
/** | ||
* When the first parameter value of the `setopt` function is set to `pycurl.URL`, | ||
* the second parameter value is the request resource link. | ||
* | ||
* See http://pycurl.io/docs/latest/curlobject.html#pycurl.Curl.setopt. | ||
*/ | ||
private class OutgoingRequestCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
OutgoingRequestCall() { | ||
this = instance().getMember("setopt").getACall() and | ||
this.getArg(0).asCfgNode().(AttrNode).getName() = "URL" | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { | ||
result in [this.getArg(1), this.getArgByName("value")] | ||
} | ||
|
||
override string getFramework() { result = "pycurl.Curl" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,71 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `urllib` module, part of | ||
* the Python standard library. | ||
* | ||
* See | ||
* - https://docs.python.org/2/library/urllib.html | ||
* - https://docs.python.org/3/library/urllib.html | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `urllib` module, part of | ||
* the Python standard library. | ||
* | ||
* See | ||
* - https://docs.python.org/2/library/urllib.html | ||
* - https://docs.python.org/3/library/urllib.html | ||
*/ | ||
private module Urllib { | ||
/** | ||
* Provides models for the `urllib.request` extension library | ||
* | ||
* See https://docs.python.org/3.9/library/urllib.request.html | ||
*/ | ||
module Request { | ||
/** | ||
* See | ||
* - https://docs.python.org/3.9/library/urllib.request.html#urllib.request.Request | ||
*/ | ||
private class RequestCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
RequestCall() { | ||
this = API::moduleImport("urllib").getMember("request").getMember("Request").getACall() | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { result in [this.getArg(0), this.getArgByName("url")] } | ||
|
||
override string getFramework() { result = "urllib.request.Request" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
|
||
/** | ||
* See | ||
* - https://docs.python.org/3.9/library/urllib.request.html#urllib.request.urlopen | ||
*/ | ||
private class UrlOpenCall extends HTTP::Client::Request::Range, DataFlow::CallCfgNode { | ||
UrlOpenCall() { | ||
this = API::moduleImport("urllib").getMember("request").getMember("urlopen").getACall() | ||
} | ||
|
||
override DataFlow::Node getAUrlPart() { result in [this.getArg(0), this.getArgByName("url")] } | ||
|
||
override string getFramework() { result = "urllib.request.urlopen" } | ||
|
||
override predicate disablesCertificateValidation( | ||
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin | ||
) { | ||
// TODO: Look into disabling certificate validation | ||
none() | ||
} | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.