Skip to content

Commit 3a83ecf

Browse files
committed
Python: Add test for taint in django forms/fields
1 parent c6a69e1 commit 3a83ecf

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

python/ql/test/library-tests/frameworks/django-v2-v3/TestTaint.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
| response_test.py:61 | ok | get_redirect_url | foo |
2+
| taint_forms.py:6 | fail | to_python | value |
3+
| taint_forms.py:9 | fail | validate | value |
4+
| taint_forms.py:12 | fail | run_validators | value |
5+
| taint_forms.py:15 | fail | clean | value |
6+
| taint_forms.py:33 | fail | clean | cleaned_data |
7+
| taint_forms.py:34 | fail | clean | cleaned_data["key"] |
8+
| taint_forms.py:35 | fail | clean | cleaned_data.get(..) |
9+
| taint_forms.py:39 | fail | clean | self.cleaned_data |
10+
| taint_forms.py:40 | fail | clean | self.cleaned_data["key"] |
11+
| taint_forms.py:41 | fail | clean | self.cleaned_data.get(..) |
12+
| taint_forms.py:46 | fail | clean_foo | self.cleaned_data |
213
| taint_test.py:8 | ok | test_taint | bar |
314
| taint_test.py:8 | ok | test_taint | foo |
415
| taint_test.py:9 | ok | test_taint | baz |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import django.forms
2+
3+
4+
class MyField(django.forms.Field):
5+
def to_python(self, value):
6+
ensure_tainted(value)
7+
8+
def validate(self, value):
9+
ensure_tainted(value)
10+
11+
def run_validators(self, value):
12+
ensure_tainted(value)
13+
14+
def clean(self, value):
15+
ensure_tainted(value)
16+
17+
# # Base definition of `clean` looks like the following, so there is actually
18+
# # _data flow_ from the methods, but we will ignore for simplicity.
19+
# value = self.to_python(value)
20+
# self.validate(value)
21+
# self.run_validators(value)
22+
# return value
23+
24+
25+
class MyForm(django.forms.Form):
26+
27+
foo = MyField()
28+
29+
def clean(self):
30+
cleaned_data = super().clean()
31+
32+
ensure_tainted(
33+
cleaned_data,
34+
cleaned_data["key"],
35+
cleaned_data.get("key"),
36+
)
37+
38+
ensure_tainted(
39+
self.cleaned_data,
40+
self.cleaned_data["key"],
41+
self.cleaned_data.get("key"),
42+
)
43+
44+
def clean_foo(self):
45+
# This method is supposed to clean a the `foo` field in context of this form.
46+
ensure_tainted(self.cleaned_data)

0 commit comments

Comments
 (0)