Skip to content

Commit e4d74cf

Browse files
yoffRasmusWL
andauthored
Apply suggestions from code review
Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
1 parent 3ff8e01 commit e4d74cf

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import python
1+
private import python
22
import TlsLibraryModel
33

44
/**
@@ -89,17 +89,14 @@ predicate unsafe_connection_creation_with_context(
8989
/**
9090
* Holds if `conectionCreation` marks the creation of a connetion witout reference to a context
9191
* and allowing `insecure_version`.
92-
*
93-
* `specific` is true iff the context is configured for a specific protocol version rather
94-
* than for a family of protocols.
9592
*/
9693
predicate unsafe_connection_creation_without_context(
9794
DataFlow::CallCfgNode connectionCreation, string insecure_version
9895
) {
9996
exists(TlsLibrary l | connectionCreation = l.insecure_connection_creation(insecure_version))
10097
}
10198

102-
/** Holds if `contextCreation` is creating a context ties to a specific insecure version. */
99+
/** Holds if `contextCreation` is creating a context tied to a specific insecure version. */
103100
predicate unsafe_context_creation(DataFlow::CallCfgNode contextCreation, string insecure_version) {
104101
exists(TlsLibrary l | contextCreation = l.insecure_context_creation(insecure_version))
105102
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import python
2-
import semmle.python.ApiGraphs
1+
/**
2+
* Provides modeling of SSL/TLS functionality of the `OpenSSL` module from the `pyOpenSSL` PyPI package.
3+
* See https://www.pyopenssl.org/en/stable/
4+
*/
5+
private import python
6+
private import semmle.python.ApiGraphs
37
import TlsLibraryModel
48

59
class PyOpenSSLContextCreation extends ContextCreation {
@@ -49,7 +53,7 @@ class SetOptionsCall extends ProtocolRestriction {
4953
}
5054

5155
class UnspecificPyOpenSSLContextCreation extends PyOpenSSLContextCreation, UnspecificContextCreation {
52-
UnspecificPyOpenSSLContextCreation() { library = "pyOpenSSL" }
56+
UnspecificPyOpenSSLContextCreation() { library instanceof PyOpenSSL }
5357
}
5458

5559
class PyOpenSSL extends TlsLibrary {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import python
2-
import semmle.python.ApiGraphs
1+
/**
2+
* Provides modeling of SSL/TLS functionality of the `ssl` module from the standard library.
3+
* See https://docs.python.org/3.9/library/ssl.html
4+
*/
5+
private import python
6+
private import semmle.python.ApiGraphs
37
import TlsLibraryModel
48

59
class SSLContextCreation extends ContextCreation {
@@ -145,12 +149,12 @@ class ContextSetVersion extends ProtocolRestriction, ProtocolUnrestriction {
145149
}
146150

147151
class UnspecificSSLContextCreation extends SSLContextCreation, UnspecificContextCreation {
148-
UnspecificSSLContextCreation() { library = "ssl" }
152+
UnspecificSSLContextCreation() { library instanceof Ssl }
149153

150154
override ProtocolVersion getUnrestriction() {
151155
result = UnspecificContextCreation.super.getUnrestriction() and
152-
// These are turned off by default
153-
// see https://docs.python.org/3/library/ssl.html#ssl-contexts
156+
// These are turned off by default since Python 3.6
157+
// see https://docs.python.org/3.6/library/ssl.html#ssl.SSLContext
154158
not result in ["SSLv2", "SSLv3"]
155159
}
156160
}
@@ -185,8 +189,8 @@ class Ssl extends TlsLibrary {
185189

186190
override DataFlow::CfgNode insecure_connection_creation(ProtocolVersion version) {
187191
result = API::moduleImport("ssl").getMember("wrap_socket").getACall() and
188-
specific_version(version).asCfgNode() =
189-
result.asCfgNode().(CallNode).getArgByName("ssl_version") and
192+
this.specific_version(version) =
193+
result.(DataFlow::CallCfgNode).getArgByName("ssl_version") and
190194
version.isInsecure()
191195
}
192196

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import python
2-
import semmle.python.ApiGraphs
1+
private import python
2+
private import semmle.python.ApiGraphs
33
import Ssl
44
import PyOpenSSL
55

66
/**
7-
* A specific protocol version.
8-
* We use this to identify a protocol.
7+
* A specific protocol version of SSL or TLS.
98
*/
109
class ProtocolVersion extends string {
1110
ProtocolVersion() { this in ["SSLv2", "SSLv3", "TLSv1", "TLSv1_1", "TLSv1_2", "TLSv1_3"] }
1211

12+
/** Gets a `ProtocolVersion` that is less than this `ProtocolVersion`, if any. */
1313
predicate lessThan(ProtocolVersion version) {
1414
this = "SSLv2" and version = "SSLv3"
1515
or
@@ -20,6 +20,7 @@ class ProtocolVersion extends string {
2020
this = ["TLSv1", "TLSv1_1", "TLSv1_2"] and version = "TLSv1_3"
2121
}
2222

23+
/** Holds if this protocol version is known to be insecure. */
2324
predicate isInsecure() { this in ["SSLv2", "SSLv3", "TLSv1", "TLSv1_1"] }
2425
}
2526

@@ -81,12 +82,13 @@ abstract class UnspecificContextCreation extends ContextCreation, ProtocolUnrest
8182

8283
/** A model of a SSL/TLS library. */
8384
abstract class TlsLibrary extends string {
84-
TlsLibrary() { this in ["ssl", "pyOpenSSL"] }
85+
bindingset[this]
86+
TlsLibrary() { any() }
8587

8688
/** The name of a specific protocol version. */
8789
abstract string specific_version_name(ProtocolVersion version);
8890

89-
/** The name of an unspecific protocol version, say TLS, known to have insecure instances. */
91+
/** Gets a name, which is a member of `version_constants`, that can be used to specify the protocol family `family`. */
9092
abstract string unspecific_version_name(ProtocolFamily family);
9193

9294
/** The module or class holding the version constants. */
@@ -97,12 +99,12 @@ abstract class TlsLibrary extends string {
9799
result = version_constants().getMember(specific_version_name(version)).getAUse()
98100
}
99101

100-
/** A dataflow node representing an unspecific protocol version, say TLS, known to have insecure instances. */
102+
/** Gets a dataflow node representing the protocol family `family`. */
101103
DataFlow::Node unspecific_version(ProtocolFamily family) {
102104
result = version_constants().getMember(unspecific_version_name(family)).getAUse()
103105
}
104106

105-
/** The creation of a context with a deafult protocol. */
107+
/** The creation of a context with a default protocol. */
106108
abstract ContextCreation default_context_creation();
107109

108110
/** The creation of a context with a specific protocol. */
@@ -115,7 +117,7 @@ abstract class TlsLibrary extends string {
115117
version.isInsecure()
116118
}
117119

118-
/** The creation of a context with an unspecific protocol version, say TLS, known to have insecure instances. */
120+
/** Gets a context that was created using `family`, known to have insecure instances. */
119121
ContextCreation unspecific_context_creation(ProtocolFamily family) {
120122
result in [specific_context_creation(), default_context_creation()] and
121123
result.getProtocol() = family

0 commit comments

Comments
 (0)