Skip to content

Commit d6ec4d3

Browse files
committed
Python: Twisted refactor of getRequestParamIndex
1 parent 8208aeb commit d6ec4d3

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,28 @@ private module Twisted {
4141
// TODO: This doesn't handle attribute assignment. Should be OK, but analysis is not as complete as with
4242
// points-to and `.lookup`, which would handle `post = my_post_handler` inside class def
4343
result = this.getAMethod() and
44-
resourceMethodRequestParamIndex(result.getName(), _)
44+
exists(getRequestParamIndex(result.getName()))
4545
}
4646
}
4747

4848
/**
49-
* Holds if the request parameter is supposed to be at index `requestParamIndex` for
50-
* the method named `methodName` in `twisted.web.resource.Resource`.
49+
* Gets the index the request parameter is supposed to be at for the method named
50+
* `methodName` in a `twisted.web.resource.Resource` subclass.
5151
*/
5252
bindingset[methodName]
53-
private predicate resourceMethodRequestParamIndex(string methodName, int requestParamIndex) {
54-
methodName.matches("render_%") and requestParamIndex = 1
53+
private int getRequestParamIndex(string methodName) {
54+
methodName.matches("render_%") and result = 1
5555
or
56-
methodName in ["render", "listDynamicEntities", "getChildForRequest"] and requestParamIndex = 1
56+
methodName in ["render", "listDynamicEntities", "getChildForRequest"] and result = 1
5757
or
58-
methodName = ["getDynamicEntity", "getChild", "getChildWithDefault"] and requestParamIndex = 2
58+
methodName = ["getDynamicEntity", "getChild", "getChildWithDefault"] and result = 2
5959
}
6060

6161
/** A method that handles incoming requests, on a `twisted.web.resource.Resource` subclass. */
6262
class TwistedResourceRequestHandler extends HTTP::Server::RequestHandler::Range {
63-
TwistedResourceRequestHandler() {
64-
any(TwistedResourceSubclass cls).getAMethod() = this and
65-
resourceMethodRequestParamIndex(this.getName(), _)
66-
}
63+
TwistedResourceRequestHandler() { this = any(TwistedResourceSubclass cls).getARequestHandler() }
6764

68-
Parameter getRequestParameter() {
69-
exists(int i |
70-
resourceMethodRequestParamIndex(this.getName(), i) and
71-
result = this.getArg(i)
72-
)
73-
}
65+
Parameter getRequestParameter() { result = this.getArg(getRequestParamIndex(this.getName())) }
7466

7567
override Parameter getARoutedParameter() { none() }
7668

0 commit comments

Comments
 (0)