SyncplayClient: explicitly pass `certifi` as trustRoot for Twisted

- py2app: include pem in the bundle
- requirements: add pem
- SyncplayClient.start: explicitly use certifi store as trustRoot
This commit is contained in:
Alberto Sottile 2022-11-05 23:45:51 +01:00
parent 2aa73122ab
commit af0d0006f1
3 changed files with 10 additions and 8 deletions

View File

@ -17,7 +17,7 @@ DATA_FILES = [
OPTIONS = { OPTIONS = {
'iconfile': 'syncplay/resources/icon.icns', 'iconfile': 'syncplay/resources/icon.icns',
'extra_scripts': 'syncplayServer.py', 'extra_scripts': 'syncplayServer.py',
'includes': {'PySide2.QtCore', 'PySide2.QtUiTools', 'PySide2.QtGui', 'PySide2.QtWidgets', 'certifi', 'cffi'}, 'includes': {'PySide2.QtCore', 'PySide2.QtUiTools', 'PySide2.QtGui', 'PySide2.QtWidgets', 'certifi', 'cffi', 'pem'},
'excludes': {'PySide', 'PySide.QtCore', 'PySide.QtUiTools', 'PySide.QtGui', 'tkinter'}, 'excludes': {'PySide', 'PySide.QtCore', 'PySide.QtUiTools', 'PySide.QtGui', 'tkinter'},
'qt_plugins': [ 'qt_plugins': [
'platforms/libqcocoa.dylib', 'platforms/libqcocoa.dylib',

View File

@ -1,4 +1,5 @@
certifi>=2018.11.29 certifi>=2018.11.29
pem>=21.2.0
twisted[tls]>=16.4.0 twisted[tls]>=16.4.0
appnope>=0.1.0; sys_platform == 'darwin' appnope>=0.1.0; sys_platform == 'darwin'
pypiwin32>=223; sys_platform == 'win32' pypiwin32>=223; sys_platform == 'win32'

View File

@ -19,11 +19,13 @@ from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor, task, defer, threads from twisted.internet import reactor, task, defer, threads
try: try:
SSL_CERT_FILE = None
import certifi import certifi
from twisted.internet.ssl import Certificate, optionsForClientTLS import pem
from twisted.internet.ssl import Certificate, optionsForClientTLS, trustRootFromCertificates
certPath = certifi.where() certPath = certifi.where()
if os.path.exists(certPath): if os.path.exists(certPath):
os.environ['SSL_CERT_FILE'] = certPath SSL_CERT_FILE = certPath
elif 'zip' in certPath: elif 'zip' in certPath:
import tempfile import tempfile
import zipfile import zipfile
@ -32,7 +34,7 @@ try:
archive = zipfile.ZipFile(zipPath, 'r') archive = zipfile.ZipFile(zipPath, 'r')
tmpDir = tempfile.gettempdir() tmpDir = tempfile.gettempdir()
extractedPath = archive.extract(memberPath, tmpDir) extractedPath = archive.extract(memberPath, tmpDir)
os.environ['SSL_CERT_FILE'] = extractedPath SSL_CERT_FILE = extractedPath
except: except:
pass pass
@ -831,10 +833,9 @@ class SyncplayClient(object):
port = int(port) port = int(port)
self._endpoint = HostnameEndpoint(reactor, host, port) self._endpoint = HostnameEndpoint(reactor, host, port)
try: try:
caCertFP = open(os.environ['SSL_CERT_FILE']) certs = pem.parse_file(SSL_CERT_FILE)
caCertTwisted = Certificate.loadPEM(caCertFP.read().encode('utf-8')) trustRoot = trustRootFromCertificates([Certificate.loadPEM(str(cert)) for cert in certs])
caCertFP.close() self.protocolFactory.options = optionsForClientTLS(hostname=host, trustRoot=trustRoot)
self.protocolFactory.options = optionsForClientTLS(hostname=host)
self._clientSupportsTLS = True self._clientSupportsTLS = True
except Exception as e: except Exception as e:
self.ui.showDebugMessage(str(e)) self.ui.showDebugMessage(str(e))