Skip to content

Security: Unvalidated User Input in Shell Command Arguments #177

@davidcforbes

Description

@davidcforbes

Summary

Agent shell command arguments lack validation for shell expansion patterns, allowing potential code execution via glob patterns, variable expansion, and command substitution.

Impact

  • Severity: High
  • Type: Security vulnerability
  • CWE: CWE-78 (OS Command Injection)

Affected Files

  • agent/core/policy.go

Vulnerability Details

The current validation only checks for path traversal but doesn't block shell expansion patterns:

  • Glob patterns: *, ?, [...]
  • Variable expansion: $VAR, ${VAR}, $(cmd)
  • Tilde expansion: ~/path
  • Windows variables: %USERPROFILE%

These can expand unexpectedly or execute commands when passed to shell.

Recommended Fix

Add comprehensive pattern blocking in denyShellArgsOutsideWorkDir():

func denyShellArgsOutsideWorkDir(workdir string, args []string) error {
    for _, arg := range args {
        // Block shell variable expansion (Unix and Windows)
        dangerousPatterns := []string{
            "$",  // Unix: $HOME, $(cmd), ${VAR}
            "`",  // Command substitution
            "%",  // Windows: %USERPROFILE%
        }
        for _, pattern := range dangerousPatterns {
            if strings.Contains(arg, pattern) {
                return PolicyDeniedError{
                    Kind: PolicyKindPathEscape,
                    Reason: fmt.Sprintf("shell arg contains forbidden pattern %q", pattern),
                }
            }
        }
        
        // Block glob patterns
        if strings.ContainsAny(arg, "*?[") {
            return PolicyDeniedError{
                Kind: PolicyKindPathEscape,
                Reason: "shell arg contains glob pattern",
            }
        }
        
        // Existing path validation...
    }
    return nil
}

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions