Skip to content

Commit 7017bec

Browse files
committed
Python: Model CookieWrite for twisted
Had to split the call to `request.cookies.append` since inline expectation tests didn't like the expectation that contained `=` :(
1 parent 4606444 commit 7017bec

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,42 @@ private module Twisted {
247247

248248
override string getMimetypeDefault() { result = "text/html" }
249249
}
250+
251+
/**
252+
* A call to the `addCookie` function on a twisted request.
253+
*
254+
* See https://twistedmatrix.com/documents/21.2.0/api/twisted.web.http.Request.html#addCookie
255+
*/
256+
class TwistedRequestAddCookieCall extends HTTP::Server::CookieWrite::Range,
257+
DataFlow::MethodCallNode {
258+
TwistedRequestAddCookieCall() { this.calls(Twisted::Request::instance(), "addCookie") }
259+
260+
override DataFlow::Node getHeaderArg() { none() }
261+
262+
override DataFlow::Node getNameArg() { result in [this.getArg(0), this.getArgByName("k")] }
263+
264+
override DataFlow::Node getValueArg() { result in [this.getArg(1), this.getArgByName("v")] }
265+
}
266+
267+
/**
268+
* A call to `append` on the `cookies` attribute of a twisted request.
269+
*
270+
* See https://twistedmatrix.com/documents/21.2.0/api/twisted.web.http.Request.html#cookies
271+
*/
272+
class TwistedRequestCookiesAppendCall extends HTTP::Server::CookieWrite::Range,
273+
DataFlow::MethodCallNode {
274+
TwistedRequestCookiesAppendCall() {
275+
exists(DataFlow::AttrRead cookiesLookup |
276+
cookiesLookup.getObject() = Twisted::Request::instance() and
277+
cookiesLookup.getAttributeName() = "cookies" and
278+
this.calls(cookiesLookup, "append")
279+
)
280+
}
281+
282+
override DataFlow::Node getHeaderArg() { result = this.getArg(0) }
283+
284+
override DataFlow::Node getNameArg() { none() }
285+
286+
override DataFlow::Node getValueArg() { none() }
287+
}
250288
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ class CookieWriting(Resource):
5151
"""Examples of providing values in response that is not in the body
5252
"""
5353
def render_GET(self, request: Request): # $ requestHandler
54-
request.addCookie("key", "value") # $ MISSING: CookieWrite CookieName="key" CookieValue="value"
55-
request.addCookie(k="key", v="value") # $ MISSING: CookieWrite CookieName="key" CookieValue="value"
56-
request.cookies.append("key2=value") # $ MISSING: CookieWrite CookieRawHeader="key2=value2"
54+
request.addCookie("key", "value") # $ CookieWrite CookieName="key" CookieValue="value"
55+
request.addCookie(k="key", v="value") # $ CookieWrite CookieName="key" CookieValue="value"
56+
val = "key2=value"
57+
request.cookies.append(val) # $ CookieWrite CookieRawHeader=val
5758

5859
request.responseHeaders.addRawHeader("key", "value")
5960
request.setHeader("Set-Cookie", "key3=value3") # $ MISSING: CookieWrite CookieRawHeader="key3=value3"

0 commit comments

Comments
 (0)