|
| 1 | +package codersdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/moby/moby/pkg/namesgenerator" |
| 8 | + "golang.org/x/xerrors" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + UsernameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$") |
| 13 | + usernameReplace = regexp.MustCompile("[^a-zA-Z0-9-]*") |
| 14 | + |
| 15 | + templateVersionName = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`) |
| 16 | + templateDisplayName = regexp.MustCompile(`^[^\s](.*[^\s])?$`) |
| 17 | +) |
| 18 | + |
| 19 | +// UsernameFrom returns a best-effort username from the provided string. |
| 20 | +// |
| 21 | +// It first attempts to validate the incoming string, which will |
| 22 | +// be returned if it is valid. It then will attempt to extract |
| 23 | +// the username from an email address. If no success happens during |
| 24 | +// these steps, a random username will be returned. |
| 25 | +func UsernameFrom(str string) string { |
| 26 | + if valid := NameValid(str); valid == nil { |
| 27 | + return str |
| 28 | + } |
| 29 | + emailAt := strings.LastIndex(str, "@") |
| 30 | + if emailAt >= 0 { |
| 31 | + str = str[:emailAt] |
| 32 | + } |
| 33 | + str = usernameReplace.ReplaceAllString(str, "") |
| 34 | + if valid := NameValid(str); valid == nil { |
| 35 | + return str |
| 36 | + } |
| 37 | + return strings.ReplaceAll(namesgenerator.GetRandomName(1), "_", "-") |
| 38 | +} |
| 39 | + |
| 40 | +// NameValid returns whether the input string is a valid name. |
| 41 | +// It is a generic validator for any name (user, workspace, template, role name, etc.). |
| 42 | +func NameValid(str string) error { |
| 43 | + if len(str) > 32 { |
| 44 | + return xerrors.New("must be <= 32 characters") |
| 45 | + } |
| 46 | + if len(str) < 1 { |
| 47 | + return xerrors.New("must be >= 1 character") |
| 48 | + } |
| 49 | + // Avoid conflicts with routes like /templates/new and /groups/create. |
| 50 | + if str == "new" || str == "create" { |
| 51 | + return xerrors.Errorf("cannot use %q as a name", str) |
| 52 | + } |
| 53 | + matched := UsernameValidRegex.MatchString(str) |
| 54 | + if !matched { |
| 55 | + return xerrors.New("must be alphanumeric with hyphens") |
| 56 | + } |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// TemplateVersionNameValid returns whether the input string is a valid template version name. |
| 61 | +func TemplateVersionNameValid(str string) error { |
| 62 | + if len(str) > 64 { |
| 63 | + return xerrors.New("must be <= 64 characters") |
| 64 | + } |
| 65 | + matched := templateVersionName.MatchString(str) |
| 66 | + if !matched { |
| 67 | + return xerrors.New("must be alphanumeric with underscores and dots") |
| 68 | + } |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// DisplayNameValid returns whether the input string is a valid template display name. |
| 73 | +func DisplayNameValid(str string) error { |
| 74 | + if len(str) == 0 { |
| 75 | + return nil // empty display_name is correct |
| 76 | + } |
| 77 | + if len(str) > 64 { |
| 78 | + return xerrors.New("must be <= 64 characters") |
| 79 | + } |
| 80 | + matched := templateDisplayName.MatchString(str) |
| 81 | + if !matched { |
| 82 | + return xerrors.New("must be alphanumeric with spaces") |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// UserRealNameValid returns whether the input string is a valid real user name. |
| 88 | +func UserRealNameValid(str string) error { |
| 89 | + if len(str) > 128 { |
| 90 | + return xerrors.New("must be <= 128 characters") |
| 91 | + } |
| 92 | + |
| 93 | + if strings.TrimSpace(str) != str { |
| 94 | + return xerrors.New("must not have leading or trailing whitespace") |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// GroupNameValid returns whether the input string is a valid group name. |
| 100 | +func GroupNameValid(str string) error { |
| 101 | + // 36 is to support using UUIDs as the group name. |
| 102 | + if len(str) > 36 { |
| 103 | + return xerrors.New("must be <= 36 characters") |
| 104 | + } |
| 105 | + // Avoid conflicts with routes like /groups/new and /groups/create. |
| 106 | + if str == "new" || str == "create" { |
| 107 | + return xerrors.Errorf("cannot use %q as a name", str) |
| 108 | + } |
| 109 | + matched := UsernameValidRegex.MatchString(str) |
| 110 | + if !matched { |
| 111 | + return xerrors.New("must be alphanumeric with hyphens") |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// NormalizeUserRealName normalizes a user name such that it will pass |
| 117 | +// validation by UserRealNameValid. This is done to avoid blocking |
| 118 | +// little Bobby Whitespace from using Coder. |
| 119 | +func NormalizeRealUsername(str string) string { |
| 120 | + s := strings.TrimSpace(str) |
| 121 | + if len(s) > 128 { |
| 122 | + s = s[:128] |
| 123 | + } |
| 124 | + return s |
| 125 | +} |
0 commit comments