Skip to content

Commit 0c110b0

Browse files
authored
Merge pull request #382 from aglushkov/master
Allow to use vips_block_untrusted_set and vips_operation_block_set methods
2 parents 27d2c3a + 83a8340 commit 0c110b0

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master
44

5+
* add `Vips.block_untrusted` method to block all untrusted operations. Only for libvips >= 8.13. [Docs](https://www.libvips.org/API/current/libvips-vips.html#vips-block-untrusted-set). [#382](https://github.com/libvips/ruby-vips/pull/382) [aglushkov](https://github.com/aglushkov)
6+
* add `Vips.block` method to block specific operation. Only for libvips >= 8.13. [Docs](https://www.libvips.org/API/current/VipsOperation.html#vips-operation-block-set). [#382](https://github.com/libvips/ruby-vips/pull/382) [aglushkov](https://github.com/aglushkov)
57
* `new_from_source` keeps a ref to the source object [taylorthurlow]
68
* some fixes to object references system
79

lib/vips.rb

+25
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,31 @@ def self.at_least_libvips?(x, y)
779779
major > x || (major == x && minor >= y)
780780
end
781781

782+
if at_least_libvips?(8, 13)
783+
attach_function :vips_block_untrusted_set, [:bool], :void
784+
attach_function :vips_operation_block_set, %i[string bool], :void
785+
786+
# Block/unblock all untrusted operations from running.
787+
# Use `vips -l` at the command-line to see the class hierarchy and which operations are marked as untrusted.
788+
def self.block_untrusted(enabled)
789+
vips_block_untrusted_set(enabled)
790+
end
791+
792+
# Block/unblock all operations in the libvips class hierarchy at specified *operation_name* and below.
793+
#
794+
# For example this will block all loaders except JPEG
795+
#
796+
# Vips.block("VipsForeignLoad", true);
797+
# Vips.block("VipsForeignLoadJpeg", false)
798+
#
799+
# Use `vips -l` at the command-line to see the class hierarchy.
800+
# This call does nothing if the named operation is not found.
801+
#
802+
def self.block(operation_name, enabled)
803+
vips_operation_block_set(operation_name, enabled)
804+
end
805+
end
806+
782807
# Get a list of all supported file suffixes.
783808
#
784809
# @return [[String]] array of supported suffixes

spec/block_operations_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "spec_helper"
2+
3+
RSpec.describe Vips, version: [8, 13] do
4+
let(:svg_image) { simg("lion.svg") }
5+
let(:jpg_image) { simg("wagon.jpg") }
6+
7+
if has_svg?
8+
it "can block untrusted operations" do
9+
untrusted_image = svg_image # svgload operation is known as untrusted
10+
11+
# Block
12+
Vips.block_untrusted(true)
13+
expect { Vips::Image.new_from_file(untrusted_image) }.to raise_error Vips::Error, /svgload/
14+
15+
# Unblock
16+
Vips.block_untrusted(false)
17+
expect { Vips::Image.new_from_file(untrusted_image) }.not_to raise_error
18+
end
19+
end
20+
21+
if has_jpeg? && has_svg?
22+
it "can block specific operations" do
23+
# Block all loaders except jpeg
24+
Vips.block("VipsForeignLoad", true)
25+
Vips.block("VipsForeignLoadJpeg", false)
26+
expect { Vips::Image.new_from_file(svg_image) }.to raise_error Vips::Error, /svgload/
27+
expect { Vips::Image.new_from_file(jpg_image) }.not_to raise_error
28+
29+
# Unblock all loaders
30+
Vips.block("VipsForeignLoad", false)
31+
expect { Vips::Image.new_from_file(svg_image) }.not_to raise_error
32+
end
33+
end
34+
end

spec/image_spec.rb

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
require "spec_helper"
22

3-
def has_jpeg?
4-
Vips.type_find("VipsOperation", "jpegload") != nil
5-
end
6-
73
RSpec.describe Vips::Image do
84
it "can save an image to a file" do
95
filename = timg "x.v"

spec/spec_helper.rb

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def timg(name)
2222
File.join(@temp_dir, name)
2323
end
2424

25+
def has_jpeg?
26+
Vips.type_find("VipsOperation", "jpegload") != nil
27+
end
28+
29+
def has_svg?
30+
Vips.type_find("VipsOperation", "svgload") != nil
31+
end
32+
2533
RSpec.configure do |config|
2634
config.around do |example|
2735
Dir.mktmpdir("ruby-vips-spec-") do |dir|

0 commit comments

Comments
 (0)