diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index 9d397fa92..7f5727113 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -355,12 +355,16 @@ func parseIncubatorArgs(args []string) (incubatorArgs, error) { flags.IntVar(&ia.envFD, "env-fd", -1, "file descriptor to read the forwarded environment from (JSON array of KEY=VALUE pairs)") flags.Parse(args) - for g := range strings.SplitSeq(groups, ",") { - gid, err := strconv.Atoi(g) - if err != nil { - return ia, fmt.Errorf("unable to parse group id %q: %w", g, err) + // A user without supplementary groups gets an empty --groups=, which is + // no groups rather than a single empty group ID. + if groups != "" { + for g := range strings.SplitSeq(groups, ",") { + gid, err := strconv.Atoi(g) + if err != nil { + return ia, fmt.Errorf("unable to parse group id %q: %w", g, err) + } + ia.gids = append(ia.gids, gid) } - ia.gids = append(ia.gids, gid) } // envFD comes from an ExtraFiles entry, so it must never name stdin/out/err diff --git a/ssh/tailssh/incubator_test.go b/ssh/tailssh/incubator_test.go index c74901e20..a5a07f0b1 100644 --- a/ssh/tailssh/incubator_test.go +++ b/ssh/tailssh/incubator_test.go @@ -261,6 +261,34 @@ func TestParseIncubatorArgsEnvFD(t *testing.T) { } } +// TestParseIncubatorArgsGroups verifies that --groups accepts an empty list, +// which a user with no supplementary groups yields, but still rejects empty +// entries within a list. +func TestParseIncubatorArgsGroups(t *testing.T) { + for _, tt := range []struct { + groups string + want []int + }{ + {"", nil}, + {"1000", []int{1000}}, + {"1000,1001", []int{1000, 1001}}, + } { + ia, err := parseIncubatorArgs([]string{"--groups=" + tt.groups}) + if err != nil { + t.Errorf("--groups=%s: got error %v, want nil", tt.groups, err) + continue + } + if !slices.Equal(ia.gids, tt.want) { + t.Errorf("--groups=%s: got gids %v, want %v", tt.groups, ia.gids, tt.want) + } + } + for _, groups := range []string{"1000,x", "1000,,1001", "1000,"} { + if _, err := parseIncubatorArgs([]string{"--groups=" + groups}); err == nil { + t.Errorf("--groups=%s: got nil error, want rejection", groups) + } + } +} + // TestLoadForwardedEnvLegacyEncodedEnv covers the deprecated --encoded-env // compatibility path: an outdated parent tailscaled passes the accepted // environment as a quoted JSON argv flag, and the child must still decode it diff --git a/ssh/tailssh/sessmonitor_test.go b/ssh/tailssh/sessmonitor_test.go index 9b2ba6c77..836f9dadb 100644 --- a/ssh/tailssh/sessmonitor_test.go +++ b/ssh/tailssh/sessmonitor_test.go @@ -214,10 +214,9 @@ func newMonitorSessionServerFor( if rule != nil { sc.mappedLocalUser = rule.SSHUsers["test"] } - // The bound user's groups, like clientAuth would have captured them - // (the incubator argv's --groups must be non-empty): for a conn bound - // to another uid use that user's own primary group, not the test - // process's (root-run) groups. + // The bound user's groups, like clientAuth would have captured them: + // for a conn bound to another uid use that user's own primary group, + // not the test process's (root-run) groups. if um.Uid == strconv.Itoa(os.Getuid()) { for _, g := range testGroups(t) { sc.userGroupIDs = append(sc.userGroupIDs, strconv.Itoa(g))