-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Bottle Framework Support #17370
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
12 commits
Select commit
Hold shift + click to select a range
5c8c99d
Add header support for bottle and tornado
Kwstubbs 326eb69
Added
Kwstubbs 581e7f5
Bottle
Kwstubbs bd2564e
Formatting
Kwstubbs 6efb3c6
QLformatting
Kwstubbs 5d12f7b
Pre formatting Bottle tests
Kwstubbs 05765c4
Formatting
Kwstubbs b1c0e28
Extra test
Kwstubbs e6b7754
Remove redundant imports
Kwstubbs 02a6643
formatting
Kwstubbs 0483b80
Feedback
Kwstubbs ac411f1
Second round feedback
Kwstubbs 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: majorAnalysis | ||
--- | ||
* Added modeling of the `bottle` framework, leading to new remote flow sources and header writes |
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,175 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `bottle` PyPI package. | ||
* See https://bottlepy.org/docs/dev/. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.frameworks.internal.InstanceTaintStepsHelper | ||
private import semmle.python.frameworks.Stdlib | ||
|
||
/** | ||
* INTERNAL: Do not use. | ||
* | ||
* Provides models for the `bottle` PyPI package. | ||
* See https://bottlepy.org/docs/dev/. | ||
*/ | ||
module Bottle { | ||
/** Gets a reference to the `bottle` module. */ | ||
API::Node bottle() { result = API::moduleImport("bottle") } | ||
|
||
/** Provides models for the `bottle` module. */ | ||
module BottleModule { | ||
/** | ||
* Provides models for Bottle applications. | ||
*/ | ||
module App { | ||
/** Gets a reference to a Bottle application (an instance of `bottle.Bottle`) */ | ||
API::Node app() { result = bottle().getMember(["Bottle", "app"]).getReturn() } | ||
} | ||
|
||
/** Provides models for functions that are possible "views" */ | ||
module View { | ||
/** | ||
* A Bottle view callable, that handles incoming requests. | ||
*/ | ||
class ViewCallable extends Function { | ||
ViewCallable() { this = any(BottleRouteSetup rs).getARequestHandler() } | ||
} | ||
|
||
/** Get methods that reprsent a route in Bottle */ | ||
string routeMethods() { result = ["route", "get", "post", "put", "delete", "patch"] } | ||
|
||
private class BottleRouteSetup extends Http::Server::RouteSetup::Range, DataFlow::CallCfgNode { | ||
BottleRouteSetup() { | ||
this = | ||
[ | ||
App::app().getMember(routeMethods()).getACall(), | ||
bottle().getMember(routeMethods()).getACall() | ||
] | ||
} | ||
|
||
override DataFlow::Node getUrlPatternArg() { | ||
result in [this.getArg(0), this.getArgByName("route")] | ||
} | ||
|
||
override string getFramework() { result = "Bottle" } | ||
|
||
override Parameter getARoutedParameter() { none() } | ||
|
||
override Function getARequestHandler() { result.getADecorator().getAFlowNode() = node } | ||
} | ||
} | ||
|
||
/** Provides models for the `bottle.response` module */ | ||
module Response { | ||
/** Gets a reference to the `bottle.response` module or instantiation of Bottle Response class. */ | ||
API::Node response() { | ||
result = [bottle().getMember("response"), bottle().getMember("Response").getReturn()] | ||
} | ||
|
||
/** A response returned by a view callable. */ | ||
class BottleReturnResponse extends Http::Server::HttpResponse::Range { | ||
BottleReturnResponse() { | ||
this.asCfgNode() = any(View::ViewCallable vc).getAReturnValueFlowNode() | ||
} | ||
|
||
override DataFlow::Node getBody() { result = this } | ||
|
||
override DataFlow::Node getMimetypeOrContentTypeArg() { none() } | ||
|
||
override string getMimetypeDefault() { result = "text/html" } | ||
} | ||
|
||
/** | ||
* A call to the `bottle.BaseResponse.set_header` or `bottle.BaseResponse.add_header` method. | ||
* | ||
* See https://bottlepy.org/docs/dev/api.html#bottle.BaseResponse.set_header | ||
*/ | ||
class BottleResponseHandlerSetHeaderCall extends Http::Server::ResponseHeaderWrite::Range, | ||
DataFlow::MethodCallNode | ||
{ | ||
BottleResponseHandlerSetHeaderCall() { | ||
this = response().getMember(["set_header", "add_header"]).getACall() | ||
} | ||
|
||
override DataFlow::Node getNameArg() { | ||
result in [this.getArg(0), this.getArgByName("name")] | ||
} | ||
|
||
override DataFlow::Node getValueArg() { | ||
result in [this.getArg(1), this.getArgByName("value")] | ||
} | ||
|
||
override predicate nameAllowsNewline() { none() } | ||
|
||
override predicate valueAllowsNewline() { none() } | ||
} | ||
} | ||
|
||
/** Provides models for the `bottle.request` module */ | ||
module Request { | ||
/** Gets a reference to the `bottle.request` module. */ | ||
API::Node request() { result = bottle().getMember("request") } | ||
|
||
private class Request extends RemoteFlowSource::Range { | ||
Request() { this = request().asSource() } | ||
|
||
override string getSourceType() { result = "bottle.request" } | ||
} | ||
|
||
/** | ||
* Taint propagation for `bottle.request`. | ||
* | ||
* See https://bottlepy.org/docs/dev/api.html#bottle.request | ||
*/ | ||
private class InstanceTaintSteps extends InstanceTaintStepsHelper { | ||
InstanceTaintSteps() { this = "bottle.request" } | ||
|
||
override DataFlow::Node getInstance() { result = request().getAValueReachableFromSource() } | ||
|
||
override string getAttributeName() { | ||
result in [ | ||
"headers", "query", "forms", "params", "json", "url", "body", "fullpath", | ||
"query_string" | ||
] | ||
} | ||
|
||
override string getMethodName() { none() } | ||
|
||
override string getAsyncMethodName() { none() } | ||
} | ||
} | ||
|
||
/** Provides models for the `bottle.headers` module */ | ||
module Headers { | ||
/** Gets a reference to the `bottle.headers` module. */ | ||
API::Node headers() { result = bottle().getMember("response").getMember("headers") } | ||
|
||
/** A dict-like write to a response header. */ | ||
class HeaderWriteSubscript extends Http::Server::ResponseHeaderWrite::Range, DataFlow::Node { | ||
DataFlow::Node name; | ||
DataFlow::Node value; | ||
|
||
HeaderWriteSubscript() { | ||
exists(SubscriptNode subscript | | ||
this.asCfgNode() = subscript and | ||
value.asCfgNode() = subscript.(DefinitionNode).getValue() and | ||
name.asCfgNode() = subscript.getIndex() and | ||
subscript.getObject() = headers().asSource().asCfgNode() | ||
) | ||
} | ||
|
||
override DataFlow::Node getNameArg() { result = name } | ||
|
||
override DataFlow::Node getValueArg() { result = value } | ||
|
||
override predicate nameAllowsNewline() { none() } | ||
|
||
override predicate valueAllowsNewline() { none() } | ||
} | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/bottle/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/bottle/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
4 changes: 4 additions & 0 deletions
4
python/ql/test/library-tests/frameworks/bottle/InlineTaintTest.expected
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 @@ | ||
argumentToEnsureNotTaintedNotMarkedAsSpurious | ||
untaintedArgumentToEnsureTaintedNotMarkedAsMissing | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/bottle/InlineTaintTest.ql
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,2 @@ | ||
import experimental.meta.InlineTaintTest | ||
import MakeInlineTaintTest<TestTaintTrackingConfig> |
11 changes: 11 additions & 0 deletions
11
python/ql/test/library-tests/frameworks/bottle/basic_test.py
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,11 @@ | ||
# Source: https://bottlepy.org/docs/dev/tutorial.html#the-application-object | ||
from bottle import Bottle, run | ||
|
||
app = Bottle() | ||
|
||
@app.route('/hello') # $ routeSetup="/hello" | ||
def hello(): # $ requestHandler | ||
return "Hello World!" # $ HttpResponse responseBody="Hello World!" mimetype=text/html | ||
|
||
if __name__ == '__main__': | ||
app.run(host='localhost', port=8080) |
11 changes: 11 additions & 0 deletions
11
python/ql/test/library-tests/frameworks/bottle/header_test.py
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,11 @@ | ||
import bottle | ||
from bottle import Bottle, response, request | ||
|
||
app = Bottle() | ||
@app.route('/test', method=['OPTIONS', 'GET']) # $ routeSetup="/test" | ||
def test1(): # $ requestHandler | ||
response.headers['Content-type'] = 'application/json' # $ headerWriteName='Content-type' headerWriteValue='application/json' | ||
response.set_header('Content-type', 'application/json') # $ headerWriteName='Content-type' headerWriteValue='application/json' | ||
return '[1]' # $ HttpResponse responseBody='[1]' mimetype=text/html | ||
|
||
app.run() |
21 changes: 21 additions & 0 deletions
21
python/ql/test/library-tests/frameworks/bottle/taint_test.py
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,21 @@ | ||
import bottle | ||
from bottle import response, request | ||
|
||
|
||
app = bottle.app() | ||
@app.route('/test', method=['OPTIONS', 'GET']) # $ routeSetup="/test" | ||
def test1(): # $ requestHandler | ||
|
||
ensure_tainted( | ||
request.headers, # $ tainted | ||
request.headers, # $ tainted | ||
request.forms, # $ tainted | ||
request.params, # $ tainted | ||
request.url, # $ tainted | ||
request.body, # $ tainted | ||
request.fullpath, # $ tainted | ||
request.query_string # $ tainted | ||
) | ||
return '[1]' # $ HttpResponse mimetype=text/html responseBody='[1]' | ||
|
||
app.run() |
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.