Skip to content

Commit f80e206

Browse files
Merge pull request github#3008 from RasmusWL/python-modernise-security-files
Python: modernise remaining security files
2 parents 3858166 + f3a10a1 commit f80e206

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ class ClassValue extends Value {
574574
/** Whether this class is a legal exception class.
575575
* What constitutes a legal exception class differs between major versions */
576576
predicate isLegalExceptionType() {
577-
not this.isNewStyle() or
577+
not this.isNewStyle()
578+
or
578579
this.getASuperType() = ClassValue::baseException()
579580
or
580581
major_version() = 2 and this = ClassValue::tuple()

python/ql/src/semmle/python/security/ClearText.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module ClearTextLogging {
4343
PrintSink() {
4444
exists(CallNode call |
4545
call.getAnArg() = this and
46-
thePrintFunction().(FunctionObject).getACall() = call
46+
call = Value::named("print").getACall()
4747
)
4848
}
4949
}

python/ql/src/semmle/python/security/Crypto.qll

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ abstract class WeakCryptoSink extends TaintSink {
1212
}
1313
}
1414

15+
/** Modeling the 'pycrypto' package https://github.com/dlitz/pycrypto (latest release 2013) */
1516
module Pycrypto {
1617

17-
ModuleObject cipher(string name) {
18-
exists(PackageObject crypto |
19-
crypto.getName() = "Crypto.Cipher" |
20-
crypto.submodule(name) = result
21-
)
18+
ModuleValue cipher(string name) {
19+
result = Module::named("Crypto.Cipher").attr(name)
2220
}
2321

2422
class CipherInstance extends TaintKind {
@@ -51,15 +49,15 @@ module Pycrypto {
5149
CipherInstanceSource() {
5250
exists(AttrNode attr |
5351
this.(CallNode).getFunction() = attr and
54-
attr.getObject("new").refersTo(cipher(instance.getName()))
52+
attr.getObject("new").pointsTo(cipher(instance.getName()))
5553
)
5654
}
5755

5856
override string toString() {
5957
result = "Source of " + instance
6058
}
6159

62-
override predicate isSourceOf(TaintKind kind) {
60+
override predicate isSourceOf(TaintKind kind) {
6361
kind = instance
6462
}
6563

@@ -70,12 +68,12 @@ module Pycrypto {
7068
string name;
7169

7270
PycryptoWeakCryptoSink() {
73-
exists(CallNode call, AttrNode method, CipherInstance Cipher |
71+
exists(CallNode call, AttrNode method, CipherInstance cipher |
7472
call.getAnArg() = this and
7573
call.getFunction() = method and
76-
Cipher.taints(method.getObject("encrypt")) and
77-
Cipher.isWeak() and
78-
Cipher.getName() = name
74+
cipher.taints(method.getObject("encrypt")) and
75+
cipher.isWeak() and
76+
cipher.getName() = name
7977
)
8078
}
8179

@@ -89,25 +87,25 @@ module Pycrypto {
8987

9088
module Cryptography {
9189

92-
PackageObject ciphers() {
93-
result.getName() = "cryptography.hazmat.primitives.ciphers"
90+
ModuleValue ciphers() {
91+
result = Module::named("cryptography.hazmat.primitives.ciphers") and
92+
result.isPackage()
9493
}
9594

96-
class CipherClass extends ClassObject {
95+
class CipherClass extends ClassValue {
9796
CipherClass() {
9897
ciphers().attr("Cipher") = this
9998
}
100-
10199
}
102100

103-
class AlgorithmClass extends ClassObject {
101+
class AlgorithmClass extends ClassValue {
104102

105103
AlgorithmClass() {
106-
ciphers().submodule("algorithms").attr(_) = this
104+
ciphers().attr("algorithms").attr(_) = this
107105
}
108106

109107
string getAlgorithmName() {
110-
result = this.declaredAttribute("name").(StringObject).getText()
108+
result = this.declaredAttribute("name").(StringValue).getText()
111109
}
112110

113111
predicate isWeak() {
@@ -134,7 +132,7 @@ module Cryptography {
134132
cls.isWeak()
135133
}
136134

137-
override TaintKind getTaintOfMethodResult(string name) {
135+
override TaintKind getTaintOfMethodResult(string name) {
138136
name = "encryptor" and
139137
result.(Encryptor).getAlgorithm() = this.getAlgorithm()
140138
}
@@ -144,11 +142,11 @@ module Cryptography {
144142
class CipherSource extends TaintSource {
145143

146144
CipherSource() {
147-
this.(CallNode).getFunction().refersTo(any(CipherClass cls))
145+
this.(CallNode).getFunction().pointsTo(any(CipherClass cls))
148146
}
149147

150148
override predicate isSourceOf(TaintKind kind) {
151-
this.(CallNode).getArg(0).refersTo(_, kind.(CipherInstance).getAlgorithm(), _)
149+
this.(CallNode).getArg(0).pointsTo().getClass() = kind.(CipherInstance).getAlgorithm()
152150
}
153151

154152
override string toString() {
@@ -203,5 +201,3 @@ private class CipherConfig extends TaintTracking::Configuration {
203201
}
204202

205203
}
206-
207-

python/ql/src/semmle/python/security/Exceptions.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ExceptionInfo extends StringKind {
2727

2828
}
2929

30-
/** A class representing sources of information about
30+
/** A class representing sources of information about
3131
* execution state exposed in tracebacks and the like.
3232
*/
3333
abstract class ErrorInfoSource extends TaintSource {}
@@ -59,9 +59,9 @@ class ExceptionKind extends TaintKind {
5959
class ExceptionSource extends ErrorInfoSource {
6060

6161
ExceptionSource() {
62-
exists(ClassObject cls |
63-
cls.isSubclassOf(theExceptionType()) and
64-
this.(ControlFlowNode).refersTo(_, cls, _)
62+
exists(ClassValue cls |
63+
cls.getASuperType() = ClassValue::baseException() and
64+
this.(ControlFlowNode).pointsTo().getClass() = cls
6565
)
6666
or
6767
this = any(ExceptStmt s).getName().getAFlowNode()
@@ -116,7 +116,7 @@ class CallToTracebackFunction extends ErrorInfoSource {
116116
}
117117
}
118118

119-
/**
119+
/**
120120
* Represents calls to functions in the `traceback` module that return a single
121121
* string of information about an exception.
122122
*/

python/ql/src/semmle/python/security/SensitiveData.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ module SensitiveData {
166166

167167
SensitiveRequestParameter() {
168168
this.(CallNode).getFunction().(AttrNode).getName() = "get" and
169-
exists(string sensitive |
170-
this.(CallNode).getAnArg().refersTo(any(StringObject s | s.getText() = sensitive)) and
171-
data = HeuristicNames::getSensitiveDataForName(sensitive)
169+
exists(StringValue sensitive |
170+
this.(CallNode).getAnArg().pointsTo(sensitive) and
171+
data = HeuristicNames::getSensitiveDataForName(sensitive.getText())
172172
)
173173
}
174174

0 commit comments

Comments
 (0)