Skip to content

Commit 1a4845f

Browse files
authored
Python: Restrict types a bit
The `CallCfgNode` restrictions are familiar and useful. Restricting `InstanceSource` to extend `LocalSourceNode` is novel, but I think it makes sense. It will act as a good reminder to anyone extending `InstanceSource` that the node in question is a `LocalSourceNode`, which will be enforced by the return type of the internal type tracker anyway.
1 parent f93b68d commit 1a4845f

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ private module Tornado {
3232

3333
/** Provides models for the `tornado.web` module */
3434
module web {
35-
3635
/**
3736
* Provides models for the `tornado.web.RequestHandler` class and subclasses.
3837
*
@@ -75,7 +74,7 @@ private module Tornado {
7574
*
7675
* Use the predicate `RequestHandler::instance()` to get references to instances of the `tornado.web.RequestHandler` class or any subclass.
7776
*/
78-
abstract class InstanceSource extends DataFlow::Node { }
77+
abstract class InstanceSource extends DataFlow::LocalSourceNode { }
7978

8079
/** The `self` parameter in a method on the `tornado.web.RequestHandler` class or any subclass. */
8180
private class SelfParam extends InstanceSource, RemoteFlowSource::Range,
@@ -120,7 +119,9 @@ private module Tornado {
120119
}
121120

122121
/** Gets a reference to one of the methods `get_arguments`, `get_body_arguments`, `get_query_arguments`. */
123-
DataFlow::Node argumentsMethod() { result = argumentsMethod(DataFlow::TypeTracker::end()) }
122+
DataFlow::Node argumentsMethod() {
123+
argumentsMethod(DataFlow::TypeTracker::end()).flowsTo(result)
124+
}
124125

125126
/** Gets a reference the `redirect` method. */
126127
private DataFlow::LocalSourceNode redirectMethod(DataFlow::TypeTracker t) {
@@ -198,12 +199,10 @@ private module Tornado {
198199
*
199200
* Use the predicate `Application::instance()` to get references to instances of `tornado.web.Application`.
200201
*/
201-
abstract class InstanceSource extends DataFlow::Node { }
202+
abstract class InstanceSource extends DataFlow::LocalSourceNode { }
202203

203204
/** A direct instantiation of `tornado.web.Application`. */
204-
class ClassInstantiation extends InstanceSource, DataFlow::CfgNode {
205-
override CallNode node;
206-
205+
class ClassInstantiation extends InstanceSource, DataFlow::CallCfgNode {
207206
ClassInstantiation() { this = classRef().getACall() }
208207
}
209208

@@ -239,7 +238,6 @@ private module Tornado {
239238

240239
/** Provides models for the `tornado.httputil` module */
241240
module httputil {
242-
243241
/**
244242
* Provides models for the `tornado.httputil.HttpServerRequest` class
245243
*
@@ -258,12 +256,10 @@ private module Tornado {
258256
*
259257
* Use the predicate `HttpServerRequest::instance()` to get references to instances of `tornado.httputil.HttpServerRequest`.
260258
*/
261-
abstract class InstanceSource extends DataFlow::Node { }
259+
abstract class InstanceSource extends DataFlow::LocalSourceNode { }
262260

263261
/** A direct instantiation of `tornado.httputil.HttpServerRequest`. */
264-
private class ClassInstantiation extends InstanceSource, DataFlow::CfgNode {
265-
override CallNode node;
266-
262+
private class ClassInstantiation extends InstanceSource, DataFlow::CallCfgNode {
267263
ClassInstantiation() { this = classRef().getACall() }
268264
}
269265

@@ -277,7 +273,6 @@ private module Tornado {
277273

278274
/** Gets a reference to an instance of `tornado.httputil.HttpServerRequest`. */
279275
DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) }
280-
281276

282277
/** Gets a reference to the `full_url` method. */
283278
private DataFlow::LocalSourceNode full_url(DataFlow::TypeTracker t) {
@@ -430,11 +425,9 @@ private module Tornado {
430425
* See https://www.tornadoweb.org/en/stable/web.html?highlight=write#tornado.web.RequestHandler.redirect
431426
*/
432427
private class TornadoRequestHandlerRedirectCall extends HTTP::Server::HttpRedirectResponse::Range,
433-
DataFlow::CfgNode {
434-
override CallNode node;
435-
428+
DataFlow::CallCfgNode {
436429
TornadoRequestHandlerRedirectCall() {
437-
node.getFunction() = tornado::web::RequestHandler::redirectMethod().asCfgNode()
430+
this.getFunction() = tornado::web::RequestHandler::redirectMethod()
438431
}
439432

440433
override DataFlow::Node getRedirectLocation() {
@@ -454,11 +447,9 @@ private module Tornado {
454447
* See https://www.tornadoweb.org/en/stable/web.html?highlight=write#tornado.web.RequestHandler.write
455448
*/
456449
private class TornadoRequestHandlerWriteCall extends HTTP::Server::HttpResponse::Range,
457-
DataFlow::CfgNode {
458-
override CallNode node;
459-
450+
DataFlow::CallCfgNode {
460451
TornadoRequestHandlerWriteCall() {
461-
node.getFunction() = tornado::web::RequestHandler::writeMethod().asCfgNode()
452+
this.getFunction() = tornado::web::RequestHandler::writeMethod()
462453
}
463454

464455
override DataFlow::Node getBody() {

0 commit comments

Comments
 (0)