123456789_123456789_123456789_123456789_123456789_

Module: ActiveStorage::ImageMagick

Relationships & Source Files
Defined in: activestorage/lib/active_storage/image_magick.rb

Overview

Adds Active Storage's ImageMagick security policy to every ImageMagick process MiniMagick spawns.

ImageMagick reads every policy.xml on its configuration search path and applies all of them, so the shipped policy adds to the operator's policy rather than replacing it. MAGICK_CONFIGURE_PATH prepends to that search path; a value the operator has already set, in the environment or in MiniMagick.cli_env, is kept after the shipped directory.

Constant Summary

Class Method Summary

Class Method Details

.apply_policy

This method is for internal use only.

Called when Active Storage loads, and again after the application initializes, so that a MAGICK_CONFIGURE_PATH or MiniMagick.cli_env set by an initializer or dotenv keeps the policy.

[ GitHub ]

  
# File 'activestorage/lib/active_storage/image_magick.rb', line 28

def self.apply_policy # :nodoc:
  operator_paths = operator_paths(MiniMagick.cli_env["MAGICK_CONFIGURE_PATH"]).presence ||
    operator_paths(ENV["MAGICK_CONFIGURE_PATH"])

  MiniMagick.cli_env = MiniMagick.cli_env.merge("MAGICK_CONFIGURE_PATH" => configure_path(operator_paths)).freeze
end

.configure_path(operator_path)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/image_magick.rb', line 35

def self.configure_path(operator_path)
  [ POLICY_DIRECTORY, *operator_paths(operator_path) ].join(File::PATH_SEPARATOR)
end

.operator_paths(path) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/image_magick.rb', line 39

def self.operator_paths(path)
  Array(path).flat_map { |entry| entry.split(File::PATH_SEPARATOR) } - [ POLICY_DIRECTORY, "" ]
end