Skip to content

Commit c770ff2

Browse files
committed
fix: allow ports in wildcard url configuration
This just forwards the port to the ui that generates urls. Our existing parsing + regex already supported ports for subdomain app requests.
1 parent 7e94606 commit c770ff2

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

coderd/workspaceapps/appurl/appurl.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ func CompileHostnamePattern(pattern string) (*regexp.Regexp, error) {
140140
if strings.Contains(pattern, "http:") || strings.Contains(pattern, "https:") {
141141
return nil, xerrors.Errorf("hostname pattern must not contain a scheme: %q", pattern)
142142
}
143-
if strings.Contains(pattern, ":") {
144-
return nil, xerrors.Errorf("hostname pattern must not contain a port: %q", pattern)
145-
}
143+
146144
if strings.HasPrefix(pattern, ".") || strings.HasSuffix(pattern, ".") {
147145
return nil, xerrors.Errorf("hostname pattern must not start or end with a period: %q", pattern)
148146
}
@@ -155,7 +153,17 @@ func CompileHostnamePattern(pattern string) (*regexp.Regexp, error) {
155153
if !strings.HasPrefix(pattern, "*") {
156154
return nil, xerrors.Errorf("hostname pattern must only contain an asterisk at the beginning: %q", pattern)
157155
}
158-
for i, label := range strings.Split(pattern, ".") {
156+
157+
// If there is a hostname:port, we only care about the hostname. For hostname
158+
// pattern reasons, we do not actually care what port the client is requesting.
159+
// Any port provided here is used for generating urls for the ui, not for
160+
// validation.
161+
hostname, _, err := net.SplitHostPort(pattern)
162+
if err == nil {
163+
pattern = hostname
164+
}
165+
166+
for i, label := range strings.Split(hostname, ".") {
159167
if i == 0 {
160168
// We have to allow the asterisk to be a valid hostname label, so
161169
// we strip the asterisk (which is only on the first one).

coderd/workspaceapps/appurl/appurl_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ func TestCompileHostnamePattern(t *testing.T) {
193193
pattern: "https://*.hi.com",
194194
errorContains: "must not contain a scheme",
195195
},
196-
{
197-
name: "Invalid_ContainsPort",
198-
pattern: "*.hi.com:8080",
199-
errorContains: "must not contain a port",
200-
},
201196
{
202197
name: "Invalid_StartPeriod",
203198
pattern: ".hi.com",
@@ -249,6 +244,13 @@ func TestCompileHostnamePattern(t *testing.T) {
249244
errorContains: "contains invalid label",
250245
},
251246

247+
{
248+
name: "Valid_ContainsPort",
249+
pattern: "*.hi.com:8080",
250+
// Although a port is provided, the regex already matches any port.
251+
// So it is ignored for validation purposes.
252+
expectedRegex: `([^.]+)\.hi\.com`,
253+
},
252254
{
253255
name: "Valid_Simple",
254256
pattern: "*.hi",

0 commit comments

Comments
 (0)