diff --git a/pkg/ide/vscode/open.go b/pkg/ide/vscode/open.go index 9db1a87cb9..16752a239e 100644 --- a/pkg/ide/vscode/open.go +++ b/pkg/ide/vscode/open.go @@ -1,22 +1,32 @@ package vscode import ( + "bytes" "context" "errors" "fmt" + "io/fs" "net/url" + "os" "os/exec" + "path/filepath" "runtime" "strings" - "github.com/devsy-org/devsy/pkg/command" pkgconfig "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/log" devsyopen "github.com/devsy-org/devsy/pkg/open" + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/status" + "github.com/devsy-org/devsy/pkg/subprocess" ) +const listExtensionsArg = "--list-extensions" + const containersExtension = "ms-vscode-remote.remote-containers" +const remoteFolderScheme = "vscode-remote" + type OpenParams struct { Workspace string Folder string @@ -83,12 +93,201 @@ var openConfigs = map[Flavor]openConfig{ }, } +// Launch stages prefix errors so a failure can be traced to the step that +// produced it rather than to the overall launch. +const ( + stageResolve = "resolve executable" + stageListExtensions = "list extensions" + stageInstallSSHExt = "install SSH extension" + stageCLIDispatch = "CLI dispatch" + stageURIDispatch = "URI dispatch" +) + +const ( + routeCLI = "cli" + routeURI = "uri" +) + +type commandRunner func(ctx context.Context, path string, args ...string) ([]byte, error) + +// hostLauncher holds the host side effects of launching an editor so tests can +// replace them without starting a GUI application. +type hostLauncher struct { + lookPath func(file string) (string, error) + run commandRunner + openURI func(uri string) error + goos string +} + +func newHostLauncher() hostLauncher { + return hostLauncher{ + lookPath: exec.LookPath, + run: runHostCLI, + openURI: devsyopen.Run, + goos: runtime.GOOS, + } +} + +func runHostCLI(ctx context.Context, path string, args ...string) ([]byte, error) { + if !validHostCLI(path) || !validHostCLIArgs(args) { + return nil, errors.New("unsupported editor command") + } + options := subprocess.Options{Redactor: secrets.NewEnvironmentRedactor(os.Environ())} + var stdout bytes.Buffer + if len(args) == 1 && args[0] == listExtensionsArg { + // Extension enumeration is functional data, not a diagnostic tail. + // Redacting or truncating it could hide an already-installed extension. + options.Stdout = &stdout + } + _, err := subprocess.Run(ctx, path, args, options) + return stdout.Bytes(), err +} + +func validHostCLI(path string) bool { + if !filepath.IsAbs(path) { + return false + } + name := filepath.Base(path) + switch strings.ToLower(filepath.Ext(name)) { + case ".exe", ".cmd", ".bat", ".com": + name = strings.TrimSuffix(name, filepath.Ext(name)) + } + for _, config := range openConfigs { + if strings.EqualFold(name, config.cliName) { + return true + } + } + return false +} + +func validHostCLIArgs(args []string) bool { + if len(args) == 0 { + return false + } + switch args[0] { + case listExtensionsArg: + return len(args) == 1 + case "--install-extension": + return validExtensionInstallArgs(args) + default: + return validFolderOpenArgs(args) + } +} + +func validExtensionInstallArgs(args []string) bool { + if len(args) != 2 { + return false + } + for _, config := range openConfigs { + if args[1] == config.sshExtension { + return true + } + } + return false +} + +func validFolderOpenArgs(args []string) bool { + if len(args) >= 2 && args[0] == "--disable-extension" && args[1] == containersExtension { + args = args[2:] + } + if len(args) != 3 { + return false + } + switch args[0] { + case "--new-window", "--reuse-window": + return args[1] == "--folder-uri" && validRemoteFolderURI(args[2]) + default: + return false + } +} + +func validRemoteFolderURI(rawURI string) bool { + folder, err := url.Parse(rawURI) + return err == nil && + folder.Scheme == remoteFolderScheme && + strings.HasPrefix(folder.Host, "ssh-remote+") && + strings.HasSuffix(folder.Host, pkgconfig.SSHHostSuffix) +} + +// stageError reports which launch stage failed and a coarse class of the +// failure. Its text never includes the wrapped error or editor output: those +// routinely contain host paths, URLs and tokens that cannot be reliably +// redacted. The original error stays reachable through Unwrap. +type stageError struct { + stage string + class string + err error +} + +func newStageError(stage string, err error) *stageError { + return &stageError{stage: stage, class: classifyFailure(err), err: err} +} + +func (e *stageError) Error() string { return e.stage + " failed: " + e.class } + +func (e *stageError) Unwrap() error { return e.err } + +func classifyFailure(err error) string { + var exitErr *exec.ExitError + switch { + case errors.Is(err, context.Canceled): + return "canceled" + case errors.Is(err, context.DeadlineExceeded): + return "timeout" + case errors.As(err, &exitErr): + return fmt.Sprintf("exit status %d", exitErr.ExitCode()) + case errors.Is(err, exec.ErrNotFound), errors.Is(err, fs.ErrNotExist): + return "not found" + case errors.Is(err, fs.ErrPermission): + return "permission denied" + default: + return "error" + } +} + func Open(ctx context.Context, params OpenParams) error { EnsureHostSettings(params.Flavor) - cliErr := openViaCLI(ctx, params) + return newHostLauncher().open(ctx, params) +} + +// open dispatches a launch request to the editor. Success means the OS or the +// editor CLI accepted the request; it does not prove the editor stayed alive or +// that the remote SSH connection was established. +// +// Records carry the status operation ID from ctx so they can be matched to the +// up operation that requested the launch. They never include editor output or +// error text, only the stage, route and failure class. +func (l hostLauncher) open(ctx context.Context, params OpenParams) error { + if err := ctx.Err(); err != nil { + return err + } + if _, ok := openConfigs[params.Flavor]; !ok { + return newStageError(stageResolve, errors.New("unsupported editor flavor")) + } + redactor := secrets.NewEnvironmentRedactor(os.Environ()) + fields := []any{ + "operation", redactor.Redact(status.OperationID(ctx)), + "flavor", redactor.Redact(string(params.Flavor)), + } + + cliErr := l.openViaCLI(ctx, params, fields) + if cliErr != nil { + log.Warnw( + "editor CLI launch failed", + append( + fields, + "route", + routeCLI, + "stage", + stageOf(cliErr), + "class", + classOf(cliErr), + )..., + ) + } if cliErr == nil { - log.Infof("opened %s via CLI", params.Flavor.DisplayName()) + log.Infow("launch request dispatched", append(fields, "route", routeCLI)...) return nil } @@ -96,16 +295,35 @@ func Open(ctx context.Context, params OpenParams) error { return ctx.Err() } - browserErr := openViaBrowser(params) - if browserErr == nil { - log.Infof("opened %s via browser", params.Flavor.DisplayName()) + log.Debugw("dispatching launch request", + append(fields, "route", routeURI, "stage", stageURIDispatch)...) + uriErr := l.openViaURI(params) + if uriErr == nil { + log.Infow("launch request dispatched", + append(fields, "route", routeURI, "after_cli_failure", true)...) return nil } - return errors.Join(cliErr, browserErr) + log.Warnw("URI launch failed", + append(fields, "route", routeURI, "stage", stageOf(uriErr), "class", classOf(uriErr))...) + return errors.Join(cliErr, uriErr) +} + +func stageOf(err error) string { + if stageErr, ok := errors.AsType[*stageError](err); ok { + return stageErr.stage + } + return "" +} + +func classOf(err error) string { + if stageErr, ok := errors.AsType[*stageError](err); ok { + return stageErr.class + } + return classifyFailure(err) } -func openViaBrowser(params OpenParams) error { +func (l hostLauncher) openViaURI(params OpenParams) error { config, ok := openConfigs[params.Flavor] if !ok { return fmt.Errorf("unknown flavor %s", params.Flavor) @@ -115,7 +333,7 @@ func openViaBrowser(params OpenParams) error { pathStr := fmt.Sprintf("/ssh-remote+%s"+pkgconfig.SSHHostSuffix+"/%s", params.Workspace, folder) u := &url.URL{ Scheme: config.scheme, - Host: "vscode-remote", + Host: remoteFolderScheme, Path: pathStr, } if params.NewWindow { @@ -125,32 +343,27 @@ func openViaBrowser(params OpenParams) error { } openURL := u.String() - log.Debugf("opening URL %s", openURL) - err := devsyopen.Run(openURL) - if err != nil { - log.Errorf( - "flavor %s is not installed on host device: %v", - params.Flavor.DisplayName(), - err, - ) - return err + if err := l.openURI(openURL); err != nil { + return newStageError(stageURIDispatch, err) } return nil } -func openViaCLI(ctx context.Context, params OpenParams) error { +func (l hostLauncher) openViaCLI(ctx context.Context, params OpenParams, fields []any) error { config, ok := openConfigs[params.Flavor] if !ok { return fmt.Errorf("unknown flavor %s", params.Flavor) } - cliPath := getCLIPath(config) - if cliPath == "" { - return fmt.Errorf("flavor %s binary is not found", params.Flavor) + cliPath, err := l.resolveCLI(config, fields) + if err != nil { + return err } - hasSSHExtension, hasContainersExtension, err := listInstalledExtensions( + log.Debugw("enumerating editor extensions", + append(fields, "route", routeCLI, "stage", stageListExtensions)...) + hasSSHExtension, hasContainersExtension, err := l.listInstalledExtensions( ctx, cliPath, config.sshExtension, @@ -160,33 +373,83 @@ func openViaCLI(ctx context.Context, params OpenParams) error { } if !hasSSHExtension { - if err := ensureSSHExtension(ctx, cliPath, config.sshExtension); err != nil { + log.Debugw("installing editor SSH extension", + append(fields, "route", routeCLI, "stage", stageInstallSSHExt)...) + if err := l.ensureSSHExtension(ctx, cliPath, config.sshExtension); err != nil { return err } } args := buildOpenArgs(params.Workspace, params.Folder, params.NewWindow, hasContainersExtension) - log.Debugf( - "flavor %s command %s %s", - params.Flavor.DisplayName(), - cliPath, - strings.Join(args, " "), - ) - out, err := exec.CommandContext(ctx, cliPath, args...).CombinedOutput() - if err != nil { - return command.WrapCommandError(out, err) + log.Debugw("dispatching launch request", + append(fields, "route", routeCLI, "stage", stageCLIDispatch)...) + if _, err := l.run(ctx, cliPath, args...); err != nil { + return newStageError(stageCLIDispatch, err) } return nil } -func listInstalledExtensions( +// resolveCLI finds the editor CLI. It logs only the file kind, not the path: on +// Windows a .cmd wrapper and a native executable fail in different ways, while +// the path itself usually contains the user's name. +func (l hostLauncher) resolveCLI(config openConfig, fields []any) (string, error) { + candidates := []string{config.cliName} + if l.goos == "darwin" { + candidates = append(candidates, config.macAppPath) + } + + var lookupErrs []error + for _, candidate := range candidates { + path, err := l.lookPath(candidate) + if err == nil { + path, err = filepath.Abs(path) + if err != nil { + lookupErrs = append(lookupErrs, err) + continue + } + log.Debugw( + "editor CLI resolved", + append( + fields, + "route", + routeCLI, + "stage", + stageResolve, + "cli_kind", + cliKind(path), + )...) + return path, nil + } + lookupErrs = append(lookupErrs, err) + } + + return "", newStageError(stageResolve, errors.Join(lookupErrs...)) +} + +// cliKind classifies the resolved file by extension only. It does not show how +// the file is executed, so a "batch wrapper" result is not evidence about +// native Windows .cmd launch behavior. +func cliKind(path string) string { + switch strings.ToLower(filepath.Ext(path)) { + case ".cmd", ".bat": + return "batch wrapper" + case ".exe": + return "native executable" + case "": + return "no extension" + default: + return "other" + } +} + +func (l hostLauncher) listInstalledExtensions( ctx context.Context, cliPath, sshExtension string, ) (hasSSH, hasContainers bool, err error) { - out, err := exec.CommandContext(ctx, cliPath, "--list-extensions").CombinedOutput() + out, err := l.run(ctx, cliPath, listExtensionsArg) if err != nil { - return false, false, command.WrapCommandError(out, err) + return false, false, newStageError(stageListExtensions, err) } for ext := range strings.SplitSeq(string(out), "\n") { @@ -206,12 +469,9 @@ func listInstalledExtensions( return hasSSH, hasContainers, nil } -func ensureSSHExtension(ctx context.Context, cliPath, sshExtension string) error { - args := []string{"--install-extension", sshExtension} - log.Debugf("%s %s", cliPath, strings.Join(args, " ")) - out, err := exec.CommandContext(ctx, cliPath, args...).CombinedOutput() - if err != nil { - return command.WrapCommandError(out, err) +func (l hostLauncher) ensureSSHExtension(ctx context.Context, cliPath, sshExtension string) error { + if _, err := l.run(ctx, cliPath, "--install-extension", sshExtension); err != nil { + return newStageError(stageInstallSSHExt, err) } return nil } @@ -230,7 +490,7 @@ func buildOpenArgs(workspace, folder string, newWindow, hasContainersExtension b } u := &url.URL{ - Scheme: "vscode-remote", + Scheme: remoteFolderScheme, Host: "ssh-remote+" + workspace + pkgconfig.SSHHostSuffix, Path: strings.TrimPrefix(folder, "/"), } @@ -239,15 +499,3 @@ func buildOpenArgs(workspace, folder string, newWindow, hasContainersExtension b return args } - -func getCLIPath(config openConfig) string { - if command.Exists(config.cliName) { - return config.cliName - } - - if runtime.GOOS == "darwin" && command.Exists(config.macAppPath) { - return config.macAppPath - } - - return "" -} diff --git a/pkg/ide/vscode/open_test.go b/pkg/ide/vscode/open_test.go new file mode 100644 index 0000000000..b23edfb596 --- /dev/null +++ b/pkg/ide/vscode/open_test.go @@ -0,0 +1,413 @@ +package vscode + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/status" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" +) + +type call struct { + path string + args []string +} + +func (c call) isListExtensions() bool { + return len(c.args) == 1 && c.args[0] == listExtensionsArg +} + +type OpenSuite struct { + suite.Suite + logs *observer.ObservedLogs + calls []call + uris []string + launcher hostLauncher + runFn func(ctx context.Context, c call) ([]byte, error) + uriErr error +} + +func TestOpenSuite(t *testing.T) { + suite.Run(t, new(OpenSuite)) +} + +func (s *OpenSuite) SetupTest() { + s.logs = log.InitTestObserved(s.T(), zapcore.DebugLevel) + s.calls = nil + s.uris = nil + s.uriErr = nil + s.runFn = func(_ context.Context, c call) ([]byte, error) { + if c.isListExtensions() { + return []byte("jeanp413.open-remote-ssh\n"), nil + } + return nil, nil + } + s.launcher = hostLauncher{ + lookPath: func(file string) (string, error) { return "/bin/" + file, nil }, + run: func(ctx context.Context, path string, args ...string) ([]byte, error) { + c := call{path: path, args: args} + s.calls = append(s.calls, c) + return s.runFn(ctx, c) + }, + openURI: func(uri string) error { + s.uris = append(s.uris, uri) + return s.uriErr + }, + goos: "linux", + } +} + +func testParams() OpenParams { + return OpenParams{Workspace: "ws", Folder: "/workspaces/ws", Flavor: FlavorCodium} +} + +func (s *OpenSuite) TestCLISuccessDispatchesOnceWithCorrelation() { + operationID, err := s.openInOperation(testParams()) + + s.Require().NoError(err) + s.Require().NotEmpty(operationID) + s.Len(s.calls, 2) + s.Empty(s.uris) + dispatched := s.records(zapcore.InfoLevel) + s.Require().Len(dispatched, 1) + s.Equal(operationID, dispatched[0].ContextMap()["operation"]) + s.Equal(string(FlavorCodium), dispatched[0].ContextMap()["flavor"]) + s.Equal(routeCLI, dispatched[0].ContextMap()["route"]) + resolved := s.logs.FilterField(zap.String("cli_kind", "no extension")).All() + s.Require().Len(resolved, 1) + s.Equal(stageResolve, resolved[0].ContextMap()["stage"]) +} + +func (s *OpenSuite) TestCLIFailureFallsBackAndKeepsCauseAndCorrelation() { + cliCause := errors.New("exit status 1") + s.runFn = func(_ context.Context, c call) ([]byte, error) { + if c.isListExtensions() { + return []byte("jeanp413.open-remote-ssh\n"), nil + } + return nil, cliCause + } + + operationID, err := s.openInOperation(testParams()) + + s.Require().NoError(err) + s.Require().Len(s.uris, 1) + s.Contains(s.uris[0], "codium://vscode-remote/") + + failed := s.records(zapcore.WarnLevel) + s.Require().Len(failed, 1) + s.Equal(operationID, failed[0].ContextMap()["operation"]) + s.Equal(routeCLI, failed[0].ContextMap()["route"]) + s.Equal(stageCLIDispatch, failed[0].ContextMap()["stage"]) + + dispatched := s.records(zapcore.InfoLevel) + s.Require().Len(dispatched, 1) + s.Equal(operationID, dispatched[0].ContextMap()["operation"]) + s.Equal(routeURI, dispatched[0].ContextMap()["route"]) + s.Equal(true, dispatched[0].ContextMap()["after_cli_failure"]) +} + +func (s *OpenSuite) TestBothRoutesFailingKeepsCausesAndLogsBothFailures() { + cliCause := errors.New("cli cause") + uriCause := errors.New("uri cause") + s.runFn = func(context.Context, call) ([]byte, error) { return nil, cliCause } + s.uriErr = uriCause + + operationID, err := s.openInOperation(testParams()) + + s.Require().Error(err) + s.ErrorIs(err, cliCause) + s.ErrorIs(err, uriCause) + failed := s.records(zapcore.WarnLevel) + s.Require().Len(failed, 2) + for _, entry := range failed { + s.Equal(operationID, entry.ContextMap()["operation"]) + } + s.Equal(stageListExtensions, failed[0].ContextMap()["stage"]) + s.Equal(stageURIDispatch, failed[1].ContextMap()["stage"]) + s.Empty(s.records(zapcore.InfoLevel)) +} + +func (s *OpenSuite) TestCancelledCLIDoesNotOpenURI() { + ctx, cancel := context.WithCancel(context.Background()) + s.runFn = func(context.Context, call) ([]byte, error) { + cancel() + return nil, context.Canceled + } + + err := s.launcher.open(ctx, testParams()) + + s.ErrorIs(err, context.Canceled) + s.Empty(s.uris) +} + +func (s *OpenSuite) TestMissingCLIFallsBackToURI() { + s.launcher.lookPath = func(string) (string, error) { return "", errors.New("not found") } + + _, err := s.openInOperation(testParams()) + + s.Require().NoError(err) + s.Empty(s.calls) + s.Len(s.uris, 1) + failed := s.records(zapcore.WarnLevel) + s.Require().Len(failed, 1) + s.Equal(stageResolve, failed[0].ContextMap()["stage"]) +} + +func (s *OpenSuite) TestMissingSSHExtensionIsInstalledBeforeDispatch() { + s.runFn = func(_ context.Context, c call) ([]byte, error) { + if c.isListExtensions() { + return []byte("ms-vscode-remote.remote-containers\n"), nil + } + return nil, nil + } + + s.Require().NoError(s.launcher.open(context.Background(), testParams())) + + s.Require().Len(s.calls, 3) + s.Equal([]string{"--install-extension", "jeanp413.open-remote-ssh"}, s.calls[1].args) + s.Contains(s.calls[2].args, "--disable-extension") +} + +func (s *OpenSuite) TestInstallFailureNamesStageAndKeepsCause() { + installErr := errors.New("install cause") + s.runFn = func(_ context.Context, c call) ([]byte, error) { + if c.isListExtensions() { + return nil, nil + } + return nil, installErr + } + + err := s.launcher.openViaCLI(context.Background(), testParams(), nil) + + s.ErrorIs(err, installErr) + s.Equal(stageInstallSSHExt, stageOf(err)) + s.Len(s.calls, 2) +} + +// Editor output, paths and error text can hold secrets that no exact-value +// redactor knows about, so none of it may reach logs or rendered errors. +func (s *OpenSuite) TestLogsAndErrorsOmitEditorOutputPathsAndErrorText() { + canaries := []string{ + `C:\Users\Alice\AppData\Local\Programs\VSCodium\bin\codium.cmd`, + `c:/users/alice/appdata/local/programs/vscodium/bin/codium.cmd`, + `\\fileserver\share\alice\profile`, + `/home/alice/.config/VSCodium`, + "build-host.internal", + "token=unknown-query-token-8841", + "boundary-secret-value-7731", + } + s.T().Setenv("DEVSY_TEST_TOKEN", canaries[6]) + // Exercise the old truncation boundary and check the leaked prefix too. + output := strings.Repeat("x", 2040) + canaries[6] + strings.Repeat("y", 100) + leaky := fmt.Errorf("%s: %s", strings.Join(canaries, " "), output) + for _, failedStage := range []string{ + stageResolve, stageListExtensions, stageInstallSSHExt, stageCLIDispatch, + } { + s.Run(failedStage, func() { + s.logs.TakeAll() + s.launcher.lookPath = func(string) (string, error) { + if failedStage == stageResolve { + return "", leaky + } + return canaries[0], nil + } + s.runFn = func(_ context.Context, c call) ([]byte, error) { + if c.isListExtensions() && failedStage != stageListExtensions { + if failedStage == stageInstallSSHExt { + return nil, nil + } + return []byte("jeanp413.open-remote-ssh\n"), nil + } + return []byte(output), leaky + } + s.uriErr = leaky + params := testParams() + params.Workspace = canaries[4] + params.Folder = canaries[1] + _, err := s.openInOperation(params) + s.Require().Error(err) + logged := s.everythingLogged() + err.Error() + for _, canary := range canaries { + s.NotContains(logged, canary) + } + s.NotContains(logged, canaries[6][:8]) + s.NotContains(logged, "Alice") + s.NotContains(logged, "alice") + s.ErrorIs(err, leaky) + failed := s.records(zapcore.WarnLevel) + s.Require().Len(failed, 2) + s.Equal(failedStage, failed[0].ContextMap()["stage"]) + s.Equal(stageURIDispatch, failed[1].ContextMap()["stage"]) + }) + } +} + +func (s *OpenSuite) TestLogsOmitResolvedPathOnSuccess() { + const resolved = `C:\Users\Alice\AppData\Local\Programs\VSCodium\bin\codium.cmd` + s.launcher.lookPath = func(string) (string, error) { return resolved, nil } + + s.Require().NoError(s.launcher.open(context.Background(), testParams())) + + s.NotContains(s.everythingLogged(), "Alice") + s.NotContains(s.everythingLogged(), "alice") +} + +func (s *OpenSuite) TestClassifyFailureUsesErrorKindsNotText() { + s.Equal("canceled", classifyFailure(fmt.Errorf("x: %w", context.Canceled))) + s.Equal("timeout", classifyFailure(fmt.Errorf("x: %w", context.DeadlineExceeded))) + s.Equal("error", classifyFailure(errors.New("anything else"))) + + _, lookErr := exec.LookPath("devsy-test-binary-that-does-not-exist") + s.Equal("not found", classifyFailure(lookErr)) +} + +func (s *OpenSuite) TestClassifyFailureReportsExitStatus() { + if runtime.GOOS == "windows" { + s.T().Skip("requires a POSIX shell") + } + exitErr := exec.Command("sh", "-c", "exit 3").Run() + + s.Equal("exit status 3", classifyFailure(exitErr)) +} + +func (s *OpenSuite) TestCLIKindClassifiesByExtensionOnly() { + s.Equal("batch wrapper", cliKind(`C:\Program Files\VSCodium\bin\codium.CMD`)) + s.Equal("native executable", cliKind(`C:\VSCodium\VSCodium.exe`)) + s.Equal("no extension", cliKind("/usr/bin/codium")) +} + +func (s *OpenSuite) TestMacAppPathIsCandidateOnlyOnDarwin() { + var looked []string + s.launcher.lookPath = func(file string) (string, error) { + looked = append(looked, file) + return "", errors.New("not found") + } + config := openConfigs[FlavorCodium] + + _, err := s.launcher.resolveCLI(config, nil) + s.Require().Error(err) + s.Equal([]string{"codium"}, looked) + + looked = nil + s.launcher.goos = "darwin" + _, err = s.launcher.resolveCLI(config, nil) + s.Require().Error(err) + s.Equal([]string{"codium", config.macAppPath}, looked) +} + +func (s *OpenSuite) TestHostCLIRejectsUntrustedCommands() { + sh, err := exec.LookPath("sh") + if err != nil { + s.T().Skip("requires a POSIX shell") + } + marker := filepath.Join(s.T().TempDir(), "executed") + _, err = runHostCLI(context.Background(), sh, "-c", "touch \"$1\"", "sh", marker) + s.Require().Error(err) + _, statErr := os.Stat(marker) + s.ErrorIs(statErr, os.ErrNotExist) + + for _, args := range [][]string{ + {"--install-extension", "untrusted.extension"}, + {"--user-data-dir", marker}, + {"--reuse-window", "--folder-uri", "https://example.invalid/"}, + {"--reuse-window", "--folder-uri", "vscode-remote://ssh-remote+ws.devsy/w", "--extra"}, + } { + _, err = runHostCLI(context.Background(), filepath.Join(s.T().TempDir(), "codium"), args...) + s.Require().Error(err) + s.NotErrorIs(err, os.ErrNotExist) + } +} + +func (s *OpenSuite) TestHostCLIEnumeratesAllExtensionsAndPreservesFolderArgv() { + if runtime.GOOS == "windows" { + s.T().Skip("fixture is a POSIX shell script, not Windows wrapper execution") + } + dir := filepath.Join(s.T().TempDir(), "editor space \u00e9") + s.Require().NoError(os.MkdirAll(dir, 0o700)) + script := `#!/bin/sh +if [ "$1" = "--list-extensions" ]; then + printf '%s\n' jeanp413.open-remote-ssh ms-vscode-remote.remote-containers + i=0 + while [ "$i" -lt 260 ]; do + printf 'test.extension-%s\n' "$i" + i=$((i+1)) + done + exit 0 +fi +if [ "$1" = "--install-extension" ]; then exit 99; fi +printf '%s\n' "$@" > "$DEVSY_TEST_ARGV_FILE" +` + root, err := os.OpenRoot(dir) + s.Require().NoError(err) + s.T().Cleanup(func() { s.NoError(root.Close()) }) + executable, err := root.OpenFile("codium", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = executable.Close() }) + _, err = executable.WriteString(script) + s.Require().NoError(err) + s.Require().NoError(executable.Chmod(0o700)) + s.Require().NoError(executable.Close()) + argvFile := filepath.Join(dir, "argv") + s.T().Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + s.T().Setenv("DEVSY_TEST_ARGV_FILE", argvFile) + s.launcher = newHostLauncher() + s.launcher.openURI = func(string) error { return errors.New("unexpected URI fallback") } + params := testParams() + params.Folder = "/workspaces/a b/\u96ea" + params.NewWindow = true + _, err = s.openInOperation(params) + s.Require().NoError(err) + argv, err := root.ReadFile("argv") + s.Require().NoError(err) + s.Equal("--disable-extension\nms-vscode-remote.remote-containers\n--new-window\n"+ + "--folder-uri\nvscode-remote://ssh-remote+ws.devsy/workspaces/a%20b/%E9%9B%AA\n", string(argv)) + s.Empty(s.records(zapcore.WarnLevel)) +} + +// openInOperation runs the launcher inside a real status operation, the way +// launchIDEPhase does, and returns the operation ID the launch was run under. +func (s *OpenSuite) openInOperation(params OpenParams) (operationID string, err error) { + err = status.Run( + context.Background(), + status.Nop(), + status.Operation{Phase: status.PhaseLaunchingIDE}, + func(ctx context.Context) error { + operationID = status.OperationID(ctx) + return s.launcher.open(ctx, params) + }, + ) + s.Require().NotEmpty(operationID) + for _, entry := range s.logs.AllUntimed() { + s.Equal(operationID, entry.ContextMap()["operation"]) + s.Equal(string(params.Flavor), entry.ContextMap()["flavor"]) + } + return operationID, err +} + +func (s *OpenSuite) records(level zapcore.Level) []observer.LoggedEntry { + return s.logs.FilterLevelExact(level).AllUntimed() +} + +// everythingLogged renders every message and structured field of every entry, +// at every level, so privacy assertions cannot miss a field. +func (s *OpenSuite) everythingLogged() string { + var b strings.Builder + for _, entry := range s.logs.AllUntimed() { + b.WriteString(entry.Message + "\n") + for key, value := range entry.ContextMap() { + fmt.Fprintf(&b, "%s=%v\n", key, value) + } + } + return b.String() +} diff --git a/pkg/subprocess/capture.go b/pkg/subprocess/capture.go index 8aad2940d6..df57ee83a6 100644 --- a/pkg/subprocess/capture.go +++ b/pkg/subprocess/capture.go @@ -25,9 +25,13 @@ const ( // Options configures a command invocation. type Options struct { - Dir string - Env []string - Stdin io.Reader + Dir string + Env []string + Stdin io.Reader + // Stdout optionally receives complete, unredacted stdout for a protocol + // consumer. It must not be a diagnostic log sink. Result capture remains + // bounded and redacted independently. + Stdout io.Writer Redactor *secrets.Redactor // SensitiveValues contains secret values that may appear in argv or in // subprocess output but are not represented by a sensitive environment @@ -163,7 +167,7 @@ func Run(ctx context.Context, binary string, args []string, options Options) (Re } cmd.Stdin = options.Stdin redactor := commandRedactor(options) - result, err := RunCommand(cmd, redactor) + result, err := runCommand(cmd, redactor, options.Stdout) err = withContextError(ctx, err) result.OperationID = options.OperationID return result, err @@ -202,12 +206,19 @@ func withContextError(ctx context.Context, err error) error { // stdout and stderr capture. Existing command setup such as an explicit // environment, working directory, or stdin is preserved. func RunCommand(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { + return runCommand(cmd, redactor, nil) +} + +func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, stdout io.Writer) (Result, error) { if cmd == nil { return Result{}, errors.New("subprocess command is nil") } out := NewBuffer(redactor) errOut := NewBuffer(redactor) cmd.Stdout = out + if stdout != nil { + cmd.Stdout = io.MultiWriter(out, stdout) + } cmd.Stderr = errOut binary := cmd.Path args := []string(nil) diff --git a/pkg/subprocess/capture_test.go b/pkg/subprocess/capture_test.go index c8b3e8c878..31ee966dfb 100644 --- a/pkg/subprocess/capture_test.go +++ b/pkg/subprocess/capture_test.go @@ -180,6 +180,32 @@ func TestRunCapturesFailureOutput(t *testing.T) { } } +func TestRunKeepsFunctionalStdoutIndependentOfDiagnosticLimits(t *testing.T) { + if runtime.GOOS == testWindows { + t.Skip("test command uses sh") + } + payload := captureTestSecret + "\n" + strings.Repeat("extension\n", MaxCapturedLines+20) + + captureTestSecret + "\n" + var stdout bytes.Buffer + result, err := Run(context.Background(), "sh", []string{"-c", "cat"}, Options{ + Stdin: strings.NewReader(payload), + Stdout: &stdout, + Redactor: secrets.NewRedactor([]string{captureToken + captureTestSecret}), + }) + if err != nil { + t.Fatal(err) + } + if stdout.String() != payload { + t.Fatal("functional stdout was truncated or redacted") + } + if !result.Truncated || strings.Contains(result.Stdout, captureTestSecret) { + t.Fatal("diagnostic stdout must remain bounded and redacted") + } + if !strings.Contains(result.Stdout, "***") { + t.Fatal("trailing credential should be masked in diagnostics") + } +} + func TestRunRedactsSensitiveEnvironmentAndArgvValues(t *testing.T) { if runtime.GOOS == testWindows { t.Skip("test command uses sh") diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx index 552b8f32bf..d23421c742 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx @@ -45,6 +45,28 @@ Pass `--log-output json` to get errors as JSON with a stable `code` and a `messa Run with `--debug` to see the full error chain. +### VS Code-family editor launch diagnostics + +For VS Code, VSCodium, and related editors, a failed CLI launch records the +`flavor`, `operation`, `route`, failed `stage`, and failure `class` (for example, +`exit status 1` or `permission denied`). A successful URI-handler fallback +does not erase that CLI warning. With `--debug`, the logs also record executable +resolution, extension enumeration/installation, and dispatch stages. Use the +same `operation` value to correlate these records within the workspace task's +logs; it comes from the active IDE-launch status operation. + +Executable paths, command arguments, folder URIs, editor output, and underlying +error text are deliberately omitted from these diagnostics because they may +contain private paths, host names, or credentials that cannot be reliably +redacted. Failure classes and stage names remain available; callers can inspect +the original errors programmatically. The resolved file's `cli_kind` identifies +an executable or batch-wrapper filename, not proof that a Windows wrapper ran. + +"Launch request dispatched" means the editor CLI or OS URI handler accepted the +request. It does not prove that the editor stayed running or connected over SSH. +If the editor closes or does not connect, collect its own logs separately and +review them for private data before sharing. + ### Windows: `line 2: $'\r': command not found` Windows line endings break shell scripts inside the container. Add this to `.gitattributes`: