Skip to content

Commit 0d02ca0

Browse files
committed
Python: Add certificate disable test of urllib/urllib2
1 parent 049e872 commit 0d02ca0

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

python/ql/lib/semmle/python/frameworks/Stdlib/Urllib.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ private module Urllib {
4242
override predicate disablesCertificateValidation(
4343
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin
4444
) {
45-
// TODO: Look into disabling certificate validation
45+
// cannot enable/disable certificate validation on this object, only when used
46+
// with `urlopen`, which is modeled below
4647
none()
4748
}
4849
}
@@ -63,7 +64,8 @@ private module Urllib {
6364
override predicate disablesCertificateValidation(
6465
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin
6566
) {
66-
// TODO: Look into disabling certificate validation
67+
// will validate certificate by default, see https://github.com/python/cpython/blob/243ed5439c32e8517aa745bc2ca9774d99c99d0f/Lib/http/client.py#L1420-L1421
68+
// TODO: Handling of insecure SSLContext passed to context argument
6769
none()
6870
}
6971
}

python/ql/lib/semmle/python/frameworks/Stdlib/Urllib2.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ private module Urllib2 {
3030
override predicate disablesCertificateValidation(
3131
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin
3232
) {
33-
// TODO: Look into disabling certificate validation
33+
// cannot enable/disable certificate validation on this object, only when used
34+
// with `urlopen`, which is modeled below
3435
none()
3536
}
3637
}
@@ -49,7 +50,8 @@ private module Urllib2 {
4950
override predicate disablesCertificateValidation(
5051
DataFlow::Node disablingNode, DataFlow::Node argumentOrigin
5152
) {
52-
// TODO: Look into disabling certificate validation
53+
// will validate certificate by default
54+
// TODO: Handling of insecure SSLContext passed to context argument
5355
none()
5456
}
5557
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import urllib2
2+
import ssl
23

34
resp = urllib2.Request("url") # $ clientRequestUrlPart="url"
45
resp = urllib2.Request(url="url") # $ clientRequestUrlPart="url"
56

67
resp = urllib2.urlopen("url") # $ clientRequestUrlPart="url"
7-
resp = urllib2.urlopen(url="url") # $ clientRequestUrlPart="url"
8+
resp = urllib2.urlopen(url="url") # $ clientRequestUrlPart="url"
9+
10+
# ==============================================================================
11+
# Certificate validation disabled
12+
# ==============================================================================
13+
14+
# A manually constructed SSLContext does not have safe defaults, so is effectively the
15+
# same as turning off SSL validation
16+
context = ssl.SSLContext()
17+
assert context.check_hostname == False
18+
assert context.verify_mode == ssl.VerifyMode.CERT_NONE
19+
20+
urllib2.urlopen("url", context=context) # $ clientRequestUrlPart="url" MISSING: clientRequestCertValidationDisabled
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
import ssl
12
from urllib.request import Request, urlopen
23

34
Request("url") # $ clientRequestUrlPart="url"
45
Request(url="url") # $ clientRequestUrlPart="url"
56

67
urlopen("url") # $ clientRequestUrlPart="url"
7-
urlopen(url="url") # $ clientRequestUrlPart="url"
8+
urlopen(url="url") # $ clientRequestUrlPart="url"
9+
10+
# ==============================================================================
11+
# Certificate validation disabled
12+
# ==============================================================================
13+
14+
# A manually constructed SSLContext does not have safe defaults, so is effectively the
15+
# same as turning off SSL validation
16+
context = ssl.SSLContext()
17+
assert context.check_hostname == False
18+
assert context.verify_mode == ssl.VerifyMode.CERT_NONE
19+
20+
urlopen("url", context=context) # $ clientRequestUrlPart="url" MISSING: clientRequestCertValidationDisabled

0 commit comments

Comments
 (0)