Skip to content

Commit f4e0abd

Browse files
committed
Python: Modernise django library
1 parent 18b28b1 commit f4e0abd

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

python/ql/src/semmle/python/web/django/Db.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class DjangoDbCursor extends DbCursor {
1111

1212
}
1313

14-
private Object theDjangoConnectionObject() {
15-
ModuleObject::named("django.db").attr("connection") = result
14+
private Value theDjangoConnectionObject() {
15+
result = Value::named("django.db.connection")
1616
}
1717

1818
/** A kind of taint source representing sources of django cursor objects.
@@ -22,7 +22,7 @@ class DjangoDbCursorSource extends DbConnectionSource {
2222
DjangoDbCursorSource() {
2323
exists(AttrNode cursor |
2424
this.(CallNode).getFunction()= cursor and
25-
cursor.getObject("cursor").refersTo(theDjangoConnectionObject())
25+
cursor.getObject("cursor").pointsTo(theDjangoConnectionObject())
2626
)
2727
}
2828

@@ -37,8 +37,8 @@ class DjangoDbCursorSource extends DbConnectionSource {
3737
}
3838

3939

40-
ClassObject theDjangoRawSqlClass() {
41-
result = ModuleObject::named("django.db.models.expressions").attr("RawSQL")
40+
ClassValue theDjangoRawSqlClass() {
41+
result = Value::named("django.db.models.expressions.RawSQL")
4242
}
4343

4444
/**

python/ql/src/semmle/python/web/django/Model.qll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import semmle.python.web.Http
66
import semmle.python.security.injection.Sql
77

88
/** A django model class */
9-
class DjangoModel extends ClassObject {
9+
class DjangoModel extends ClassValue {
1010

1111
DjangoModel() {
12-
ModuleObject::named("django.db.models").attr("Model") = this.getAnImproperSuperType()
12+
Value::named("django.db.models.Model") = this.getASuperType()
1313
}
1414

1515
}
@@ -55,7 +55,7 @@ class DjangoDbTableObjects extends TaintKind {
5555
class DjangoModelObjects extends TaintSource {
5656

5757
DjangoModelObjects() {
58-
this.(AttrNode).isLoad() and this.(AttrNode).getObject("objects").refersTo(any(DjangoModel m))
58+
this.(AttrNode).isLoad() and this.(AttrNode).getObject("objects").pointsTo(any(DjangoModel m))
5959
}
6060

6161
override predicate isSourceOf(TaintKind kind) {
@@ -73,7 +73,7 @@ class DjangoModelFieldWrite extends SqlInjectionSink {
7373

7474
DjangoModelFieldWrite() {
7575
exists(AttrNode attr, DjangoModel model |
76-
this = attr and attr.isStore() and attr.getObject(_).refersTo(model)
76+
this = attr and attr.isStore() and attr.getObject(_).pointsTo(model)
7777
)
7878
}
7979

@@ -87,7 +87,7 @@ class DjangoModelFieldWrite extends SqlInjectionSink {
8787

8888
}
8989

90-
/** A direct reference to a django model object, which is a vulnerable to external data. */
90+
/** A direct reference to a django model object, which is vulnerable to external data. */
9191
class DjangoModelDirectObjectReference extends TaintSink {
9292

9393
DjangoModelDirectObjectReference() {
@@ -111,7 +111,6 @@ class DjangoModelDirectObjectReference extends TaintSink {
111111
* A call to the `raw` method on a django model. This allows a raw SQL query
112112
* to be sent to the database, which is a security risk.
113113
*/
114-
115114
class DjangoModelRawCall extends SqlInjectionSink {
116115

117116
DjangoModelRawCall() {
@@ -135,8 +134,6 @@ class DjangoModelRawCall extends SqlInjectionSink {
135134
* A call to the `extra` method on a django model. This allows a raw SQL query
136135
* to be sent to the database, which is a security risk.
137136
*/
138-
139-
140137
class DjangoModelExtraCall extends SqlInjectionSink {
141138

142139
DjangoModelExtraCall() {

python/ql/src/semmle/python/web/django/Request.qll

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ abstract class DjangoRequestSource extends HttpRequestTaintSource {
6767
private class DjangoFunctionBasedViewRequestArgument extends DjangoRequestSource {
6868

6969
DjangoFunctionBasedViewRequestArgument() {
70-
exists(FunctionObject view |
70+
exists(FunctionValue view |
7171
url_dispatch(_, _, view) and
72-
this = view.getFunction().getArg(0).asName().getAFlowNode()
72+
this = view.getScope().getArg(0).asName().getAFlowNode()
7373
)
7474
}
7575

@@ -79,23 +79,24 @@ private class DjangoFunctionBasedViewRequestArgument extends DjangoRequestSource
7979
* https://docs.djangoproject.com/en/1.11/topics/class-based-views/
8080
*
8181
*/
82-
private class DjangoView extends ClassObject {
82+
private class DjangoView extends ClassValue {
8383

8484
DjangoView() {
85-
ModuleObject::named("django.views.generic").attr("View") = this.getAnImproperSuperType()
85+
Value::named("django.views.generic.View") = this.getASuperType()
8686
}
87+
8788
}
8889

89-
private FunctionObject djangoViewHttpMethod() {
90+
private FunctionValue djangoViewHttpMethod() {
9091
exists(DjangoView view |
91-
view.lookupAttribute(httpVerbLower()) = result
92+
view.attr(httpVerbLower()) = result
9293
)
9394
}
9495

9596
class DjangoClassBasedViewRequestArgument extends DjangoRequestSource {
9697

9798
DjangoClassBasedViewRequestArgument() {
98-
this = djangoViewHttpMethod().getFunction().getArg(1).asName().getAFlowNode()
99+
this = djangoViewHttpMethod().getScope().getArg(1).asName().getAFlowNode()
99100
}
100101

101102
}
@@ -107,11 +108,11 @@ class DjangoClassBasedViewRequestArgument extends DjangoRequestSource {
107108

108109

109110
/* Function based views */
110-
predicate url_dispatch(CallNode call, ControlFlowNode regex, FunctionObject view) {
111-
exists(FunctionObject url |
112-
ModuleObject::named("django.conf.urls").attr("url") = url and
111+
predicate url_dispatch(CallNode call, ControlFlowNode regex, FunctionValue view) {
112+
exists(FunctionValue url |
113+
Value::named("django.conf.urls.url") = url and
113114
url.getArgumentForCall(call, 0) = regex and
114-
url.getArgumentForCall(call, 1).refersTo(view)
115+
url.getArgumentForCall(call, 1).pointsTo(view)
115116
)
116117
}
117118

@@ -130,7 +131,7 @@ class UrlRouting extends CallNode {
130131
url_dispatch(this, _, _)
131132
}
132133

133-
FunctionObject getViewFunction() {
134+
FunctionValue getViewFunction() {
134135
url_dispatch(this, _, result)
135136
}
136137

@@ -149,7 +150,7 @@ class HttpRequestParameter extends HttpRequestTaintSource {
149150
HttpRequestParameter() {
150151
exists(UrlRouting url |
151152
this.(ControlFlowNode).getNode() =
152-
url.getViewFunction().getFunction().getArgByName(url.getNamedArgument())
153+
url.getViewFunction().getScope().getArgByName(url.getNamedArgument())
153154
)
154155
}
155156

python/ql/src/semmle/python/web/django/Response.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ class DjangoResponse extends TaintKind {
1717

1818
}
1919

20-
private ClassObject theDjangoHttpResponseClass() {
21-
result = ModuleObject::named("django.http.response").attr("HttpResponse") and
20+
private ClassValue theDjangoHttpResponseClass() {
21+
result = Value::named("django.http.response.HttpResponse") and
2222
not result = theDjangoHttpRedirectClass()
2323
}
2424

2525
/** Instantiation of a django response. */
2626
class DjangoResponseSource extends TaintSource {
2727

2828
DjangoResponseSource() {
29-
exists(ClassObject cls |
30-
cls.getAnImproperSuperType() = theDjangoHttpResponseClass() and
29+
exists(ClassValue cls |
30+
cls.getASuperType() = theDjangoHttpResponseClass() and
3131
cls.getACall() = this
3232
)
3333
}
@@ -64,9 +64,9 @@ class DjangoResponseWrite extends HttpResponseTaintSink {
6464
class DjangoResponseContent extends HttpResponseTaintSink {
6565

6666
DjangoResponseContent() {
67-
exists(CallNode call, ClassObject cls |
68-
cls.getAnImproperSuperType() = theDjangoHttpResponseClass() and
69-
call.getFunction().refersTo(cls) |
67+
exists(CallNode call, ClassValue cls |
68+
cls.getASuperType() = theDjangoHttpResponseClass() and
69+
call.getFunction().pointsTo(cls) |
7070
call.getArg(0) = this
7171
or
7272
call.getArgByName("content") = this
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import python
22

3-
FunctionObject redirect() {
4-
result = ModuleObject::named("django.shortcuts").attr("redirect")
3+
/** django.shortcuts.redirect */
4+
FunctionValue redirect() {
5+
result = Value::named("django.shortcuts.redirect")
56
}
67

7-
ClassObject theDjangoHttpRedirectClass() {
8-
result = ModuleObject::named("django.http.response").attr("HttpResponseRedirectBase")
8+
ClassValue theDjangoHttpRedirectClass() {
9+
result = Value::named("django.http.response.HttpResponseRedirectBase")
910
}

0 commit comments

Comments
 (0)