Skip to content

Commit 4f4dec5

Browse files
committed
Python: Model ResovlerMatch in Django
Like before, omitted ClassInstantiation
1 parent 6f0a622 commit 4f4dec5

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,47 @@ private module Django {
410410
UploadedFileFileLikeInstances() { this.(DataFlow::AttrRead).accesses(instance(), "file") }
411411
}
412412
}
413+
414+
/**
415+
* Provides models for the `django.urls.ResolverMatch` class
416+
*
417+
* See https://docs.djangoproject.com/en/3.0/ref/urlresolvers/#django.urls.ResolverMatch.
418+
*/
419+
module ResolverMatch {
420+
/**
421+
* A source of instances of `django.urls.ResolverMatch`, extend this class to model new instances.
422+
*
423+
* This can include instantiations of the class, return values from function
424+
* calls, or a special parameter that will be set when functions are called by an external
425+
* library.
426+
*
427+
* Use the predicate `ResolverMatch::instance()` to get references to instances of `django.urls.ResolverMatch`.
428+
*/
429+
abstract class InstanceSource extends DataFlow::LocalSourceNode { }
430+
431+
/** Gets a reference to an instance of `django.urls.ResolverMatch`. */
432+
private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) {
433+
t.start() and
434+
result instanceof InstanceSource
435+
or
436+
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
437+
}
438+
439+
/** Gets a reference to an instance of `django.urls.ResolverMatch`. */
440+
DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) }
441+
442+
/**
443+
* Taint propagation for `django.urls.ResolverMatch`.
444+
*/
445+
class ResolverMatchAdditionalTaintStep extends TaintTracking::AdditionalTaintStep {
446+
override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
447+
// Attributes
448+
nodeFrom = instance() and
449+
nodeTo.(DataFlow::AttrRead).getObject() = nodeFrom and
450+
nodeTo.(DataFlow::AttrRead).getAttributeName() in ["args", "kwargs"]
451+
}
452+
}
453+
}
413454
}
414455

415456
/**
@@ -2053,7 +2094,6 @@ private module PrivateDjango {
20532094
// MultiValueDict[str, UploadedFile]
20542095
"FILES",
20552096
// django.urls.ResolverMatch
2056-
// TODO: Model ResolverMatch
20572097
"resolver_match"
20582098
]
20592099
// TODO: Handle that a HttpRequest is iterable
@@ -2068,6 +2108,14 @@ private module PrivateDjango {
20682108
}
20692109
}
20702110

2111+
/** An attribute read on an django request that is a `ResolverMatch` instance. */
2112+
class DjangoHttpRequestResolverMatchInstances extends Django::ResolverMatch::InstanceSource {
2113+
DjangoHttpRequestResolverMatchInstances() {
2114+
this.(DataFlow::AttrRead).getObject() = django::http::request::HttpRequest::instance() and
2115+
this.(DataFlow::AttrRead).getAttributeName() = "resolver_match"
2116+
}
2117+
}
2118+
20712119
/** An `UploadedFile` instance that originates from a django request. */
20722120
class DjangoHttpRequestUploadedFileInstances extends Django::UploadedFile::InstanceSource {
20732121
DjangoHttpRequestUploadedFileInstances() {

python/ql/test/library-tests/frameworks/django-v2-v3/taint_test.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def test_taint(request: HttpRequest, foo, bar, baz=None): # $requestHandler rou
1111
# Manually inspected all fields of the HttpRequest object
1212
# https://docs.djangoproject.com/en/3.0/ref/request-response/#httprequest-objects
1313

14+
import django.urls
15+
django.urls.ResolverMatch
16+
1417
ensure_tainted(
1518
request, # $ tainted
1619

@@ -91,10 +94,10 @@ def test_taint(request: HttpRequest, foo, bar, baz=None): # $requestHandler rou
9194

9295
# django.urls.ResolverMatch
9396
request.resolver_match, # $ tainted
94-
request.resolver_match.args, # $ MISSING: tainted
95-
request.resolver_match.args[0], # $ MISSING: tainted
96-
request.resolver_match.kwargs, # $ MISSING: tainted
97-
request.resolver_match.kwargs["key"], # $ MISSING: tainted
97+
request.resolver_match.args, # $ tainted
98+
request.resolver_match.args[0], # $ tainted
99+
request.resolver_match.kwargs, # $ tainted
100+
request.resolver_match.kwargs["key"], # $ tainted
98101

99102
request.get_full_path(), # $ tainted
100103
request.get_full_path_info(), # $ tainted

0 commit comments

Comments
 (0)