diff --git a/lib/vips/operation.rb b/lib/vips/operation.rb index ddaa1b2..2a36665 100644 --- a/lib/vips/operation.rb +++ b/lib/vips/operation.rb @@ -11,6 +11,26 @@ module Vips if at_least_libvips?(8, 13) attach_function :vips_block_untrusted_set, [:bool], :void attach_function :vips_operation_block_set, %i[string bool], :void + + # Block/unblock all untrusted operations from running. + # Use `vips -l` at the command-line to see the class hierarchy and which operations are marked as untrusted. + def self.block_untrusted(enabled = true) + vips_block_untrusted_set(enabled) + end + + # Block/unblock all operations in the libvips class hierarchy at specified *operation_name* and below. + # + # For example this will block all loaders except JPEG + # + # Vips.block("VipsForeignLoad"); + # Vips.block("VipsForeignLoadJpeg", false) + # + # Use `vips -l` at the command-line to see the class hierarchy. + # This call does nothing if the named operation is not found. + # + def self.block(operation_name, enabled = true) + vips_operation_block_set(operation_name, enabled) + end end private diff --git a/spec/block_operations_spec.rb b/spec/block_operations_spec.rb index 314937c..eff6c47 100644 --- a/spec/block_operations_spec.rb +++ b/spec/block_operations_spec.rb @@ -9,11 +9,11 @@ untrusted_image = svg_image # svgload operation is known as untrusted # Block - Vips.vips_block_untrusted_set(true) + Vips.block_untrusted expect { Vips::Image.new_from_file(untrusted_image) }.to raise_error Vips::Error, /svgload/ # Unblock - Vips.vips_block_untrusted_set(false) + Vips.block_untrusted(false) expect { Vips::Image.new_from_file(untrusted_image) }.not_to raise_error end end @@ -21,13 +21,13 @@ if has_jpeg? && has_svg? it "can block specific operations" do # Block all loaders except jpeg - Vips.vips_operation_block_set("VipsForeignLoad", true) - Vips.vips_operation_block_set("VipsForeignLoadJpeg", false) + Vips.block("VipsForeignLoad") + Vips.block("VipsForeignLoadJpeg", false) expect { Vips::Image.new_from_file(svg_image) }.to raise_error Vips::Error, /svgload/ expect { Vips::Image.new_from_file(jpg_image) }.not_to raise_error # Unblock all loaders - Vips.vips_operation_block_set("VipsForeignLoad", false) + Vips.block("VipsForeignLoad", false) expect { Vips::Image.new_from_file(svg_image) }.not_to raise_error end end