executil

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HardMaxTimeout             = 10 * time.Minute
	HardMaxOutputBytes   int64 = 4 * 1024 * 1024 // per stream
	HardMaxCommands            = 64
	HardMaxCommandLength       = 64 * 1024 // bytes
	MinOutputBytes       int64 = 1024

	DefaultTimeout                = 60 * time.Second
	DefaultMaxOutputBytes   int64 = 256 * 1024
	DefaultMaxCommands            = 64
	DefaultMaxCommandLength       = 64 * 1024
)

Fixed, package-wide hard limits (single source of truth).

Variables

View Source
var HardBlockedCommands = func() map[string]struct{} {

	hard := []string{

		"sudo", "su",
		"rm",
		"mkfs",
		"shutdown", "reboot", "halt", "poweroff",

		"vim", "vi", "nano", "emacs", "less", "more", "top", "htop",

		"curl", "wget",
		"nc", "netcat", "ncat", "socat",
		"ssh", "scp", "sftp",
		"ftp", "tftp", "telnet",

		"invoke-webrequest", "iwr",
		"invoke-restmethod", "irm",

		"diskpart",
		"format.com",
		"del", "erase", "rmdir", "rd",
		"remove-item", "ri",
	}

	m := make(map[string]struct{}, len(hard))
	for _, c := range hard {
		m[c] = struct{}{}
	}
	return m
}()

Functions

func CommandFromArgv

func CommandFromArgv(selectedShell ShellName, argv []string) (string, error)

CommandFromArgv builds a safely-quoted command string for ShellTool (which executes via shell "-c"/"-Command").

Why this exists: ShellTool accepts command strings; without a canonical helper, wrappers duplicate quoting and can accidentally introduce injection bugs. This keeps quoting logic centralized and consistent.

Supported dialects:

  • sh-like shells (bash/zsh/sh/dash/ksh/fish): POSIX single-quote strategy
  • PowerShell (pwsh/powershell): single-quote strategy + "&" call operator prefix

cmd.exe quoting is intentionally not supported here (too error-prone); pass a raw command string instead.

func EffectiveEnv

func EffectiveEnv(overrides map[string]string) ([]string, error)

EffectiveEnv returns the current process environment merged with overrides. It is equivalent to session-less ShellSession.GetEffectiveEnv.

func EffectiveEnvWithBase added in v0.14.0

func EffectiveEnvWithBase(baseEnv, overrides map[string]string) ([]string, error)

EffectiveEnvWithBase returns the current process environment merged with base env and overrides.

func NormalizeBlockedCommand

func NormalizeBlockedCommand(s string) (string, error)

func RejectDangerousCommand

func RejectDangerousCommand(
	cmd, shellPath string, shellName ShellName,
	blockedCommands map[string]struct{},
	enableHeuristicChecks bool,
) error

func ValidateEnvMap

func ValidateEnvMap(m map[string]string) error

Types

type SelectedShell

type SelectedShell struct {
	Name ShellName
	Path string
}

type SessionStore

type SessionStore struct {
	// contains filtered or unexported fields
}

func NewSessionStore

func NewSessionStore() *SessionStore

func (*SessionStore) Delete

func (ss *SessionStore) Delete(id string)

func (*SessionStore) Get

func (ss *SessionStore) Get(id string) (*ShellSession, bool)

func (*SessionStore) NewSession

func (ss *SessionStore) NewSession() *ShellSession

func (*SessionStore) SetMaxSessions

func (ss *SessionStore) SetMaxSessions(maxSessions int)

func (*SessionStore) SetTTL

func (ss *SessionStore) SetTTL(ttl time.Duration)

func (*SessionStore) Size

func (ss *SessionStore) Size() int

type ShellCommandExecResult

type ShellCommandExecResult struct {
	Command   string    `json:"command"`
	WorkDir   string    `json:"workDir"`
	Shell     ShellName `json:"shell"`
	ShellPath string    `json:"shellPath"`

	ExitCode   int   `json:"exitCode"`
	TimedOut   bool  `json:"timedOut"`
	DurationMS int64 `json:"durationMS"`

	Stdout string `json:"stdout"`
	Stderr string `json:"stderr"`

	StdoutTruncated bool `json:"stdoutTruncated"`
	StderrTruncated bool `json:"stderrTruncated"`
}

func RunOneShellCommand

func RunOneShellCommand(
	parent context.Context,
	sel SelectedShell,
	command string,
	workdir string,
	env []string,
	timeout time.Duration,
	maxOut int64,
) (ShellCommandExecResult, error)

type ShellName

type ShellName string
const (
	ShellNameAuto       ShellName = "auto"
	ShellNameBash       ShellName = "bash"
	ShellNameZsh        ShellName = "zsh"
	ShellNameSh         ShellName = "sh"
	ShellNameDash       ShellName = "dash"
	ShellNameKsh        ShellName = "ksh"
	ShellNameFish       ShellName = "fish"
	ShellNamePwsh       ShellName = "pwsh"
	ShellNamePowershell ShellName = "powershell"
	ShellNameCmd        ShellName = "cmd"
)

type ShellSession

type ShellSession struct {
	// contains filtered or unexported fields
}

func (*ShellSession) AddToEnv

func (sess *ShellSession) AddToEnv(additionalEnv map[string]string) error

func (*ShellSession) GetEffectiveEnv

func (sess *ShellSession) GetEffectiveEnv(overrides map[string]string) ([]string, error)

func (*ShellSession) GetEffectiveEnvWithBase added in v0.14.0

func (sess *ShellSession) GetEffectiveEnvWithBase(baseEnv, overrides map[string]string) ([]string, error)

func (*ShellSession) GetEffectiveWorkdir

func (sess *ShellSession) GetEffectiveWorkdir(inputWorkDir, defaultWorkDir string) (string, error)

func (*ShellSession) GetID

func (sess *ShellSession) GetID() string

func (*ShellSession) SetWorkDir

func (sess *ShellSession) SetWorkDir(workdir string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL