Skip to content

Commit 6c75502

Browse files
committed
Python: Refactor code, inline some type-tracking
1 parent 770429f commit 6c75502

File tree

1 file changed

+41
-51
lines changed

1 file changed

+41
-51
lines changed

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

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -183,64 +183,42 @@ private module CryptographyModel {
183183
.getMember(algorithmName)
184184
}
185185

186-
/**
187-
* Internal module making it easy to hide verbose type-tracking helpers.
188-
*
189-
* These turned out to be so verbose, that it was impossible to get an overview of
190-
* the relevant predicates without hiding them away.
191-
*/
192-
private module InternalTypeTracking {
193-
/** Gets a reference to a Cipher instance using algorithm with `algorithmName`. */
194-
DataFlow::LocalSourceNode cipherInstance(DataFlow::TypeTracker t, string algorithmName) {
195-
t.start() and
196-
exists(DataFlow::CallCfgNode call | result = call |
197-
call =
198-
API::moduleImport("cryptography")
199-
.getMember("hazmat")
200-
.getMember("primitives")
201-
.getMember("ciphers")
202-
.getMember("Cipher")
203-
.getACall() and
204-
algorithmClassRef(algorithmName).getReturn().getAUse() in [
205-
call.getArg(0), call.getArgByName("algorithm")
206-
]
207-
)
208-
or
209-
exists(DataFlow::TypeTracker t2 | result = cipherInstance(t2, algorithmName).track(t2, t))
210-
}
211-
212-
/** Gets a reference to the encryptor of a Cipher instance using algorithm with `algorithmName`. */
213-
DataFlow::LocalSourceNode cipherEncryptor(DataFlow::TypeTracker t, string algorithmName) {
214-
t.start() and
215-
exists(DataFlow::AttrRead attr |
216-
result.(DataFlow::CallCfgNode).getFunction() = attr and
217-
attr.getAttributeName() = "encryptor" and
218-
attr.getObject() = cipherInstance(algorithmName)
219-
)
220-
or
221-
exists(DataFlow::TypeTracker t2 | result = cipherEncryptor(t2, algorithmName).track(t2, t))
222-
}
223-
224-
/** Gets a reference to the dncryptor of a Cipher instance using algorithm with `algorithmName`. */
225-
DataFlow::LocalSourceNode cipherDecryptor(DataFlow::TypeTracker t, string algorithmName) {
226-
t.start() and
227-
exists(DataFlow::AttrRead attr |
228-
result.(DataFlow::CallCfgNode).getFunction() = attr and
229-
attr.getAttributeName() = "decryptor" and
230-
attr.getObject() = cipherInstance(algorithmName)
231-
)
232-
or
233-
exists(DataFlow::TypeTracker t2 | result = cipherDecryptor(t2, algorithmName).track(t2, t))
234-
}
186+
/** Gets a reference to a Cipher instance using algorithm with `algorithmName`. */
187+
DataFlow::LocalSourceNode cipherInstance(DataFlow::TypeTracker t, string algorithmName) {
188+
t.start() and
189+
exists(DataFlow::CallCfgNode call | result = call |
190+
call =
191+
API::moduleImport("cryptography")
192+
.getMember("hazmat")
193+
.getMember("primitives")
194+
.getMember("ciphers")
195+
.getMember("Cipher")
196+
.getACall() and
197+
algorithmClassRef(algorithmName).getReturn().getAUse() in [
198+
call.getArg(0), call.getArgByName("algorithm")
199+
]
200+
)
201+
or
202+
exists(DataFlow::TypeTracker t2 | result = cipherInstance(t2, algorithmName).track(t2, t))
235203
}
236204

237-
private import InternalTypeTracking
238-
239205
/** Gets a reference to a Cipher instance using algorithm with `algorithmName`. */
240206
DataFlow::Node cipherInstance(string algorithmName) {
241207
cipherInstance(DataFlow::TypeTracker::end(), algorithmName).flowsTo(result)
242208
}
243209

210+
/** Gets a reference to the encryptor of a Cipher instance using algorithm with `algorithmName`. */
211+
DataFlow::LocalSourceNode cipherEncryptor(DataFlow::TypeTracker t, string algorithmName) {
212+
t.start() and
213+
exists(DataFlow::AttrRead attr |
214+
result.(DataFlow::CallCfgNode).getFunction() = attr and
215+
attr.getAttributeName() = "encryptor" and
216+
attr.getObject() = cipherInstance(algorithmName)
217+
)
218+
or
219+
exists(DataFlow::TypeTracker t2 | result = cipherEncryptor(t2, algorithmName).track(t2, t))
220+
}
221+
244222
/**
245223
* Gets a reference to the encryptor of a Cipher instance using algorithm with `algorithmName`.
246224
*
@@ -250,6 +228,18 @@ private module CryptographyModel {
250228
cipherEncryptor(DataFlow::TypeTracker::end(), algorithmName).flowsTo(result)
251229
}
252230

231+
/** Gets a reference to the dncryptor of a Cipher instance using algorithm with `algorithmName`. */
232+
DataFlow::LocalSourceNode cipherDecryptor(DataFlow::TypeTracker t, string algorithmName) {
233+
t.start() and
234+
exists(DataFlow::AttrRead attr |
235+
result.(DataFlow::CallCfgNode).getFunction() = attr and
236+
attr.getAttributeName() = "decryptor" and
237+
attr.getObject() = cipherInstance(algorithmName)
238+
)
239+
or
240+
exists(DataFlow::TypeTracker t2 | result = cipherDecryptor(t2, algorithmName).track(t2, t))
241+
}
242+
253243
/**
254244
* Gets a reference to the decryptor of a Cipher instance using algorithm with `algorithmName`.
255245
*

0 commit comments

Comments
 (0)