Skip to content

Commit 0e28397

Browse files
authored
fix: use CRC32 to shorten app subdomain (#9645)
1 parent 898971b commit 0e28397

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

coderd/httpapi/url.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package httpapi
22

33
import (
44
"fmt"
5+
"hash/crc32"
56
"net"
67
"regexp"
78
"strings"
@@ -18,6 +19,8 @@ var (
1819
nameRegex))
1920

2021
validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
22+
23+
crcTable = crc32.MakeTable(crc32.IEEE)
2124
)
2225

2326
// ApplicationURL is a parsed application URL hostname.
@@ -39,7 +42,12 @@ func (a ApplicationURL) String() string {
3942
_, _ = appURL.WriteString(a.WorkspaceName)
4043
_, _ = appURL.WriteString("--")
4144
_, _ = appURL.WriteString(a.Username)
42-
return appURL.String()
45+
hostname := appURL.String()
46+
47+
if len(hostname) < 64 { // max length for the subdomain level
48+
return hostname
49+
}
50+
return fmt.Sprintf("app-%08x", crc32.Checksum([]byte(hostname), crcTable))
4351
}
4452

4553
// ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If

coderd/httpapi/url_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func TestApplicationURLString(t *testing.T) {
4242
},
4343
Expected: "8080--agent--workspace--user",
4444
},
45+
{
46+
Name: "LongAppName",
47+
URL: httpapi.ApplicationURL{
48+
AppSlugOrPort: "0123456789012345678901234567890123456789",
49+
AgentName: "agent",
50+
WorkspaceName: "workspace",
51+
Username: "user",
52+
},
53+
Expected: "app-90667f72",
54+
},
4555
}
4656

4757
for _, c := range testCases {

coderd/workspaceapps/apptest/apptest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
13661366
}, testutil.WaitLong, testutil.IntervalFast, "stats not reported")
13671367

13681368
assert.Equal(t, workspaceapps.AccessMethodPath, stats[0].AccessMethod)
1369-
assert.Equal(t, "test-app-owner", stats[0].SlugOrPort)
1369+
assert.Equal(t, proxyTestAppNameOwner, stats[0].SlugOrPort)
13701370
assert.Equal(t, 1, stats[0].Requests)
13711371
})
13721372
}

coderd/workspaceapps/apptest/setup.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import (
3232

3333
const (
3434
proxyTestAgentName = "agent-name"
35-
proxyTestAppNameFake = "test-app-fake"
36-
proxyTestAppNameOwner = "test-app-owner"
37-
proxyTestAppNameAuthenticated = "test-app-authenticated"
38-
proxyTestAppNamePublic = "test-app-public"
35+
proxyTestAppNameFake = "taf"
36+
proxyTestAppNameOwner = "tao"
37+
proxyTestAppNameAuthenticated = "taa"
38+
proxyTestAppNamePublic = "tap"
3939
proxyTestAppQuery = "query=true"
4040
proxyTestAppBody = "hello world from apps test"
4141

0 commit comments

Comments
 (0)