Skip to content

Commit f63690d

Browse files
authored
fix: use net.JoinHostPort for host:port formatting (#2603)
Replace fmt.Sprintf("%s:%d", ...) with net.JoinHostPort across the orchestrator, API discovery, cluster instance creation, and sandbox proxy code paths. Building host:port strings via fmt.Sprintf produces invalid addresses for IPv6 hosts because the IP literal is not wrapped in brackets, making the result indistinguishable from "host:port" parsers and breaking dialing/URL parsing. net.JoinHostPort handles both IPv4 and IPv6 correctly by bracketing IPv6 literals as required by RFC 3986.
1 parent 328e313 commit f63690d

5 files changed

Lines changed: 13 additions & 5 deletions

File tree

packages/api/internal/analytics_collector/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"fmt"
8+
"net"
89

910
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
1011
"google.golang.org/grpc"
@@ -38,7 +39,7 @@ func NewAnalytics(host, grpcAPIKey string) (*Analytics, error) {
3839
})
3940

4041
conn, err := grpc.NewClient(
41-
fmt.Sprintf("%s:443", host),
42+
net.JoinHostPort(host, "443"),
4243
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
4344
grpc.WithPerRPCCredentials(newGRPCAPIKey(grpcAPIKey)),
4445
grpc.WithAuthority(host),

packages/api/internal/clusters/cluster.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"errors"
66
"fmt"
77
"math/rand/v2"
8+
"net"
89
"net/http"
10+
"strconv"
911
"sync"
1012
"time"
1113

@@ -80,7 +82,7 @@ func newLocalCluster(
8082
instances := smap.New[*Instance]()
8183
instanceCreation := func(ctx context.Context, item discovery.Item) (*Instance, error) {
8284
// For local cluster we are doing direct connection to instance IP and API port and without additional cluster auth.
83-
return newInstance(ctx, tel, nil, clusterID, item, fmt.Sprintf("%s:%d", item.LocalIPAddress, item.LocalInstanceApiPort), false)
85+
return newInstance(ctx, tel, nil, clusterID, item, net.JoinHostPort(item.LocalIPAddress, strconv.FormatUint(uint64(item.LocalInstanceApiPort), 10)), false)
8486
}
8587

8688
store := instancesSyncStore{clusterID: clusterID, instances: instances, discovery: storeDiscovery, instanceCreation: instanceCreation}

packages/api/internal/orchestrator/discovery/nomad.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package discovery
33
import (
44
"context"
55
"fmt"
6+
"net"
7+
"strconv"
68

79
nomadapi "github.com/hashicorp/nomad/api"
810

@@ -48,7 +50,7 @@ func (d *nomadDiscovery) ListNodes(ctx context.Context) ([]Node, error) {
4850
out = append(out, Node{
4951
ShortID: shortID,
5052
IPAddress: n.Address,
51-
OrchestratorAddress: fmt.Sprintf("%s:%d", n.Address, consts.OrchestratorAPIPort),
53+
OrchestratorAddress: net.JoinHostPort(n.Address, strconv.FormatUint(uint64(consts.OrchestratorAPIPort), 10)),
5254
})
5355
}
5456

packages/orchestrator/pkg/cfg/model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package cfg
22

33
import (
44
"fmt"
5+
"net"
56
"os"
67
"path/filepath"
78
"reflect"
9+
"strconv"
810
"strings"
911
"time"
1012

@@ -108,7 +110,7 @@ func (c Config) NodeAddress() *string {
108110
return nil
109111
}
110112

111-
addr := fmt.Sprintf("%s:%d", c.NodeIP, c.GRPCPort)
113+
addr := net.JoinHostPort(c.NodeIP, strconv.FormatUint(uint64(c.GRPCPort), 10))
112114

113115
return &addr
114116
}

packages/orchestrator/pkg/proxy/proxy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/subtle"
66
"fmt"
7+
"net"
78
"net/http"
89
"net/url"
910
"strconv"
@@ -96,7 +97,7 @@ func NewSandboxProxy(meterProvider metric.MeterProvider, port uint16, sandboxes
9697

9798
url := &url.URL{
9899
Scheme: "http",
99-
Host: fmt.Sprintf("%s:%d", sbx.Slot.HostIPString(), port),
100+
Host: net.JoinHostPort(sbx.Slot.HostIPString(), strconv.FormatUint(port, 10)),
100101
}
101102

102103
logger := logger.L().With(

0 commit comments

Comments
 (0)