Skip to content

A platform for community discussion. Free, open, simple.

18.5 KB
Raw Blame
#!/usr/bin/env ruby
# frozen_string_literal: true

require "landlock"
require "msgpack"
require "socket"
require "tmpdir"
require "vips"

raise LoadError, "libvips 8.18 or newer is required" if !Vips.at_least_libvips?(8, 18)

class DiscourseVipsWorker
  class InvalidImage < StandardError
  end

  LINUX_PLATFORM = RUBY_PLATFORM.include?("linux")
  private_constant :LINUX_PLATFORM

  class Operations
    DEFAULT_READ_PATHS = %w[/bin /lib /lib64 /usr].freeze
    private_constant :DEFAULT_READ_PATHS

    # Dynamically loaded libvips modules use this cache to locate their shared libraries.
    DYNAMIC_LINKER_CACHE_PATH = "/etc/ld.so.cache"
    private_constant :DYNAMIC_LINKER_CACHE_PATH

    WARM_OPERATIONS = %w[text gravity flatten pngsave thumbnail sharpen].freeze
    private_constant :WARM_OPERATIONS

    SVG_DIMENSIONS_RLIMITS = { cpu_seconds: 1, memory_bytes: 1024 * 1024 * 1024 }.freeze
    private_constant :SVG_DIMENSIONS_RLIMITS

    SVG_TO_PNG_MAX_INPUT_BYTES = 3 * 1024 * 1024
    private_constant :SVG_TO_PNG_MAX_INPUT_BYTES

    SVG_TO_PNG_RLIMITS = {
      cpu_seconds: 1,
      memory_bytes: 1024 * 1024 * 1024,
      file_size_bytes: 1024 * 1024,
    }.freeze
    private_constant :SVG_TO_PNG_RLIMITS

    def self.warm
      WARM_OPERATIONS.each do |name|
        operation = Vips.vips_operation_new(name)
        raise "unable to initialize libvips operation #{name}" if operation.null?

        GObject.g_object_unref(operation)
      end
    end

    def self.build(command)
      operation, *arguments = command

      case operation
      when "version"
        version
      when "letter-avatar"
        letter_avatar(*arguments)
      when "dominant-color"
        dominant_color(*arguments)
      when "animated"
        animated(*arguments)
      when "svg-dimensions"
        svg_dimensions(*arguments)
      when "heif-to-jpeg"
        heif_to_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
      when "png-to-jpeg"
        png_to_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
      when "reencode-jpeg"
        reencode_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
      when "svg-to-png"
        svg_to_png(input_path: arguments[0], output_path: arguments[1])
      when "thumbnail"
        thumbnail(
          input_path: arguments[0],
          output_path: arguments[1],
          width: arguments[2],
          height: arguments[3],
          scale: arguments[4],
          max_pixels: arguments[5],
          size: arguments[6],
          crop: arguments[7],
          sharpen: arguments[8],
          quality: arguments[9],
          strip_metadata: arguments[10],
          gravity: arguments[11],
          input_format: arguments[12],
          output_format: arguments[13],
          animated: arguments[14],
        )
      else
        raise ArgumentError, "unsupported libvips operation"
      end
    end

    def initialize(rlimits: {}, &action)
      @rlimits = rlimits
      @action = action
    end

    def call
      @action.call
    end

    def sandbox_paths(scratch:, read:, write:)
      {
        "read" => existing_paths([*DEFAULT_READ_PATHS, DYNAMIC_LINKER_CACHE_PATH, *read]),
        "write" => existing_paths([scratch, *write]),
      }
    end

    def sandbox_rlimits(defaults:)
      limits = defaults.merge(@rlimits)
      # macOS rejects finite address-space limits with EINVAL.
      limits.delete(:memory_bytes) if RUBY_PLATFORM.include?("darwin")
      limits
    end

    def self.version
      new { Vips.version_string }
    end
    private_class_method :version

    def self.letter_avatar(letter, output_path, background_color, font, font_path)
      new do
        if !letter || !output_path || !font_path
          raise ArgumentError, "invalid letter avatar arguments"
        end
        raise ArgumentError, "font file does not exist" if !File.file?(font_path)
        raise ArgumentError, "invalid background color" if !background_color.match?(/\A\h{6}\z/)

        background = background_color.scan(/\h{2}/).map { |channel| Integer(channel, 16) }

        block_loaders
        markup = %(<span foreground="#ffffff" alpha="80%">#{letter}</span>)
        text = Vips::Image.text(markup, font:, dpi: 72, fontfile: font_path, rgba: true)
        canvas =
          text.gravity(:centre, 360, 360, extend: :background, background: [*background, 255])
        canvas.flatten(background:).pngsave(output_path, compression: 6)
        nil
      end
    end
    private_class_method :letter_avatar

    def self.dominant_color(input_path)
      new do
        raise ArgumentError, "invalid dominant color arguments" if !input_path

        block_loaders(
          allowed: %w[
            VipsForeignLoadJpeg
            VipsForeignLoadPng
            VipsForeignLoadNsgif
            VipsForeignLoadWebp
            VipsForeignLoadHeif
            VipsForeignLoadJxl
          ],
        )
        components =
          begin
            image = Vips::Image.new_from_file(input_path, access: :sequential)
            if image.has_alpha?
              alpha_weighted_components(image)
            elsif %i[float double].include?(image.format)
              normalize_color(image)
                .thumbnail_image(1, height: 1, size: :force)
                .getpoint(0, 0)
                .first(3)
                .map(&:round)
            else
              thumbnail = Vips::Image.thumbnail(input_path, 1, height: 1, size: :force)
              normalize_color(thumbnail).getpoint(0, 0).first(3).map(&:round)
            end
          rescue Vips::Error => error
            raise InvalidImage, error.message
          end
        format("%02X%02X%02X", *components)
      end
    end
    private_class_method :dominant_color

    def self.animated(input_path)
      new do
        block_loaders(allowed: %w[VipsForeignLoadNsgif VipsForeignLoadWebp VipsForeignLoadHeif])

        begin
          image = Vips::Image.new_from_file(input_path, access: :sequential)
          image.get_typeof("n-pages") != 0 && image.get("n-pages") > 1
        rescue Vips::Error => error
          raise InvalidImage, error.message
        end
      end
    end
    private_class_method :animated

    def self.heif_to_jpeg(input_path:, output_path:, quality:)
      new do
        block_loaders(allowed: %w[VipsForeignLoadHeif])

        begin
          save_jpeg(
            Vips::Image.heifload(input_path, access: :sequential),
            input_path:,
            output_path:,
            quality:,
          )
        rescue Vips::Error => error
          raise InvalidImage, error.message
        end
      end
    end
    private_class_method :heif_to_jpeg

    def self.png_to_jpeg(input_path:, output_path:, quality:)
      new do
        block_loaders(allowed: %w[VipsForeignLoadPng])

        begin
          save_jpeg(Vips::Image.pngload(input_path), input_path:, output_path:, quality:)
        rescue Vips::Error => error
          raise InvalidImage, error.message
        end
      end
    end
    private_class_method :png_to_jpeg

    def self.reencode_jpeg(input_path:, output_path:, quality:)
      new do
        quality = Integer(quality)
        raise ArgumentError, "invalid JPEG quality" unless (1..100).cover?(quality)

        block_loaders(allowed: %w[VipsForeignLoadJpeg])
        begin
          image = Vips::Image.jpegload(input_path, access: :random)
          save_jpeg(image, input_path:, output_path:, quality:)
          nil
        rescue Vips::Error => error
          raise InvalidImage, error.message
        end
      end
    end
    private_class_method :reencode_jpeg

    def self.save_jpeg(image, input_path:, output_path:, quality:)
      if File.expand_path(input_path) == File.expand_path(output_path) ||
           (File.exist?(output_path) && File.identical?(input_path, output_path))
        raise ArgumentError, "JPEG conversion requires separate input and output files"
      end

      image = image.autorot
      image = image.colourspace(image.bands == 2 ? :b_w : :srgb) if image.has_alpha?

      image.jpegsave(
        output_path,
        background: [255],
        Q: Integer(quality),
        interlace: false,
        # Optimized Huffman coding reduces JPEG size without changing decoded pixels.
        optimize_coding: true,
      )
      nil
    end
    private_class_method :save_jpeg

    def self.svg_dimensions(input_path)
      new(rlimits: SVG_DIMENSIONS_RLIMITS) do
        raise ArgumentError, "invalid SVG dimensions arguments" if !input_path

        block_loaders(allowed: %w[VipsForeignLoadSvg])
        begin
          Vips.concurrency_set(1)
          image = Vips::Image.svgload(input_path, dpi: 96, scale: 0.75)
          [image.width, image.height]
        rescue Vips::Error => error
          raise InvalidImage, error.message
        end
      end
    end
    private_class_method :svg_dimensions

    def self.svg_to_png(input_path:, output_path:)
      new(rlimits: SVG_TO_PNG_RLIMITS) do
        if File.expand_path(input_path) == File.expand_path(output_path) ||
             (File.exist?(output_path) && File.identical?(input_path, output_path))
          raise ArgumentError, "SVG input and PNG output must be different files"
        end
        if File.size(input_path) > SVG_TO_PNG_MAX_INPUT_BYTES
          raise ArgumentError, "SVG exceeds #{SVG_TO_PNG_MAX_INPUT_BYTES} bytes"
        end

        block_loaders(allowed: %w[VipsForeignLoadSvg])
        Vips.concurrency_set(1)
        image = Vips::Image.svgload(input_path, dpi: 96, scale: 0.75)
        image.flatten(background: [255, 255, 255]).pngsave(output_path)
        nil
      end
    end
    private_class_method :svg_to_png

    def self.thumbnail(
      input_path:,
      output_path:,
      width:,
      height:,
      scale:,
      max_pixels:,
      size:,
      crop:,
      sharpen:,
      quality:,
      strip_metadata:,
      gravity:,
      input_format:,
      output_format:,
      animated:
    )
      new do
        raise ArgumentError, "invalid sharpening flag" if !%w[true false].include?(sharpen)
        if !%w[true false].include?(strip_metadata)
          raise ArgumentError, "invalid metadata stripping flag"
        end
        raise ArgumentError, "invalid animated flag" if !%w[true false].include?(animated)

        quality = quality.empty? ? nil : Integer(quality)
        animated = animated == "true"
        if animated && !gravity.empty?
          raise ArgumentError, "gravity is not supported for animated thumbnails"
        end

        loaders = {
          "jpg" => "VipsForeignLoadJpeg",
          "jpeg" => "VipsForeignLoadJpeg",
          "png" => "VipsForeignLoadPng",
          "gif" => "VipsForeignLoadNsgif",
          "webp" => "VipsForeignLoadWebp",
          "avif" => "VipsForeignLoadHeif",
        }
        allowed = loaders.values
        if !input_format.empty?
          loader =
            loaders.fetch(input_format.downcase) { raise ArgumentError, "unsupported input format" }
          allowed = [loader]
        end
        block_loaders(allowed: allowed)

        source =
          begin
            Vips::Image.new_from_file(input_path, **(animated ? { n: -1 } : {}))
          rescue Vips::Error => error
            raise InvalidImage, error.message
          end

        source = source.autorot unless animated

        source_height =
          animated && source.get_typeof("n-pages") != 0 ? source.get("page-height") : source.height

        if !width.empty? && !height.empty?
          width = Integer(width)
          height = Integer(height)
        else
          max_pixels = max_pixels.empty? ? nil : Integer(max_pixels)
          scale =
            if max_pixels
              raise ArgumentError, "invalid resize scale" if !max_pixels.positive?

              Math.sqrt(max_pixels.fdiv(source.width * source_height))
            else
              Float(scale)
            end
          raise ArgumentError, "invalid resize scale" if !scale.finite? || !scale.positive?

          if max_pixels
            width = (source.width * scale).floor.clamp(1, max_pixels)
            height = (source_height * scale).floor.clamp(1, max_pixels)
          else
            width = height = [([source.width, source_height].max * scale).round, 1].max
          end
        end

        image =
          begin
            Vips::Image.thumbnail(
              animated ? "#{input_path}[n=-1]" : input_path,
              width,
              height: height,
              size: size.to_sym,
              crop: crop.to_sym,
              output_profile: "srgb",
            )
          rescue Vips::Error => error
            raise InvalidImage, error.message
          end

        image = image.gravity(gravity.to_sym, width, height) if !gravity.empty?
        image = image.sharpen if sharpen == "true"
        options = { strip: strip_metadata == "true" }
        options[:Q] = quality if quality
        if output_format.empty?
          image.write_to_file(output_path, **options)
        else
          image.write_to_target(
            Vips::Target.new_to_file(output_path),
            ".#{output_format.downcase}",
            **options,
          )
        end
        nil
      end
    end
    private_class_method :thumbnail

    def self.normalize_color(image)
      image = image.colourspace(:srgb)
      image = (image * 255).round(:rint) if %i[float double].include?(image.format)
      image.cast(:uchar, shift: true)
    end
    private_class_method :normalize_color

    def self.alpha_weighted_components(image)
      alpha = image[image.bands - 1]
      colors = normalize_color(image.extract_band(0, n: image.bands - 1))
      weighted_colors = colors * alpha
      statistics = alpha.bandjoin(weighted_colors).stats
      alpha_sum = statistics.getpoint(2, 1).first
      return 0, 0, 0 if alpha_sum.zero?

      3.times.map { |band| (statistics.getpoint(2, band + 2).first / alpha_sum).round }
    end
    private_class_method :alpha_weighted_components

    def self.block_loaders(allowed: [])
      Vips.block_untrusted(true)
      Vips.block("VipsForeignLoad", true)
      allowed.each { |loader| Vips.block(loader, false) }
    end
    private_class_method :block_loaders

    private

    def existing_paths(paths)
      paths
        .filter { |path| path.to_s != "" && File.exist?(path) }
        .map { |path| File.expand_path(path) }
        .uniq
    end
  end

  RLIMITS = {
    cpu_seconds: 300,
    memory_bytes: 4 * 1024 * 1024 * 1024,
    file_size_bytes: 10 * 1024 * 1024 * 1024,
    open_files: 1024,
  }.freeze
  private_constant :RLIMITS

  def self.run(server_fd:, owner_fd:)
    parent_pid = Process.ppid
    Landlock::Native.set_parent_death_signal! if LINUX_PLATFORM
    exit! 1 if Process.ppid != parent_pid

    Process.setproctitle("discourse vips worker")
    new(server: UNIXServer.for_fd(server_fd), owner_reader: IO.for_fd(owner_fd)).run
  end

  def initialize(server:, owner_reader:)
    @server = server
    @socket_path = server.local_address.unix_path
    @socket_identity = File.stat(@socket_path).then { |stat| [stat.dev, stat.ino] }
    @owner_reader = owner_reader
  end

  def run
    Signal.trap("CHLD") { reap_connection_processes }
    Operations.warm

    loop do
      ready_ios = IO.select([@server, @owner_reader]).first
      break if ready_ios.include?(@owner_reader)

      connection = @server.accept
      fork_connection_process(connection)
    end
  ensure
    @server.close unless @server.closed?
    @owner_reader.close unless @owner_reader.closed?
    remove_socket_directory
    Process.kill("KILL", -Process.pid)
  end

  private

  def fork_connection_process(connection)
    parent_pid = Process.pid
    fork do
      Landlock::Native.set_parent_death_signal! if LINUX_PLATFORM
      exit! 1 if Process.ppid != parent_pid
      Signal.trap("CHLD", "DEFAULT")
      @server.close
      @owner_reader.close
      handle_connection(connection)
      exit! 0
    end

    connection.close
  end

  def reap_connection_processes
    while Process.waitpid(-1, Process::WNOHANG)
    end
  rescue Errno::ECHILD
  end

  def handle_connection(connection)
    request = MessagePack.unpack(connection.read)
    operation = Operations.build(request.fetch("command"))
    response = run_sandboxed(request:, operation:)
    write_response(connection, response)
  rescue StandardError => error
    write_response(connection, "status" => "error", "message" => error.message.byteslice(0, 4096))
  ensure
    connection.close unless connection.closed?
  end

  def write_response(connection, response)
    connection.write(MessagePack.pack(response))
  rescue IOError, SystemCallError
  end

  def run_sandboxed(request:, operation:)
    Dir.mktmpdir("discourse-vips-") do |scratch|
      sandbox_paths =
        operation.sandbox_paths(
          scratch:,
          read: request.fetch("read"),
          write: request.fetch("write"),
        )
      result =
        Landlock.fork(
          read: sandbox_paths.fetch("read"),
          write: sandbox_paths.fetch("write"),
          execute: [],
          timeout: Float(request.fetch("timeout")),
          env: child_environment(scratch),
          unsetenv_others: true,
          rlimits: operation.sandbox_rlimits(defaults: RLIMITS),
          seccomp_deny_network: LINUX_PLATFORM,
          seccomp_deny_child_processes: LINUX_PLATFORM,
          on_unsupported: :run_without_landlock,
        ) do |stdout, _stderr|
          apply_nice(request)
          stdout.write(MessagePack.pack(operation_response(operation)))
        end

      if result.timed_out?
        { "status" => "timeout" }
      elsif result.success?
        MessagePack.unpack(result.stdout)
      else
        { "status" => "error", "message" => "libvips operation failed" }
      end
    end
  end

  def remove_socket_directory
    stat = File.stat(@socket_path)
    return if [stat.dev, stat.ino] != @socket_identity

    File.unlink(@socket_path)
    Dir.rmdir(File.dirname(@socket_path))
  rescue SystemCallError
  end

  def child_environment(scratch)
    {
      "HOME" => scratch,
      "TMPDIR" => scratch,
      "XDG_CACHE_HOME" => scratch,
      "MALLOC_ARENA_MAX" => "2",
    }
  end

  def apply_nice(request)
    Process.setpriority(Process::PRIO_PROCESS, 0, Integer(request.fetch("nice"))) if request["nice"]
  end

  def operation_response(operation)
    value = operation.call
    { "status" => "ok", "value" => value }
  rescue InvalidImage => error
    { "status" => "invalid_image", "message" => error.message.byteslice(0, 4096) }
  rescue StandardError => error
    { "status" => "error", "message" => error.message.byteslice(0, 4096) }
  end
end

if $PROGRAM_NAME == __FILE__
  server_fd, owner_fd = ARGV.map { |argument| Integer(argument) }
  DiscourseVipsWorker.run(server_fd:, owner_fd:)
end