From 93e823931b8245bdda37a0a283fb70e3df6a4684 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 24 Feb 2026 23:46:43 -0600 Subject: [PATCH] fix: allow sharing ports >9999 (#22273) Closes https://github.com/coder/coder/issues/22267 --- coderd/workspaceapps/appurl/appurl.go | 5 ++++- coderd/workspaceapps/appurl/appurl_test.go | 24 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/coderd/workspaceapps/appurl/appurl.go b/coderd/workspaceapps/appurl/appurl.go index 65dced6c10..fc8ea791d7 100644 --- a/coderd/workspaceapps/appurl/appurl.go +++ b/coderd/workspaceapps/appurl/appurl.go @@ -19,7 +19,10 @@ var ( appURL = regexp.MustCompile(fmt.Sprintf( `^(?P%[1]s)(?:--(?P%[1]s))?--(?P%[1]s)--(?P%[1]s)$`, nameRegex)) - PortRegex = regexp.MustCompile(`^\d{4}s?$`) + // PortRegex should not be able to be greater than 65535. In usage though, if a + // user tries to use a greater port, the proxy will just block it and not cause + // any issues. This is a good enough regex check. + PortRegex = regexp.MustCompile(`^\d{4,5}s?$`) validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`) ) diff --git a/coderd/workspaceapps/appurl/appurl_test.go b/coderd/workspaceapps/appurl/appurl_test.go index a02a2a1efb..d2bf326494 100644 --- a/coderd/workspaceapps/appurl/appurl_test.go +++ b/coderd/workspaceapps/appurl/appurl_test.go @@ -193,6 +193,16 @@ func TestParseSubdomainAppURL(t *testing.T) { Username: "user", }, }, + { + Name: "Port(5)--Agent--Workspace--User", + Subdomain: "12412--agent--workspace--user", + Expected: appurl.ApplicationURL{ + AppSlugOrPort: "12412", + AgentName: "agent", + WorkspaceName: "workspace", + Username: "user", + }, + }, { Name: "Port--Agent--Workspace--User", Subdomain: "8080s--agent--workspace--user", @@ -225,11 +235,11 @@ func TestParseSubdomainAppURL(t *testing.T) { }, }, { - Name: "5DigitAppSlug--Workspace--User", - Subdomain: "30000--workspace--user", + Name: "5DigitPort--agent--Workspace--User", + Subdomain: "30000--agent--workspace--user", Expected: appurl.ApplicationURL{ AppSlugOrPort: "30000", - AgentName: "", + AgentName: "agent", WorkspaceName: "workspace", Username: "user", }, @@ -599,6 +609,14 @@ func TestURLGenerationVsParsing(t *testing.T) { Name: "5DigitAppSlug_AgentOmittedInParsing", AppSlugOrPort: "30000", AgentName: "agent", + ExpectedParsed: "agent", + }, + { + // 6 digits is not a valid port, so it is treated as an app slug. + // App slugs do not require the agent name, so it is dropped + Name: "6DigitAppSlug_AgentOmittedInParsing", + AppSlugOrPort: "300000", + AgentName: "agent", ExpectedParsed: "", }, }