Class: Nanobot::Agent::Tools::Exec
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- Nanobot::Agent::Tools::Exec
- Defined in:
- lib/nanobot/agent/tools/shell.rb
Overview
Tool for executing shell commands.
IMPORTANT: The DENY_PATTERNS list below is accident prevention, not a security boundary. It catches common destructive typos and mistakes but is trivially bypassed (nested shells, alternative interpreters, creative flag combinations, data exfiltration via curl/wget, etc.). For actual isolation, use OS-level sandboxing (bubblewrap, containers).
Constant Summary collapse
- DENY_PATTERNS =
Common destructive patterns blocked as accident prevention. NOT a security boundary — see class comment above.
[ /rm\s+-rf/i, %r{rmdir\s+/s}i, /format\s/i, /mkfs/i, /shutdown/i, /reboot/i, /halt/i, /poweroff/i, /dd\s+if=/i, /:\(\)\{\s*:\|:&\s*\};:/, # fork bomb %r{>/dev/sd[a-z]}i # writing to disk devices ].freeze
Instance Method Summary collapse
-
#execute(command:) ⇒ String
Execute a shell command and return its combined output.
-
#initialize(working_dir: nil, timeout: 60, restrict_to_workspace: false) ⇒ Exec
constructor
A new instance of Exec.
Constructor Details
#initialize(working_dir: nil, timeout: 60, restrict_to_workspace: false) ⇒ Exec
Returns a new instance of Exec.
40 41 42 43 44 45 |
# File 'lib/nanobot/agent/tools/shell.rb', line 40 def initialize(working_dir: nil, timeout: 60, restrict_to_workspace: false) super() @working_dir = working_dir ? Pathname.new(working_dir)..to_s : Dir.pwd @timeout = timeout @restrict_to_workspace = restrict_to_workspace end |
Instance Method Details
#execute(command:) ⇒ String
Execute a shell command and return its combined output.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/nanobot/agent/tools/shell.rb', line 50 def execute(command:) # Accident prevention: block common destructive patterns return 'Error: Command blocked — matches a known destructive pattern.' if dangerous?(command) # Best-effort workspace restriction. # NOTE: This is NOT a true security boundary. # Shell commands can bypass this check via aliases, symlinks, subshells, etc. For # stronger isolation, use containers or OS-level sandboxing. if @restrict_to_workspace && command_leaves_workspace?(command) return "Error: Command appears to access paths outside the workspace (#{@working_dir}). " \ 'Restricted to workspace directory.' end begin # Execute command with timeout stdout, stderr, status = execute_with_timeout(command) # Format output output = [] output << "Exit code: #{status.exitstatus}" if stdout && !stdout.empty? output << "\nStdout:" output << stdout end if stderr && !stderr.empty? output << "\nStderr:" output << stderr end output.join("\n") rescue Timeout::Error "Error: Command timed out after #{@timeout} seconds" rescue StandardError => e "Error executing command: #{e.}" end end |