Skip to content

Commit 49def68

Browse files
amirejazclaude
andcommitted
Use net/url to extract host in IsClientIDMetadataDocumentURL
net/url is already imported by four other files in pkg/oauthproto, so the previous comment about keeping the package "import-free" was incorrect. url.Parse handles IPv6 brackets and other edge cases correctly without manual string manipulation. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 84e1fab commit 49def68

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

pkg/oauthproto/cimd.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package oauthproto
55

6-
import "strings"
6+
import (
7+
"net/url"
8+
"strings"
9+
)
710

811
// ToolHiveClientMetadataDocumentURL is the stable HTTPS URL where ToolHive's
912
// client metadata document is hosted. ToolHive presents this URL as its
@@ -28,23 +31,11 @@ func IsClientIDMetadataDocumentURL(clientID string) bool {
2831
// development and integration testing. These are the only HTTP URLs that
2932
// FetchClientMetadataDocument / validateCIMDClientURL also accept.
3033
if strings.HasPrefix(clientID, "http://") {
31-
return IsLoopbackHost(hostFromURL(clientID))
32-
}
33-
return false
34-
}
35-
36-
// hostFromURL extracts the host (and port, if present) from a raw URL string
37-
// for the narrow purpose of IsClientIDMetadataDocumentURL's loopback check.
38-
// It avoids importing net/url so this leaf package stays import-free. A full
39-
// URL parse is performed by FetchClientMetadataDocument before any network I/O.
40-
func hostFromURL(rawURL string) string {
41-
// Strip scheme
42-
rest := strings.TrimPrefix(rawURL, "http://")
43-
// Extract host (up to first '/', '?', or '#')
44-
for i, c := range rest {
45-
if c == '/' || c == '?' || c == '#' {
46-
return rest[:i]
34+
parsed, err := url.Parse(clientID)
35+
if err != nil {
36+
return false
4737
}
38+
return IsLoopbackHost(parsed.Host)
4839
}
49-
return rest
40+
return false
5041
}

0 commit comments

Comments
 (0)