Skip to content

Commit 25ee023

Browse files
committed
Catch VerificationError but keep the rest of ClientTLSOptions
1 parent a087d25 commit 25ee023

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

scrapy/core/downloader/contextfactory.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
from OpenSSL import SSL
22
from twisted.internet.ssl import ClientContextFactory
33

4-
54
try:
65

76
from zope.interface.declarations import implementer
87

98
# the following should be available from Twisted 14.0.0
10-
from twisted.internet.ssl import optionsForClientTLS, CertificateOptions, platformTrust
11-
from twisted.internet._sslverify import ClientTLSOptions
9+
from twisted.internet.ssl import (optionsForClientTLS,
10+
CertificateOptions,
11+
platformTrust)
12+
1213
from twisted.web.client import BrowserLikePolicyForHTTPS
1314
from twisted.web.iweb import IPolicyForHTTPS
1415

15-
16-
class ScrapyClientTLSOptions(ClientTLSOptions):
17-
def _identityVerifyingInfoCallback(self, connection, where, ret):
18-
pass
16+
from scrapy.core.downloader.tls import ScrapyClientTLSOptions
1917

2018

2119
@implementer(IPolicyForHTTPS)

scrapy/core/downloader/tls.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import logging
12
from OpenSSL import SSL
23

34

5+
logger = logging.getLogger(__name__)
6+
47
METHOD_SSLv3 = 'SSLv3'
58
METHOD_TLS = 'TLS'
69
METHOD_TLSv10 = 'TLSv1.0'
@@ -14,3 +17,36 @@
1417
METHOD_TLSv11: getattr(SSL, 'TLSv1_1_METHOD', 5), # TLS 1.1 only
1518
METHOD_TLSv12: getattr(SSL, 'TLSv1_2_METHOD', 6), # TLS 1.2 only
1619
}
20+
21+
# ClientTLSOptions requires a recent-enough version of Twisted
22+
try:
23+
24+
# taken from twisted/twisted/internet/_sslverify.py
25+
try:
26+
from OpenSSL.SSL import SSL_CB_HANDSHAKE_DONE, SSL_CB_HANDSHAKE_START
27+
except ImportError:
28+
SSL_CB_HANDSHAKE_START = 0x10
29+
SSL_CB_HANDSHAKE_DONE = 0x20
30+
31+
from twisted.internet._sslverify import (ClientTLSOptions,
32+
_maybeSetHostNameIndication,
33+
verifyHostname,
34+
VerificationError)
35+
36+
class ScrapyClientTLSOptions(ClientTLSOptions):
37+
# same as Twisted's ClientTLSOptions,
38+
# except that VerificationError is caught
39+
# and doesn't close the connection
40+
def _identityVerifyingInfoCallback(self, connection, where, ret):
41+
if where & SSL_CB_HANDSHAKE_START:
42+
_maybeSetHostNameIndication(connection, self._hostnameBytes)
43+
elif where & SSL_CB_HANDSHAKE_DONE:
44+
try:
45+
verifyHostname(connection, self._hostnameASCII)
46+
except VerificationError as e:
47+
logger.warning(e)
48+
49+
except ImportError:
50+
# ImportError should not matter for older Twisted versions
51+
# as the above is not used in the fallback ScrapyClientContextFactory
52+
pass

0 commit comments

Comments
 (0)