Skip to content

Commit 23f668f

Browse files
committed
Python: Model redirects in twisted
1 parent a210391 commit 23f668f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

python/ql/src/semmle/python/frameworks/Twisted.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,33 @@ private module Twisted {
226226

227227
override string getMimetypeDefault() { result = "text/html" }
228228
}
229+
230+
/**
231+
* A call to the `redirect` function on a twisted request.
232+
*
233+
* See https://twistedmatrix.com/documents/21.2.0/api/twisted.web.http.Request.html#redirect
234+
*/
235+
class TwistedRequestRedirectCall extends HTTP::Server::HttpRedirectResponse::Range,
236+
DataFlow::CallCfgNode {
237+
TwistedRequestRedirectCall() {
238+
// TODO: When we have tools that make it easy, model these properly to handle
239+
// `meth = obj.meth; meth()`. Until then, we'll use this more syntactic approach
240+
// (since it allows us to at least capture the most common cases).
241+
exists(DataFlow::AttrRead read |
242+
this.getFunction() = read and
243+
read.getObject() = Request::instance() and
244+
read.getAttributeName() = "redirect"
245+
)
246+
}
247+
248+
override DataFlow::Node getBody() { none() }
249+
250+
override DataFlow::Node getRedirectLocation() {
251+
result.asCfgNode() in [node.getArg(0), node.getArgByName("url")]
252+
}
253+
254+
override DataFlow::Node getMimetypeOrContentTypeArg() { none() }
255+
256+
override string getMimetypeDefault() { result = "text/html" }
257+
}
229258
}

python/ql/test/library-tests/frameworks/twisted/response_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def render(self, request: Request): # $ requestHandler
3838

3939
class Redirect(Resource):
4040
def render_GET(self, request: Request): # $ requestHandler
41-
request.redirect("/new-location") # $ MISSING: HttpRedirectResponse
41+
request.redirect("/new-location") # $ HttpRedirectResponse redirectLocation="/new-location" HttpResponse mimetype=text/html
4242
# By default, this `hello` output is not returned... not even when
4343
# requested with curl.
4444
return b"hello" # $ SPURIOUS: HttpResponse mimetype=text/html responseBody=b"hello"

0 commit comments

Comments
 (0)