@@ -30,6 +30,16 @@ type Group struct {
3030var users map [string ]User
3131var groups map [string ]Group
3232
33+ // regexUidGid detects valid Linux uid or gid.
34+ var regexUidGid = regexp .MustCompile ("^[0-9]+$" ) //nolint:revive
35+
36+ // regexUsername matches user and group names to be valid for `useradd`.
37+ // It allows a trailing '$', but it feels prudent to map those to the fallback user as well.
38+ var regexUsername = regexp .MustCompile ("^[a-z_][a-z0-9_-]*$" )
39+
40+ // regexPath detects valid Linux path.
41+ var regexPath = regexp .MustCompile ("^[/a-zA-Z0-9_-]+$" )
42+
3343func LookupUser (name string ) (User , error ) {
3444 if users == nil {
3545 users = make (map [string ]User )
@@ -101,12 +111,9 @@ func LimaUser(warn bool) (*user.User, error) {
101111 cache .Do (func () {
102112 cache .u , cache .err = user .Current ()
103113 if cache .err == nil {
104- // `useradd` only allows user and group names matching the following pattern:
105- // (it allows a trailing '$', but it feels prudent to map those to the fallback user as well)
106- validName := "^[a-z_][a-z0-9_-]*$"
107- if ! regexp .MustCompile (validName ).Match ([]byte (cache .u .Username )) {
114+ if ! regexUsername .MatchString (cache .u .Username ) {
108115 warning := fmt .Sprintf ("local user %q is not a valid Linux username (must match %q); using %q username instead" ,
109- cache .u .Username , validName , fallbackUser )
116+ cache .u .Username , regexUsername . String () , fallbackUser )
110117 cache .warnings = append (cache .warnings , warning )
111118 cache .u .Username = fallbackUser
112119 }
@@ -119,7 +126,7 @@ func LimaUser(warn bool) (*user.User, error) {
119126 if err != nil {
120127 uid = fallbackUid
121128 }
122- if ! regexp . MustCompile ( "^[0-9]+$" ). Match ([] byte ( cache .u .Uid ) ) {
129+ if ! regexUidGid . MatchString ( cache .u .Uid ) {
123130 warning := fmt .Sprintf ("local uid %q is not a valid Linux uid (must be integer); using %d uid instead" ,
124131 cache .u .Uid , uid )
125132 cache .warnings = append (cache .warnings , warning )
@@ -133,7 +140,7 @@ func LimaUser(warn bool) (*user.User, error) {
133140 if err != nil {
134141 gid = fallbackGid
135142 }
136- if ! regexp . MustCompile ( "^[0-9]+$" ). Match ([] byte ( cache .u .Gid ) ) {
143+ if ! regexUidGid . MatchString ( cache .u .Gid ) {
137144 warning := fmt .Sprintf ("local gid %q is not a valid Linux gid (must be integer); using %d gid instead" ,
138145 cache .u .Gid , gid )
139146 cache .warnings = append (cache .warnings , warning )
@@ -150,10 +157,9 @@ func LimaUser(warn bool) (*user.User, error) {
150157 prefix := strings .ToLower (fmt .Sprintf ("/%c" , drive [0 ]))
151158 home = strings .Replace (home , drive , prefix , 1 )
152159 }
153- validPath := "^[/a-zA-Z0-9_-]+$"
154- if ! regexp .MustCompile (validPath ).Match ([]byte (cache .u .HomeDir )) {
160+ if ! regexPath .MatchString (cache .u .HomeDir ) {
155161 warning := fmt .Sprintf ("local home %q is not a valid Linux path (must match %q); using %q home instead" ,
156- cache .u .HomeDir , validPath , home )
162+ cache .u .HomeDir , regexPath . String () , home )
157163 cache .warnings = append (cache .warnings , warning )
158164 cache .u .HomeDir = home
159165 }
0 commit comments