Files
tailscale/util/osuser/group_ids_test.go
James Tucker 3e61adf832 util/osuser: validate 'id' input, reject corrupt getent passwd output
Before invoking 'id -Gz <user>', apply the same input validation as the
getent path (checkGetentInput), so a crafted username like '-u' can't be
interpreted as an option by the id command.

parseGetentUser now rejects multi-line (multiple or corrupted records)
output and validates the username and shell fields (no leading dashes,
control characters, or invalid UTF-8), the fields that are used to look
up and run a session. Also add the missing leading-dash regression test
for the 'id' call path.

The passwd format has no escaping — gecos sub-fields are comma-separated
— so an eighth colon-separated field means a stray colon in some field,
shifting every field after it: home parses as a gecos fragment and the
shell as the real home directory. Short records keep the existing
treatment (padded, with the default-shell fallback); over-long records
now fail closed like the other corrupt-output cases.

Updates tailscale/corp#50380

Signed-off-by: James Tucker <[email protected]>
2026-10-24 13:05:02 -07:00

99 lines
2.2 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package osuser
import (
"context"
"slices"
"testing"
)
func TestParseGroupIds(t *testing.T) {
tests := []struct {
in string
expected []string
}{
{"5000\x005001\n", []string{"5000", "5001"}},
{"5000\n", []string{"5000"}},
{"\n", []string{}},
{"5000 5001 5002\n", []string{"5000", "5001", "5002"}},
{"5000\t5001\n", []string{"5000", "5001"}},
}
for _, test := range tests {
actual := parseGroupIds([]byte(test.in))
if !slices.Equal(actual, test.expected) {
t.Errorf("parseGroupIds(%q) = %q, wanted %s", test.in, actual, test.expected)
}
}
}
func TestGetGroupIdsWithIdValidatesInput(t *testing.T) {
origExecId := execId
t.Cleanup(func() {
execId = origExecId
})
tests := []struct {
name string
username string
wantExec bool
wantExecArg string // the username argument passed to "id"
wantErr bool
}{
{
name: "valid-username",
username: "alice",
wantExec: true,
wantExecArg: "alice",
},
{
name: "leading-dash",
username: "-u",
wantErr: true,
},
{
name: "empty",
username: "",
wantErr: true,
},
{
name: "control-character",
username: "ali\x00ce",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
execId = func(_ context.Context, args ...string) ([]byte, error) {
if !tt.wantExec {
t.Fatalf("exec 'id' called with %q; want it not to be called", args)
}
wantArg := tt.wantExecArg
gotFlag := args[0]
if gotFlag != "-Gz" && gotFlag != "-G" {
t.Errorf("unexpected 'id' flag %q", gotFlag)
}
if got := args[len(args)-1]; got != wantArg {
t.Errorf("id called with %q; want username %q", got, wantArg)
}
return []byte("1000\x001001\x00"), nil
}
ids, err := getGroupIdsWithId(tt.username)
if tt.wantErr {
if err == nil {
t.Fatalf("getGroupIdsWithId(%q) = %v; want error", tt.username, ids)
}
return
}
if err != nil {
t.Fatalf("getGroupIdsWithId(%q) error: %v", tt.username, err)
}
if !slices.Equal(ids, []string{"1000", "1001"}) {
t.Errorf("ids = %v; want [1000 1001]", ids)
}
})
}
}