Skip to content

Commit c096461

Browse files
authored
Merge pull request github#6111 from tausbn/python-a-few-minor-cleanups
Python: A few minor bits of cleanup
2 parents fa215bc + 317c686 commit c096461

File tree

13 files changed

+104
-121
lines changed

13 files changed

+104
-121
lines changed

python/ql/src/Security/CWE-327/InsecureProtocol.ql

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,33 @@ class ProtocolConfiguration extends DataFlow::Node {
2727
unsafe_context_creation(this, _)
2828
}
2929

30-
AstNode getNode() { result = this.asCfgNode().(CallNode).getFunction().getNode() }
30+
DataFlow::Node getNode() { result = this.(DataFlow::CallCfgNode).getFunction() }
3131
}
3232

3333
// Helper for pretty printer `callName`.
3434
// This is a consequence of missing pretty priting.
3535
// We do not want to evaluate our bespoke pretty printer
36-
// for all `AstNode`s so we define a sub class of interesting ones.
37-
//
38-
// Note that AstNode is abstract and AstNode_ is a library class, so
39-
// we have to extend @py_ast_node.
40-
class Nameable extends @py_ast_node {
36+
// for all `DataFlow::Node`s so we define a sub class of interesting ones.
37+
class Nameable extends DataFlow::Node {
4138
Nameable() {
4239
this = any(ProtocolConfiguration pc).getNode()
4340
or
44-
exists(Nameable attr | this = attr.(Attribute).getObject())
41+
this = any(Nameable attr).(DataFlow::AttrRef).getObject()
4542
}
46-
47-
string toString() { result = "AstNode" }
4843
}
4944

5045
string callName(Nameable call) {
51-
result = call.(Name).getId()
46+
result = call.asExpr().(Name).getId()
5247
or
53-
exists(Attribute a | a = call | result = callName(a.getObject()) + "." + a.getName())
48+
exists(DataFlow::AttrRef a | a = call |
49+
result = callName(a.getObject()) + "." + a.getAttributeName()
50+
)
5451
}
5552

5653
string configName(ProtocolConfiguration protocolConfiguration) {
57-
result =
58-
"call to " + callName(protocolConfiguration.asCfgNode().(CallNode).getFunction().getNode())
54+
result = "call to " + callName(protocolConfiguration.(DataFlow::CallCfgNode).getFunction())
5955
or
60-
not protocolConfiguration.asCfgNode() instanceof CallNode and
56+
not protocolConfiguration instanceof DataFlow::CallCfgNode and
6157
not protocolConfiguration instanceof ContextCreation and
6258
result = "context modification"
6359
}

python/ql/src/Security/CWE-327/PyOpenSSL.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class PyOpenSSLContextCreation extends ContextCreation, DataFlow::CallCfgNode {
1313
}
1414

1515
override string getProtocol() {
16-
exists(ControlFlowNode protocolArg, PyOpenSSL pyo |
17-
protocolArg in [node.getArg(0), node.getArgByName("method")]
16+
exists(DataFlow::Node protocolArg, PyOpenSSL pyo |
17+
protocolArg in [this.getArg(0), this.getArgByName("method")]
1818
|
19-
protocolArg =
20-
[pyo.specific_version(result).getAUse(), pyo.unspecific_version(result).getAUse()]
21-
.asCfgNode()
19+
protocolArg in [
20+
pyo.specific_version(result).getAUse(), pyo.unspecific_version(result).getAUse()
21+
]
2222
)
2323
}
2424
}
@@ -29,7 +29,7 @@ class ConnectionCall extends ConnectionCreation, DataFlow::CallCfgNode {
2929
}
3030

3131
override DataFlow::CfgNode getContext() {
32-
result.getNode() in [node.getArg(0), node.getArgByName("context")]
32+
result in [this.getArg(0), this.getArgByName("context")]
3333
}
3434
}
3535

@@ -43,8 +43,8 @@ class SetOptionsCall extends ProtocolRestriction, DataFlow::CallCfgNode {
4343
}
4444

4545
override ProtocolVersion getRestriction() {
46-
API::moduleImport("OpenSSL").getMember("SSL").getMember("OP_NO_" + result).getAUse().asCfgNode() in [
47-
node.getArg(0), node.getArgByName("options")
46+
API::moduleImport("OpenSSL").getMember("SSL").getMember("OP_NO_" + result).getAUse() in [
47+
this.getArg(0), this.getArgByName("options")
4848
]
4949
}
5050
}

