Fix empty user/group detection on chuser

This should fix #1216.
This commit is contained in:
Neil Alexander 2024-12-13 16:54:14 +00:00
parent 7adf5f18b7
commit 657f7e0db3
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 14 additions and 8 deletions

View File

@ -14,6 +14,12 @@ import (
func chuser(input string) error { func chuser(input string) error {
givenUser, givenGroup, _ := strings.Cut(input, ":") givenUser, givenGroup, _ := strings.Cut(input, ":")
if givenUser == "" {
return fmt.Errorf("user is empty")
}
if strings.Index(input, ":") > -1 && givenGroup == "" {
return fmt.Errorf("group is empty")
}
var ( var (
err error err error

View File

@ -4,33 +4,33 @@
package main package main
import ( import (
"testing"
"os/user" "os/user"
"testing"
) )
// Usernames must not contain a number sign. // Usernames must not contain a number sign.
func TestEmptyString (t *testing.T) { func TestEmptyString(t *testing.T) {
if chuser("") == nil { if chuser("") == nil {
t.Fatal("the empty string is not a valid user") t.Fatal("the empty string is not a valid user")
} }
} }
// Either omit delimiter and group, or omit both. // Either omit delimiter and group, or omit both.
func TestEmptyGroup (t *testing.T) { func TestEmptyGroup(t *testing.T) {
if chuser("0:") == nil { if chuser("0:") == nil {
t.Fatal("the empty group is not allowed") t.Fatal("the empty group is not allowed")
} }
} }
// Either user only or user and group. // Either user only or user and group.
func TestGroupOnly (t *testing.T) { func TestGroupOnly(t *testing.T) {
if chuser(":0") == nil { if chuser(":0") == nil {
t.Fatal("group only is not allowed") t.Fatal("group only is not allowed")
} }
} }
// Usenames must not contain the number sign. // Usenames must not contain the number sign.
func TestInvalidUsername (t *testing.T) { func TestInvalidUsername(t *testing.T) {
const username = "#user" const username = "#user"
if chuser(username) == nil { if chuser(username) == nil {
t.Fatalf("'%s' is not a valid username", username) t.Fatalf("'%s' is not a valid username", username)
@ -38,14 +38,14 @@ func TestInvalidUsername (t *testing.T) {
} }
// User IDs must be non-negative. // User IDs must be non-negative.
func TestInvalidUserid (t *testing.T) { func TestInvalidUserid(t *testing.T) {
if chuser("-1") == nil { if chuser("-1") == nil {
t.Fatal("User ID cannot be negative") t.Fatal("User ID cannot be negative")
} }
} }
// Change to the current user by ID. // Change to the current user by ID.
func TestCurrentUserid (t *testing.T) { func TestCurrentUserid(t *testing.T) {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -61,7 +61,7 @@ func TestCurrentUserid (t *testing.T) {
} }
// Change to a common user by name. // Change to a common user by name.
func TestCommonUsername (t *testing.T) { func TestCommonUsername(t *testing.T) {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)