{"modulePath":"github.com/step-security/dev-machine-guard","version":"v1.19.1","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package executor\n\n## Functions\n\n```go\nfunc HardenCommand(cmd *exec.Cmd)\n```\nHardenCommand applies the process-level safeguards every command this agent spawns needs: no console window on Windows, and a cancellation that reaches the whole process group rather than only the immediate child.\n\nExported for callers that build their own \\*exec.Cmd because they need stream or environment handling the methods above do not express. Those callers still need the teardown, and this is where it is defined rather than re-derived, correctly or otherwise, beside each such command. Run and RunInDir apply the same pair inline.\n\n```go\nfunc LookPathWithContext(ctx context.Context, exec Executor, name string) (string, error)\n```\nLookPathWithContext resolves name without outliving the caller's deadline.\n\n```go\nfunc MockDirEntry(name string, isDir bool) os.DirEntry\n```\nMockDirEntry creates an os.DirEntry for use with SetDirEntries.\n\n```go\nfunc MockIrregularDirEntry(name string) os.DirEntry\n```\nMockIrregularDirEntry creates an os.DirEntry whose Type() reports os.ModeIrregular (IsDir() and symlink both false) — what os.ReadDir reports for a Windows directory junction. Pair it with SetReadlink for the target.\n\n```go\nfunc MockSymlinkDirEntry(name string) os.DirEntry\n```\nMockSymlinkDirEntry creates an os.DirEntry whose Type() reports os.ModeSymlink (IsDir() is false), for exercising symlinked directory entries. Pair it with SetSymlink to stub the resolution target.\n\n```go\nfunc ResolveHome(exec Executor) string\n```\nResolveHome returns the home directory of the console (GUI) user when present, falling back to the process's current user. Returns an empty string when neither resolves — callers degrade gracefully.\n\nOn macOS the LoggedInUser path uses \\`stat /dev/console\\` to find the real GUI user even when the agent runs as root via launchd (issue #63), so this is the correct anchor for per-user paths like the TCC skip list. Shared by both community-mode scan and enterprise telemetry to keep them in lock-step.\n\n```go\nfunc UserEnvironmentError(exec Executor) error\n```\nUserEnvironmentError reports a failed target-user environment snapshot.\n\n## Types\n\n```go\ntype Executor interface {\n\t// GuardedFiles restricts ReadFile, ReadDir, Stat and EvalSymlinks to roots\n\t// and guard. Other operations retain their original behavior.\n\tGuardedFiles(roots []string, guard func(string) string, maxReadBytes int64) Executor\n\t// Run executes a command and returns stdout, stderr, and exit code.\n\tRun(ctx context.Context, name string, args ...string) (stdout, stderr string, exitCode int, err error)\n\t// RunWithTimeout executes a command with a timeout.\n\tRunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (stdout, stderr string, exitCode int, err error)\n\t// RunInDir executes a command with a working directory and timeout.\n\t// Avoids shell quoting issues with cd on Windows.\n\tRunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (stdout, stderr string, exitCode int, err error)\n\t// RunAsUser runs a shell command as a specific user (for root -\u003e user delegation).\n\tRunAsUser(ctx context.Context, username, command string) (string, error)\n\t// StartDetached launches a command and returns as soon as it has started,\n\t// without waiting for it to finish and without tying its lifetime to this\n\t// process. Used to trigger a scan inside a WSL distribution: the scan\n\t// outlives the run that started it, and its own agent reports the result.\n\t// The error covers failure to *start* only — anything the child does\n\t// afterwards is invisible here by design.\n\tStartDetached(name string, args ...string) error\n\t// LookPath searches for an executable in PATH.\n\tLookPath(name string) (string, error)\n\t// FileExists checks if a file exists and is not a directory.\n\tFileExists(path string) bool\n\t// DirExists checks if a directory exists.\n\tDirExists(path string) bool\n\t// ReadFile reads a file's contents.\n\tReadFile(path string) ([]byte, error)\n\t// ReadDir lists directory entries.\n\tReadDir(path string) ([]os.DirEntry, error)\n\t// ReadDirLimit lists at most max entries, reading no more than max+1 from\n\t// the directory; more reports that entries were left unread. Entries are\n\t// sorted by name.\n\tReadDirLimit(path string, max int) (entries []os.DirEntry, more bool, err error)\n\t// Stat returns file info.\n\tStat(path string) (os.FileInfo, error)\n\t// Hostname returns the system hostname.\n\tHostname() (string, error)\n\t// Getenv reads an environment variable.\n\tGetenv(key string) string\n\t// HasEnvPrefix checks process environment names without exposing their values.\n\tHasEnvPrefix(prefix string) bool\n\t// EnvNames lists process environment names with prefix, never their values.\n\tEnvNames(prefix string) []string\n\t// IsRoot returns true if the process is running as root.\n\tIsRoot() bool\n\t// CurrentUser returns the current OS user.\n\tCurrentUser() (*user.User, error)\n\t// HomeDir returns the home directory for a given username.\n\tHomeDir(username string) (string, error)\n\t// Glob returns filenames matching a pattern.\n\tGlob(pattern string) ([]string, error)\n\t// EvalSymlinks resolves symbolic links in a path. Returns the resolved\n\t// canonical path. If the path is not a symlink, returns it unchanged.\n\tEvalSymlinks(path string) (string, error)\n\t// Readlink returns the stored target of the symlink (or Windows directory\n\t// junction) at path without following it — possibly relative, possibly an\n\t// NT-namespace spelling (\\??\\C:\\...). A non-link path is an error.\n\tReadlink(path string) (string, error)\n\t// LoggedInUser returns the actual logged-in console user.\n\t// When running as root on macOS (e.g., via LaunchDaemon), this detects the\n\t// real console user via /dev/console rather than returning root.\n\t// Falls back to CurrentUser() when not root or on non-macOS platforms.\n\tLoggedInUser() (*user.User, error)\n\t// LoggedInUserContext is the deadline-aware form for budgeted probes.\n\tLoggedInUserContext(context.Context) (*user.User, error)\n\t// GOOS returns the runtime operating system.\n\tGOOS() string\n\t// IsAppleCLTStub reports whether binPath is an Apple Command Line Tools\n\t// shim (under /usr/bin/) that would trigger the macOS \"install developer\n\t// tools\" GUI prompt when invoked. Returns false on non-darwin systems, for\n\t// paths outside /usr/bin/, or when CLT is installed. Detectors must guard\n\t// every Run/RunWithTimeout on /usr/bin/-resolved binaries with this check\n\t// to avoid disrupting end users on machines without CLT installed.\n\tIsAppleCLTStub(ctx context.Context, binPath string) bool\n\t// DiskCapacityBytes returns the total bytes on the filesystem containing\n\t// path. Returns 0 on any error (lookup failures shouldn't block a scan).\n\tDiskCapacityBytes(path string) uint64\n}\n```\nExecutor defines the interface for all OS interactions. Every detector depends on this interface, enabling full unit-test coverage via mocks.\n\n```go\nfunc NewUserAwareExecutor(inner Executor, username string) Executor\n```\nNewUserAwareExecutor returns a wrapped executor that runs commands through the given user's login shell (rc files sourced for a full PATH) on Unix, in both deployment modes:\n\n  - root (LaunchDaemon / MDM \"Run Script\"): RunAsUser sudo's to the user.\n  - non-root (LaunchAgent's periodic fire): RunAsUser runs as the current user.\n\nlaunchd hands both a stripped PATH, so package managers (brew/pip3/npm via nvm/fnm/homebrew/npm-global) need the user's rc-sourced shell to be resolved. Passes through to the inner executor unchanged when username is empty or on Windows.\n\n```go\ntype Mock struct {\n\n\t// DetachedCalls records StartDetached invocations, newest last.\n\t// StartDetachedErr makes every spawn fail.\n\tDetachedCalls    []string\n\tStartDetachedErr error\n\t// contains filtered or unexported fields\n}\n```\nMock implements Executor for unit testing.\n\n```go\nfunc NewMock() *Mock\n```\n\n```go\nfunc (m *Mock) CreateFileOnce(path string, data []byte, _ os.FileMode) error\n```\nCreateFileOnce stores data unless path already exists.\n\n```go\nfunc (m *Mock) CurrentUser() (*user.User, error)\n```\n\n```go\nfunc (m *Mock) DirExists(path string) bool\n```\n\n```go\nfunc (m *Mock) DiskCapacityBytes(path string) uint64\n```\n\n```go\nfunc (m *Mock) EnvNames(prefix string) []string\n```\n\n```go\nfunc (m *Mock) EvalSymlinks(path string) (string, error)\n```\n\n```go\nfunc (m *Mock) FileExists(path string) bool\n```\n\n```go\nfunc (m *Mock) GOOS() string\n```\n\n```go\nfunc (m *Mock) Getenv(key string) string\n```\n\n```go\nfunc (m *Mock) Glob(pattern string) ([]string, error)\n```\n\n```go\nfunc (m *Mock) GuardedFiles(_ []string, _ func(string) string, _ int64) Executor\n```\nGuardedFiles retains the in-memory filesystem. Native guarded I/O is covered by real-filesystem tests; mocks never access the host filesystem.\n\n```go\nfunc (m *Mock) HasEnvPrefix(prefix string) bool\n```\n\n```go\nfunc (m *Mock) HomeDir(_ string) (string, error)\n```\n\n```go\nfunc (m *Mock) Hostname() (string, error)\n```\n\n```go\nfunc (m *Mock) IsAppleCLTStub(_ context.Context, binPath string) bool\n```\n\n```go\nfunc (m *Mock) IsRoot() bool\n```\n\n```go\nfunc (m *Mock) LoggedInUser() (*user.User, error)\n```\n\n```go\nfunc (m *Mock) LoggedInUserContext(ctx context.Context) (*user.User, error)\n```\n\n```go\nfunc (m *Mock) LookPath(name string) (string, error)\n```\n\n```go\nfunc (m *Mock) ReadDir(path string) ([]os.DirEntry, error)\n```\n\n```go\nfunc (m *Mock) ReadDirLimit(path string, max int) ([]os.DirEntry, bool, error)\n```\n\n```go\nfunc (m *Mock) ReadFile(path string) ([]byte, error)\n```\n\n```go\nfunc (m *Mock) Readlink(path string) (string, error)\n```\n\n```go\nfunc (m *Mock) Run(_ context.Context, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (m *Mock) RunAsUser(ctx context.Context, _ string, command string) (string, error)\n```\n\n```go\nfunc (m *Mock) RunInDir(ctx context.Context, _ string, _ time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (m *Mock) RunWithTimeout(ctx context.Context, _ time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (m *Mock) SetAppleCLTInstalled(installed bool)\n```\nSetAppleCLTInstalled controls the value returned by IsAppleCLTStub. Default is false (CLT not installed → binaries in the appleCLTStubBinaries allowlist are reported as stubs; other /usr/bin/ paths are unaffected).\n\n```go\nfunc (m *Mock) SetCommand(stdout, stderr string, exitCode int, name string, args ...string)\n```\n\n```go\nfunc (m *Mock) SetCommandError(err error, name string, args ...string)\n```\n\n```go\nfunc (m *Mock) SetDir(path string)\n```\n\n```go\nfunc (m *Mock) SetDirEntries(path string, entries []os.DirEntry)\n```\n\n```go\nfunc (m *Mock) SetDiskCapacityBytes(path string, bytes uint64)\n```\nSetDiskCapacityBytes stubs the result of DiskCapacityBytes(path).\n\n```go\nfunc (m *Mock) SetEnv(key, value string)\n```\n\n```go\nfunc (m *Mock) SetExecutable(path string)\n```\nSetExecutable registers \\`path\\` as an existing executable file (mode 0755) so Stat-based lookups (UserAwareExecutor.LookPath's native PATH walk) can resolve it.\n\n```go\nfunc (m *Mock) SetFile(path string, content []byte)\n```\n\n```go\nfunc (m *Mock) SetFileInfo(path string, info os.FileInfo)\n```\n\n```go\nfunc (m *Mock) SetFileMtime(path string, unixSec int64)\n```\nSetFileMtime registers a stat result for \\`path\\` with a custom mtime (Unix seconds) AND marks the file as existing (FileExists returns true). Useful for cache-invalidation tests that need to assert behavior across mtime windows.\n\n```go\nfunc (m *Mock) SetGOOS(goos string)\n```\n\n```go\nfunc (m *Mock) SetGlob(pattern string, matches []string)\n```\n\n```go\nfunc (m *Mock) SetHomeDir(h string)\n```\n\n```go\nfunc (m *Mock) SetHostname(h string)\n```\n\n```go\nfunc (m *Mock) SetIsRoot(v bool)\n```\n\n```go\nfunc (m *Mock) SetLoggedInUserError(err error)\n```\nSetLoggedInUserError makes LoggedInUser return (nil, err). Pass nil to reset to default behavior (delegating to CurrentUser).\n\n```go\nfunc (m *Mock) SetPath(name, path string)\n```\n\n```go\nfunc (m *Mock) SetReadlink(path, target string)\n```\nSetReadlink stubs the stored target Readlink(path) returns (relative, or a junction's \\\\??\\\\ spelling). Without it Readlink falls back to the SetSymlink target, so a plain symlink fixture needs only one call.\n\n```go\nfunc (m *Mock) SetSymlink(path, target string)\n```\nSetSymlink stubs a symlink resolution: EvalSymlinks(path) -\u003e target. If a path is not registered, EvalSymlinks returns the path unchanged (matching the behavior of filepath.EvalSymlinks on a non-symlink).\n\n```go\nfunc (m *Mock) SetSymlinkError(path string, err error)\n```\nSetSymlinkError makes EvalSymlinks(path) return (\", err), simulating a dangling or unresolvable symlink. Takes precedence over any SetSymlink stub.\n\n```go\nfunc (m *Mock) SetUsername(u string)\n```\n\n```go\nfunc (m *Mock) StartDetached(name string, args ...string) error\n```\nDetachedCalls records every StartDetached invocation as \"name arg arg ...\", so a test can assert what the WSL-scan phase launched without running it. StartDetachedErr, when set, fails every spawn.\n\n```go\nfunc (m *Mock) Stat(path string) (os.FileInfo, error)\n```\n\n```go\nfunc (m *Mock) WriteFileAtomic(path string, data []byte, _ os.FileMode) error\n```\nWriteFileAtomic stores data so ReadFile returns it.\n\n```go\ntype Real struct {\n\t// contains filtered or unexported fields\n}\n```\nReal implements Executor using actual OS calls.\n\n```go\nfunc NewReal() *Real\n```\n\n```go\nfunc (r *Real) CreateFileOnce(path string, data []byte, perm os.FileMode) error\n```\nCreateFileOnce publishes a complete file at path only if none exists (hard link, so concurrent creators can't clobber each other); otherwise it returns an error matching os.ErrExist. Not part of Executor; callers type-assert.\n\n```go\nfunc (r *Real) CurrentUser() (*user.User, error)\n```\n\n```go\nfunc (r *Real) DirExists(path string) bool\n```\n\n```go\nfunc (r *Real) DiskCapacityBytes(path string) uint64\n```\n\n```go\nfunc (r *Real) EnvNames(prefix string) []string\n```\n\n```go\nfunc (r *Real) EvalSymlinks(path string) (string, error)\n```\n\n```go\nfunc (r *Real) FileExists(path string) bool\n```\n\n```go\nfunc (r *Real) GOOS() string\n```\n\n```go\nfunc (r *Real) Getenv(key string) string\n```\n\n```go\nfunc (r *Real) Glob(pattern string) ([]string, error)\n```\n\n```go\nfunc (r *Real) GuardedFiles(roots []string, guard func(string) string, maxReadBytes int64) Executor\n```\n\n```go\nfunc (r *Real) HasEnvPrefix(prefix string) bool\n```\n\n```go\nfunc (r *Real) HomeDir(username string) (string, error)\n```\n\n```go\nfunc (r *Real) Hostname() (string, error)\n```\n\n```go\nfunc (r *Real) IsAppleCLTStub(_ context.Context, binPath string) bool\n```\nIsAppleCLTStub reports whether binPath is an Apple Command Line Tools shim that would trigger the macOS \"install developer tools\" GUI prompt when invoked. Returns true iff:\n\n 1. GOOS is darwin,\n 2. binPath is a known Apple CLT shim (see appleCLTStubBinaries), AND\n 3. Xcode Command Line Tools are not installed (\\`xcode-select -p\\` fails).\n\nThe CLT-presence result is cached per Real instance via sync.Once. The probe deliberately uses context.Background() (with its own 5 s timeout) rather than the caller-provided ctx: sync.Once consumes the slot on the first call, so a caller arriving with a canceled or near-deadline ctx could otherwise poison the cache with cltPresent=false and make every subsequent check treat real binaries as stubs. The caller's ctx is retained in the signature for symmetry with the Executor interface.\n\n\\`xcode-select -p\\` itself does NOT trigger the install prompt — it just prints the developer-dir path or exits non-zero when CLT is absent.\n\n```go\nfunc (r *Real) IsRoot() bool\n```\n\n```go\nfunc (r *Real) LoggedInUser() (*user.User, error)\n```\n\n```go\nfunc (r *Real) LoggedInUserContext(ctx context.Context) (*user.User, error)\n```\nLoggedInUserContext bounds both the console probe and directory lookup by the caller's deadline. The default cap also applies when no deadline was supplied.\n\n```go\nfunc (r *Real) LookPath(name string) (string, error)\n```\n\n```go\nfunc (r *Real) ReadDir(path string) ([]os.DirEntry, error)\n```\n\n```go\nfunc (r *Real) ReadDirLimit(path string, max int) ([]os.DirEntry, bool, error)\n```\n\n```go\nfunc (r *Real) ReadFile(path string) ([]byte, error)\n```\n\n```go\nfunc (r *Real) Readlink(path string) (string, error)\n```\n\n```go\nfunc (r *Real) Run(ctx context.Context, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (r *Real) RunAsUser(ctx context.Context, username, command string) (string, error)\n```\n\n```go\nfunc (r *Real) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (r *Real) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (r *Real) StartDetached(name string, args ...string) error\n```\nStartDetached starts the process and lets it go. It deliberately does not Wait: the child must outlive this process.\n\ndetachAttrs supplies platform creation flags that keep the child from being reaped with this process, most isolated first, falling back because job breakaway fails outright when the job forbids it. See detach\\_windows.go: a plain spawn was not observed to fail, so those flags are insurance against a silent failure mode rather than a fix for a measured one.\n\nRelease drops our handle afterwards so nothing here holds the child.\n\n```go\nfunc (r *Real) Stat(path string) (os.FileInfo, error)\n```\n\n```go\nfunc (r *Real) WriteFileAtomic(path string, data []byte, perm os.FileMode) error\n```\nWriteFileAtomic replaces path via a synced temp file and rename, so readers never see a partial file. A missing parent is created world-traversable. Not part of Executor; callers type-assert for it.\n\n```go\ntype UserAwareExecutor struct {\n\t// contains filtered or unexported fields\n}\n```\nUserAwareExecutor wraps an Executor and delegates LookPath/Run/RunWithTimeout/ RunInDir to the logged-in user's login shell (with rc files sourced for a full PATH). This ensures commands like \"brew list\", \"pip3 list\", \"npm --version\" execute in the user's context with their real PATH — whether the agent runs as root (sudo to the user) or as the user itself under a LaunchAgent. launchd strips PATH in both cases, so package managers installed via nvm/fnm/homebrew aren't found by a bare exec.\n\nAll other Executor methods are forwarded unchanged.\n\n```go\nfunc (e *UserAwareExecutor) CurrentUser() (*user.User, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) DirExists(path string) bool\n```\n\n```go\nfunc (e *UserAwareExecutor) DiskCapacityBytes(path string) uint64\n```\n\n```go\nfunc (e *UserAwareExecutor) EnvNames(prefix string) []string\n```\nEnvNames observes only this process; it never sources the user's shell.\n\n```go\nfunc (e *UserAwareExecutor) EvalSymlinks(path string) (string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) FileExists(path string) bool\n```\n\n```go\nfunc (e *UserAwareExecutor) GOOS() string\n```\n\n```go\nfunc (e *UserAwareExecutor) Getenv(key string) string\n```\n\n```go\nfunc (e *UserAwareExecutor) Glob(pattern string) ([]string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) GuardedFiles(roots []string, guard func(string) string, maxReadBytes int64) Executor\n```\n\n```go\nfunc (e *UserAwareExecutor) HasEnvPrefix(prefix string) bool\n```\nHasEnvPrefix observes only this process; it never sources the user's shell.\n\n```go\nfunc (e *UserAwareExecutor) HomeDir(username string) (string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) Hostname() (string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) IsAppleCLTStub(ctx context.Context, binPath string) bool\n```\n\n```go\nfunc (e *UserAwareExecutor) IsRoot() bool\n```\n\n```go\nfunc (e *UserAwareExecutor) LoggedInUser() (*user.User, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) LoggedInUserContext(ctx context.Context) (*user.User, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) LookPath(name string) (string, error)\n```\nLookPath resolves name against the user's login-shell $PATH natively (stat + executable bit) instead of a \\`which\\` login shell per tool. Each of those re-sourced the user's rc files, where zsh compinit can block on its interactive \"insecure directories\" prompt. Aliases and shell functions are deliberately not matched: the agent needs a real executable. Falls back to \\`which\\` when the PATH fetch failed.\n\n```go\nfunc (e *UserAwareExecutor) ReadDir(path string) ([]os.DirEntry, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) ReadDirLimit(path string, max int) ([]os.DirEntry, bool, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) ReadFile(path string) ([]byte, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) Readlink(path string) (string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) Run(ctx context.Context, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) RunAsUser(ctx context.Context, username, command string) (string, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)\n```\n\n```go\nfunc (e *UserAwareExecutor) StartDetached(name string, args ...string) error\n```\nStartDetached delegates: a detached spawn carries no user-impersonation concern of its own, and the WSL relay it launches already runs as the distro's default user.\n\n```go\nfunc (e *UserAwareExecutor) Stat(path string) (os.FileInfo, error)\n```\n\n","path":"github.com/step-security/dev-machine-guard/internal/executor","name":"executor","synopsis":"","isRedistributable":true}
