From 2d6abfac491fa42d41dfc7c99f8a7e8f1101a114 Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:48:29 +0000 Subject: [PATCH] fix(coderd): ignore leading zeros in MCP OAuth2 resource origin ports RFC 9110 section 4.3.1 removes leading zeros from an origin's port, so a server URL on port 0443 now matches an advertised resource on the default HTTPS port. --- coderd/mcp.go | 6 +++++- coderd/mcp_internal_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/coderd/mcp.go b/coderd/mcp.go index bf7035fc6a9..1900dddb903 100644 --- a/coderd/mcp.go +++ b/coderd/mcp.go @@ -2004,7 +2004,11 @@ func sameHost(a, b string) bool { func portOrDefault(u *url.URL) string { if port := u.Port(); port != "" { - return port + // An origin's port has no leading zeros (RFC 9110 section 4.3.1). + if trimmed := strings.TrimLeft(port, "0"); trimmed != "" { + return trimmed + } + return "0" } switch strings.ToLower(u.Scheme) { case "http": diff --git a/coderd/mcp_internal_test.go b/coderd/mcp_internal_test.go index 491d939ebfb..eb464b4535b 100644 --- a/coderd/mcp_internal_test.go +++ b/coderd/mcp_internal_test.go @@ -381,6 +381,12 @@ func TestMCPOAuth2ResourceIndicator(t *testing.T) { advertised: []string{"http://mcp.example.com:80/mcp"}, want: "http://mcp.example.com:80/mcp", }, + { + name: "PortLeadingZerosAreIgnored", + serverURL: "https://mcp.example.com:0443/mcp", + advertised: []string{"https://mcp.example.com/mcp"}, + want: "https://mcp.example.com/mcp", + }, { name: "EscapedSlashIsNotSegmentBoundary", serverURL: "https://mcp.example.com/mcp%2Ftenant",