From be6a2b305ca8677a722bc7244ca3496dc6b7291a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Sat, 6 Jul 2024 00:02:43 +0200 Subject: [PATCH] socket: Fix `Host` header logic in negotiate_connection() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Håvard Sørbø --- lib/base/socket.vala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/base/socket.vala b/lib/base/socket.vala index 528b3a9c3..d7df7182e 100644 --- a/lib/base/socket.vala +++ b/lib/base/socket.vala @@ -152,7 +152,14 @@ namespace Frida { var request = new StringBuilder.sized (256); request.append ("GET /ws HTTP/1.1\r\n"); string protocol = (transport == TLS) ? "wss" : "ws"; - var uri = Uri.build (UriFlags.NONE, protocol, null, host, -1, "/ws", null, null); + NetworkAddress addr; + try { + addr = NetworkAddress.parse (host, 0); + } catch (GLib.Error e) { + throw new Error.INVALID_ARGUMENT ("%s", e.message); + } + uint16 port = addr.get_port (); + var uri = Uri.build (UriFlags.NONE, protocol, null, addr.get_hostname (), (port != 0) ? port : -1, "/ws", null, null); var msg = new Soup.Message.from_uri ("GET", uri); Soup.websocket_client_prepare_handshake (msg, origin, null, null); msg.request_headers.replace ("Host", make_host_header_value (uri)); @@ -221,7 +228,10 @@ namespace Frida { private string make_host_header_value (Uri uri) { unowned string host = uri.get_host (); - return Hostname.is_ip_address (host) ? @"[$host]" : host; + if (!Hostname.is_ip_address (host)) + return host; + var inet_addr = new InetAddress.from_string (host); + return (inet_addr.get_family () == IPV6) ? @"[$host]" : host; } public class WebService : Object {