Skip to content

Commit

Permalink
Fix empty user/group detection on chuser
Browse files Browse the repository at this point in the history
This should fix #1216.
  • Loading branch information
neilalexander committed Dec 13, 2024
1 parent 7adf5f1 commit 657f7e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/yggdrasil/chuser_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (

func chuser(input string) error {
givenUser, givenGroup, _ := strings.Cut(input, ":")
if givenUser == "" {
return fmt.Errorf("user is empty")
}
if strings.Index(input, ":") > -1 && givenGroup == "" {

Check failure on line 20 in cmd/yggdrasil/chuser_unix.go

View workflow job for this annotation

GitHub Actions / Lint

S1003: should use strings.Contains(input, ":") instead (gosimple)
return fmt.Errorf("group is empty")
}

var (
err error
Expand Down
16 changes: 8 additions & 8 deletions cmd/yggdrasil/chuser_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
package main

import (
"testing"
"os/user"
"testing"
)

// Usernames must not contain a number sign.
func TestEmptyString (t *testing.T) {
func TestEmptyString(t *testing.T) {
if chuser("") == nil {
t.Fatal("the empty string is not a valid user")
}
}

// Either omit delimiter and group, or omit both.
func TestEmptyGroup (t *testing.T) {
func TestEmptyGroup(t *testing.T) {
if chuser("0:") == nil {
t.Fatal("the empty group is not allowed")
}
}

// Either user only or user and group.
func TestGroupOnly (t *testing.T) {
func TestGroupOnly(t *testing.T) {
if chuser(":0") == nil {
t.Fatal("group only is not allowed")
}
}

// Usenames must not contain the number sign.
func TestInvalidUsername (t *testing.T) {
func TestInvalidUsername(t *testing.T) {
const username = "#user"
if chuser(username) == nil {
t.Fatalf("'%s' is not a valid username", username)
}
}

// User IDs must be non-negative.
func TestInvalidUserid (t *testing.T) {
func TestInvalidUserid(t *testing.T) {
if chuser("-1") == nil {
t.Fatal("User ID cannot be negative")
}
}

// Change to the current user by ID.
func TestCurrentUserid (t *testing.T) {
func TestCurrentUserid(t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)
Expand All @@ -61,7 +61,7 @@ func TestCurrentUserid (t *testing.T) {
}

// Change to a common user by name.
func TestCommonUsername (t *testing.T) {
func TestCommonUsername(t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 657f7e0

Please sign in to comment.