Skip to content

Commit 657f7e0

Browse files
committed
Fix empty user/group detection on chuser
This should fix #1216.
1 parent 7adf5f1 commit 657f7e0

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

cmd/yggdrasil/chuser_unix.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414

1515
func chuser(input string) error {
1616
givenUser, givenGroup, _ := strings.Cut(input, ":")
17+
if givenUser == "" {
18+
return fmt.Errorf("user is empty")
19+
}
20+
if strings.Index(input, ":") > -1 && givenGroup == "" {
21+
return fmt.Errorf("group is empty")
22+
}
1723

1824
var (
1925
err error

cmd/yggdrasil/chuser_unix_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,48 @@
44
package main
55

66
import (
7-
"testing"
87
"os/user"
8+
"testing"
99
)
1010

1111
// Usernames must not contain a number sign.
12-
func TestEmptyString (t *testing.T) {
12+
func TestEmptyString(t *testing.T) {
1313
if chuser("") == nil {
1414
t.Fatal("the empty string is not a valid user")
1515
}
1616
}
1717

1818
// Either omit delimiter and group, or omit both.
19-
func TestEmptyGroup (t *testing.T) {
19+
func TestEmptyGroup(t *testing.T) {
2020
if chuser("0:") == nil {
2121
t.Fatal("the empty group is not allowed")
2222
}
2323
}
2424

2525
// Either user only or user and group.
26-
func TestGroupOnly (t *testing.T) {
26+
func TestGroupOnly(t *testing.T) {
2727
if chuser(":0") == nil {
2828
t.Fatal("group only is not allowed")
2929
}
3030
}
3131

3232
// Usenames must not contain the number sign.
33-
func TestInvalidUsername (t *testing.T) {
33+
func TestInvalidUsername(t *testing.T) {
3434
const username = "#user"
3535
if chuser(username) == nil {
3636
t.Fatalf("'%s' is not a valid username", username)
3737
}
3838
}
3939

4040
// User IDs must be non-negative.
41-
func TestInvalidUserid (t *testing.T) {
41+
func TestInvalidUserid(t *testing.T) {
4242
if chuser("-1") == nil {
4343
t.Fatal("User ID cannot be negative")
4444
}
4545
}
4646

4747
// Change to the current user by ID.
48-
func TestCurrentUserid (t *testing.T) {
48+
func TestCurrentUserid(t *testing.T) {
4949
usr, err := user.Current()
5050
if err != nil {
5151
t.Fatal(err)
@@ -61,7 +61,7 @@ func TestCurrentUserid (t *testing.T) {
6161
}
6262

6363
// Change to a common user by name.
64-
func TestCommonUsername (t *testing.T) {
64+
func TestCommonUsername(t *testing.T) {
6565
usr, err := user.Current()
6666
if err != nil {
6767
t.Fatal(err)

0 commit comments

Comments
 (0)