Skip to content

Commit ffa2231

Browse files
committed
Enforce IPv4 for localhost
Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent d87251b commit ffa2231

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

cmd/thv/app/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stacklok/toolhive/pkg/labels"
1414
"github.com/stacklok/toolhive/pkg/logger"
1515
"github.com/stacklok/toolhive/pkg/secrets"
16+
"github.com/stacklok/toolhive/pkg/transport"
1617
)
1718

1819
var configCmd = &cobra.Command{
@@ -262,7 +263,7 @@ func addRunningMCPsToClient(ctx context.Context, clientName string) error {
262263
}
263264

264265
// Generate URL for the MCP server
265-
url := client.GenerateMCPServerURL("localhost", port, name)
266+
url := client.GenerateMCPServerURL(transport.LocalhostIPv4, port, name)
266267

267268
// Update each configuration file
268269
for _, clientConfig := range clientConfigs {

cmd/thv/app/list.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stacklok/toolhive/pkg/labels"
1414
"github.com/stacklok/toolhive/pkg/lifecycle"
1515
"github.com/stacklok/toolhive/pkg/logger"
16+
"github.com/stacklok/toolhive/pkg/transport"
1617
)
1718

1819
var listCmd = &cobra.Command{
@@ -28,10 +29,7 @@ var (
2829
)
2930

3031
// Constants for list command
31-
const (
32-
defaultHost = "localhost"
33-
unknownTransport = "unknown"
34-
)
32+
const unknownTransport = "unknown"
3533

3634
// ContainerOutput represents container information for JSON output
3735
type ContainerOutput struct {
@@ -101,9 +99,9 @@ func printJSONOutput(containers []rt.ContainerInfo) error {
10199
}
102100

103101
// Get transport type from labels
104-
transport := labels.GetTransportType(c.Labels)
105-
if transport == "" {
106-
transport = unknownTransport
102+
t := labels.GetTransportType(c.Labels)
103+
if t == "" {
104+
t = unknownTransport
107105
}
108106

109107
// Get tool type from labels
@@ -118,15 +116,15 @@ func printJSONOutput(containers []rt.ContainerInfo) error {
118116
// Generate URL for the MCP server
119117
url := ""
120118
if port > 0 {
121-
url = client.GenerateMCPServerURL(defaultHost, port, name)
119+
url = client.GenerateMCPServerURL(transport.LocalhostIPv4, port, name)
122120
}
123121

124122
output = append(output, ContainerOutput{
125123
ID: truncatedID,
126124
Name: name,
127125
Image: c.Image,
128126
State: c.State,
129-
Transport: transport,
127+
Transport: t,
130128
ToolType: toolType,
131129
Port: port,
132130
URL: url,
@@ -174,7 +172,7 @@ func printMCPServersOutput(containers []rt.ContainerInfo) error {
174172
// Generate URL for the MCP server
175173
url := ""
176174
if port > 0 {
177-
url = client.GenerateMCPServerURL(defaultHost, port, name)
175+
url = client.GenerateMCPServerURL(transport.LocalhostIPv4, port, name)
178176
}
179177

180178
// Add the MCP server to the map
@@ -217,9 +215,9 @@ func printTextOutput(containers []rt.ContainerInfo) {
217215
}
218216

219217
// Get transport type from labels
220-
transport := labels.GetTransportType(c.Labels)
221-
if transport == "" {
222-
transport = unknownTransport
218+
t := labels.GetTransportType(c.Labels)
219+
if t == "" {
220+
t = unknownTransport
223221
}
224222

225223
// Get port from labels
@@ -231,7 +229,7 @@ func printTextOutput(containers []rt.ContainerInfo) {
231229
// Generate URL for the MCP server
232230
url := ""
233231
if port > 0 {
234-
url = client.GenerateMCPServerURL(defaultHost, port, name)
232+
url = client.GenerateMCPServerURL(transport.LocalhostIPv4, port, name)
235233
}
236234

237235
// Print container information
@@ -240,7 +238,7 @@ func printTextOutput(containers []rt.ContainerInfo) {
240238
name,
241239
c.Image,
242240
c.State,
243-
transport,
241+
t,
244242
port,
245243
url,
246244
)

cmd/thv/app/proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
)
3333

3434
func init() {
35-
proxyCmd.Flags().StringVar(&proxyHost, "host", transport.LocalhostName, "Host for the HTTP proxy to listen on (IP or hostname)")
35+
proxyCmd.Flags().StringVar(&proxyHost, "host", transport.LocalhostIPv4, "Host for the HTTP proxy to listen on (IP or hostname)")
3636
proxyCmd.Flags().IntVar(&proxyPort, "port", 0, "Port for the HTTP proxy to listen on (host port)")
3737
proxyCmd.Flags().StringVar(
3838
&proxyTargetURI,

cmd/thv/app/run.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ var (
7575
func init() {
7676
runCmd.Flags().StringVar(&runTransport, "transport", "stdio", "Transport mode (sse or stdio)")
7777
runCmd.Flags().StringVar(&runName, "name", "", "Name of the MCP server (auto-generated from image if not provided)")
78-
runCmd.Flags().StringVar(&runHost, "host", transport.LocalhostName, "Host for the HTTP proxy to listen on (IP or hostname)")
78+
runCmd.Flags().StringVar(&runHost, "host", transport.LocalhostIPv4, "Host for the HTTP proxy to listen on (IP or hostname)")
7979
runCmd.Flags().IntVar(&runPort, "port", 0, "Port for the HTTP proxy to listen on (host port)")
8080
runCmd.Flags().IntVar(&runTargetPort, "target-port", 0, "Port for the container to expose (only applicable to SSE transport)")
8181
runCmd.Flags().StringVar(
8282
&runTargetHost,
8383
"target-host",
84-
"localhost",
84+
transport.LocalhostIPv4,
8585
"Host to forward traffic to (only applicable to SSE transport)")
8686
runCmd.Flags().StringVar(
8787
&runPermissionProfile,

pkg/transport/sse.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
const (
1818
// LocalhostName is the standard hostname for localhost
1919
LocalhostName = "localhost"
20+
// LocalhostIPv4 is the standard IPv4 address for localhost
21+
LocalhostIPv4 = "127.0.0.1"
2022
)
2123

2224
// SSETransport implements the Transport interface using Server-Sent Events.
@@ -56,12 +58,12 @@ func NewSSETransport(
5658
middlewares ...types.Middleware,
5759
) *SSETransport {
5860
if host == "" {
59-
host = LocalhostName
61+
host = LocalhostIPv4
6062
}
6163

6264
// If targetHost is not specified, default to localhost
6365
if targetHost == "" {
64-
targetHost = LocalhostName
66+
targetHost = LocalhostIPv4
6567
}
6668

6769
return &SSETransport{

0 commit comments

Comments
 (0)