From 524edbce24f54ffa2dbf9c4c19bd437199729efa Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 08:30:49 -0600 Subject: [PATCH 1/7] fix(runtime): reject runArgs that replace the image or set reserved labels --- pkg/driver/apple/lifecycle.go | 9 + .../apple/native_runargs_integration_test.go | 113 +++++++++++ pkg/driver/apple/runargs_validation_test.go | 52 ++++++ pkg/driver/docker/lifecycle.go | 9 + .../docker/native_runargs_integration_test.go | 110 +++++++++++ pkg/driver/docker/runargs_validation_test.go | 33 ++++ pkg/driver/runargs/grammar.go | 69 +++++++ pkg/driver/runargs/runargs.go | 176 ++++++++++++++++++ pkg/driver/runargs/runargs_test.go | 156 ++++++++++++++++ .../devcontainer-json.mdx | 4 +- 10 files changed, 730 insertions(+), 1 deletion(-) create mode 100644 pkg/driver/apple/native_runargs_integration_test.go create mode 100644 pkg/driver/apple/runargs_validation_test.go create mode 100644 pkg/driver/docker/native_runargs_integration_test.go create mode 100644 pkg/driver/docker/runargs_validation_test.go create mode 100644 pkg/driver/runargs/grammar.go create mode 100644 pkg/driver/runargs/runargs.go create mode 100644 pkg/driver/runargs/runargs_test.go diff --git a/pkg/driver/apple/lifecycle.go b/pkg/driver/apple/lifecycle.go index 10d850721..29cd5d56e 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" ) @@ -150,6 +151,14 @@ func (d *appleDriver) RunImageDevContainer( ctx context.Context, params *driver.RunImageDevContainerParams, ) error { + if err := runargs.Validate( + runargs.AppleContainer, + params.ParsedConfig.RunArgs, + params.LocalWorkspaceFolder, + ); 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..59701e948 --- /dev/null +++ b/pkg/driver/apple/native_runargs_integration_test.go @@ -0,0 +1,113 @@ +package apple + +import ( + "context" + "fmt" + "os" + "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" +) + +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: "/bin/sh", + Cmd: []string{"-c", "sleep 120"}, + }, + }, + ) +} + +func (s *NativeRunArgsSuite) assertNoContainer(name string) { + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + if err != nil || len(details) == 0 { + s.T().Logf("%s: no container", name) + return + } + 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..a62eeed78 --- /dev/null +++ b/pkg/driver/apple/runargs_validation_test.go @@ -0,0 +1,52 @@ +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" +) + +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) 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}, + }) +} + +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) TestOrdinaryRunArgsReachTheRuntime() { + s.Require().NoError(s.run("--label", "team=a")) + s.Equal([]string{"pull", "run"}, s.client.calls) + s.Require().Len(s.client.ranArgs, 1) + s.Contains(s.client.ranArgs[0], "team=a") +} diff --git a/pkg/driver/docker/lifecycle.go b/pkg/driver/docker/lifecycle.go index 981f6110e..3f4ec114a 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" @@ -317,6 +318,14 @@ func (d *dockerDriver) RunImageDevContainer( ctx context.Context, params *driver.RunImageDevContainerParams, ) error { + if err := runargs.Validate( + runargs.DockerCompatible, + params.ParsedConfig.RunArgs, + params.LocalWorkspaceFolder, + ); 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..74e134ab2 --- /dev/null +++ b/pkg/driver/docker/native_runargs_integration_test.go @@ -0,0 +1,110 @@ +package docker + +import ( + "context" + "fmt" + "os" + "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) { + details, err := s.helper.InspectContainers(context.Background(), []string{name}) + if err != nil || len(details) == 0 { + s.T().Logf("%s: no container", name) + return + } + 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..184aa143b --- /dev/null +++ b/pkg/driver/docker/runargs_validation_test.go @@ -0,0 +1,33 @@ +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") +} diff --git a/pkg/driver/runargs/grammar.go b/pkg/driver/runargs/grammar.go new file mode 100644 index 000000000..be1aa9dbf --- /dev/null +++ b/pkg/driver/runargs/grammar.go @@ -0,0 +1,69 @@ +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 --rootfs --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", +} + +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 = newGrammar(true, dockerCompatibleOptions) + AppleContainer = newGrammar(false, appleContainerOptions) +) + +func newGrammar(attachedShortValues bool, names map[optionKind]string) Grammar { + options := make(map[string]optionKind) + for kind, list := range names { + for _, name := range strings.Fields(list) { + options[name] = kind + } + } + return Grammar{options: options, attachedShortValues: attachedShortValues} +} diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go new file mode 100644 index 000000000..b7f8c7d7d --- /dev/null +++ b/pkg/driver/runargs/runargs.go @@ -0,0 +1,176 @@ +package runargs + +import ( + "bufio" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "unicode" + + "github.com/devsy-org/devsy/pkg/config" +) + +type optionKind int + +const ( + unknownOption optionKind = iota + switchOption + valueOption + labelOption + labelFileOption +) + +type Grammar struct { + options map[string]optionKind + attachedShortValues 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) { + 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 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:] + switch { + case a.kind == unknownOption: + return assignment{}, false, fmt.Errorf("unknown option %s", a.name) + case len(rest) > 1 && rest[0] == '=': + a.value = rest[1:] + return a, false, nil + case a.kind == switchOption: + continue + 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, + ) + } + } + return assignment{kind: switchOption}, false, nil +} + +func readLabelFile(path, dir string) ([]string, error) { + resolved, err := resolveLabelFile(path, dir) + if err != nil { + return nil, err + } + file, err := os.Open(resolved) + if err != nil { + return nil, fmt.Errorf("read label file: %w", err) + } + defer func() { _ = file.Close() }() + + var labels []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimPrefix(scanner.Text(), "\uFEFF") + labels = append(labels, strings.TrimLeftFunc(line, unicode.IsSpace)) + } + 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 strings.ContainsAny(path, ",\"\r\n") { + return "", fmt.Errorf( + "label file path %q must not contain a comma, quote or line break", path, + ) + } + if filepath.IsAbs(path) { + return path, nil + } + if filepath.VolumeName(path) != "" || strings.HasPrefix(path, `\`) || + strings.HasPrefix(path, "/") { + return "", fmt.Errorf( + "label file path %q must be absolute or relative to the workspace", path, + ) + } + return filepath.Join(dir, path), nil +} diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go new file mode 100644 index 000000000..fca26926d --- /dev/null +++ b/pkg/driver/runargs/runargs_test.go @@ -0,0 +1,156 @@ +package runargs + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/suite" +) + +const imageReadMessage = "the runtime would read it as the image" + +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) writeFile(name, content string) { + s.Require().NoError(os.WriteFile(filepath.Join(s.dir, name), []byte(content), 0o600)) +} + +func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { + s.writeFile("labels.env", "team=a\n# devsy.user=commented\n\n") + args := []string{ + "--cap-add=SYS_PTRACE", + "--security-opt", "seccomp=unconfined", + "--network", "host", + "-it", + "-v", "/a:/b", + "-p8080:8080", + "-eFOO=bar", + "--label", "team=a", + "-l=team=b", + "--entrypoint", "-x", + "--label-file", "labels.env", + "--future-flag=value", + "--rm=true", + } + s.NoError(Validate(DockerCompatible, args, s.dir)) +} + +func (s *RunArgsSuite) TestRejectsTokensTheRuntimeReadsAsTheImage() { + for _, args := range [][]string{ + {"--privileged", "busybox"}, + {"--tty", "false"}, + {"--network", "host", "busybox", "sh"}, + {"--"}, + {"-"}, + {""}, + {"-li", "team=a"}, + } { + s.ErrorContains(Validate(DockerCompatible, args, s.dir), imageReadMessage, args) + } +} + +func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { + for _, tc := range []struct { + args []string + want string + }{ + {[]string{"--label"}, "runArgs: item 1: --label needs a value"}, + {[]string{"-it", "-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"}, + } { + 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{ + {"--label", "devsy.user=secret-value"}, + {"--label=devsy.user=secret-value"}, + {"-l", "devsy.user=secret-value"}, + {"-l=devsy.user=secret-value"}, + {"-ldevsy.user=secret-value"}, + {"-il", "devsy.user=secret-value"}, + {"--label-file", "bom.env"}, + {"--label-file=spaced.env"}, + {"--label-file", "key-only.env"}, + {"--label-file", 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{"--label-file", "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{"--label-file", `\labels.env`}, s.dir), + `runArgs: label file path "\\labels.env" must be absolute or relative to the workspace`, + ) + s.ErrorContains( + Validate(DockerCompatible, []string{"--label-file", "missing.env"}, s.dir), + "runArgs: read label file:", + ) +} + +func (s *RunArgsSuite) TestAppleRules() { + s.NoError( + Validate(AppleContainer, []string{"-il", "team=a", "--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", "team=a"}, "runArgs: item 1: put the value of -l in the next item"}, + { + []string{"--label-file", "labels.env"}, + "runArgs: item 1: unknown option --label-file; write it as --label-file=VALUE", + }, + {[]string{"-h"}, "runArgs: item 1: unknown option -h"}, + { + []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", "busybox"}, 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.Fields(list) { + s.False(seen[name], name) + seen[name] = true + s.Regexp(`^(-[a-zA-Z]|--[a-z0-9][a-z0-9-]*)$`, name) + } + } + } +} 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..1dc70b56d 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 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. From d44cb8195adc27a929edec4e9f0426c34080bd24 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 09:10:20 -0600 Subject: [PATCH 2/7] fix(runtime): reject label file paths with parent elements and satisfy lint --- .../apple/native_runargs_integration_test.go | 3 +- pkg/driver/apple/runargs_validation_test.go | 22 +++--- pkg/driver/runargs/grammar.go | 2 +- pkg/driver/runargs/runargs.go | 70 ++++++++++++------- pkg/driver/runargs/runargs_test.go | 64 ++++++++++------- 5 files changed, 100 insertions(+), 61 deletions(-) diff --git a/pkg/driver/apple/native_runargs_integration_test.go b/pkg/driver/apple/native_runargs_integration_test.go index 59701e948..340818d2c 100644 --- a/pkg/driver/apple/native_runargs_integration_test.go +++ b/pkg/driver/apple/native_runargs_integration_test.go @@ -16,6 +16,7 @@ import ( const ( nativeIntendedImage = "alpine:3.22" nativePositionalImage = "busybox:1" + nativeEntrypoint = "/bin/sh" ) type NativeRunArgsSuite struct { @@ -89,7 +90,7 @@ func (s *NativeRunArgsSuite) runDevContainer(name string, runArgs ...string) err Options: &driver.RunOptions{ Image: nativeIntendedImage, Labels: []string{config.UserLabel + "=root"}, - Entrypoint: "/bin/sh", + Entrypoint: nativeEntrypoint, Cmd: []string{"-c", "sleep 120"}, }, }, diff --git a/pkg/driver/apple/runargs_validation_test.go b/pkg/driver/apple/runargs_validation_test.go index a62eeed78..f2e7e59a8 100644 --- a/pkg/driver/apple/runargs_validation_test.go +++ b/pkg/driver/apple/runargs_validation_test.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/suite" ) +const runCall = "run" + type RunArgsValidationSuite struct { suite.Suite client *mockClient @@ -25,15 +27,6 @@ func (s *RunArgsValidationSuite) SetupTest() { s.driver = &appleDriver{Apple: s.client} } -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}, - }) -} - func (s *RunArgsValidationSuite) TestRejectsBeforeAnyRuntimeCall() { s.ErrorContains(s.run("--rm", "busybox"), "the runtime would read it as the image") s.EqualError( @@ -46,7 +39,16 @@ func (s *RunArgsValidationSuite) TestRejectsBeforeAnyRuntimeCall() { func (s *RunArgsValidationSuite) TestOrdinaryRunArgsReachTheRuntime() { s.Require().NoError(s.run("--label", "team=a")) - s.Equal([]string{"pull", "run"}, s.client.calls) + 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/runargs/grammar.go b/pkg/driver/runargs/grammar.go index be1aa9dbf..a618ab9ac 100644 --- a/pkg/driver/runargs/grammar.go +++ b/pkg/driver/runargs/grammar.go @@ -61,7 +61,7 @@ var ( func newGrammar(attachedShortValues bool, names map[optionKind]string) Grammar { options := make(map[string]optionKind) for kind, list := range names { - for _, name := range strings.Fields(list) { + for name := range strings.FieldsSeq(list) { options[name] = kind } } diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go index b7f8c7d7d..902f83971 100644 --- a/pkg/driver/runargs/runargs.go +++ b/pkg/driver/runargs/runargs.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" "unicode" @@ -112,34 +113,41 @@ func (g Grammar) parseShort(cluster string) (assignment, bool, error) { a := assignment{name: "-" + cluster[i:i+1]} a.kind = g.options[a.name] rest := cluster[i+1:] - switch { - case a.kind == unknownOption: + if a.kind == unknownOption { return assignment{}, false, fmt.Errorf("unknown option %s", a.name) - case len(rest) > 1 && rest[0] == '=': - a.value = rest[1:] - return a, false, nil - case a.kind == switchOption: - continue - 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, - ) + } + 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 } - file, err := os.Open(resolved) + file, err := os.Open(filepath.Clean(resolved)) if err != nil { return nil, fmt.Errorf("read label file: %w", err) } @@ -158,19 +166,33 @@ func readLabelFile(path, dir string) ([]string, error) { } 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( + return fmt.Errorf( "label file path %q must not contain a comma, quote or line break", path, ) } - if filepath.IsAbs(path) { - return path, nil + if slices.Contains(strings.FieldsFunc(path, isPathSeparator), "..") { + return fmt.Errorf("label file path %q must not contain a .. element", path) } - if filepath.VolumeName(path) != "" || strings.HasPrefix(path, `\`) || - strings.HasPrefix(path, "/") { - return "", fmt.Errorf( + 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 filepath.Join(dir, path), nil + 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 index fca26926d..b3289bc7d 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -9,7 +9,14 @@ import ( "github.com/stretchr/testify/suite" ) -const imageReadMessage = "the runtime would read it as the image" +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" +) type RunArgsSuite struct { suite.Suite @@ -24,10 +31,6 @@ func (s *RunArgsSuite) SetupTest() { s.dir = s.T().TempDir() } -func (s *RunArgsSuite) writeFile(name, content string) { - s.Require().NoError(os.WriteFile(filepath.Join(s.dir, name), []byte(content), 0o600)) -} - func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { s.writeFile("labels.env", "team=a\n# devsy.user=commented\n\n") args := []string{ @@ -38,10 +41,10 @@ func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { "-v", "/a:/b", "-p8080:8080", "-eFOO=bar", - "--label", "team=a", + labelFlag, teamLabel, "-l=team=b", "--entrypoint", "-x", - "--label-file", "labels.env", + labelFileFlag, "labels.env", "--future-flag=value", "--rm=true", } @@ -50,13 +53,13 @@ func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { func (s *RunArgsSuite) TestRejectsTokensTheRuntimeReadsAsTheImage() { for _, args := range [][]string{ - {"--privileged", "busybox"}, + {"--privileged", positionalImage}, {"--tty", "false"}, - {"--network", "host", "busybox", "sh"}, + {"--network", "host", positionalImage, "sh"}, {"--"}, {"-"}, {""}, - {"-li", "team=a"}, + {"-li", teamLabel}, } { s.ErrorContains(Validate(DockerCompatible, args, s.dir), imageReadMessage, args) } @@ -67,7 +70,7 @@ func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { args []string want string }{ - {[]string{"--label"}, "runArgs: item 1: --label needs a value"}, + {[]string{labelFlag}, "runArgs: item 1: --label needs a value"}, {[]string{"-it", "-e"}, "runArgs: item 2: -e needs a value"}, { []string{"--future-flag", "x"}, @@ -84,16 +87,16 @@ func (s *RunArgsSuite) TestRejectsReservedLabelsInEveryForm() { 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{ - {"--label", "devsy.user=secret-value"}, + {labelFlag, reservedUser}, {"--label=devsy.user=secret-value"}, - {"-l", "devsy.user=secret-value"}, + {"-l", reservedUser}, {"-l=devsy.user=secret-value"}, {"-ldevsy.user=secret-value"}, - {"-il", "devsy.user=secret-value"}, - {"--label-file", "bom.env"}, + {"-il", reservedUser}, + {labelFileFlag, "bom.env"}, {"--label-file=spaced.env"}, - {"--label-file", "key-only.env"}, - {"--label-file", filepath.Join(s.dir, "bom.env")}, + {labelFileFlag, "key-only.env"}, + {labelFileFlag, filepath.Join(s.dir, "bom.env")}, } { err := Validate(DockerCompatible, args, s.dir) s.Require().Error(err, args) @@ -104,31 +107,35 @@ func (s *RunArgsSuite) TestRejectsReservedLabelsInEveryForm() { func (s *RunArgsSuite) TestRejectsLabelFilesItCannotReadAsTheRuntimeDoes() { s.EqualError( - Validate(DockerCompatible, []string{"--label-file", "a.env,b.env"}, s.dir), + 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{"--label-file", `\labels.env`}, s.dir), + 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.ErrorContains( - Validate(DockerCompatible, []string{"--label-file", "missing.env"}, s.dir), + Validate(DockerCompatible, []string{labelFileFlag, "missing.env"}, s.dir), "runArgs: read label file:", ) } func (s *RunArgsSuite) TestAppleRules() { s.NoError( - Validate(AppleContainer, []string{"-il", "team=a", "--cpus", "2", "-l=team=b"}, s.dir), + 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", "team=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{"--label-file", "labels.env"}, + []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"}, @@ -139,14 +146,17 @@ func (s *RunArgsSuite) TestAppleRules() { } { s.EqualError(Validate(AppleContainer, tc.args, s.dir), tc.want, tc.args) } - s.ErrorContains(Validate(AppleContainer, []string{"--rm", "busybox"}, s.dir), imageReadMessage) + 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.Fields(list) { + 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) @@ -154,3 +164,7 @@ func (s *RunArgsSuite) TestOptionTablesNameEachOptionOnce() { } } } + +func (s *RunArgsSuite) writeFile(name, content string) { + s.Require().NoError(os.WriteFile(filepath.Join(s.dir, name), []byte(content), 0o600)) +} From 9cc486a646de86ea4bb8f33b3d8431c0e94ae404 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 09:19:09 -0600 Subject: [PATCH 3/7] fix(runtime): check runArgs before recreate removes the container --- pkg/devcontainer/capabilities_test.go | 2 ++ pkg/devcontainer/single.go | 5 ++++- pkg/driver/apple/driver.go | 5 +++-- pkg/driver/apple/lifecycle.go | 14 ++++++++++---- pkg/driver/apple/runargs_validation_test.go | 13 +++++++++++++ pkg/driver/docker/docker.go | 1 + pkg/driver/docker/lifecycle.go | 14 ++++++++++---- pkg/driver/docker/runargs_validation_test.go | 12 ++++++++++++ pkg/driver/runargs/runargs.go | 7 +++++++ pkg/driver/runargs/runargs_test.go | 5 +++++ 10 files changed, 67 insertions(+), 11 deletions(-) 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 29cd5d56e..b0a1884e5 100644 --- a/pkg/driver/apple/lifecycle.go +++ b/pkg/driver/apple/lifecycle.go @@ -147,15 +147,21 @@ func (d *appleDriver) TagDevContainer(ctx context.Context, image, tag string) er return nil } -func (d *appleDriver) RunImageDevContainer( - ctx context.Context, +func (d *appleDriver) ValidateRunImageDevContainer( params *driver.RunImageDevContainerParams, ) error { - if err := runargs.Validate( + return runargs.Validate( runargs.AppleContainer, params.ParsedConfig.RunArgs, params.LocalWorkspaceFolder, - ); err != nil { + ) +} + +func (d *appleDriver) RunImageDevContainer( + ctx context.Context, + params *driver.RunImageDevContainerParams, +) error { + if err := d.ValidateRunImageDevContainer(params); err != nil { return err } diff --git a/pkg/driver/apple/runargs_validation_test.go b/pkg/driver/apple/runargs_validation_test.go index f2e7e59a8..1dd9d530d 100644 --- a/pkg/driver/apple/runargs_validation_test.go +++ b/pkg/driver/apple/runargs_validation_test.go @@ -37,6 +37,19 @@ func (s *RunArgsValidationSuite) TestRejectsBeforeAnyRuntimeCall() { 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) 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 3f4ec114a..c64009485 100644 --- a/pkg/driver/docker/lifecycle.go +++ b/pkg/driver/docker/lifecycle.go @@ -314,15 +314,21 @@ func (d *dockerDriver) GetDevContainerLogs( return d.Docker.GetContainerLogs(ctx, container.ID, stdout, stderr) } -func (d *dockerDriver) RunImageDevContainer( - ctx context.Context, +func (d *dockerDriver) ValidateRunImageDevContainer( params *driver.RunImageDevContainerParams, ) error { - if err := runargs.Validate( + return runargs.Validate( runargs.DockerCompatible, params.ParsedConfig.RunArgs, params.LocalWorkspaceFolder, - ); err != nil { + ) +} + +func (d *dockerDriver) RunImageDevContainer( + ctx context.Context, + params *driver.RunImageDevContainerParams, +) error { + if err := d.ValidateRunImageDevContainer(params); err != nil { return err } diff --git a/pkg/driver/docker/runargs_validation_test.go b/pkg/driver/docker/runargs_validation_test.go index 184aa143b..469e229d2 100644 --- a/pkg/driver/docker/runargs_validation_test.go +++ b/pkg/driver/docker/runargs_validation_test.go @@ -31,3 +31,15 @@ func (s *DockerDriverTestSuite) TestRunImageDevContainerRejectsRunArgsBeforeRunt ) 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/runargs.go b/pkg/driver/runargs/runargs.go index 902f83971..7db7f5a1c 100644 --- a/pkg/driver/runargs/runargs.go +++ b/pkg/driver/runargs/runargs.go @@ -147,6 +147,13 @@ func readLabelFile(path, dir string) ([]string, error) { 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) diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go index b3289bc7d..aee89fb09 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -118,6 +118,11 @@ func (s *RunArgsSuite) TestRejectsLabelFilesItCannotReadAsTheRuntimeDoes() { Validate(DockerCompatible, []string{"--label-file=link/../labels.env"}, s.dir), `runArgs: label file path "link/../labels.env" must not contain a .. element`, ) + 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:", From 621eac604cfdcdb802822c448627e4a615532eba Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 09:29:56 -0600 Subject: [PATCH 4/7] fix(runtime): reject rootfs runArgs and require a listing for absence --- .../apple/native_runargs_integration_test.go | 16 ++++++++++++++-- .../docker/native_runargs_integration_test.go | 16 ++++++++++++++-- pkg/driver/runargs/grammar.go | 7 ++++--- pkg/driver/runargs/runargs.go | 5 +++++ pkg/driver/runargs/runargs_test.go | 8 ++++++++ .../devcontainer-json.mdx | 2 +- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/pkg/driver/apple/native_runargs_integration_test.go b/pkg/driver/apple/native_runargs_integration_test.go index 340818d2c..bb5ffc2f1 100644 --- a/pkg/driver/apple/native_runargs_integration_test.go +++ b/pkg/driver/apple/native_runargs_integration_test.go @@ -1,9 +1,12 @@ package apple import ( + "bytes" "context" "fmt" "os" + "slices" + "strings" "testing" "time" @@ -98,11 +101,20 @@ func (s *NativeRunArgsSuite) runDevContainer(name string, runArgs ...string) err } func (s *NativeRunArgsSuite) assertNoContainer(name string) { - details, err := s.helper.InspectContainers(context.Background(), []string{name}) - if err != nil || len(details) == 0 { + 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", diff --git a/pkg/driver/docker/native_runargs_integration_test.go b/pkg/driver/docker/native_runargs_integration_test.go index 74e134ab2..ffbb55421 100644 --- a/pkg/driver/docker/native_runargs_integration_test.go +++ b/pkg/driver/docker/native_runargs_integration_test.go @@ -1,9 +1,12 @@ package docker import ( + "bytes" "context" "fmt" "os" + "slices" + "strings" "testing" "time" @@ -94,11 +97,20 @@ func (s *NativeRunArgsSuite) runDevContainer(name string, runArgs ...string) err } func (s *NativeRunArgsSuite) assertNoContainer(name string) { - details, err := s.helper.InspectContainers(context.Background(), []string{name}) - if err != nil || len(details) == 0 { + 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", diff --git a/pkg/driver/runargs/grammar.go b/pkg/driver/runargs/grammar.go index a618ab9ac..2c02254be 100644 --- a/pkg/driver/runargs/grammar.go +++ b/pkg/driver/runargs/grammar.go @@ -6,7 +6,7 @@ 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 --rootfs --sig-proxy --tls-verify " + + "--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 " + @@ -37,8 +37,9 @@ var dockerCompatibleOptions = map[optionKind]string{ "--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", + labelOption: "-l --label", + labelFileOption: "--label-file", + imageSourceOption: "--rootfs", } var appleContainerOptions = map[optionKind]string{ diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go index 7db7f5a1c..88a0779a3 100644 --- a/pkg/driver/runargs/runargs.go +++ b/pkg/driver/runargs/runargs.go @@ -21,6 +21,7 @@ const ( valueOption labelOption labelFileOption + imageSourceOption ) type Grammar struct { @@ -97,6 +98,10 @@ 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: diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go index aee89fb09..1b6ae4ec4 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -77,6 +77,14 @@ func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { "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{"-it", "--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) } 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 1dc70b56d..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 @@ -169,4 +169,4 @@ Custom identification labels, including `dev.containers.id`, remain configurable 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 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. +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. From 82dc72adec72e5f1f5b16c81cbdd82480843ccc4 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 09:43:06 -0600 Subject: [PATCH 5/7] test(runtime): name the repeated runArgs switch cluster --- pkg/driver/runargs/runargs_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go index 1b6ae4ec4..7a694322a 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -16,6 +16,7 @@ const ( positionalImage = "busybox" reservedUser = "devsy.user=secret-value" teamLabel = "team=a" + interactiveTTY = "-it" ) type RunArgsSuite struct { @@ -37,7 +38,7 @@ func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", "--network", "host", - "-it", + interactiveTTY, "-v", "/a:/b", "-p8080:8080", "-eFOO=bar", @@ -71,7 +72,7 @@ func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { want string }{ {[]string{labelFlag}, "runArgs: item 1: --label needs a value"}, - {[]string{"-it", "-e"}, "runArgs: item 2: -e 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", @@ -82,7 +83,7 @@ func (s *RunArgsSuite) TestRejectsOptionsItCannotPlace() { "runArgs: item 1: option --rootfs makes the runtime read the image as a root filesystem", }, { - []string{"-it", "--rootfs=true"}, + []string{interactiveTTY, "--rootfs=true"}, "runArgs: item 2: option --rootfs makes the runtime read the image as a root filesystem", }, } { From 924884166b88cc498bc6990721eb3e395cbb24b6 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 10:22:36 -0600 Subject: [PATCH 6/7] fix(runtime): reject label wildcards and Apple dash values in runArgs --- pkg/driver/runargs/grammar.go | 12 ++++++++---- pkg/driver/runargs/runargs.go | 15 ++++++++++++--- pkg/driver/runargs/runargs_test.go | 6 ++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/driver/runargs/grammar.go b/pkg/driver/runargs/grammar.go index 2c02254be..994bdb2a4 100644 --- a/pkg/driver/runargs/grammar.go +++ b/pkg/driver/runargs/grammar.go @@ -55,16 +55,20 @@ var appleContainerOptions = map[optionKind]string{ } var ( - DockerCompatible = newGrammar(true, dockerCompatibleOptions) - AppleContainer = newGrammar(false, appleContainerOptions) + DockerCompatible = Grammar{ + options: optionNames(dockerCompatibleOptions), + attachedShortValues: true, + dashValues: true, + } + AppleContainer = Grammar{options: optionNames(appleContainerOptions)} ) -func newGrammar(attachedShortValues bool, names map[optionKind]string) Grammar { +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 Grammar{options: options, attachedShortValues: attachedShortValues} + return options } diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go index 88a0779a3..b7cac390a 100644 --- a/pkg/driver/runargs/runargs.go +++ b/pkg/driver/runargs/runargs.go @@ -27,6 +27,7 @@ const ( type Grammar struct { options map[string]optionKind attachedShortValues bool + dashValues bool } type assignment struct { @@ -71,7 +72,7 @@ func (g Grammar) parse(args []string) ([]assignment, error) { return nil, fmt.Errorf("item %d: %w", i+1, err) } if consumesNext { - if i+1 == len(args) { + 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++ @@ -164,12 +165,20 @@ func readLabelFile(path, dir string) ([]string, error) { 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.TrimPrefix(scanner.Text(), "\uFEFF") - labels = append(labels, strings.TrimLeftFunc(line, unicode.IsSpace)) + line := strings.TrimLeftFunc(strings.TrimPrefix(scanner.Text(), "\uFEFF"), unicode.IsSpace) + if !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) diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go index 7a694322a..73ecc00b6 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -127,6 +127,11 @@ func (s *RunArgsSuite) TestRejectsLabelFilesItCannotReadAsTheRuntimeDoes() { 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), @@ -153,6 +158,7 @@ func (s *RunArgsSuite) TestAppleRules() { "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`, From f72b41e3d1fcd0dfca27ad83ddda030e230bc911 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Thu, 12 Nov 2026 10:27:43 -0600 Subject: [PATCH 7/7] fix(runtime): skip label file comments in the wildcard check --- pkg/driver/runargs/runargs.go | 3 ++- pkg/driver/runargs/runargs_test.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/driver/runargs/runargs.go b/pkg/driver/runargs/runargs.go index b7cac390a..c39f2a2c1 100644 --- a/pkg/driver/runargs/runargs.go +++ b/pkg/driver/runargs/runargs.go @@ -173,7 +173,8 @@ func scanLabelFile(file *os.File) ([]string, error) { scanner := bufio.NewScanner(file) for scanner.Scan() { line := strings.TrimLeftFunc(strings.TrimPrefix(scanner.Text(), "\uFEFF"), unicode.IsSpace) - if !strings.Contains(line, "=") && strings.HasSuffix(line, "*") { + 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, ) diff --git a/pkg/driver/runargs/runargs_test.go b/pkg/driver/runargs/runargs_test.go index 73ecc00b6..6b8e70607 100644 --- a/pkg/driver/runargs/runargs_test.go +++ b/pkg/driver/runargs/runargs_test.go @@ -33,7 +33,7 @@ func (s *RunArgsSuite) SetupTest() { } func (s *RunArgsSuite) TestAcceptsCommonRunArgs() { - s.writeFile("labels.env", "team=a\n# devsy.user=commented\n\n") + 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",