Skip to content

Commit 5125c7a

Browse files
committed
Python: Add taint tests for encode/decode functions
1 parent 31b3989 commit 5125c7a

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

python/ql/src/experimental/dataflow/internal/TaintTrackingPrivate.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,7 @@ predicate stringMethods(DataFlow::CfgNode nodeFrom, DataFlow::CfgNode nodeTo) {
119119
fmt.getRight() = nodeFrom.getNode()
120120
)
121121
)
122+
// TODO: Handle encode/decode from base64/quopri
123+
// TODO: Handle os.path.join
124+
// TODO: Handle functions in https://docs.python.org/3/library/binascii.html
122125
}

python/ql/test/experimental/dataflow/tainttracking/string/TestTaint.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@
4747
| test.py:110 | ok | percent_fmt | BinaryExpr |
4848
| test.py:111 | ok | percent_fmt | BinaryExpr |
4949
| test.py:112 | fail | percent_fmt | BinaryExpr |
50+
| test.py:122 | fail | binary_decode_encode | base64.b64encode(..) |
51+
| test.py:123 | fail | binary_decode_encode | base64.b64decode(..) |
52+
| test.py:125 | fail | binary_decode_encode | base64.standard_b64encode(..) |
53+
| test.py:126 | fail | binary_decode_encode | base64.standard_b64decode(..) |
54+
| test.py:128 | fail | binary_decode_encode | base64.urlsafe_b64encode(..) |
55+
| test.py:129 | fail | binary_decode_encode | base64.urlsafe_b64decode(..) |
56+
| test.py:131 | fail | binary_decode_encode | base64.b32encode(..) |
57+
| test.py:132 | fail | binary_decode_encode | base64.b32decode(..) |
58+
| test.py:134 | fail | binary_decode_encode | base64.b16encode(..) |
59+
| test.py:135 | fail | binary_decode_encode | base64.b16decode(..) |
60+
| test.py:150 | fail | binary_decode_encode | base64.encodestring(..) |
61+
| test.py:151 | fail | binary_decode_encode | base64.decodestring(..) |
62+
| test.py:156 | fail | binary_decode_encode | quopri.encodestring(..) |
63+
| test.py:157 | fail | binary_decode_encode | quopri.decodestring(..) |

python/ql/test/experimental/dataflow/tainttracking/string/test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,55 @@ def percent_fmt():
113113
)
114114

115115

116+
def binary_decode_encode():
117+
print("\n#percent_fmt")
118+
tb = TAINTED_BYTES
119+
import base64
120+
121+
ensure_tainted(
122+
base64.b64encode(tb),
123+
base64.b64decode(base64.b64encode(tb)),
124+
125+
base64.standard_b64encode(tb),
126+
base64.standard_b64decode(base64.standard_b64encode(tb)),
127+
128+
base64.urlsafe_b64encode(tb),
129+
base64.urlsafe_b64decode(base64.urlsafe_b64encode(tb)),
130+
131+
base64.b32encode(tb),
132+
base64.b32decode(base64.b32encode(tb)),
133+
134+
base64.b16encode(tb),
135+
base64.b16decode(base64.b16encode(tb)),
136+
137+
# # New in Python 3.4
138+
# base64.a85encode(tb),
139+
# base64.a85decode(base64.a85encode(tb)),
140+
141+
# # New in Python 3.4
142+
# base64.b85encode(tb),
143+
# base64.b85decode(base64.b85encode(tb)),
144+
145+
# # New in Python 3.1
146+
# base64.encodebytes(tb),
147+
# base64.decodebytes(base64.encodebytes(tb)),
148+
149+
# deprecated since Python 3.1, but still works
150+
base64.encodestring(tb),
151+
base64.decodestring(base64.encodestring(tb)),
152+
)
153+
154+
import quopri
155+
ensure_tainted(
156+
quopri.encodestring(tb),
157+
quopri.decodestring(quopri.encodestring(tb)),
158+
)
159+
160+
116161
# Make tests runable
117162

118163
str_operations()
119164
str_methods()
120165
non_syntactic()
121166
percent_fmt()
167+
binary_decode_encode()

0 commit comments

Comments
 (0)