Skip to content

Commit 14f5439

Browse files
committed
http: Add missing tls setup when using proxy tunnel
1 parent 5b4759b commit 14f5439

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

lib/std/http/Client.zig

+34-1
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@ pub fn connectTunnel(
14341434
proxy: *Proxy,
14351435
tunnel_host: []const u8,
14361436
tunnel_port: u16,
1437+
tunnel_protocol: Connection.Protocol,
14371438
) !*Connection {
14381439
if (!proxy.supports_connect) return error.TunnelNotSupported;
14391440

@@ -1487,6 +1488,38 @@ pub fn connectTunnel(
14871488
conn.port = tunnel_port;
14881489
conn.closing = false;
14891490

1491+
if (tunnel_protocol == .tls) {
1492+
if (disable_tls) unreachable;
1493+
1494+
conn.tls_client = try client.allocator.create(std.crypto.tls.Client);
1495+
errdefer client.allocator.destroy(conn.tls_client);
1496+
1497+
const ssl_key_log_file: ?std.fs.File = if (std.options.http_enable_ssl_key_log_file) ssl_key_log_file: {
1498+
const ssl_key_log_path = std.process.getEnvVarOwned(client.allocator, "SSLKEYLOGFILE") catch |err| switch (err) {
1499+
error.EnvironmentVariableNotFound, error.InvalidWtf8 => break :ssl_key_log_file null,
1500+
error.OutOfMemory => return error.OutOfMemory,
1501+
};
1502+
defer client.allocator.free(ssl_key_log_path);
1503+
break :ssl_key_log_file std.fs.cwd().createFile(ssl_key_log_path, .{
1504+
.truncate = false,
1505+
.mode = switch (builtin.os.tag) {
1506+
.windows, .wasi => 0,
1507+
else => 0o600,
1508+
},
1509+
}) catch null;
1510+
} else null;
1511+
errdefer if (ssl_key_log_file) |key_log_file| key_log_file.close();
1512+
1513+
conn.tls_client.* = std.crypto.tls.Client.init(conn.stream, .{
1514+
.host = .{ .explicit = tunnel_host },
1515+
.ca = .{ .bundle = client.ca_bundle },
1516+
.ssl_key_log_file = ssl_key_log_file,
1517+
}) catch return error.TlsInitializationFailed;
1518+
// This is appropriate for HTTPS because the HTTP headers contain
1519+
// the content length which is used to detect truncation attacks.
1520+
conn.tls_client.allow_truncation_attacks = true;
1521+
conn.protocol = .tls;
1522+
}
14901523
return conn;
14911524
}) catch {
14921525
// something went wrong with the tunnel
@@ -1524,7 +1557,7 @@ pub fn connect(
15241557
}
15251558

15261559
if (proxy.supports_connect) tunnel: {
1527-
return connectTunnel(client, proxy, host, port) catch |err| switch (err) {
1560+
return connectTunnel(client, proxy, host, port, protocol) catch |err| switch (err) {
15281561
error.TunnelNotSupported => break :tunnel,
15291562
else => |e| return e,
15301563
};

0 commit comments

Comments
 (0)