diff --git a/README.md b/README.md index a06b7dc..a1e6220 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,47 @@ version tags are published, it selects the latest release version. Prebuilt archives for Linux, macOS, and Windows will be available with tagged [GitHub releases](https://github.com/boostsecurityio/emBarrass/releases). +### Shell completion + +Tab completion covers emBarrass subcommands, flags, and `--chain` values +(`all`, `emsign`, and `sectigo`), including flags on `embarass batch`. + +For **Zsh**, add this to `~/.zshrc` after your existing completion setup: + +```zsh +autoload -Uz compinit +compinit +source <(embarass completion zsh) +``` + +If your shell framework already runs `compinit`, only add the `source` line. +Run the same lines in your current shell to enable completion immediately. + +For **Bash**, install your OS's `bash-completion` package, then add this to +`~/.bashrc` after loading it: + +```bash +source <(embarass completion bash) +``` + +For **Fish**, generate its automatically loaded completion file: + +```fish +mkdir -p ~/.config/fish/completions +embarass completion fish > ~/.config/fish/completions/embarass.fish +``` + +For **PowerShell**, add this to your `$PROFILE`: + +```powershell +embarass completion powershell | Out-String | Invoke-Expression +``` + +Reopen your shell, then try `embarass ba`, `embarass --he`, or +`embarass batch --chain `. For a local build, load the script with +`./embarass completion zsh` (or your shell's name). Completion covers +emBarrass's own interface; it does not complete a wrapped tool's flags. + ## Why this exists Eight eMudhra P-384 intermediate CA private scalars were generated with values below 256. Six were TLS issuing keys and two were code-signing keys. Each key appeared in both a directly issued eMudhra certificate and a Sectigo cross-certificate. @@ -106,7 +147,10 @@ examples. `embarass --help` includes the complete list above. ### Batch testing installed tools `embarass batch` discovers `curl`, `git`, `gh`, `wget`, `npm`, `go`, -`pip`/`pip3`, `docker`, and `crane` in `PATH`. Docker contributes two probes: +`pip`/`pip3`, `docker`, `crane`, `aws`, `gcloud`, `cargo`, `uv`, `kubectl`, +Python (`python3`/`python`), Node.js (`node`/`nodejs`), RubyGems (`gem`), +`dotnet`, `pnpm`, HTTPie (`http`/`https`), `helm`, and `composer` in `PATH`. +Docker contributes two probes: `docker` tests the host-side CLI with a manifest request, while `docker/git` runs Git inside a historical, pre-mitigation, digest-pinned image to test its embedded CA bundle. This does not test or make any claim about Chainguard's @@ -125,8 +169,8 @@ selected for each installed tool and marks missing tools as skipped. The prompt defaults to **no**; only `y` starts the sweep. The probes run sequentially against public endpoints. Child output is -discarded, the intercepted application request is not forwarded upstream, npm -and Go caches are placed inside one temporary workspace, and that workspace is +discarded, the intercepted application request is not forwarded upstream, tool +caches and temporary configurations are placed inside one workspace, which is removed afterward. Package installation, repository checkout, and writes to the current directory are deliberately avoided. @@ -143,6 +187,37 @@ isolated configuration directory. It cannot read or transmit the user's GitHub credentials. The `crane` probe similarly uses an isolated Docker configuration directory while fetching only a public image manifest. +The additional probes work without logging in or installing packages: + +| Tool | Read-only probe | Isolation | +|---|---|---| +| AWS CLI (`aws`) | Unsigned `s3api head-bucket` for the public `noaa-goes16` bucket | Temporary default profile, no shared credential file, metadata lookup disabled | +| `gcloud` | `components list` fetches Google's public component catalog | Temporary configuration, credential loading and usage reporting disabled | +| `cargo` | `search serde --limit 1 --registry crates-io` | Temporary Cargo home and sparse registry access; rustup toolchain downloads disabled | +| `uv` | Fetch a public Requests requirements file with `pip compile` | Temporary cache, no configuration discovery, builds, Python downloads, or keyring access | +| `kubectl` | `get --raw=/version` against `https://example.com` | Empty kubeconfig and temporary cache; no cluster credentials or credential plugins | +| Python | Standard-library `urllib.request` against `https://example.com/` | Isolated Python mode; no third-party packages required | +| Node.js | A TLS `HEAD` request to `https://example.com/` | Embedded probe script uses the local proxy and Node's default certificate verification | +| RubyGems (`gem`) | Remote search for `rake` on RubyGems.org | No gemrc configuration; temporary gem home, search path, and cache | +| .NET (`dotnet`) | Search for `Newtonsoft.Json` on NuGet.org | Temporary CLI home and NuGet configuration/cache; telemetry and first-run development certificate generation disabled | +| `pnpm` | Query `is-number` metadata on npmjs.org | Isolated npm configuration and caches; existing Corepack executable cache preserved and bootstrap downloads disabled | +| HTTPie | `HEAD https://example.com/` | Temporary configuration; stdin and netrc credentials ignored | +| `helm` | Show the public `hello-world` chart metadata | Temporary configuration, cache, and plugin paths; empty kubeconfig | +| `composer` | Show public `symfony/console` metadata | Temporary project/configuration; plugins, scripts, cache, and interaction disabled | + +The Python probe requires Python 3, and the .NET probe requires SDK 8.0.2xx or +later. Node's probe explicitly opens the local proxy tunnel so it also works on +versions that do not automatically honor proxy environment variables. These +runtime probes test the built-in TLS client; third-party HTTP libraries may +use different trust stores. Older or incomplete tool installations can return +an inconclusive result. + +The `kubectl` endpoint deliberately exercises HTTPS without requiring a real +cluster. Only the TLS decision matters; the request is never forwarded. +AWS uses [`--no-sign-request`](https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html) +and gcloud uses [`auth/disable_credentials`](https://docs.cloud.google.com/sdk/gcloud/reference/topic/configurations) +to reach TLS without loading cloud credentials. + The same credential protection applies automatically to the exact individual probe `gh api meta [--silent]`: emBarrass substitutes a throwaway token for the child process so `gh` reaches TLS even when the user is logged out. Other `gh` diff --git a/cmd/embarass/batch.go b/cmd/embarass/batch.go index 695f906..0eeb436 100644 --- a/cmd/embarass/batch.go +++ b/cmd/embarass/batch.go @@ -42,6 +42,7 @@ immutable, digest-pinned image.`, command.Flags().BoolVarP(&app.batchYes, "yes", "y", false, "run the displayed probes without prompting") command.Flags().DurationVar(&app.timeout, "timeout", defaultTimeout, "maximum runtime per tool and trust path; 0 disables the deadline") command.Flags().StringVar(&app.chain, "chain", chainAll, chainFlagUsage) + registerChainCompletion(command) command.Flags().BoolVar(&app.headless, "headless", false, "emit compact log lines without the terminal UI") command.Flags().BoolVarP(&app.verbose, "verbose", "v", false, "enable debug logging") return command diff --git a/cmd/embarass/completion.go b/cmd/embarass/completion.go new file mode 100644 index 0000000..6d458c8 --- /dev/null +++ b/cmd/embarass/completion.go @@ -0,0 +1,31 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +package main + +import ( + "strings" + + "github.com/spf13/cobra" +) + +func registerChainCompletion(command *cobra.Command) { + err := command.RegisterFlagCompletionFunc("chain", func(_ *cobra.Command, _ []string, prefix string) ([]string, cobra.ShellCompDirective) { + choices := make([]string, 0, 1+len(allChains)) + choices = append(choices, chainAll) + for _, chain := range allChains { + choices = append(choices, string(chain)) + } + var matches []string + for _, choice := range choices { + if strings.HasPrefix(choice, prefix) { + matches = append(matches, choice) + } + } + return matches, cobra.ShellCompDirectiveNoFileComp + }) + if err != nil { + // A missing or duplicate registration is a command construction bug. + panic(err) + } +} diff --git a/cmd/embarass/completion_test.go b/cmd/embarass/completion_test.go new file mode 100644 index 0000000..e380e0c --- /dev/null +++ b/cmd/embarass/completion_test.go @@ -0,0 +1,82 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +package main + +import ( + "bytes" + "context" + "strings" + "testing" + + "github.com/boostsecurityio/emBarrass/pkg/runner" + "github.com/stretchr/testify/require" +) + +func TestShellCompletion(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + name string + args []string + want []string + }{ + {"batch command", []string{"ba"}, []string{"batch", ":4"}}, + {"version command", []string{"ver"}, []string{"version", ":4"}}, + {"completion command", []string{"comp"}, []string{"completion", ":4"}}, + {"root flag", []string{"--he"}, []string{"--headless", "--help", ":4"}}, + {"batch flag", []string{"batch", "--y"}, []string{"--yes", ":4"}}, + {"root chains", []string{"--chain", ""}, []string{"all", "emsign", "sectigo", ":4"}}, + {"batch chains", []string{"batch", "--chain", ""}, []string{"all", "emsign", "sectigo", ":4"}}, + {"chain prefix", []string{"--chain", "em"}, []string{"emsign", ":4"}}, + {"chain equals", []string{"batch", "--chain=se"}, []string{"sectigo", ":4"}}, + {"unknown chain", []string{"--chain", "unknown"}, []string{":4"}}, + {"after flag", []string{"--headless", "--ch"}, []string{"--chain", ":4"}}, + {"child flags", []string{"curl", "--he"}, []string{":0"}}, + {"child chain flag", []string{"curl", "--chain", ""}, []string{":0"}}, + {"separator", []string{"--", "curl", "--he"}, []string{":0"}}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + output := executeCompletion(t, "embarass", append([]string{"__completeNoDesc"}, test.args...)) + require.ElementsMatch(t, test.want, strings.Split(strings.TrimSpace(output), "\n")) + }) + } +} + +func TestCompletionScriptsUseStableCommandName(t *testing.T) { + t.Parallel() + + for _, shell := range []string{"bash", "zsh", "fish", "powershell"} { + for _, invocation := range []string{"embarass", "./embarass", "/tmp/local build/embarass", `.\embarass.exe`} { + t.Run(shell+"/"+invocation, func(t *testing.T) { + t.Parallel() + output := executeCompletion(t, invocation, []string{"completion", shell}) + require.Contains(t, output, "embarass") + require.Contains(t, output, "__complete") + if invocation != "embarass" { + require.NotContains(t, output, invocation) + } + }) + } + } +} + +func executeCompletion(t *testing.T, invocation string, args []string) string { + t.Helper() + app := &application{ + invocation: invocation, + run: func(context.Context, runner.Options) (runner.Result, error) { + t.Fatal("completion must not run a TLS probe") + return runner.Result{}, nil + }, + } + command := newRootCommand(app) + var output, stderr bytes.Buffer + command.SetOut(&output) + command.SetErr(&stderr) + command.SetArgs(args) + require.NoError(t, command.Execute(), stderr.String()) + require.Equal(t, exitProtected, app.exitCode) + return output.String() +} diff --git a/cmd/embarass/root.go b/cmd/embarass/root.go index 6c49162..fb83008 100644 --- a/cmd/embarass/root.go +++ b/cmd/embarass/root.go @@ -73,8 +73,11 @@ func Execute() int { func newRootCommand(app *application) *cobra.Command { invocation := app.displayedInvocation() command := &cobra.Command{ - Use: invocation + " [flags] -- command [args...]", + Use: "embarass [flags] -- command [args...]", Short: "Wrap any command and test its TLS trust", + // Keep shell function names independent of argv[0], while preserving + // the copyable invocation in help (for example, ./embarass). + Annotations: map[string]string{cobra.CommandDisplayNameAnnotation: invocation}, Long: tagline + ` emBarrass routes one command through an authenticated local HTTPS interceptor. @@ -114,6 +117,7 @@ forward intercepted traffic, or change the host trust store.`, command.Flags().SetInterspersed(false) command.Flags().DurationVar(&app.timeout, "timeout", defaultTimeout, "maximum runtime per trust path; 0 disables the deadline") command.Flags().StringVar(&app.chain, "chain", chainAll, chainFlagUsage) + registerChainCompletion(command) command.Flags().BoolVar(&app.headless, "headless", false, "emit compact log lines without the terminal UI") command.Flags().BoolVarP(&app.verbose, "verbose", "v", false, "enable debug logging") command.AddCommand(newBatchCommand(app), newVersionCommand()) diff --git a/pkg/batchtest/everyday.go b/pkg/batchtest/everyday.go new file mode 100644 index 0000000..2ae24b0 --- /dev/null +++ b/pkg/batchtest/everyday.go @@ -0,0 +1,220 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +package batchtest + +import ( + _ "embed" + "fmt" + "os" + "path/filepath" + "runtime" + "strings" +) + +//go:embed probes/node.cjs +var nodeProbeScript []byte + +func prepareEverydayProbes(workspace string) error { + files := map[string][]byte{ + "node-probe.cjs": nodeProbeScript, + "NuGet.Config": []byte(``), + "composer.json": []byte(`{"repositories":[{"type":"composer","url":"https://repo.packagist.org"},{"packagist.org":false}]}`), + "npm-user-config": {}, + "npm-global-config": {}, + } + for name, contents := range files { + if err := os.WriteFile(filepath.Join(workspace, name), contents, 0o600); err != nil { + return fmt.Errorf("prepare batch probe file %s: %w", name, err) + } + } + return nil +} + +func everydayTools() []toolDefinition { + return []toolDefinition{ + { + name: "python", + executables: []string{"python3", "python"}, + build: func(executable, _ string) Probe { + const script = "import sys, urllib.request; urllib.request.urlopen(sys.argv[1]).close()" + return Probe{ + Name: "python", + Executable: executable, + Arguments: []string{"-I", "-c", script, "https://example.com/"}, + Preview: "python3 -I -c '" + script + "' https://example.com/", + } + }, + }, + { + name: "node", + executables: []string{"node", "nodejs"}, + build: func(executable, workspace string) Probe { + arguments := []string{filepath.Join(workspace, "node-probe.cjs"), "https://example.com/"} + return Probe{ + Name: "node", + Executable: executable, + Arguments: arguments, + Preview: "node " + strings.Join(arguments, " "), + } + }, + }, + { + name: "gem", + executables: []string{"gem"}, + build: func(executable, workspace string) Probe { + arguments := []string{"search", "rake", "--exact", "--remote", "--norc", "--clear-sources", "--source", "https://rubygems.org/"} + return Probe{ + Name: "gem", + Executable: executable, + Arguments: arguments, + Preview: "gem " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "GEM_HOME": filepath.Join(workspace, "gem-home"), + "GEM_PATH": filepath.Join(workspace, "gem-home"), + "GEM_SPEC_CACHE": filepath.Join(workspace, "gem-cache"), + }, + } + }, + }, + { + name: "dotnet", + executables: []string{"dotnet"}, + build: func(executable, workspace string) Probe { + arguments := []string{ + "package", "search", "Newtonsoft.Json", "--exact-match", + "--source", "https://api.nuget.org/v3/index.json", + "--configfile", filepath.Join(workspace, "NuGet.Config"), + } + return Probe{ + Name: "dotnet", + Executable: executable, + Arguments: arguments, + Preview: "dotnet " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "DOTNET_CLI_HOME": filepath.Join(workspace, "dotnet-home"), + "DOTNET_CLI_TELEMETRY_OPTOUT": "1", + "DOTNET_SKIP_FIRST_TIME_EXPERIENCE": "1", + "DOTNET_GENERATE_ASPNET_CERTIFICATE": "false", + "DOTNET_ADD_GLOBAL_TOOLS_TO_PATH": "false", + "DOTNET_CLI_WORKLOAD_UPDATE_NOTIFY_DISABLE": "true", + "NUGET_PACKAGES": filepath.Join(workspace, "nuget-packages"), + "NUGET_HTTP_CACHE_PATH": filepath.Join(workspace, "nuget-cache"), + "NUGET_PLUGINS_CACHE_PATH": filepath.Join(workspace, "nuget-plugins"), + }, + } + }, + }, + { + name: "pnpm", + executables: []string{"pnpm"}, + build: func(executable, workspace string) Probe { + arguments := []string{"view", "is-number", "version", "--registry=https://registry.npmjs.org/"} + return Probe{ + Name: "pnpm", + Executable: executable, + Arguments: arguments, + Preview: "pnpm " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "NPM_CONFIG_USERCONFIG": filepath.Join(workspace, "npm-user-config"), + "NPM_CONFIG_GLOBALCONFIG": filepath.Join(workspace, "npm-global-config"), + "NPM_CONFIG_CACHE": filepath.Join(workspace, "pnpm-cache"), + "NPM_CONFIG_FETCH_RETRIES": "0", + "NPM_CONFIG_UPDATE_NOTIFIER": "false", + "PNPM_HOME": filepath.Join(workspace, "pnpm-home"), + "XDG_CACHE_HOME": filepath.Join(workspace, "pnpm-xdg-cache"), + "XDG_CONFIG_HOME": filepath.Join(workspace, "pnpm-xdg-config"), + "XDG_DATA_HOME": filepath.Join(workspace, "pnpm-xdg-data"), + "XDG_STATE_HOME": filepath.Join(workspace, "pnpm-xdg-state"), + "COREPACK_HOME": corepackHome(workspace), + "COREPACK_ENABLE_NETWORK": "0", + "COREPACK_ENABLE_DOWNLOAD_PROMPT": "0", + }, + } + }, + }, + { + name: "httpie", + executables: []string{"http", "https"}, + build: func(executable, workspace string) Probe { + arguments := []string{"--ignore-stdin", "--ignore-netrc", "--headers", "HEAD", "https://example.com/"} + return Probe{ + Name: "httpie", + Executable: executable, + Arguments: arguments, + Preview: "http " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "HTTPIE_CONFIG_DIR": filepath.Join(workspace, "httpie-config"), + }, + } + }, + }, + { + name: "helm", + executables: []string{"helm"}, + build: func(executable, workspace string) Probe { + arguments := []string{"show", "chart", "hello-world", "--repo", "https://helm.github.io/examples"} + return Probe{ + Name: "helm", + Executable: executable, + Arguments: arguments, + Preview: "helm " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "HELM_CACHE_HOME": filepath.Join(workspace, "helm-cache"), + "HELM_CONFIG_HOME": filepath.Join(workspace, "helm-config"), + "HELM_DATA_HOME": filepath.Join(workspace, "helm-data"), + "HELM_REPOSITORY_CONFIG": filepath.Join(workspace, "helm-config", "repositories.yaml"), + "HELM_REPOSITORY_CACHE": filepath.Join(workspace, "helm-cache", "repository"), + "HELM_REGISTRY_CONFIG": filepath.Join(workspace, "helm-config", "registry.json"), + "HELM_PLUGINS": filepath.Join(workspace, "helm-plugins"), + "KUBECONFIG": os.DevNull, + }, + } + }, + }, + { + name: "composer", + executables: []string{"composer"}, + build: func(executable, workspace string) Probe { + arguments := []string{"--no-plugins", "--no-scripts", "--no-interaction", "--no-cache", "show", "--all", "symfony/console"} + return Probe{ + Name: "composer", + Executable: executable, + Arguments: arguments, + Preview: "composer " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "COMPOSER": filepath.Join(workspace, "composer.json"), + "COMPOSER_HOME": filepath.Join(workspace, "composer-home"), + "COMPOSER_CACHE_DIR": filepath.Join(workspace, "composer-cache"), + "COMPOSER_AUTH": "{}", + }, + } + }, + }, + } +} + +// Resolve Corepack's executable cache before XDG_CACHE_HOME is isolated. This +// follows nodejs/corepack's sources/folderUtils.ts, including its Windows +// fallback. Package-manager downloads remain disabled for the probe. +func corepackHome(workspace string) string { + if home, ok := os.LookupEnv("COREPACK_HOME"); ok { + return home + } + for _, key := range []string{"XDG_CACHE_HOME", "LOCALAPPDATA"} { + if cache, ok := os.LookupEnv(key); ok { + return filepath.Join(cache, "node", "corepack") + } + } + home, err := os.UserHomeDir() + if err != nil { + // Without a home, leave Corepack an empty isolated cache. It will + // fail without bootstrapping instead of guessing a user's cache. + return filepath.Join(workspace, "corepack") + } + cache := ".cache" + if runtime.GOOS == "windows" { + cache = filepath.Join("AppData", "Local") + } + return filepath.Join(home, cache, "node", "corepack") +} diff --git a/pkg/batchtest/everyday_test.go b/pkg/batchtest/everyday_test.go new file mode 100644 index 0000000..bfde694 --- /dev/null +++ b/pkg/batchtest/everyday_test.go @@ -0,0 +1,223 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +package batchtest + +import ( + "bytes" + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/boostsecurityio/emBarrass/pkg/certforge" + "github.com/boostsecurityio/emBarrass/pkg/runner" + "github.com/stretchr/testify/require" +) + +func TestPNPMPreservesCorepackCache(t *testing.T) { + for _, test := range []struct { + name string + env map[string]string + want string + }{ + {"explicit", map[string]string{"COREPACK_HOME": "custom", "XDG_CACHE_HOME": "xdg"}, "custom"}, + {"xdg", map[string]string{"XDG_CACHE_HOME": "xdg", "LOCALAPPDATA": "local"}, filepath.Join("xdg", "node", "corepack")}, + {"local app data", map[string]string{"LOCALAPPDATA": "local"}, filepath.Join("local", "node", "corepack")}, + {"home", nil, ""}, + } { + t.Run(test.name, func(t *testing.T) { + for _, key := range []string{"COREPACK_HOME", "XDG_CACHE_HOME", "LOCALAPPDATA"} { + // Setenv registers restoration before removing the variable. + t.Setenv(key, "") + require.NoError(t, os.Unsetenv(key)) + } + for key, value := range test.env { + t.Setenv(key, value) + } + want := test.want + if want == "" { + home, err := os.UserHomeDir() + require.NoError(t, err) + cache := ".cache" + if runtime.GOOS == "windows" { + cache = filepath.Join("AppData", "Local") + } + want = filepath.Join(home, cache, "node", "corepack") + } + plan := discover(t.TempDir(), func(name string) (string, error) { + if name == "pnpm" { + return "pnpm", nil + } + return "", errors.New("not found") + }) + require.Len(t, plan.Probes, 1) + env := plan.Probes[0].EnvironmentOverrides + require.Equal(t, want, env["COREPACK_HOME"]) + require.Equal(t, "0", env["COREPACK_ENABLE_NETWORK"]) + require.Equal(t, filepath.Join(plan.WorkingDirectory(), "pnpm-xdg-cache"), env["XDG_CACHE_HOME"]) + }) + } +} + +// Exercise the real Corepack launcher with a cached package-manager fixture. +// No package download or external request is needed; the fixture uses our +// embedded Node TLS probe in place of pnpm's registry client. +func TestInstalledCorepackLaunchesCachedPNPM(t *testing.T) { + if os.Getenv("EMBARASS_TEST_INSTALLED_TOOLS") != "1" { + t.Skip("set EMBARASS_TEST_INSTALLED_TOOLS=1 to test installed clients") + } + corepack, err := exec.LookPath("corepack") + if err != nil { + t.Skip("Corepack not installed") + } + t.Setenv("COREPACK_HOME", "") + require.NoError(t, os.Unsetenv("COREPACK_HOME")) + cache := t.TempDir() + t.Setenv("XDG_CACHE_HOME", cache) + home := filepath.Join(cache, "node", "corepack") + install := filepath.Join(home, "v1", "pnpm", "10.0.0") + require.NoError(t, os.MkdirAll(install, 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(home, "lastKnownGood.json"), []byte(`{"pnpm":"10.0.0"}`), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(install, ".corepack"), []byte(`{"bin":{"pnpm":"pnpm.cjs"},"hash":"sha512.fixture"}`), 0o600)) + // The fixture must receive the actual pnpm probe arguments before it can + // reach TLS, proving the cached executable (not a bootstrapper) ran. + script := []byte(`if (JSON.stringify(process.argv.slice(2)) !== JSON.stringify(["view", "is-number", "version", "--registry=https://registry.npmjs.org/"])) process.exit(42); +process.argv[2] = "https://registry.npmjs.org/"; +`) + script = append(script, nodeProbeScript...) + require.NoError(t, os.WriteFile(filepath.Join(install, "pnpm.cjs"), script, 0o600)) + plan := discover(t.TempDir(), func(name string) (string, error) { + if name == "pnpm" { + return corepack, nil + } + return "", errors.New("not found") + }) + require.Len(t, plan.Probes, 1) + probe := plan.Probes[0] + var stderr bytes.Buffer + result, err := runner.Run(context.Background(), runner.Options{ + Command: append([]string{corepack, "pnpm"}, probe.Arguments...), + Timeout: 5 * time.Second, Chain: certforge.ChainEMSign, + WorkingDirectory: plan.WorkingDirectory(), EnvironmentOverrides: probe.EnvironmentOverrides, + Stderr: &stderr, + }) + require.NoError(t, err) + require.Contains(t, []runner.Outcome{runner.OutcomeRejected, runner.OutcomeVulnerable}, result.Outcome, "%+v\n%s", result, stderr.String()) + require.Equal(t, "registry.npmjs.org", result.Host) +} + +var everydayProbeHosts = map[string]string{ + "python": "example.com", + "node": "example.com", + "gem": "rubygems.org", + "dotnet": "api.nuget.org", + "pnpm": "registry.npmjs.org", + "httpie": "example.com", + "helm": "helm.github.io", + "composer": "repo.packagist.org", +} + +func TestEverydayProbeDiscoverySupportsExecutableAliases(t *testing.T) { + t.Parallel() + for _, aliases := range [][]string{ + {"python3", "node", "http"}, + {"python", "nodejs", "https"}, + } { + workspace := t.TempDir() + installed := map[string]bool{"gem": true, "dotnet": true, "pnpm": true, "helm": true, "composer": true} + for _, alias := range aliases { + installed[alias] = true + } + plan := discover(workspace, func(name string) (string, error) { + if installed[name] { + return filepath.Join(workspace, "bin", name), nil + } + return "", errors.New("not found") + }) + require.Equal(t, []string{"python", "node", "gem", "dotnet", "pnpm", "httpie", "helm", "composer"}, probeNames(plan.Probes)) + require.Equal(t, filepath.Join(workspace, "bin", aliases[0]), plan.Probes[0].Executable) + require.Equal(t, filepath.Join(workspace, "bin", aliases[1]), plan.Probes[1].Executable) + require.Equal(t, filepath.Join(workspace, "bin", aliases[2]), plan.Probes[5].Executable) + } +} + +func TestEverydayProbesAvoidInstallationAndIsolateState(t *testing.T) { + t.Parallel() + workspace := t.TempDir() + testCases := []struct { + name string + arguments []string + paths []string + settings map[string]string + }{ + {name: "python", arguments: []string{"-I", "-c", "https://example.com/"}}, + {name: "node", arguments: []string{filepath.Join(workspace, "node-probe.cjs"), "https://example.com/"}}, + {name: "gem", arguments: []string{"search", "--remote", "--norc", "--clear-sources", "https://rubygems.org/"}, paths: []string{"GEM_HOME", "GEM_PATH", "GEM_SPEC_CACHE"}}, + { + name: "dotnet", arguments: []string{"package", "search", "--configfile", filepath.Join(workspace, "NuGet.Config"), "https://api.nuget.org/v3/index.json"}, + paths: []string{"DOTNET_CLI_HOME", "NUGET_PACKAGES", "NUGET_HTTP_CACHE_PATH", "NUGET_PLUGINS_CACHE_PATH"}, + settings: map[string]string{"DOTNET_GENERATE_ASPNET_CERTIFICATE": "false", "DOTNET_ADD_GLOBAL_TOOLS_TO_PATH": "false", "DOTNET_CLI_TELEMETRY_OPTOUT": "1"}, + }, + { + name: "pnpm", arguments: []string{"view", "--registry=https://registry.npmjs.org/"}, + paths: []string{"NPM_CONFIG_USERCONFIG", "NPM_CONFIG_GLOBALCONFIG", "NPM_CONFIG_CACHE", "PNPM_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME"}, + settings: map[string]string{"COREPACK_ENABLE_NETWORK": "0", "COREPACK_ENABLE_DOWNLOAD_PROMPT": "0"}, + }, + {name: "httpie", arguments: []string{"--ignore-stdin", "--ignore-netrc", "HEAD", "https://example.com/"}, paths: []string{"HTTPIE_CONFIG_DIR"}}, + { + name: "helm", arguments: []string{"show", "chart", "--repo", "https://helm.github.io/examples"}, + paths: []string{"HELM_CACHE_HOME", "HELM_CONFIG_HOME", "HELM_DATA_HOME", "HELM_REPOSITORY_CONFIG", "HELM_REPOSITORY_CACHE", "HELM_REGISTRY_CONFIG", "HELM_PLUGINS"}, + settings: map[string]string{"KUBECONFIG": os.DevNull}, + }, + { + name: "composer", arguments: []string{"show", "--all", "--no-plugins", "--no-scripts", "--no-interaction", "--no-cache"}, + paths: []string{"COMPOSER", "COMPOSER_HOME", "COMPOSER_CACHE_DIR"}, settings: map[string]string{"COMPOSER_AUTH": "{}"}, + }, + } + definitions := everydayTools() + for i, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + probe := definitions[i].build("/tools/"+testCase.name, workspace) + require.Equal(t, testCase.name, probe.Name) + for _, argument := range testCase.arguments { + require.Contains(t, probe.Arguments, argument) + } + for _, key := range testCase.paths { + require.Contains(t, probe.EnvironmentOverrides, key) + require.True(t, filepath.IsLocal(mustRelativePath(t, workspace, probe.EnvironmentOverrides[key])), key) + } + for key, value := range testCase.settings { + require.Equal(t, value, probe.EnvironmentOverrides[key], key) + } + require.False(t, probe.Containerized) + require.False(t, probe.MayPullImage) + }) + } +} + +func mustRelativePath(t *testing.T, root, path string) string { + t.Helper() + relative, err := filepath.Rel(root, path) + require.NoError(t, err) + return relative +} + +func TestEverydayProbeFilesArePreparedAndCleanedUp(t *testing.T) { + t.Parallel() + plan, err := Discover() + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, plan.Cleanup()) }) + for _, name := range []string{"node-probe.cjs", "NuGet.Config", "composer.json", "npm-user-config", "npm-global-config"} { + require.FileExists(t, filepath.Join(plan.WorkingDirectory(), name)) + } + script, err := os.ReadFile(filepath.Join(plan.WorkingDirectory(), "node-probe.cjs")) + require.NoError(t, err) + require.Equal(t, nodeProbeScript, script) + require.NoError(t, plan.Cleanup()) + require.NoDirExists(t, plan.WorkingDirectory()) +} diff --git a/pkg/batchtest/plan.go b/pkg/batchtest/plan.go index f0b6c8d..87ff301 100644 --- a/pkg/batchtest/plan.go +++ b/pkg/batchtest/plan.go @@ -6,6 +6,7 @@ package batchtest import ( + "errors" "fmt" "os" "os/exec" @@ -52,6 +53,19 @@ func Discover() (Plan, error) { if err != nil { return Plan{}, fmt.Errorf("create batch workspace: %w", err) } + // An explicit profile must exist even for unsigned AWS requests. Keep it + // separate from the user's configuration and credential-process settings. + if err := os.WriteFile(filepath.Join(workspace, "aws-config"), []byte("[default]\n"), 0o600); err != nil { + return Plan{}, errors.Join(fmt.Errorf("prepare AWS batch configuration: %w", err), (Plan{workspace: workspace}).Cleanup()) + } + // With file logging disabled, gcloud may open its database before creating + // the configuration directory itself. + if err := os.Mkdir(filepath.Join(workspace, "gcloud-config"), 0o700); err != nil { + return Plan{}, errors.Join(fmt.Errorf("prepare gcloud batch configuration: %w", err), (Plan{workspace: workspace}).Cleanup()) + } + if err := prepareEverydayProbes(workspace); err != nil { + return Plan{}, errors.Join(err, (Plan{workspace: workspace}).Cleanup()) + } return discover(workspace, exec.LookPath), nil } @@ -103,7 +117,7 @@ func firstExecutable(candidates []string, lookup pathLookup) string { } func supportedTools() []toolDefinition { - return []toolDefinition{ + return append([]toolDefinition{ { name: "curl", executables: []string{"curl"}, @@ -251,6 +265,117 @@ func supportedTools() []toolDefinition { } }, }, + { + name: "aws", + executables: []string{"aws"}, + build: func(executable, workspace string) Probe { + arguments := []string{ + "s3api", "head-bucket", "--bucket", "noaa-goes16", + "--region", "us-east-1", "--endpoint-url", "https://s3.us-east-1.amazonaws.com", + "--no-sign-request", + } + return Probe{ + Name: "aws", + Executable: executable, + Arguments: arguments, + Preview: "aws " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "AWS_CONFIG_FILE": filepath.Join(workspace, "aws-config"), + "AWS_SHARED_CREDENTIALS_FILE": os.DevNull, + "AWS_PROFILE": "default", + "AWS_DEFAULT_PROFILE": "default", + "AWS_EC2_METADATA_DISABLED": "true", + "AWS_CLI_AUTO_PROMPT": "off", + "AWS_PAGER": "", + "AWS_MAX_ATTEMPTS": "1", + }, + } + }, + }, + { + name: "gcloud", + executables: []string{"gcloud"}, + build: func(executable, workspace string) Probe { + arguments := []string{"components", "list", "--quiet"} + return Probe{ + Name: "gcloud", + Executable: executable, + Arguments: arguments, + Preview: "gcloud " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "CLOUDSDK_CONFIG": filepath.Join(workspace, "gcloud-config"), + "CLOUDSDK_ACTIVE_CONFIG_NAME": "default", + "CLOUDSDK_AUTH_DISABLE_CREDENTIALS": "true", + "CLOUDSDK_CORE_DISABLE_PROMPTS": "true", + "CLOUDSDK_CORE_DISABLE_USAGE_REPORTING": "true", + "CLOUDSDK_CORE_DISABLE_FILE_LOGGING": "true", + "CLOUDSDK_CORE_CHECK_GCE_METADATA": "false", + "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK": "true", + "CLOUDSDK_COMPONENT_MANAGER_SNAPSHOT_URL": "https://dl.google.com/dl/cloudsdk/channels/rapid/components-2.json", + }, + } + }, + }, + { + name: "cargo", + executables: []string{"cargo"}, + build: func(executable, workspace string) Probe { + arguments := []string{"search", "serde", "--limit", "1", "--registry", "crates-io"} + return Probe{ + Name: "cargo", + Executable: executable, + Arguments: arguments, + Preview: "cargo " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "CARGO_HOME": filepath.Join(workspace, "cargo"), + "CARGO_REGISTRIES_CRATES_IO_PROTOCOL": "sparse", + "CARGO_NET_RETRY": "0", + "RUSTUP_AUTO_INSTALL": "0", + }, + } + }, + }, + { + name: "uv", + executables: []string{"uv"}, + build: func(executable, workspace string) Probe { + arguments := []string{ + "--no-config", "--no-python-downloads", "pip", "compile", + "https://raw.githubusercontent.com/psf/requests/main/requirements-dev.txt", + "--no-build", "--no-deps", "--python-version", "3.12", + "--keyring-provider", "disabled", + } + return Probe{ + Name: "uv", + Executable: executable, + Arguments: arguments, + Preview: "uv " + strings.Join(arguments, " "), + EnvironmentOverrides: map[string]string{ + "UV_CACHE_DIR": filepath.Join(workspace, "uv-cache"), + "UV_WORKING_DIR": workspace, + "UV_HTTP_RETRIES": "0", + "NETRC": os.DevNull, + }, + } + }, + }, + { + name: "kubectl", + executables: []string{"kubectl"}, + build: func(executable, workspace string) Probe { + arguments := []string{ + "--kubeconfig", os.DevNull, "--server", "https://example.com", + "--cache-dir", filepath.Join(workspace, "kubectl-cache"), + "get", "--raw=/version", + } + return Probe{ + Name: "kubectl", + Executable: executable, + Arguments: arguments, + Preview: "kubectl " + strings.Join(arguments, " "), + } + }, + }, { name: "crane", executables: []string{"crane"}, @@ -266,7 +391,7 @@ func supportedTools() []toolDefinition { } }, }, - } + }, everydayTools()...) } func dockerGitArguments(goos string) []string { diff --git a/pkg/batchtest/plan_test.go b/pkg/batchtest/plan_test.go index 067db32..a6db56a 100644 --- a/pkg/batchtest/plan_test.go +++ b/pkg/batchtest/plan_test.go @@ -4,13 +4,19 @@ package batchtest import ( + "bytes" + "context" "errors" + "io" "os" "path/filepath" "runtime" "strings" "testing" + "time" + "github.com/boostsecurityio/emBarrass/pkg/certforge" + "github.com/boostsecurityio/emBarrass/pkg/runner" "github.com/stretchr/testify/require" ) @@ -39,7 +45,10 @@ func TestDiscoverBuildsSafeProbesForInstalledTools(t *testing.T) { []string{"curl", "gh", "go", "pip", "docker", "docker/git", "crane"}, probeNames(plan.Probes), ) - require.Equal(t, []string{"git", "wget", "npm"}, plan.Unavailable) + require.Equal(t, []string{ + "git", "wget", "npm", "aws", "gcloud", "cargo", "uv", "kubectl", + "python", "node", "gem", "dotnet", "pnpm", "httpie", "helm", "composer", + }, plan.Unavailable) require.Equal(t, workspace, plan.WorkingDirectory()) ghProbe := plan.Probes[1] @@ -89,6 +98,125 @@ func TestDiscoverBuildsSafeProbesForInstalledTools(t *testing.T) { ) } +func TestAdditionalProbesIsolateCredentialsAndWrites(t *testing.T) { + t.Parallel() + + workspace := t.TempDir() + installed := []string{"aws", "gcloud", "cargo", "uv", "kubectl"} + plan := discover(workspace, func(name string) (string, error) { + for _, tool := range installed { + if name == tool { + return filepath.Join(workspace, "bin", name), nil + } + } + return "", errors.New("not found") + }) + require.Equal(t, installed, probeNames(plan.Probes)) + for _, probe := range plan.Probes { + require.Equal(t, filepath.Join(workspace, "bin", probe.Name), probe.Command()[0]) + require.Equal(t, probe.Name+" "+strings.Join(probe.Arguments, " "), probe.Preview) + require.False(t, probe.Containerized) + require.False(t, probe.MayPullImage) + } + + aws := plan.Probes[0] + require.Contains(t, aws.Arguments, "--no-sign-request") + require.Contains(t, aws.Arguments, "head-bucket") + require.Contains(t, aws.Arguments, "https://s3.us-east-1.amazonaws.com") + require.Equal(t, filepath.Join(workspace, "aws-config"), aws.EnvironmentOverrides["AWS_CONFIG_FILE"]) + require.Equal(t, os.DevNull, aws.EnvironmentOverrides["AWS_SHARED_CREDENTIALS_FILE"]) + require.Equal(t, "default", aws.EnvironmentOverrides["AWS_PROFILE"]) + require.Equal(t, "default", aws.EnvironmentOverrides["AWS_DEFAULT_PROFILE"]) + require.Equal(t, "true", aws.EnvironmentOverrides["AWS_EC2_METADATA_DISABLED"]) + + gcloud := plan.Probes[1] + require.Equal(t, "true", gcloud.EnvironmentOverrides["CLOUDSDK_AUTH_DISABLE_CREDENTIALS"]) + require.Equal(t, filepath.Join(workspace, "gcloud-config"), gcloud.EnvironmentOverrides["CLOUDSDK_CONFIG"]) + require.Equal(t, "true", gcloud.EnvironmentOverrides["CLOUDSDK_CORE_DISABLE_USAGE_REPORTING"]) + require.Equal(t, "false", gcloud.EnvironmentOverrides["CLOUDSDK_CORE_CHECK_GCE_METADATA"]) + require.Equal(t, []string{"components", "list", "--quiet"}, gcloud.Arguments) + require.Equal(t, "https://dl.google.com/dl/cloudsdk/channels/rapid/components-2.json", gcloud.EnvironmentOverrides["CLOUDSDK_COMPONENT_MANAGER_SNAPSHOT_URL"]) + + cargo := plan.Probes[2] + require.Equal(t, filepath.Join(workspace, "cargo"), cargo.EnvironmentOverrides["CARGO_HOME"]) + require.Equal(t, "sparse", cargo.EnvironmentOverrides["CARGO_REGISTRIES_CRATES_IO_PROTOCOL"]) + require.Equal(t, "0", cargo.EnvironmentOverrides["RUSTUP_AUTO_INSTALL"]) + require.Equal(t, []string{"search", "serde", "--limit", "1", "--registry", "crates-io"}, cargo.Arguments) + + uv := plan.Probes[3] + for _, flag := range []string{"--no-config", "--no-python-downloads", "--no-build", "--no-deps"} { + require.Contains(t, uv.Arguments, flag) + } + require.Equal(t, filepath.Join(workspace, "uv-cache"), uv.EnvironmentOverrides["UV_CACHE_DIR"]) + require.Equal(t, os.DevNull, uv.EnvironmentOverrides["NETRC"]) + + kubectl := plan.Probes[4] + require.Equal(t, []string{ + "--kubeconfig", os.DevNull, "--server", "https://example.com", + "--cache-dir", filepath.Join(workspace, "kubectl-cache"), "get", "--raw=/version", + }, kubectl.Arguments) +} + +func TestDiscoverPreparesCloudConfiguration(t *testing.T) { + t.Parallel() + + plan, err := Discover() + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, plan.Cleanup()) }) + config, err := os.ReadFile(filepath.Join(plan.WorkingDirectory(), "aws-config")) + require.NoError(t, err) + require.Equal(t, "[default]\n", string(config)) + require.DirExists(t, filepath.Join(plan.WorkingDirectory(), "gcloud-config")) +} + +// Opt in to exercise real installed clients through the non-forwarding proxy. +// Ordinary unit tests do not depend on external CLI installations. +func TestInstalledAdditionalProbesReachTLS(t *testing.T) { + if os.Getenv("EMBARASS_TEST_INSTALLED_TOOLS") != "1" { + t.Skip("set EMBARASS_TEST_INSTALLED_TOOLS=1 to test installed clients") + } + // A configured shell must not require these profiles or credential files. + t.Setenv("AWS_PROFILE", "embarass-nonexistent-profile") + t.Setenv("AWS_DEFAULT_PROFILE", "embarass-nonexistent-profile") + t.Setenv("CLOUDSDK_AUTH_ACCESS_TOKEN_FILE", filepath.Join(t.TempDir(), "missing-token")) + t.Setenv("CARGO_REGISTRY_DEFAULT", "embarass-nonexistent-registry") + plan, err := Discover() + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, plan.Cleanup()) }) + for _, name := range []string{ + "aws", "gcloud", "cargo", "uv", "kubectl", + "python", "node", "gem", "dotnet", "pnpm", "httpie", "helm", "composer", + } { + t.Run(name, func(t *testing.T) { + for _, probe := range plan.Probes { + if probe.Name != name { + continue + } + for _, chain := range []certforge.Chain{certforge.ChainEMSign, certforge.ChainSectigo} { + t.Run(string(chain), func(t *testing.T) { + var stderr bytes.Buffer + result, err := runner.Run(context.Background(), runner.Options{ + Command: probe.Command(), Timeout: 20 * time.Second, + Chain: chain, + WorkingDirectory: plan.WorkingDirectory(), + EnvironmentOverrides: probe.EnvironmentOverrides, + Stdout: io.Discard, Stderr: &stderr, + }) + require.NoError(t, err) + require.Contains(t, []runner.Outcome{runner.OutcomeRejected, runner.OutcomeVulnerable}, result.Outcome, "%+v\n%s", result, stderr.String()) + if host, ok := everydayProbeHosts[name]; ok { + require.Equal(t, host, result.Host) + } + t.Logf("%s/%s: %s (%s)", name, chain, result.Outcome, result.Host) + }) + } + return + } + t.Skip("executable not installed") + }) + } +} + func TestDockerGitArgumentsUsePortableHostBridge(t *testing.T) { t.Parallel() diff --git a/pkg/batchtest/probes/node.cjs b/pkg/batchtest/probes/node.cjs new file mode 100644 index 0000000..ef82498 --- /dev/null +++ b/pkg/batchtest/probes/node.cjs @@ -0,0 +1,33 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +// Older Node releases do not honor HTTPS_PROXY automatically. Establish only +// the proxy tunnel explicitly, leaving certificate verification to Node TLS. +const http = require('http'); +const tls = require('tls'); +const target = new URL(process.argv[2]); +const proxy = new URL(process.env.HTTPS_PROXY); +const credentials = decodeURIComponent(proxy.username) + ':' + decodeURIComponent(proxy.password); +const request = http.request({ + hostname: proxy.hostname, + port: proxy.port, + method: 'CONNECT', + path: target.host + ':443', + agent: false, + headers: { 'Proxy-Authorization': 'Basic ' + Buffer.from(credentials).toString('base64') }, +}); + +request.on('error', () => { process.exitCode = 1; }); +request.on('connect', (response, socket) => { + if (response.statusCode !== 200) { + socket.destroy(); + process.exitCode = 1; + return; + } + const client = tls.connect({ socket, servername: target.hostname }, () => { + client.end('HEAD / HTTP/1.1\r\nHost: ' + target.host + '\r\nConnection: close\r\n\r\n'); + }); + client.on('error', () => { process.exitCode = 1; }); + client.resume(); +}); +request.end();