diff --git a/pkg/devcontainer/capabilities_test.go b/pkg/devcontainer/capabilities_test.go index 021772671..c7d100f74 100644 --- a/pkg/devcontainer/capabilities_test.go +++ b/pkg/devcontainer/capabilities_test.go @@ -271,6 +271,7 @@ func TestImageRunValidationPreservesContainerBeforeRecreate(t *testing.T) { r.workspaceConfig.Workspace = &provider.Workspace{ID: r.id, UID: workspaceUid} r.imageBackend = &separateImages{details: &config.ImageDetails{ID: imageValue}} r.reporter = status.Nop() + r.localWorkspaceFolder = t.TempDir() p := recreateResolveParams() p.parsedConfig.Config.Image = alpine p.parsedConfig.Raw = config.CloneDevContainerConfig(p.parsedConfig.Config) @@ -283,6 +284,7 @@ func TestImageRunValidationPreservesContainerBeforeRecreate(t *testing.T) { } require.NotNil(t, d.validationParams) require.Equal(t, r.id, d.validationParams.WorkspaceID) + require.Equal(t, r.localWorkspaceFolder, d.validationParams.LocalWorkspaceFolder) require.NotEmpty(t, d.validationParams.Options.Image) assert.Equal(t, !reject, d.stopCalled) assert.False(t, d.deleteCalled) diff --git a/pkg/devcontainer/single.go b/pkg/devcontainer/single.go index 3e08fa499..68ebb6759 100644 --- a/pkg/devcontainer/single.go +++ b/pkg/devcontainer/single.go @@ -646,7 +646,10 @@ func (r *runner) validateImageRun( } return validator.ValidateRunImageDevContainer( &driver.RunImageDevContainerParams{ - WorkspaceID: r.id, Options: options, ParsedConfig: parsedConfig, + WorkspaceID: r.id, + Options: options, + ParsedConfig: parsedConfig, + LocalWorkspaceFolder: r.localWorkspaceFolder, }, ) } diff --git a/pkg/driver/apple/driver.go b/pkg/driver/apple/driver.go index e94328a91..4fe81f8c6 100644 --- a/pkg/driver/apple/driver.go +++ b/pkg/driver/apple/driver.go @@ -37,8 +37,9 @@ var ( _ driver.RecreatePolicyDriver = (*appleDriver)(nil) _ driver.WorkspaceChowner = (*appleDriver)(nil) - _ driver.ImageDriver = (*appleDriver)(nil) - _ driver.Preflighter = (*appleDriver)(nil) + _ driver.ImageDriver = (*appleDriver)(nil) + _ driver.Preflighter = (*appleDriver)(nil) + _ driver.ImageRunValidator = (*appleDriver)(nil) ) // NewAppleDriver verifies the host is supported and constructs the driver; diff --git a/pkg/driver/apple/lifecycle.go b/pkg/driver/apple/lifecycle.go index 10d850721..b0a1884e5 100644 --- a/pkg/driver/apple/lifecycle.go +++ b/pkg/driver/apple/lifecycle.go @@ -8,6 +8,7 @@ import ( "github.com/devsy-org/devsy/pkg/apple" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/driver" + "github.com/devsy-org/devsy/pkg/driver/runargs" "github.com/devsy-org/devsy/pkg/log" ) @@ -146,10 +147,24 @@ func (d *appleDriver) TagDevContainer(ctx context.Context, image, tag string) er return nil } +func (d *appleDriver) ValidateRunImageDevContainer( + params *driver.RunImageDevContainerParams, +) error { + return runargs.Validate( + runargs.AppleContainer, + params.ParsedConfig.RunArgs, + params.LocalWorkspaceFolder, + ) +} + func (d *appleDriver) RunImageDevContainer( ctx context.Context, params *driver.RunImageDevContainerParams, ) error { + if err := d.ValidateRunImageDevContainer(params); err != nil { + return err + } + if err := d.EnsureImage(ctx, params.Options); err != nil { return err } diff --git a/pkg/driver/apple/native_runargs_integration_test.go b/pkg/driver/apple/native_runargs_integration_test.go new file mode 100644 index 000000000..bb5ffc2f1 --- /dev/null +++ b/pkg/driver/apple/native_runargs_integration_test.go @@ -0,0 +1,126 @@ +package apple + +import ( + "bytes" + "context" + "fmt" + "os" + "slices" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/apple" + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/suite" +) + +const ( + nativeIntendedImage = "alpine:3.22" + nativePositionalImage = "busybox:1" + nativeEntrypoint = "/bin/sh" +) + +type NativeRunArgsSuite struct { + suite.Suite + helper *apple.AppleHelper +} + +func TestNativeRunArgsSuite(t *testing.T) { + suite.Run(t, new(NativeRunArgsSuite)) +} + +func (s *NativeRunArgsSuite) SetupTest() { + if os.Getenv("DEVSY_APPLE_E2E") != "1" { + s.T().Skip("set DEVSY_APPLE_E2E=1 to run against a real container CLI") + } + s.helper = &apple.AppleHelper{Command: "container"} +} + +func (s *NativeRunArgsSuite) TestPositionalImageIsRejected() { + name := s.containerName("positional") + err := s.runDevContainer(name, "--name", name, "--read-only", nativePositionalImage, "echo") + s.T().Logf("RunImageDevContainer error: %v", err) + s.assertNoContainer(name) + s.Require().ErrorContains(err, "the runtime would read it as the image") +} + +func (s *NativeRunArgsSuite) TestReservedLabelIsRejected() { + name := s.containerName("reserved") + err := s.runDevContainer( + name, "--name", name, "--label", "sh.devsy.recovery=fixture-private-value", + ) + s.T().Logf("RunImageDevContainer error: %v", err) + s.assertNoContainer(name) + s.Require().ErrorContains(err, `label "sh.devsy.recovery" is reserved by Devsy`) + s.NotContains(err.Error(), "fixture-private-value") +} + +func (s *NativeRunArgsSuite) TestOrdinaryLabelStarts() { + name := s.containerName("ordinary") + s.Require().NoError(s.runDevContainer(name, "--name", name, "--label", "team=a")) + s.Require().NoError(s.helper.WaitContainerRunning(context.Background(), name)) + + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + s.Require().NoError(err) + s.Require().Len(details, 1) + s.Equal("a", details[0].Config.Labels["team"]) + s.Equal("root", details[0].Config.Labels[config.UserLabel]) +} + +func (s *NativeRunArgsSuite) containerName(kind string) string { + name := fmt.Sprintf("devsy-runargs-%s-%d", kind, time.Now().UnixNano()) + s.T().Cleanup(func() { + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + _ = s.helper.Stop(ctx, name) + _ = s.helper.Remove(ctx, name) + }) + return name +} + +func (s *NativeRunArgsSuite) runDevContainer(name string, runArgs ...string) error { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + return (&appleDriver{Apple: s.helper}).RunImageDevContainer( + ctx, + &driver.RunImageDevContainerParams{ + WorkspaceID: name, + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: runArgs}, + }, + Options: &driver.RunOptions{ + Image: nativeIntendedImage, + Labels: []string{config.UserLabel + "=root"}, + Entrypoint: nativeEntrypoint, + Cmd: []string{"-c", "sleep 120"}, + }, + }, + ) +} + +func (s *NativeRunArgsSuite) assertNoContainer(name string) { + var out bytes.Buffer + err := s.helper.Run( + context.Background(), + []string{"list", "--all", "--quiet"}, + apple.Streams{Stdout: &out}, + ) + s.Require().NoError(err) + if !slices.Contains(strings.Fields(out.String()), name) { + s.T().Logf("%s: no container", name) + return + } + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + s.Require().NoError(err) + s.Require().NotEmpty(details) + s.Failf( + "container was created", + "%s: status=%s image=%s labels=%v", + name, + details[0].State.Status, + details[0].Config.LegacyImage, + details[0].Config.Labels, + ) +} diff --git a/pkg/driver/apple/runargs_validation_test.go b/pkg/driver/apple/runargs_validation_test.go new file mode 100644 index 000000000..1dd9d530d --- /dev/null +++ b/pkg/driver/apple/runargs_validation_test.go @@ -0,0 +1,67 @@ +package apple + +import ( + "context" + "errors" + "testing" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/suite" +) + +const runCall = "run" + +type RunArgsValidationSuite struct { + suite.Suite + client *mockClient + driver *appleDriver +} + +func TestRunArgsValidationSuite(t *testing.T) { + suite.Run(t, new(RunArgsValidationSuite)) +} + +func (s *RunArgsValidationSuite) SetupTest() { + s.client = &mockClient{inspectErr: errors.New("image missing")} + s.driver = &appleDriver{Apple: s.client} +} + +func (s *RunArgsValidationSuite) TestRejectsBeforeAnyRuntimeCall() { + s.ErrorContains(s.run("--rm", "busybox"), "the runtime would read it as the image") + s.EqualError( + s.run("--label", "sh.devsy.recovery=true"), + `runArgs: label "sh.devsy.recovery" is reserved by Devsy; remove the user-supplied label`, + ) + s.Empty(s.client.calls) + s.Empty(s.client.ranArgs) +} + +func (s *RunArgsValidationSuite) TestValidatesBeforeRecreateRemovesTheContainer() { + err := s.driver.ValidateRunImageDevContainer(&driver.RunImageDevContainerParams{ + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: []string{"-l", "devsy.user=root"}}, + }, + }) + s.EqualError( + err, + `runArgs: label "devsy.user" is reserved by Devsy; remove the user-supplied label`, + ) + s.Empty(s.client.calls) +} + +func (s *RunArgsValidationSuite) TestOrdinaryRunArgsReachTheRuntime() { + s.Require().NoError(s.run("--label", "team=a")) + s.Equal([]string{"pull", runCall}, s.client.calls) + s.Require().Len(s.client.ranArgs, 1) + s.Contains(s.client.ranArgs[0], "team=a") +} + +func (s *RunArgsValidationSuite) run(runArgs ...string) error { + return s.driver.RunImageDevContainer(context.Background(), &driver.RunImageDevContainerParams{ + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: runArgs}, + }, + Options: &driver.RunOptions{Image: testImageRef}, + }) +} diff --git a/pkg/driver/docker/docker.go b/pkg/driver/docker/docker.go index 77e801fc2..248dc3b80 100644 --- a/pkg/driver/docker/docker.go +++ b/pkg/driver/docker/docker.go @@ -109,6 +109,7 @@ var ( _ driver.DockerHelperProvider = (*dockerDriver)(nil) _ driver.SnapshotCapableDriver = (*dockerDriver)(nil) _ driver.Preflighter = (*dockerDriver)(nil) + _ driver.ImageRunValidator = (*dockerDriver)(nil) ) // Preflight checks the runtime binary is installed and its daemon reachable, diff --git a/pkg/driver/docker/lifecycle.go b/pkg/driver/docker/lifecycle.go index 981f6110e..c64009485 100644 --- a/pkg/driver/docker/lifecycle.go +++ b/pkg/driver/docker/lifecycle.go @@ -12,6 +12,7 @@ import ( "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/driver" + "github.com/devsy-org/devsy/pkg/driver/runargs" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/subprocess" @@ -313,10 +314,24 @@ func (d *dockerDriver) GetDevContainerLogs( return d.Docker.GetContainerLogs(ctx, container.ID, stdout, stderr) } +func (d *dockerDriver) ValidateRunImageDevContainer( + params *driver.RunImageDevContainerParams, +) error { + return runargs.Validate( + runargs.DockerCompatible, + params.ParsedConfig.RunArgs, + params.LocalWorkspaceFolder, + ) +} + func (d *dockerDriver) RunImageDevContainer( ctx context.Context, params *driver.RunImageDevContainerParams, ) error { + if err := d.ValidateRunImageDevContainer(params); err != nil { + return err + } + if err := d.EnsureImage(ctx, params.Options); err != nil { return err } diff --git a/pkg/driver/docker/native_runargs_integration_test.go b/pkg/driver/docker/native_runargs_integration_test.go new file mode 100644 index 000000000..ffbb55421 --- /dev/null +++ b/pkg/driver/docker/native_runargs_integration_test.go @@ -0,0 +1,122 @@ +package docker + +import ( + "bytes" + "context" + "fmt" + "os" + "slices" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/suite" +) + +const ( + nativeIntendedImage = "alpine:3.22" + nativePositionalImage = "busybox:1" +) + +type NativeRunArgsSuite struct { + suite.Suite + helper *docker.DockerHelper +} + +func TestNativeRunArgsSuite(t *testing.T) { + suite.Run(t, new(NativeRunArgsSuite)) +} + +func (s *NativeRunArgsSuite) SetupTest() { + command := os.Getenv("DEVSY_DOCKER_E2E") + if command == "" { + s.T(). + Skip("set DEVSY_DOCKER_E2E=docker or DEVSY_DOCKER_E2E=podman to run against a real CLI") + } + s.helper = &docker.DockerHelper{DockerCommand: command} +} + +func (s *NativeRunArgsSuite) TestPositionalImageIsRejected() { + name := s.containerName("positional") + err := s.runDevContainer(name, "--name", name, "--privileged", nativePositionalImage, "echo") + s.T().Logf("RunImageDevContainer error: %v", err) + s.assertNoContainer(name) + s.Require().ErrorContains(err, "the runtime would read it as the image") +} + +func (s *NativeRunArgsSuite) TestReservedLabelIsRejected() { + name := s.containerName("reserved") + err := s.runDevContainer( + name, "--name", name, "--label", "sh.devsy.recovery=fixture-private-value", + ) + s.T().Logf("RunImageDevContainer error: %v", err) + s.assertNoContainer(name) + s.Require().ErrorContains(err, `label "sh.devsy.recovery" is reserved by Devsy`) + s.NotContains(err.Error(), "fixture-private-value") +} + +func (s *NativeRunArgsSuite) TestOrdinaryLabelStarts() { + name := s.containerName("ordinary") + s.Require().NoError(s.runDevContainer(name, "--name", name, "--label", "team=a")) + + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + s.Require().NoError(err) + s.Require().Len(details, 1) + s.Equal(config.ContainerStatusRunning, details[0].State.Status) + s.Equal("a", details[0].Config.Labels["team"]) + s.Equal("root", details[0].Config.Labels[config.UserLabel]) +} + +func (s *NativeRunArgsSuite) containerName(kind string) string { + name := fmt.Sprintf("devsy-runargs-%s-%d", kind, time.Now().UnixNano()) + s.T().Cleanup(func() { + _ = s.helper.Run(context.Background(), []string{"rm", "-f", name}, docker.Streams{}) + }) + return name +} + +func (s *NativeRunArgsSuite) runDevContainer(name string, runArgs ...string) error { + d := &dockerDriver{Docker: s.helper, UpdateRemoteUserUIDDefault: "off"} + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + return d.RunImageDevContainer(ctx, &driver.RunImageDevContainerParams{ + WorkspaceID: name, + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: runArgs}, + }, + Options: &driver.RunOptions{ + Image: nativeIntendedImage, + Labels: []string{config.UserLabel + "=root"}, + Entrypoint: "/bin/sh", + Cmd: []string{"-c", "sleep 120"}, + }, + }) +} + +func (s *NativeRunArgsSuite) assertNoContainer(name string) { + var out bytes.Buffer + err := s.helper.Run( + context.Background(), + []string{"ps", "--all", "--format", "{{.Names}}"}, + docker.Streams{Stdout: &out}, + ) + s.Require().NoError(err) + if !slices.Contains(strings.Fields(out.String()), name) { + s.T().Logf("%s: no container", name) + return + } + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + s.Require().NoError(err) + s.Require().NotEmpty(details) + s.Failf( + "container was created", + "%s: status=%s image=%s labels=%v", + name, + details[0].State.Status, + details[0].Config.LegacyImage, + details[0].Config.Labels, + ) +} diff --git a/pkg/driver/docker/runargs_validation_test.go b/pkg/driver/docker/runargs_validation_test.go new file mode 100644 index 000000000..469e229d2 --- /dev/null +++ b/pkg/driver/docker/runargs_validation_test.go @@ -0,0 +1,45 @@ +package docker + +import ( + "context" + "path/filepath" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/driver" +) + +func (s *DockerDriverTestSuite) TestRunImageDevContainerRejectsRunArgsBeforeRuntimeCall() { + missing := filepath.Join(s.T().TempDir(), "missing-runtime") + d := &dockerDriver{Docker: &docker.DockerHelper{DockerCommand: missing}} + run := func(runArgs ...string) error { + return d.RunImageDevContainer(context.Background(), &driver.RunImageDevContainerParams{ + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: runArgs}, + }, + Options: &driver.RunOptions{Image: "alpine:3.22"}, + }) + } + + s.EqualError( + run("--privileged", "busybox"), + "runArgs: item 2: not an option or option value; the runtime would read it as the image", + ) + s.EqualError( + run("-l=devsy.user=root"), + `runArgs: label "devsy.user" is reserved by Devsy; remove the user-supplied label`, + ) + s.ErrorContains(run("--label", "team=a"), "missing-runtime") +} + +func (s *DockerDriverTestSuite) TestValidateRunImageDevContainerChecksRunArgs() { + err := (&dockerDriver{}).ValidateRunImageDevContainer(&driver.RunImageDevContainerParams{ + ParsedConfig: &config.DevContainerConfig{ + NonComposeBase: config.NonComposeBase{RunArgs: []string{"--tty", "false"}}, + }, + }) + s.EqualError( + err, + "runArgs: item 2: not an option or option value; the runtime would read it as the image", + ) +} diff --git a/pkg/driver/runargs/grammar.go b/pkg/driver/runargs/grammar.go new file mode 100644 index 000000000..994bdb2a4 --- /dev/null +++ b/pkg/driver/runargs/grammar.go @@ -0,0 +1,74 @@ +package runargs + +import "strings" + +var dockerCompatibleOptions = map[optionKind]string{ + switchOption: "-d --detach -i --interactive -P --publish-all -q --quiet -t --tty " + + "--disable-content-trust --env-host --http-proxy --init --no-healthcheck " + + "--no-hostname --no-hosts --oom-kill-disable --passwd --privileged --read-only " + + "--read-only-tmpfs --replace --rm --rmi --sig-proxy --tls-verify " + + "--unsetenv-all --use-api-socket", + valueOption: "-a --attach -c --cpu-shares -e --env -h --hostname -m --memory " + + "-p --publish -u --user -v --volume -w --workdir --add-host --annotation --arch " + + "--authfile --blkio-weight --blkio-weight-device --cap-add --cap-drop --cert-dir " + + "--cgroup-conf --cgroup-parent --cgroupns --cgroups --chrootdirs --cidfile " + + "--conmon-pidfile --cosign-certificate-identity " + + "--cosign-certificate-identity-regexp --cosign-certificate-oidc-issuer " + + "--cosign-certificate-oidc-issuer-regexp --cosign-key --cpu-count --cpu-percent " + + "--cpu-period --cpu-quota --cpu-rt-period --cpu-rt-runtime --cpus --cpuset-cpus " + + "--cpuset-mems --creds --decryption-key --detach-keys --device " + + "--device-cgroup-rule --device-read-bps --device-read-iops --device-write-bps " + + "--device-write-iops --dns --dns-opt --dns-option --dns-search --domainname " + + "--entrypoint --env-file --env-merge --expose --gidmap --gpus --group-add " + + "--group-entry --health-cmd --health-interval --health-log-destination " + + "--health-max-log-count --health-max-log-size --health-on-failure " + + "--health-retries --health-start-interval --health-start-period " + + "--health-startup-cmd --health-startup-interval --health-startup-retries " + + "--health-startup-success --health-startup-timeout --health-timeout --hosts-file " + + "--hostuser --image-volume --init-binary --init-path --io-maxbandwidth " + + "--io-maxiops --ip --ip6 --ipc --ipfs-address --isolation --kernel-memory --link " + + "--link-local-ip --log-driver --log-opt --mac-address --memory-reservation " + + "--memory-swap --memory-swappiness --mount --name --net --net-alias --network " + + "--network-alias --oom-score-adj --os --passwd-entry --personality --pid " + + "--pidfile --pids-limit --platform --pod --pod-id-file --preserve-fd " + + "--preserve-fds --pull --rdt-class --requires --restart --retry --retry-delay " + + "--runtime --sdnotify --seccomp-policy --secret --security-opt --shm-size " + + "--shm-size-systemd --signature-policy --stop-signal --stop-timeout --storage-opt " + + "--subgidname --subuidname --sysctl --systemd --timeout --tmpfs --tz --uidmap " + + "--ulimit --umask --unsetenv --userns --uts --variant --verify --volume-driver " + + "--volumes-from", + labelOption: "-l --label", + labelFileOption: "--label-file", + imageSourceOption: "--rootfs", +} + +var appleContainerOptions = map[optionKind]string{ + switchOption: "-d --detach -i --interactive -t --tty --debug --init --no-dns " + + "--read-only --remove --rm --rosetta --ssh --virtualization", + valueOption: "-a --arch -c --cpus -e --env -k --kernel -m --memory -p --publish " + + "-u --user -v --volume -w --workdir --cwd --cap-add --cap-drop --cidfile --dns " + + "--dns-domain --dns-option --dns-search --entrypoint --env-file --gid " + + "--init-image --kernel-arg --masked-path --max-concurrent-downloads --mount --name " + + "--network --os --platform --progress --publish-socket --read-only-path --runtime " + + "--scheme --shm-size --tmpfs --uid --ulimit", + labelOption: "-l --label", +} + +var ( + DockerCompatible = Grammar{ + options: optionNames(dockerCompatibleOptions), + attachedShortValues: true, + dashValues: true, + } + AppleContainer = Grammar{options: optionNames(appleContainerOptions)} +) + +func optionNames(names map[optionKind]string) map[string]optionKind { + options := make(map[string]optionKind) + for kind, list := range names { + for name := range strings.FieldsSeq(list) { + options[name] = kind + } + } + return options +} diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go new file mode 100644 index 000000000..c39f2a2c1 --- /dev/null +++ b/pkg/driver/runargs/runargs.go @@ -0,0 +1,220 @@ +package runargs + +import ( + "bufio" + "errors" + "fmt" + "os" + "path/filepath" + "slices" + "strings" + "unicode" + + "github.com/devsy-org/devsy/pkg/config" +) + +type optionKind int + +const ( + unknownOption optionKind = iota + switchOption + valueOption + labelOption + labelFileOption + imageSourceOption +) + +type Grammar struct { + options map[string]optionKind + attachedShortValues bool + dashValues bool +} + +type assignment struct { + name string + kind optionKind + value string +} + +func Validate(grammar Grammar, args []string, dir string) error { + if err := validate(grammar, args, dir); err != nil { + return fmt.Errorf("runArgs: %w", err) + } + return nil +} + +func validate(grammar Grammar, args []string, dir string) error { + assignments, err := grammar.parse(args) + if err != nil { + return err + } + var labels []string + for _, a := range assignments { + switch a.kind { + case labelOption: + labels = append(labels, a.value) + case labelFileOption: + fileLabels, err := readLabelFile(a.value, dir) + if err != nil { + return err + } + labels = append(labels, fileLabels...) + } + } + return config.ValidateUserLabels(labels) +} + +func (g Grammar) parse(args []string) ([]assignment, error) { + var assignments []assignment + for i := 0; i < len(args); i++ { + a, consumesNext, err := g.parseArg(args[i]) + if err != nil { + return nil, fmt.Errorf("item %d: %w", i+1, err) + } + if consumesNext { + if i+1 == len(args) || !g.dashValues && strings.HasPrefix(args[i+1], "-") { + return nil, fmt.Errorf("item %d: %s needs a value", i+1, a.name) + } + i++ + a.value = args[i] + } + assignments = append(assignments, a) + } + return assignments, nil +} + +func (g Grammar) parseArg(arg string) (assignment, bool, error) { + if arg == "-" || arg == "--" || !strings.HasPrefix(arg, "-") { + return assignment{}, false, errors.New( + "not an option or option value; the runtime would read it as the image", + ) + } + if strings.HasPrefix(arg, "--") { + return g.parseLong(arg) + } + return g.parseShort(arg[1:]) +} + +func (g Grammar) parseLong(arg string) (assignment, bool, error) { + name, value, hasValue := strings.Cut(arg, "=") + a := assignment{name: name, kind: g.options[name], value: value} + switch { + case a.kind == imageSourceOption: + return assignment{}, false, fmt.Errorf( + "option %s makes the runtime read the image as a root filesystem", name, + ) + case hasValue: + return a, false, nil + case a.kind == unknownOption: + return assignment{}, false, fmt.Errorf( + "unknown option %s; write it as %s=VALUE", name, name, + ) + default: + return a, a.kind != switchOption, nil + } +} + +func (g Grammar) parseShort(cluster string) (assignment, bool, error) { + for i := range len(cluster) { + a := assignment{name: "-" + cluster[i:i+1]} + a.kind = g.options[a.name] + rest := cluster[i+1:] + if a.kind == unknownOption { + return assignment{}, false, fmt.Errorf("unknown option %s", a.name) + } + if a.kind != switchOption || hasInlineValue(rest) { + return g.shortValue(a, rest) + } + } + return assignment{kind: switchOption}, false, nil +} + +func (g Grammar) shortValue(a assignment, rest string) (assignment, bool, error) { + switch { + case hasInlineValue(rest): + a.value = rest[1:] + return a, false, nil + case rest == "": + return a, true, nil + case g.attachedShortValues: + a.value = rest + return a, false, nil + default: + return assignment{}, false, fmt.Errorf("put the value of %s in the next item", a.name) + } +} + +func hasInlineValue(rest string) bool { + return len(rest) > 1 && rest[0] == '=' +} + +func readLabelFile(path, dir string) ([]string, error) { + resolved, err := resolveLabelFile(path, dir) + if err != nil { + return nil, err + } + info, err := os.Stat(resolved) + if err != nil { + return nil, fmt.Errorf("read label file: %w", err) + } + if !info.Mode().IsRegular() { + return nil, fmt.Errorf("label file %q is not a regular file", path) + } + file, err := os.Open(filepath.Clean(resolved)) + if err != nil { + return nil, fmt.Errorf("read label file: %w", err) + } + defer func() { _ = file.Close() }() + return scanLabelFile(file) +} + +func scanLabelFile(file *os.File) ([]string, error) { + var labels []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimLeftFunc(strings.TrimPrefix(scanner.Text(), "\uFEFF"), unicode.IsSpace) + if !strings.HasPrefix(line, "#") && !strings.Contains(line, "=") && + strings.HasSuffix(line, "*") { + return nil, fmt.Errorf( + "label file entry %q copies variables from the runtime environment", line, + ) + } + labels = append(labels, line) + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("read label file: %w", err) + } + return labels, nil +} + +func resolveLabelFile(path, dir string) (string, error) { + if err := checkLabelFilePath(path); err != nil { + return "", err + } + if filepath.IsAbs(path) { + return path, nil + } + return filepath.Join(dir, path), nil +} + +func checkLabelFilePath(path string) error { + if strings.ContainsAny(path, ",\"\r\n") { + return fmt.Errorf( + "label file path %q must not contain a comma, quote or line break", path, + ) + } + if slices.Contains(strings.FieldsFunc(path, isPathSeparator), "..") { + return fmt.Errorf("label file path %q must not contain a .. element", path) + } + if !filepath.IsAbs(path) && + (filepath.VolumeName(path) != "" || strings.IndexFunc(path, isPathSeparator) == 0) { + return fmt.Errorf( + "label file path %q must be absolute or relative to the workspace", path, + ) + } + return nil +} + +func isPathSeparator(r rune) bool { + return r == '/' || r == '\\' +} diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go new file mode 100644 index 000000000..6b8e70607 --- /dev/null +++ b/pkg/driver/runargs/runargs_test.go @@ -0,0 +1,190 @@ +package runargs + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/suite" +) + +const ( + imageReadMessage = "the runtime would read it as the image" + labelFlag = "--label" + labelFileFlag = "--label-file" + positionalImage = "busybox" + reservedUser = "devsy.user=secret-value" + teamLabel = "team=a" + interactiveTTY = "-it" +) + +type RunArgsSuite struct { + suite.Suite + dir string +} + +func TestRunArgsSuite(t *testing.T) { + suite.Run(t, new(RunArgsSuite)) +} + +func (s *RunArgsSuite) SetupTest() { + s.dir = s.T().TempDir() +} + +func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { + s.writeFile("labels.env", "team=a\n# devsy.user=commented\n # copied from sh.devsy.*\n\n") + args := []string{ + "--cap-add=SYS_PTRACE", + "--security-opt", "seccomp=unconfined", + "--network", "host", + interactiveTTY, + "-v", "/a:/b", + "-p8080:8080", + "-eFOO=bar", + labelFlag, teamLabel, + "-l=team=b", + "--entrypoint", "-x", + labelFileFlag, "labels.env", + "--future-flag=value", + "--rm=true", + } + s.NoError(Validate(DockerCompatible, args, s.dir)) +} + +func (s *RunArgsSuite) TestRejectsTokensTheRuntimeReadsAsTheImage() { + for _, args := range [][]string{ + {"--privileged", positionalImage}, + {"--tty", "false"}, + {"--network", "host", positionalImage, "sh"}, + {"--"}, + {"-"}, + {""}, + {"-li", teamLabel}, + } { + s.ErrorContains(Validate(DockerCompatible, args, s.dir), imageReadMessage, args) + } +} + +func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { + for _, tc := range []struct { + args []string + want string + }{ + {[]string{labelFlag}, "runArgs: item 1: --label needs a value"}, + {[]string{interactiveTTY, "-e"}, "runArgs: item 2: -e needs a value"}, + { + []string{"--future-flag", "x"}, + "runArgs: item 1: unknown option --future-flag; write it as --future-flag=VALUE", + }, + {[]string{"-itZ"}, "runArgs: item 1: unknown option -Z"}, + { + []string{"--rootfs"}, + "runArgs: item 1: option --rootfs makes the runtime read the image as a root filesystem", + }, + { + []string{interactiveTTY, "--rootfs=true"}, + "runArgs: item 2: option --rootfs makes the runtime read the image as a root filesystem", + }, + } { + s.EqualError(Validate(DockerCompatible, tc.args, s.dir), tc.want, tc.args) + } +} + +func (s *RunArgsSuite) TestRejectsReservedLabelsInEveryForm() { + s.writeFile("bom.env", "\uFEFFsh.devsy.recovery=secret-value\n") + s.writeFile("spaced.env", "team=a\n \tdevsy.user=secret-value\n") + s.writeFile("key-only.env", "devsy.overlay.structure\n") + for _, args := range [][]string{ + {labelFlag, reservedUser}, + {"--label=devsy.user=secret-value"}, + {"-l", reservedUser}, + {"-l=devsy.user=secret-value"}, + {"-ldevsy.user=secret-value"}, + {"-il", reservedUser}, + {labelFileFlag, "bom.env"}, + {"--label-file=spaced.env"}, + {labelFileFlag, "key-only.env"}, + {labelFileFlag, filepath.Join(s.dir, "bom.env")}, + } { + err := Validate(DockerCompatible, args, s.dir) + s.Require().Error(err, args) + s.Contains(err.Error(), "is reserved by Devsy", args) + s.NotContains(err.Error(), "secret-value", args) + } +} + +func (s *RunArgsSuite) TestRejectsLabelFilesItCannotReadAsTheRuntimeDoes() { + s.EqualError( + Validate(DockerCompatible, []string{labelFileFlag, "a.env,b.env"}, s.dir), + `runArgs: label file path "a.env,b.env" must not contain a comma, quote or line break`, + ) + s.EqualError( + Validate(DockerCompatible, []string{labelFileFlag, `\labels.env`}, s.dir), + `runArgs: label file path "\\labels.env" must be absolute or relative to the workspace`, + ) + s.EqualError( + Validate(DockerCompatible, []string{"--label-file=link/../labels.env"}, s.dir), + `runArgs: label file path "link/../labels.env" must not contain a .. element`, + ) + s.writeFile("wildcard.env", "team=a\n sh.devsy.*\n") + s.EqualError( + Validate(DockerCompatible, []string{labelFileFlag, "wildcard.env"}, s.dir), + `runArgs: label file entry "sh.devsy.*" copies variables from the runtime environment`, + ) + s.Require().NoError(os.Mkdir(filepath.Join(s.dir, "labels.d"), 0o700)) + s.EqualError( + Validate(DockerCompatible, []string{labelFileFlag, "labels.d"}, s.dir), + `runArgs: label file "labels.d" is not a regular file`, + ) + s.ErrorContains( + Validate(DockerCompatible, []string{labelFileFlag, "missing.env"}, s.dir), + "runArgs: read label file:", + ) +} + +func (s *RunArgsSuite) TestAppleRules() { + s.NoError( + Validate(AppleContainer, []string{"-il", teamLabel, "--cpus", "2", "-l=team=b"}, s.dir), + ) + for _, tc := range []struct { + args []string + want string + }{ + {[]string{"-lteam=a"}, "runArgs: item 1: put the value of -l in the next item"}, + {[]string{"-li", teamLabel}, "runArgs: item 1: put the value of -l in the next item"}, + { + []string{labelFileFlag, "labels.env"}, + "runArgs: item 1: unknown option --label-file; write it as --label-file=VALUE", + }, + {[]string{"-h"}, "runArgs: item 1: unknown option -h"}, + {[]string{labelFlag, "--rm"}, "runArgs: item 1: --label needs a value"}, + { + []string{"-l", "devsy.user=root"}, + `runArgs: label "devsy.user" is reserved by Devsy; remove the user-supplied label`, + }, + } { + s.EqualError(Validate(AppleContainer, tc.args, s.dir), tc.want, tc.args) + } + s.ErrorContains( + Validate(AppleContainer, []string{"--rm", positionalImage}, s.dir), + imageReadMessage, + ) +} + +func (s *RunArgsSuite) TestOptionTablesNameEachOptionOnce() { + for _, table := range []map[optionKind]string{dockerCompatibleOptions, appleContainerOptions} { + seen := map[string]bool{} + for _, list := range table { + for name := range strings.FieldsSeq(list) { + s.False(seen[name], name) + seen[name] = true + s.Regexp(`^(-[a-zA-Z]|--[a-z0-9][a-z0-9-]*)$`, name) + } + } + } +} + +func (s *RunArgsSuite) writeFile(name, content string) { + s.Require().NoError(os.WriteFile(filepath.Join(s.dir, name), []byte(content), 0o600)) +} diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx index 01303c9cd..1914554b8 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx @@ -167,4 +167,6 @@ Do not supply these keys through `--id-label`, Kubernetes provider labels, or Ku Custom identification labels, including `dev.containers.id`, remain configurable. The `devcontainer.metadata` image label remains a supported configuration source and can contain private values. Reserving a label does not make every value stored under it public. -The top-level `containerLabels` and `labels` fields in devcontainer.json are ignored. External providers reject `runArgs` rather than translate them into request labels. Native Docker, Podman, nerdctl, Apple and Compose arguments and inherited image labels are outside this explicit map reservation. Avoid using reserved keys there. Native argument validation is a separate follow-up. +The top-level `containerLabels` and `labels` fields in devcontainer.json are ignored. External providers reject `runArgs` rather than translate them into request labels. + +For Docker, Podman, nerdctl and Apple, Devsy checks `runArgs` before it creates the container. It rejects reserved keys set with `--label`, `-l` or `--label-file`. It also rejects `--rootfs`, any item the runtime would read as the image, such as a word after a switch like `--privileged`, and an option that is missing its value. Write an option Devsy does not recognize as `--option=value`. Compose labels and labels inherited from an image are not checked. `runArgs` can still set privileges and mounts, so only open a devcontainer.json you trust.