python/ql/src/Security/CWE-327/Ssl.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class SSLContextCreation extends ContextCreation, DataFlow::CallCfgNode {
1111
SSLContextCreation() { this = API::moduleImport("ssl").getMember("SSLContext").getACall() }
1212

1313
override string getProtocol() {
14-
exists(ControlFlowNode protocolArg, Ssl ssl |
15-
protocolArg in [node.getArg(0), node.getArgByName("protocol")]
14+
exists(DataFlow::Node protocolArg, Ssl ssl |
15+
protocolArg in [this.getArg(0), this.getArgByName("protocol")]
1616
|
1717
protocolArg =
1818
[ssl.specific_version(result).getAUse(), ssl.unspecific_version(result).getAUse()]
19-
.asCfgNode()
2019
)
2120
or
22-
not exists(node.getAnArg()) and
21+
not exists(this.getArg(_)) and
22+
not exists(this.getArgByName(_)) and
2323
result = "TLS"
2424
}
2525
}
@@ -131,7 +131,7 @@ class ContextSetVersion extends ProtocolRestriction, ProtocolUnrestriction, Data
131131

132132
ContextSetVersion() {
133133
exists(DataFlow::AttrWrite aw |
134-
aw.getObject().asCfgNode() = node and
134+
this = aw.getObject() and
135135
aw.getAttributeName() = "minimum_version" and
136136
aw.getValue() =
137137
API::moduleImport("ssl").getMember("TLSVersion").getMember(restriction).getAUse()

python/ql/src/semmle/python/Concepts.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ module HTTP {
345345
/** Gets the URL pattern for this route, if it can be statically determined. */
346346
string getUrlPattern() {
347347
exists(StrConst str |
348-
DataFlow::exprNode(str).(DataFlow::LocalSourceNode).flowsTo(this.getUrlPatternArg()) and
348+
this.getUrlPatternArg().getALocalSource() = DataFlow::exprNode(str) and
349349
result = str.getText()
350350
)
351351
}
@@ -478,9 +478,7 @@ module HTTP {
478478
/** Gets the mimetype of this HTTP response, if it can be statically determined. */
479479
string getMimetype() {
480480
exists(StrConst str |
481-
DataFlow::exprNode(str)
482-
.(DataFlow::LocalSourceNode)
483-
.flowsTo(this.getMimetypeOrContentTypeArg()) and
481+
this.getMimetypeOrContentTypeArg().getALocalSource() = DataFlow::exprNode(str) and
484482
result = str.getText().splitAt(";", 0)
485483
)
486484
or

python/ql/src/semmle/python/dataflow/new/SensitiveDataSources.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private module SensitiveDataModeling {
243243
SensitiveDataClassification classification;
244244

245245
SensitiveGetCall() {
246-
this.getFunction().asCfgNode().(AttrNode).getName() = "get" and
246+
this.getFunction().(DataFlow::AttrRef).getAttributeName() = "get" and
247247
this.getArg(0) = sensitiveLookupStringConst(classification)
248248
}
249249

python/ql/src/semmle/python/dataflow/new/internal/LocalSources.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class LocalSourceNode extends Node {
5959
*/
6060
AttrRead getAnAttributeRead(string attrName) { result = getAnAttributeReference(attrName) }
6161

62+
/**
63+
* Gets a write of attribute `attrName` on this node.
64+
*/
65+
AttrWrite getAnAttributeWrite(string attrName) { result = getAnAttributeReference(attrName) }
66+
6267
/**
6368
* Gets a reference (read or write) of any attribute on this node.
6469
*/
@@ -73,6 +78,11 @@ class LocalSourceNode extends Node {
7378
*/
7479
AttrRead getAnAttributeRead() { result = getAnAttributeReference() }
7580

81+
/**
82+
* Gets a write of any attribute on this node.
83+
*/
84+
AttrWrite getAnAttributeWrite() { result = getAnAttributeReference() }
85+
7686
/**
7787
* Gets a call to this node.
7888
*/

0 commit comments

Comments
 (0)