diff --git a/ssh/tailssh/tailssh_test.go b/ssh/tailssh/tailssh_test.go index 8092b28a7..eb0109e5c 100644 --- a/ssh/tailssh/tailssh_test.go +++ b/ssh/tailssh/tailssh_test.go @@ -1415,3 +1415,28 @@ func mockRecordingServer(t *testing.T, handleRecord http.HandlerFunc) *httptest. t.Cleanup(srv.Close) return srv } + +func TestIsRootUser(t *testing.T) { + tests := []struct { + uid, username string + want bool + }{ + {uid: "0", username: "root", want: true}, + {uid: "0", username: "alice", want: true}, + // A non-canonical numeric uid 0 is also root. + {uid: "00", username: "alice", want: true}, + {uid: "00000000", username: "alice", want: true}, + {uid: "1000", username: "root", want: true}, + {uid: "1000", username: "alice", want: false}, + {uid: "1", username: "daemon", want: false}, + // Non-numeric uids keep the string behavior. + {uid: "notanumber", username: "root", want: true}, + {uid: "notanumber", username: "alice", want: false}, + } + for _, tt := range tests { + got := isRootUser(&userMeta{User: user.User{Uid: tt.uid, Username: tt.username}}) + if got != tt.want { + t.Errorf("isRootUser(uid=%q, username=%q) = %v; want %v", tt.uid, tt.username, got, tt.want) + } + } +} diff --git a/ssh/tailssh/user_unix.go b/ssh/tailssh/user_unix.go index eb1941886..6c24f43d6 100644 --- a/ssh/tailssh/user_unix.go +++ b/ssh/tailssh/user_unix.go @@ -29,7 +29,17 @@ func (u *userMeta) GroupIds() ([]string, error) { // isRootUser reports whether u is the superuser. func isRootUser(u *userMeta) bool { - return u.Uid == "0" || u.Username == "root" + if u.Username == "root" { + return true + } + // Compare the uid numerically: a non-canonical uid like "00" also + // denotes root, and must not bypass the non-root checks that later + // parse the uid numerically (e.g. the incubator's --uid). Non-numeric + // uids keep the previous string behavior (not root). + if uid, err := strconv.ParseUint(u.Uid, 10, 32); err == nil { + return uid == 0 + } + return false } func (u *userMeta) LoginShell() string { diff --git a/util/osuser/user.go b/util/osuser/user.go index 5a9e26ff1..6e4cc8c12 100644 --- a/util/osuser/user.go +++ b/util/osuser/user.go @@ -17,6 +17,7 @@ import ( "strings" "sync" "time" + "unicode" "unicode/utf8" "tailscale.com/version/distro" @@ -272,7 +273,7 @@ func checkParsedGetentField(s string) bool { return false } for _, r := range s { - if r < ' ' || r == 0x7f || r == utf8.RuneError { + if unicode.IsControl(r) || r == utf8.RuneError { return false } }