Module: SafeImage::NativeHelper
- Defined in:
- lib/safe_image/native_helper.rb
Constant Summary collapse
- DEFAULT_HELPER =
File.("safe_image_vips_helper", __dir__).freeze
- HELPER =
ENV.fetch("SAFE_IMAGE_VIPS_HELPER", DEFAULT_HELPER).freeze
- STDERR_TAIL_BYTES =
2000
Class Method Summary collapse
- .append_helper_detail(message, _original_class, stderr_tail) ⇒ Object
- .available? ⇒ Boolean
- .call!(command, sandbox: SafeImage.sandbox?, **options) ⇒ Object
- .convert(input, output, format, quality, max_pixels) ⇒ Object
- .crop_north(input, output, width, height, format, quality, max_pixels) ⇒ Object
- .dominant_color(path, max_pixels) ⇒ Object
- .ensure_available! ⇒ Object
- .exit_status(status) ⇒ Object
- .helper_env(tmpdir) ⇒ Object
- .landlock_abi ⇒ Object
- .letter_avatar(output, size, red, green, blue, markup, font, fontfile) ⇒ Object
- .orientation(path, max_pixels) ⇒ Object
- .pages(path, max_pixels) ⇒ Object
- .png_from_rgba(raw_input, width, height, output) ⇒ Object
- .probe(path, max_pixels) ⇒ Object
- .raise_helper_error(payload, status, command, stderr: nil, stdout: nil) ⇒ Object
- .read_paths(options) ⇒ Object
- .read_response(path, stderr: nil) ⇒ Object
- .resize(input, output, scale, format, quality, max_pixels) ⇒ Object
- .run_process!(argv, tmpdir) ⇒ Object
- .run_sandboxed!(argv, tmpdir, options) ⇒ Object
- .tail(value) ⇒ Object
- .thumbnail(input, output, width, height, format, quality, max_pixels) ⇒ Object
- .verify! ⇒ Object
- .write_paths(options) ⇒ Object
Class Method Details
.append_helper_detail(message, _original_class, stderr_tail) ⇒ Object
261 262 263 264 265 |
# File 'lib/safe_image/native_helper.rb', line 261 def append_helper_detail(, _original_class, stderr_tail) return if stderr_tail.nil? || stderr_tail.empty? "#{} (native vips helper stderr tail: #{stderr_tail})" end |
.available? ⇒ Boolean
15 16 17 18 19 20 |
# File 'lib/safe_image/native_helper.rb', line 15 def available? verify! true rescue Error, CommandError, LoadError false end |
.call!(command, sandbox: SafeImage.sandbox?, **options) ⇒ Object
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 |
# File 'lib/safe_image/native_helper.rb', line 111 def call!(command, sandbox: SafeImage.sandbox?, **) ensure_available! Dir.mktmpdir("safe-image-vips-helper-") do |tmpdir| response = File.join(tmpdir, "response.json") argv = [HELPER, command, "--response", response] .each do |key, value| next if value.nil? argv << "--#{key.to_s.tr("_", "-")}" << value.to_s end stdout = stderr = nil status = nil if sandbox begin stdout, stderr, status = run_sandboxed!(argv, tmpdir, ) rescue Sandbox.landlock_command_error => e stdout = e.stdout stderr = e.stderr status = e.status payload = read_response(response, stderr: stderr) raise_helper_error(payload, status, command, stderr: stderr, stdout: stdout) end else begin stdout, stderr, status = run_process!(argv, tmpdir) rescue CommandError => e stdout = e.stdout stderr = e.stderr status = e.status payload = read_response(response, stderr: stderr) raise_helper_error(payload, status, command, stderr: stderr, stdout: stdout) end end payload = read_response(response, stderr: stderr) helper_failed = status.respond_to?(:success?) ? !status.success? : false raise_helper_error(payload, status, command, stderr: stderr, stdout: stdout) if helper_failed || !payload[:ok] payload.reject { |key, _| key == :ok } end rescue LoadError raise Error, "landlock sandbox requested but the landlock gem is unavailable" end |
.convert(input, output, format, quality, max_pixels) ⇒ Object
77 78 79 |
# File 'lib/safe_image/native_helper.rb', line 77 def convert(input, output, format, quality, max_pixels) call!("convert", input: input, output: output, format: format, quality: quality, max_pixels: max_pixels) end |
.crop_north(input, output, width, height, format, quality, max_pixels) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/safe_image/native_helper.rb', line 64 def crop_north(input, output, width, height, format, quality, max_pixels) call!( "crop-north", input: input, output: output, width: width, height: height, format: format, quality: quality, max_pixels: max_pixels ) end |
.dominant_color(path, max_pixels) ⇒ Object
99 100 101 |
# File 'lib/safe_image/native_helper.rb', line 99 def dominant_color(path, max_pixels) call!("dominant-color", input: path, max_pixels: max_pixels).fetch(:value) end |
.ensure_available! ⇒ Object
22 23 24 25 26 |
# File 'lib/safe_image/native_helper.rb', line 22 def ensure_available! return if File.executable?(HELPER) raise VipsUnavailableError, "compiled safe_image_vips_helper is missing or not executable: #{HELPER}" end |
.exit_status(status) ⇒ Object
257 258 259 |
# File 'lib/safe_image/native_helper.rb', line 257 def exit_status(status) status.respond_to?(:exitstatus) ? status.exitstatus : status end |
.helper_env(tmpdir) ⇒ Object
187 188 189 |
# File 'lib/safe_image/native_helper.rb', line 187 def helper_env(tmpdir) Runner.command_env(tmpdir).merge("SAFE_IMAGE_SANDBOX_CHILD" => "1") end |
.landlock_abi ⇒ Object
191 192 193 |
# File 'lib/safe_image/native_helper.rb', line 191 def landlock_abi @landlock_abi ||= Landlock.abi_version end |
.letter_avatar(output, size, red, green, blue, markup, font, fontfile) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/safe_image/native_helper.rb', line 85 def letter_avatar(output, size, red, green, blue, markup, font, fontfile) call!( "letter-avatar", output: output, size: size, red: red, green: green, blue: blue, markup: markup, font: font, fontfile: fontfile ) end |
.orientation(path, max_pixels) ⇒ Object
107 108 109 |
# File 'lib/safe_image/native_helper.rb', line 107 def orientation(path, max_pixels) call!("orientation", input: path, max_pixels: max_pixels).fetch(:value) end |
.pages(path, max_pixels) ⇒ Object
103 104 105 |
# File 'lib/safe_image/native_helper.rb', line 103 def pages(path, max_pixels) call!("pages", input: path, max_pixels: max_pixels).fetch(:value) end |
.png_from_rgba(raw_input, width, height, output) ⇒ Object
81 82 83 |
# File 'lib/safe_image/native_helper.rb', line 81 def png_from_rgba(raw_input, width, height, output) call!("png-from-rgba", raw_input: raw_input, width: width, height: height, output: output) end |
.probe(path, max_pixels) ⇒ Object
35 36 37 |
# File 'lib/safe_image/native_helper.rb', line 35 def probe(path, max_pixels) call!("probe", input: path, max_pixels: max_pixels) end |
.raise_helper_error(payload, status, command, stderr: nil, stdout: nil) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/safe_image/native_helper.rb', line 220 def raise_helper_error(payload, status, command, stderr: nil, stdout: nil) = ( if payload[:message].to_s.empty? "native vips helper failed with status #{exit_status(status).inspect}" else payload[:message] end ) original_class = payload[:error].to_s stderr_tail = payload[:stderr_tail] || tail(stderr) case original_class when "LimitError" raise LimitError.new(, original_error_class: original_class, stderr_tail: stderr_tail) when "UnsupportedFormatError" raise UnsupportedFormatError.new(, original_error_class: original_class, stderr_tail: stderr_tail) when "VipsUnavailableError" raise VipsUnavailableError.new(, original_error_class: original_class, stderr_tail: stderr_tail) when "ArgumentError" raise ArgumentError, append_helper_detail(, original_class, stderr_tail) when "InvalidImageError", "" raise InvalidImageError.new(, original_error_class: original_class, stderr_tail: stderr_tail) else raise CommandError.new( "native vips helper downgraded unknown error class #{original_class.inspect}: #{}", command: [HELPER, command], status: exit_status(status), stdout: stdout.to_s, stderr: stderr.to_s, category: :native_helper, operation: command, original_error_class: original_class, stderr_tail: stderr_tail ) end end |
.read_paths(options) ⇒ Object
267 268 269 |
# File 'lib/safe_image/native_helper.rb', line 267 def read_paths() [[:input], [:raw_input], [:fontfile]].compact.reject(&:empty?) end |
.read_response(path, stderr: nil) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/safe_image/native_helper.rb', line 202 def read_response(path, stderr: nil) JSON.parse(File.read(path), symbolize_names: true) rescue Errno::ENOENT { ok: false, error: "CommandError", message: "native vips helper did not write a response", stderr_tail: tail(stderr) } rescue JSON::ParserError => e { ok: false, error: "CommandError", message: "native vips helper wrote invalid JSON: #{e.}", stderr_tail: tail(stderr) } end |
.resize(input, output, scale, format, quality, max_pixels) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/safe_image/native_helper.rb', line 52 def resize(input, output, scale, format, quality, max_pixels) call!( "resize", input: input, output: output, scale: scale, format: format, quality: quality, max_pixels: max_pixels ) end |
.run_process!(argv, tmpdir) ⇒ Object
156 157 158 159 |
# File 'lib/safe_image/native_helper.rb', line 156 def run_process!(argv, tmpdir) stdout, stderr = Runner.run_process!(argv, helper_env(tmpdir), timeout: Runner::DEFAULT_TIMEOUT) [stdout, stderr, nil] end |
.run_sandboxed!(argv, tmpdir, options) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/safe_image/native_helper.rb', line 161 def run_sandboxed!(argv, tmpdir, ) require "landlock" result = Sandbox.landlock_capture!( argv, read: Sandbox.existing_paths([*Sandbox.default_read_paths, *Sandbox.runtime_read_paths, *read_paths()]), write: Sandbox.existing_paths([tmpdir, *write_paths()]), execute: Sandbox.existing_paths([File.dirname(HELPER), *Sandbox.default_execute_paths]), env: helper_env(tmpdir), unsetenv_others: true, bind_tcp: landlock_abi >= 4 ? [1] : [], scope: landlock_abi >= 6 ? %i[abstract_unix_socket signal] : [], timeout: Runner::DEFAULT_TIMEOUT, rlimits: Sandbox::DEFAULT_RLIMITS, seccomp_deny_network: true, max_output_bytes: Runner::MAX_OUTPUT_BYTES, truncate_output: false ) stdout = result.stdout if result.respond_to?(:stdout) stderr = result.stderr if result.respond_to?(:stderr) status = result.status if result.respond_to?(:status) [stdout, stderr, status] end |
.tail(value) ⇒ Object
195 196 197 198 199 200 |
# File 'lib/safe_image/native_helper.rb', line 195 def tail(value) text = value.to_s return nil if text.empty? text.bytesize > STDERR_TAIL_BYTES ? text.byteslice(-STDERR_TAIL_BYTES, STDERR_TAIL_BYTES) : text end |
.thumbnail(input, output, width, height, format, quality, max_pixels) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/safe_image/native_helper.rb', line 39 def thumbnail(input, output, width, height, format, quality, max_pixels) call!( "thumbnail", input: input, output: output, width: width, height: height, format: format, quality: quality, max_pixels: max_pixels ) end |
.verify! ⇒ Object
28 29 30 31 32 33 |
# File 'lib/safe_image/native_helper.rb', line 28 def verify! return true if @verified call!("version", sandbox: false) @verified = true end |
.write_paths(options) ⇒ Object
271 272 273 274 275 276 277 278 279 |
# File 'lib/safe_image/native_helper.rb', line 271 def write_paths() output = [:output] return [] unless output = File.(output) paths = [File.dirname()] paths << if File.exist?() paths end |