Skip to content

Commit c7c6c83

Browse files
committed
Address review comments.
1 parent 129baea commit c7c6c83

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@ man-in-the-middle attacks.
1313

1414
<recommendation>
1515
<p>
16-
Do not accept unknown host keys. For the Paramiko library in particular, avoid
17-
setting the missing host key policy to either <code>AutoAddPolicy</code> or
18-
<code>WarningPolicy</code>, as both of these will continue even when the host
19-
key is unknown. The default <code>RejectPolicy</code> throws an exception when
20-
unknown host keys are encountered.
16+
Do not accept unknown host keys. In particular, do not set the default missing
17+
host key policy for the Paramiko library to either <code>AutoAddPolicy</code> or
18+
<code>WarningPolicy</code>. Both of these policies continue even when the host
19+
key is unknown. The default setting of <code>RejectPolicy</code> is secure
20+
because it throws an exception when it encounters an unknown host key.
2121
</p>
2222
</recommendation>
2323

2424
<example>
2525
<p>
26-
The following example opens a connection to <code>example.com</code> with the
27-
missing host key policy set to <code>AutoAddPolicy</code>. If the host key
28-
verification fails, the client will continue to interact with the server, even
29-
though the connection may be compromised.
26+
The following example shows two ways of opening an SSH connection to
27+
<code>example.com</code>. The first function sets the missing host key policy to
28+
<code>AutoAddPolicy</code>. If the host key verification fails, the client will
29+
continue to interact with the server, even though the connection may be
30+
compromised. The second function sets the host key policy to
31+
<code>RejectPolicy</code>, and will throw an exception if the host key
32+
verification fails.
3033
</p>
3134
<sample src="examples/paramiko_host_key.py" />
3235
</example>

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* @name Accepting unknown host keys.
2+
* @name Accepting unknown SSH host keys when using Paramiko
33
* @description Accepting unknown host keys can allow man-in-the-middle attacks.
44
* @kind problem
55
* @problem.severity error
66
* @precision high
7-
* @id py/missing-host-key-validation
7+
* @id py/paramiko-missing-host-key-validation
88
* @tags security
99
* external/cwe/cwe-295
1010
*/
@@ -22,11 +22,15 @@ private ClassObject unsafe_paramiko_policy(string name) {
2222
result = theParamikoClientModule().attr(name)
2323
}
2424

25-
from CallNode call, string name
25+
from CallNode call, ControlFlowNode arg, string name
2626
where
2727
call = theParamikoSSHClientClass()
28-
.declaredAttribute("set_missing_host_key_policy")
29-
.(FunctionObject)
30-
.getACall() and
31-
call.getAnArg().refersTo(unsafe_paramiko_policy(name))
28+
.lookupAttribute("set_missing_host_key_policy")
29+
.(FunctionObject)
30+
.getACall() and
31+
arg = call.getAnArg() and
32+
(
33+
arg.refersTo(unsafe_paramiko_policy(name)) or
34+
arg.refersTo(_, unsafe_paramiko_policy(name), _)
35+
)
3236
select call, "Setting missing host key policy to " + name + " may be unsafe."
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
from paramiko.client import SSHClient, AutoAddPolicy
1+
from paramiko.client import SSHClient, AutoAddPolicy, RejectPolicy
22

3-
client = SSHClient()
4-
client.set_missing_host_key_policy(AutoAddPolicy)
5-
client.connect("example.com")
3+
def unsafe_connect():
4+
client = SSHClient()
5+
client.set_missing_host_key_policy(AutoAddPolicy)
6+
client.connect("example.com")
67

7-
# ... interaction with server
8+
# ... interaction with server
89

9-
client.close()
10+
client.close()
11+
12+
def safe_connect():
13+
client = SSHClient()
14+
client.set_missing_host_key_policy(RejectPolicy)
15+
client.connect("example.com")
16+
17+
# ... interaction with server
18+
19+
client.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| paramiko_host_key.py:5:1:5:49 | ControlFlowNode for Attribute() | Setting missing host key policy to AutoAddPolicy may be unsafe. |
22
| paramiko_host_key.py:7:1:7:49 | ControlFlowNode for Attribute() | Setting missing host key policy to WarningPolicy may be unsafe. |
3+
| paramiko_host_key.py:11:1:11:51 | ControlFlowNode for Attribute() | Setting missing host key policy to AutoAddPolicy may be unsafe. |
4+
| paramiko_host_key.py:13:1:13:51 | ControlFlowNode for Attribute() | Setting missing host key policy to WarningPolicy may be unsafe. |

python/ql/test/query-tests/Security/CWE-295/paramiko_host_key.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55
client.set_missing_host_key_policy(AutoAddPolicy) # bad
66
client.set_missing_host_key_policy(RejectPolicy) # good
77
client.set_missing_host_key_policy(WarningPolicy) # bad
8+
9+
# Using instances
10+
11+
client.set_missing_host_key_policy(AutoAddPolicy()) # bad
12+
client.set_missing_host_key_policy(RejectPolicy()) # good
13+
client.set_missing_host_key_policy(WarningPolicy()) # bad

0 commit comments

Comments
 (0)