From 9fd5d05fdc98dc1a8a124f2a7fd258b2e8f6cb60 Mon Sep 17 00:00:00 2001 From: Erik Chen Date: Thu, 9 Apr 2015 19:17:01 -0400 Subject: [PATCH 1/3] Supress SSL certificate errors. By default, each SSL certificate error generates a full traceback. This is pretty pointless, since we know that all SSL requests are going to have certificate errors. --- httpproxy.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/httpproxy.py b/httpproxy.py index b4a7d19..64afc18 100644 --- a/httpproxy.py +++ b/httpproxy.py @@ -20,6 +20,7 @@ import socket import SocketServer import ssl +import sys import time import urlparse @@ -284,6 +285,18 @@ def cleanup(self): def get_active_request_count(self): return self.num_active_requests + def handle_error(self, request, client_address): + exc_type, exc_value, exc_traceback = sys.exc_info() + exceptions_to_ignore = [ + 'tlsv1 alert unknown ca', + 'sslv3 alert certificate unknown', + ] + for exception_to_ignore in exceptions_to_ignore: + if exception_to_ignore in str(exc_value): + return + + raise + class HttpsProxyServer(HttpProxyServer): """SSL server that generates certs for each host.""" From 2c69d8a5fcb58e46394c1cdb77229ea032543f59 Mon Sep 17 00:00:00 2001 From: Erik Chen Date: Thu, 9 Apr 2015 21:42:57 -0400 Subject: [PATCH 2/3] Only supress SSL certificate errors on servers that are serving HTTPS traffic. --- httpproxy.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/httpproxy.py b/httpproxy.py index 64afc18..581fdf1 100644 --- a/httpproxy.py +++ b/httpproxy.py @@ -30,6 +30,23 @@ import sslproxy +def _HandleSSLCertificateError(): + """ + This method is intended to be called from + BaseHTTPServer.HTTPServer.handle_error(). + """ + exc_type, exc_value, exc_traceback = sys.exc_info() + exceptions_to_ignore = [ + 'tlsv1 alert unknown ca', + 'sslv3 alert certificate unknown', + ] + for exception_to_ignore in exceptions_to_ignore: + if exception_to_ignore in str(exc_value): + return + + raise + + class HttpProxyError(Exception): """Module catch-all error.""" pass @@ -285,18 +302,6 @@ def cleanup(self): def get_active_request_count(self): return self.num_active_requests - def handle_error(self, request, client_address): - exc_type, exc_value, exc_traceback = sys.exc_info() - exceptions_to_ignore = [ - 'tlsv1 alert unknown ca', - 'sslv3 alert certificate unknown', - ] - for exception_to_ignore in exceptions_to_ignore: - if exception_to_ignore in str(exc_value): - return - - raise - class HttpsProxyServer(HttpProxyServer): """SSL server that generates certs for each host.""" @@ -333,6 +338,9 @@ def get_certificate(self, host): self._host_to_cert_map[host] = cert return cert + def handle_error(self, request, client_address): + _HandleSSLCertificateError() + class SingleCertHttpsProxyServer(HttpProxyServer): """SSL server.""" @@ -346,6 +354,9 @@ def __init__(self, http_archive_fetch, custom_handlers, do_handshake_on_connect=False) # Ancestor class, DaemonServer, calls serve_forever() during its __init__. + def handle_error(self, request, client_address): + _HandleSSLCertificateError() + class HttpToHttpsProxyServer(HttpProxyServer): """Listens for HTTP requests but sends them to the target as HTTPS requests""" @@ -353,3 +364,6 @@ class HttpToHttpsProxyServer(HttpProxyServer): def __init__(self, http_archive_fetch, custom_handlers, **kwargs): HttpProxyServer.__init__(self, http_archive_fetch, custom_handlers, is_ssl=True, protocol='HTTP-to-HTTPS', **kwargs) + + def handle_error(self, request, client_address): + _HandleSSLCertificateError() From 51a19afcc4c7e247ce03bfb927ba51605455c921 Mon Sep 17 00:00:00 2001 From: Erik Chen Date: Thu, 9 Apr 2015 21:56:39 -0400 Subject: [PATCH 3/3] Suppress all SSL errors. --- httpproxy.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/httpproxy.py b/httpproxy.py index 581fdf1..c5e6853 100644 --- a/httpproxy.py +++ b/httpproxy.py @@ -36,13 +36,8 @@ def _HandleSSLCertificateError(): BaseHTTPServer.HTTPServer.handle_error(). """ exc_type, exc_value, exc_traceback = sys.exc_info() - exceptions_to_ignore = [ - 'tlsv1 alert unknown ca', - 'sslv3 alert certificate unknown', - ] - for exception_to_ignore in exceptions_to_ignore: - if exception_to_ignore in str(exc_value): - return + if isinstance(exc_value, ssl.SSLError): + return raise