From dc0dea4419ba6074c950f53debd194b17afcde7f Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 29 Apr 2024 19:32:59 -0700 Subject: [PATCH] fix(cli): Fix client IPv6 URI generation for gRPC connections (#3830) Connecting to gRPC repository API using an IPv6 address does not correctly configure the URI for the client's dial. Reconstructing the parsed URL into `hostname + ":" + port` will remove the square brackets required for IPv6 addressing, resulting in a `too many colons in address` error. Fix the issue by instead using the helper `net.JoinHostPort()`, which will add square brackets for IPv6 hostnames. Tested by running `TestServer` with `httptest.serve` flag set, forcing the test server to listen on the IPv6 loopback: ``` cd internal/server go test -v -run=TestServer$ --httptest.serve=[::1]:0 ./server ``` Fails without fix: ``` server_test.go:48: Error Trace: /workspaces/kopia/internal/server/server_test.go:48 Error: Received unexpected error: failed to exit idle mode: invalid target address ::1:45373, error info: address ::1:45373:443: too many colons in address ``` Passes with fix: ``` --- PASS: TestServer (0.81s) PASS ``` --- repo/grpc_repository_client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repo/grpc_repository_client.go b/repo/grpc_repository_client.go index 80d6f4407..ba568ac2f 100644 --- a/repo/grpc_repository_client.go +++ b/repo/grpc_repository_client.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "io" + "net" "net/url" "runtime" "sync" @@ -837,7 +838,7 @@ func openGRPCAPIRepository(ctx context.Context, si *APIServerInfo, password stri return nil, errors.Errorf("invalid server address, must be 'https://host:port' or 'unix+https://") } - uri := u.Hostname() + ":" + u.Port() + uri := net.JoinHostPort(u.Hostname(), u.Port()) if u.Scheme == "unix+https" { uri = "unix:" + u.Path }