Skip to content

Commit 49084e2

Browse files
committed
Reuse regex from validation
1 parent 82df6f1 commit 49084e2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

coderd/httpapi/username.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var (
1212
usernameReplace = regexp.MustCompile("[^a-zA-Z0-9-]*")
1313
)
1414

15+
func UsernameValidRegexString() string {
16+
return usernameValid.String()
17+
}
18+
1519
// UsernameValid returns whether the input string is a valid username.
1620
func UsernameValid(str string) bool {
1721
if len(str) > 32 {

coderd/subdomain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"regexp"
77
"strings"
88

9+
"github.com/coder/coder/coderd/httpapi"
10+
911
"github.com/coder/coder/coderd/httpmw"
1012

1113
"github.com/go-chi/chi/v5"
@@ -70,7 +72,8 @@ func (api *API) handleSubdomain(middlewares ...func(http.Handler) http.Handler)
7072
}
7173

7274
var (
73-
nameRegex = `[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*`
75+
// Remove the "starts with" and "ends with" regex components.
76+
nameRegex = strings.Trim(httpapi.UsernameValidRegexString(), "^$")
7477
appURL = regexp.MustCompile(fmt.Sprintf(
7578
// {USERNAME}--{WORKSPACE_NAME}}--{{AGENT_NAME}}--{{PORT}}
7679
`^(?P<UserName>%[1]s)--(?P<WorkspaceName>%[1]s)(--(?P<AgentName>%[1]s))?--(?P<AppName>%[1]s)$`,

0 commit comments

Comments
 (0)