Skip to content

Commit c386f4a

Browse files
authored
Python: Clean up py/insecure-protocol
Going all the way to the AST layer seemed excessive to me, so I rewrote it to do most of the logic at the data-flow layer. In principle this _could_ result in more names being computed (due to splitting), but in practice I don't expect this make a big difference.
1 parent f24a9a4 commit c386f4a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,34 @@ 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) {
5754
result =
58-
"call to " + callName(protocolConfiguration.asCfgNode().(CallNode).getFunction().getNode())
55+
"call to " + callName(protocolConfiguration.(DataFlow::CallCfgNode).getFunction())
5956
or
60-
not protocolConfiguration.asCfgNode() instanceof CallNode and
57+
not protocolConfiguration instanceof DataFlow::CallCfgNode and
6158
not protocolConfiguration instanceof ContextCreation and
6259
result = "context modification"
6360
}

0 commit comments

Comments
 (0)