Skip to content

Commit

Permalink
Fix client for HTTPS endpoints for python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavsingh committed Aug 12, 2024
1 parent 74c42f6 commit 0a2dc9f
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions proxy/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import ssl
from typing import Optional

import certifi

from .parser import HttpParser, httpParserTypes
from ..common.types import TcpOrTlsSocket
from ..common.utils import build_http_request, new_socket_connection
from ..common.constants import HTTPS_PROTO, DEFAULT_TIMEOUT
from ..common.constants import (
HTTPS_PROTO, DEFAULT_TIMEOUT, DEFAULT_SSL_CONTEXT_OPTIONS,
)


def client(
Expand Down Expand Up @@ -41,18 +46,18 @@ def client(
conn = new_socket_connection((host.decode(), port))
except ConnectionRefusedError:
return None
try:
sock = (
ssl.wrap_socket(sock=conn, ssl_version=ssl.PROTOCOL_TLSv1_2)
if scheme == HTTPS_PROTO
else conn
)
except Exception:
conn.close()
return None
parser = HttpParser(
httpParserTypes.RESPONSE_PARSER,
)
sock: TcpOrTlsSocket = conn

Check warning on line 49 in proxy/http/client.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/client.py#L49

Added line #L49 was not covered by tests
if scheme == HTTPS_PROTO:
try:
ctx = ssl.SSLContext(protocol=(ssl.PROTOCOL_TLS_CLIENT))
ctx.options |= DEFAULT_SSL_CONTEXT_OPTIONS
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(cafile=certifi.where())
sock = ctx.wrap_socket(conn, server_hostname=host.decode())

Check failure

Code scanning / CodeQL

Use of insecure SSL/TLS version High

Insecure SSL/TLS protocol version TLSv1 allowed by
call to ssl.SSLContext
.
Insecure SSL/TLS protocol version TLSv1_1 allowed by
call to ssl.SSLContext
.
except Exception:
conn.close()
return None
parser = HttpParser(httpParserTypes.RESPONSE_PARSER)

Check warning on line 60 in proxy/http/client.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/client.py#L51-L60

Added lines #L51 - L60 were not covered by tests
sock.settimeout(timeout)
try:
sock.sendall(request)
Expand Down

0 comments on commit 0a2dc9f

Please sign in to comment.