Skip to content

Commit c3d2001

Browse files
authored
Merge pull request github#5251 from tausbn/python-port-missing-host-key-validation-query
Python: Port missing host key validation query
2 parents 1d654fe + f241dba commit c3d2001

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

python/ql/src/Security/CWE-295/MissingHostKeyValidation.ql

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010
*/
1111

1212
import python
13+
import semmle.python.dataflow.new.DataFlow
14+
import semmle.python.ApiGraphs
1315

14-
private ModuleValue theParamikoClientModule() { result = Value::named("paramiko.client") }
15-
16-
private ClassValue theParamikoSSHClientClass() {
17-
result = theParamikoClientModule().attr("SSHClient")
16+
private API::Node unsafe_paramiko_policy(string name) {
17+
name in ["AutoAddPolicy", "WarningPolicy"] and
18+
result = API::moduleImport("paramiko").getMember("client").getMember(name)
1819
}
1920

20-
private ClassValue unsafe_paramiko_policy(string name) {
21-
(name = "AutoAddPolicy" or name = "WarningPolicy") and
22-
result = theParamikoClientModule().attr(name)
21+
private API::Node paramikoSSHClientInstance() {
22+
result = API::moduleImport("paramiko").getMember("client").getMember("SSHClient").getReturn()
2323
}
2424

25-
from CallNode call, ControlFlowNode arg, string name
25+
from DataFlow::CallCfgNode call, DataFlow::Node arg, string name
2626
where
27-
call =
28-
theParamikoSSHClientClass().lookup("set_missing_host_key_policy").(FunctionValue).getACall() and
27+
call = paramikoSSHClientInstance().getMember("set_missing_host_key_policy").getACall() and
2928
arg = call.getAnArg() and
3029
(
31-
arg.pointsTo(unsafe_paramiko_policy(name)) or
32-
arg.pointsTo().getClass() = unsafe_paramiko_policy(name)
30+
arg = unsafe_paramiko_policy(name).getAUse() or
31+
arg = unsafe_paramiko_policy(name).getReturn().getAUse()
3332
)
3433
select call, "Setting missing host key policy to " + name + " may be unsafe."

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ class CallCfgNode extends CfgNode {
193193

194194
/** Gets the data-flow node corresponding to the named argument of the call corresponding to this data-flow node */
195195
Node getArgByName(string name) { result.asCfgNode() = node.getArgByName(name) }
196+
197+
/** Gets the data-flow node corresponding to an argument of the call corresponding to this data-flow node */
198+
Node getAnArg() {
199+
exists(int n | result = this.getArg(n))
200+
or
201+
exists(string name | result = this.getArgByName(name))
202+
}
196203
}
197204

198205
/**

0 commit comments

Comments
 (0)