|
| 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 | +funcUsernameFrom(strstring)string { |
| 26 | +ifvalid:=NameValid(str);valid==nil { |
| 27 | +returnstr |
| 28 | +} |
| 29 | +emailAt:=strings.LastIndex(str,"@") |
| 30 | +ifemailAt>=0 { |
| 31 | +str=str[:emailAt] |
| 32 | +} |
| 33 | +str=usernameReplace.ReplaceAllString(str,"") |
| 34 | +ifvalid:=NameValid(str);valid==nil { |
| 35 | +returnstr |
| 36 | +} |
| 37 | +returnstrings.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 | +funcNameValid(strstring)error { |
| 43 | +iflen(str)>32 { |
| 44 | +returnxerrors.New("must be <= 32 characters") |
| 45 | +} |
| 46 | +iflen(str)<1 { |
| 47 | +returnxerrors.New("must be >= 1 character") |
| 48 | +} |
| 49 | +// Avoid conflicts with routes like /templates/new and /groups/create. |
| 50 | +ifstr=="new"||str=="create" { |
| 51 | +returnxerrors.Errorf("cannot use %q as a name",str) |
| 52 | +} |
| 53 | +matched:=UsernameValidRegex.MatchString(str) |
| 54 | +if!matched { |
| 55 | +returnxerrors.New("must be alphanumeric with hyphens") |
| 56 | +} |
| 57 | +returnnil |
| 58 | +} |
| 59 | + |
| 60 | +// TemplateVersionNameValid returns whether the input string is a valid template version name. |
| 61 | +funcTemplateVersionNameValid(strstring)error { |
| 62 | +iflen(str)>64 { |
| 63 | +returnxerrors.New("must be <= 64 characters") |
| 64 | +} |
| 65 | +matched:=templateVersionName.MatchString(str) |
| 66 | +if!matched { |
| 67 | +returnxerrors.New("must be alphanumeric with underscores and dots") |
| 68 | +} |
| 69 | +returnnil |
| 70 | +} |
| 71 | + |
| 72 | +// DisplayNameValid returns whether the input string is a valid template display name. |
| 73 | +funcDisplayNameValid(strstring)error { |
| 74 | +iflen(str)==0 { |
| 75 | +returnnil// empty display_name is correct |
| 76 | +} |
| 77 | +iflen(str)>64 { |
| 78 | +returnxerrors.New("must be <= 64 characters") |
| 79 | +} |
| 80 | +matched:=templateDisplayName.MatchString(str) |
| 81 | +if!matched { |
| 82 | +returnxerrors.New("must be alphanumeric with spaces") |
| 83 | +} |
| 84 | +returnnil |
| 85 | +} |
| 86 | + |
| 87 | +// UserRealNameValid returns whether the input string is a valid real user name. |
| 88 | +funcUserRealNameValid(strstring)error { |
| 89 | +iflen(str)>128 { |
| 90 | +returnxerrors.New("must be <= 128 characters") |
| 91 | +} |
| 92 | + |
| 93 | +ifstrings.TrimSpace(str)!=str { |
| 94 | +returnxerrors.New("must not have leading or trailing whitespace") |
| 95 | +} |
| 96 | +returnnil |
| 97 | +} |
| 98 | + |
| 99 | +// GroupNameValid returns whether the input string is a valid group name. |
| 100 | +funcGroupNameValid(strstring)error { |
| 101 | +// 36 is to support using UUIDs as the group name. |
| 102 | +iflen(str)>36 { |
| 103 | +returnxerrors.New("must be <= 36 characters") |
| 104 | +} |
| 105 | +// Avoid conflicts with routes like /groups/new and /groups/create. |
| 106 | +ifstr=="new"||str=="create" { |
| 107 | +returnxerrors.Errorf("cannot use %q as a name",str) |
| 108 | +} |
| 109 | +matched:=UsernameValidRegex.MatchString(str) |
| 110 | +if!matched { |
| 111 | +returnxerrors.New("must be alphanumeric with hyphens") |
| 112 | +} |
| 113 | +returnnil |
| 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 | +funcNormalizeRealUsername(strstring)string { |
| 120 | +s:=strings.TrimSpace(str) |
| 121 | +iflen(s)>128 { |
| 122 | +s=s[:128] |
| 123 | +} |
| 124 | +returns |
| 125 | +} |