Skip to content

Commit db583be

Browse files
committed
Add support for custom HTTP user agent
1 parent 761892b commit db583be

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

sslyze/connection_helpers/http_request_generator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from sslyze import __version__
24

35

@@ -16,5 +18,7 @@ class HttpRequestGenerator:
1618
)
1719

1820
@classmethod
19-
def get_request(cls, host: str, path: str = "/") -> bytes:
20-
return cls.HTTP_GET_FORMAT.format(host=host, path=path, user_agent=cls.DEFAULT_USER_AGENT).encode("utf-8")
21+
def get_request(cls, host: str, path: str = "/", user_agent: Optional[str] = None) -> bytes:
22+
if not user_agent:
23+
user_agent = cls.DEFAULT_USER_AGENT
24+
return cls.HTTP_GET_FORMAT.format(host=host, path=path, user_agent=user_agent).encode("utf-8")

sslyze/plugins/early_data_plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ def _test_early_data_support(server_info: ServerConnectivityInfo) -> bool:
8989
# Perform an SSL handshake and keep the session
9090
ssl_connection.connect()
9191
# Send and receive data for the TLS session to be created
92-
ssl_connection.ssl_client.write(HttpRequestGenerator.get_request(host=server_info.server_location.hostname))
92+
ssl_connection.ssl_client.write(
93+
HttpRequestGenerator.get_request(
94+
host=server_info.server_location.hostname,
95+
user_agent=server_info.network_configuration.http_user_agent,
96+
)
97+
)
9398
ssl_connection.ssl_client.read(2048)
9499
session = ssl_connection.ssl_client.get_session()
95100
except ServerRejectedTlsHandshake:

sslyze/plugins/http_headers_plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ def _retrieve_and_analyze_http_response(server_info: ServerConnectivityInfo) ->
200200
# Send an HTTP GET request to the server
201201
ssl_connection.ssl_client.write(
202202
HttpRequestGenerator.get_request(
203-
host=server_info.network_configuration.tls_server_name_indication, path=next_location_path
203+
host=server_info.network_configuration.tls_server_name_indication,
204+
path=next_location_path,
205+
user_agent=server_info.network_configuration.http_user_agent,
204206
)
205207
)
206208
http_response = HttpResponseParser.parse_from_ssl_connection(ssl_connection.ssl_client)
@@ -225,7 +227,9 @@ def _retrieve_and_analyze_http_response(server_info: ServerConnectivityInfo) ->
225227

226228
# Prepare the results
227229
initial_http_request = HttpRequestGenerator.get_request(
228-
host=server_info.network_configuration.tls_server_name_indication, path="/"
230+
host=server_info.network_configuration.tls_server_name_indication,
231+
path="/",
232+
user_agent=server_info.network_configuration.http_user_agent,
229233
).decode("ascii")
230234

231235
if http_error_trace:

sslyze/server_setting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class ServerNetworkConfiguration:
173173
xmpp_to_hostname: The hostname to set within the `to` attribute of the XMPP stream. If not supplied, the
174174
server's hostname will be used. Should only be set if the supplied `tls_wrapped_protocol` is an
175175
XMPP protocol.
176+
http_user_agent: The User-Agent to send in HTTP requests. If not supplied, a default Chrome-like
177+
is used that includes the sslyze version.
176178
network_timeout: The timeout (in seconds) to be used when attempting to establish a connection to the
177179
server.
178180
network_max_retries: The number of retries SSLyze will perform when attempting to establish a connection
@@ -184,6 +186,7 @@ class ServerNetworkConfiguration:
184186
tls_client_auth_credentials: Optional[ClientAuthenticationCredentials] = None
185187

186188
xmpp_to_hostname: Optional[str] = None
189+
http_user_agent: Optional[str] = None
187190

188191
network_timeout: int = 5
189192
network_max_retries: int = 3

0 commit comments

Comments
 (0)