Module: SafeImage::Sandbox

Defined in:
lib/safe_image/sandbox.rb

Constant Summary collapse

DEFAULT_RLIMITS =
{
  cpu_seconds: 30,
  memory_bytes: 2 * 1024 * 1024 * 1024,
  file_size_bytes: 1024 * 1024 * 1024,
  open_files: 256
}.freeze
OPERATIONS =
%w[
  probe thumbnail type size dimensions info orientation optimize resize crop downsize convert convert_to_jpeg fix_orientation
  convert_favicon_to_png frame_count animated? letter_avatar optimize_image!
  sanitize_svg!
].freeze

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/safe_image/sandbox.rb', line 24

def available?
  require "landlock"
  Landlock::SafeExec.supported?
rescue LoadError
  false
end

.capture_command!(argv, read:, write:, timeout: Runner::DEFAULT_TIMEOUT, env: nil, rlimits: DEFAULT_RLIMITS) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/safe_image/sandbox.rb', line 31

def capture_command!(argv, read:, write:, timeout: Runner::DEFAULT_TIMEOUT, env: nil, rlimits: DEFAULT_RLIMITS)
  require "landlock"
  env ||= Runner.command_env(Dir.tmpdir)

  result = Landlock::SafeExec.capture!(
    *argv.map(&:to_s),
    read: existing_paths([*Landlock::SafeExec.default_read_paths, *runtime_read_paths, *read]),
    write: existing_paths(write),
    execute: existing_paths([*Landlock::SafeExec.default_execute_paths, File.dirname(RbConfig.ruby)]),
    env: env.merge("SAFE_IMAGE_SANDBOX_CHILD" => "1"),
    inherit_env: false,
    timeout: timeout,
    rlimits: rlimits,
    seccomp_deny_network: true,
    max_output_bytes: 512 * 1024,
    truncate_output: false
  )
  [result.stdout, result.stderr]
rescue LoadError
  raise Error, "landlock sandbox requested but the landlock gem is unavailable"
rescue Landlock::SafeExec::CommandError => e
  raise CommandError.new(
    "sandboxed command failed",
    command: argv,
    status: e.status&.exitstatus,
    stdout: e.stdout,
    stderr: e.stderr
  )
end

.existing_paths(paths) ⇒ Object



228
229
230
# File 'lib/safe_image/sandbox.rb', line 228

def existing_paths(paths)
  paths.flatten.compact.map(&:to_s).reject(&:empty?).select { |path| File.exist?(path) }.uniq
end

.looks_like_path?(value) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/safe_image/sandbox.rb', line 212

def looks_like_path?(value)
  value.start_with?("/", "./", "../") || File.extname(value) != ""
end

.public_call!(operation, args:, kwargs:) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/safe_image/sandbox.rb', line 61

def public_call!(operation, args:, kwargs:)
  operation = operation.to_s
  raise ArgumentError, "unsupported sandbox operation: #{operation}" unless OPERATIONS.include?(operation)
  result = run_worker!(operation, { args: args, kwargs: kwargs })
  operation == "type" && result ? result.to_sym : result
end

.run_worker!(operation, request) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/safe_image/sandbox.rb', line 76

