Class: Ask::Sandbox::Docker
Overview
Executes commands in a Docker container with hardened security settings.
Uses the docker CLI binary directly (no gem dependencies). Requires a running Docker daemon.
Security measures:
-
Read-only root filesystem (
--read-only) -
All Linux capabilities dropped (+–cap-drop ALL+)
-
No privilege escalation (+–security-opt no-new-privileges+)
-
Network egress disabled by default (+–network none+)
-
PIDs limit (+–pids-limit 100+)
-
Memory and CPU limits
-
Container auto-removal (
--rm)
Instance Method Summary collapse
-
#call(command, timeout: @default_timeout, workdir: nil, env: {}, stdin: nil) ⇒ Ask::Sandbox::Result
Execute a command in the sandbox.
-
#initialize(image: "ruby:3.4-alpine", memory: "512m", cpus: 1.0, network: false, read_only: true, cap_drop: "ALL", user: nil, timeout: 30, remove: true) ⇒ Docker
constructor
A new instance of Docker.
Constructor Details
#initialize(image: "ruby:3.4-alpine", memory: "512m", cpus: 1.0, network: false, read_only: true, cap_drop: "ALL", user: nil, timeout: 30, remove: true) ⇒ Docker
Returns a new instance of Docker.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ask/sandbox/docker.rb', line 33 def initialize( image: "ruby:3.4-alpine", memory: "512m", cpus: 1.0, network: false, read_only: true, cap_drop: "ALL", user: nil, timeout: 30, remove: true ) @image = image @memory = memory @cpus = cpus @network = network @read_only = read_only @cap_drop = cap_drop @user = user @default_timeout = timeout @remove = remove end |
Instance Method Details
#call(command, timeout: @default_timeout, workdir: nil, env: {}, stdin: nil) ⇒ Ask::Sandbox::Result
Execute a command in the sandbox.
56 57 58 59 60 61 62 63 64 |
# File 'lib/ask/sandbox/docker.rb', line 56 def call(command, timeout: @default_timeout, workdir: nil, env: {}, stdin: nil) raise ArgumentError, "command must not be nil" if command.nil? raise ArgumentError, "command must not be empty" if command.respond_to?(:empty?) && command.empty? check_docker_available! docker_argv = build_docker_argv(command, env, workdir) run_docker(docker_argv, timeout, stdin) end |