def run_worker!(operation, request)
  operation = operation.to_s
  raise ArgumentError, "unsupported sandbox operation: #{operation}" unless OPERATIONS.include?(operation)

  require "landlock"
  payload = JSON.dump({ operation: operation, request: request })
  code = <<~'RUBY'
    require "json"
    require "safe_image"

    def deep_symbolize(value)
      case value
      when Hash
        value.each_with_object({}) { |(k, v), h| h[k.to_sym] = deep_symbolize(v) }
      when Array
        value.map { |v| deep_symbolize(v) }
      else
        value
      end
    end

    payload = JSON.parse(ARGV.fetch(0), symbolize_names: true)
    operation = payload.fetch(:operation).to_s
    allowed_operations = %w[
      probe thumbnail type size dimensions info orientation optimize resize crop downsize convert convert_to_jpeg fix_orientation
      convert_favicon_to_png frame_count animated? letter_avatar optimize_image! sanitize_svg!
    ]
    raise ArgumentError, "unsupported sandbox operation: #{operation}" unless allowed_operations.include?(operation)

    request = payload.fetch(:request)
    args = request[:args] || []
    kwargs = deep_symbolize(request[:kwargs] || {})

    result = SafeImage.with_sandbox_disabled do
      SafeImage.__send__(operation, *args, **kwargs)
    end

    if defined?(SafeImage::Result) && result.is_a?(SafeImage::Result)
      puts JSON.dump({ __type: "Result", data: result.to_h })
    elsif defined?(SafeImage::Info) && result.is_a?(SafeImage::Info)
      puts JSON.dump({ __type: "Info", data: result.to_h })
    else
      puts JSON.dump({ __type: "Value", data: result })
    end
  RUBY

  paths = sandbox_paths(request, operation)
  Dir.mktmpdir("safe-image-worker-") do |tmpdir|
    worker_env = Runner.command_env(tmpdir).merge(
      "SAFE_IMAGE_SANDBOX_CHILD" => "1",
      "GEM_HOME" => ENV["GEM_HOME"].to_s,
      "GEM_PATH" => ENV["GEM_PATH"].to_s,
      "RUBYLIB" => $LOAD_PATH.select { |p| p && File.directory?(p) }.join(File::PATH_SEPARATOR)
    )

    stdout, = Landlock::SafeExec.capture!(
      RbConfig.ruby,
      "-I#{File.expand_path("../../", __dir__)}",
      "-rjson",
      "-e",
      code,
      payload,
      read: existing_paths([*Landlock::SafeExec.default_read_paths, *runtime_read_paths, *paths.fetch(:read), tmpdir]),
      write: existing_paths([*paths.fetch(:write), tmpdir]),
      execute: existing_paths([*Landlock::SafeExec.default_execute_paths, File.dirname(RbConfig.ruby)]),
      env: worker_env,
      inherit_env: false,
      timeout: Runner::DEFAULT_TIMEOUT,
      rlimits: DEFAULT_RLIMITS,
      seccomp_deny_network: true,
      max_output_bytes: 512 * 1024,
      truncate_output: false
    )
    response = JSON.parse(stdout, symbolize_names: true)
    if response[:__type] == "Result"
      data = response.fetch(:data)
      Result.new(**data)
    elsif response[:__type] == "Info"
      data = response.fetch(:data)
      Info.new(**data)
    else
      response[:data]
    end
  end
rescue LoadError
  raise Error, "landlock sandbox requested but the landlock gem is unavailable"
rescue Landlock::SafeExec::CommandError => e
  raise CommandError.new(
    "sandboxed worker failed",
    command: [RbConfig.ruby, "-e", "..."],
    status: e.status&.exitstatus,
    stdout: e.stdout,
    stderr: e.stderr
  )
end

.runtime_read_pathsObject



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/safe_image/sandbox.rb', line 216

def runtime_read_paths
  paths = []
  paths.concat(Gem.path) if defined?(Gem)
  paths.concat($LOAD_PATH.select { |path| path && path != "." })
  paths << RbConfig::CONFIG["rubylibdir"]
  paths << RbConfig::CONFIG["rubyarchdir"]
  paths << RbConfig::CONFIG["sitearchdir"]
  paths << RbConfig::CONFIG["vendorarchdir"]
  paths << File.dirname(RbConfig.ruby)
  paths
end

.sandbox_paths(request, operation) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/safe_image/sandbox.rb', line 172

def sandbox_paths(request, operation)
  read = []
  write = []

  values = []
  values.concat(Array(request[:args]))
  values.concat(Array(request.dig(:kwargs)&.values))
  values.flatten.compact.each do |value|
    next unless value.is_a?(String)
    next if value.empty? || value.include?("\0")

    expanded = File.expand_path(value) rescue next
    if File.exist?(expanded)
      read << expanded
    elsif looks_like_path?(value)
      write << File.dirname(expanded)
    end
  end

  # Common keyword names for generated outputs. Include the containing dir
  # even when a stale file already exists, because operations may replace it.
  kwargs = request[:kwargs] || {}
  %i[output to path].each do |key|
    next unless kwargs[key].is_a?(String)
    write << File.dirname(File.expand_path(kwargs[key]))
  end

  # In-place mutators need write permission for an existing input path too.
  if %w[optimize optimize_image! sanitize_svg! fix_orientation].include?(operation.to_s)
    first = Array(request[:args]).first
    if first.is_a?(String) && File.exist?(first)
      expanded = File.expand_path(first)
      write << expanded
      write << File.dirname(expanded)
    end
  end

  { read: read.uniq, write: write.uniq }
end

.symbolize(hash) ⇒ Object



232
233
234
# File 'lib/safe_image/sandbox.rb', line 232

def symbolize(hash)
  hash.transform_keys(&:to_sym)
end

.thumbnail(request) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/safe_image/sandbox.rb', line 68

def thumbnail(request)
  public_call!(
    :thumbnail,
    args: [],
    kwargs: request.merge(execution: :inline)
  )
end