Skip to content

Commit 52ed5c0

Browse files
committed
add setBlock() and setBlockUntrusted()
1 parent e2289d4 commit 52ed5c0

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to `php-vips` will be documented in this file.
55
## master
66

77
- better ffi startup diagnostics [ping-localhost]
8+
- add setBlock() and setBlockUntrusted() to control operation blocking [jcupitt]
89

910
## 2.6.1 - 2025-12-10
1011

src/Config.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,53 @@ public static function concurrencySet(int $value): void
138138
FFI::vips()->vips_concurrency_set($value);
139139
}
140140

141+
/**
142+
* Set the block state on all operations in the libvips class hierarchy at
143+
* name and below.
144+
*
145+
* For example:
146+
*
147+
* ```php
148+
* Vips\Config::setBlockUntrusted(true);
149+
* Vips\Config::setBlock("VipsForeignLoadSvg", false);
150+
* ```
151+
*
152+
* Will block all untrusted loaders, but allow SVG.
153+
*
154+
* @param string $name The name of the class to block.
155+
* @param bool $state The block state to set.
156+
*
157+
* @return void
158+
*/
159+
public static function setBlock(string $name, bool $state): void
160+
{
161+
if (FFI::atLeast(8, 13)) {
162+
FFI::vips()->vips_operation_block_set($name, $state);
163+
}
164+
}
165+
166+
/**
167+
* Set the block state on all untrusted operations.
168+
*
169+
* For example:
170+
*
171+
* ```php
172+
* Vips\Config::setBlockUntrusted(true);
173+
* ```
174+
*
175+
* Will prevent all untrusted loaders from running.
176+
*
177+
* @param bool $state The block state to set.
178+
*
179+
* @return void
180+
*/
181+
public static function setBlockUntrusted(bool $state): void
182+
{
183+
if (FFI::atLeast(8, 13)) {
184+
FFI::vips()->vips_block_untrusted_set($state);
185+
}
186+
}
187+
141188
/**
142189
* Gets the libvips version number as a string of the form
143190
* MAJOR.MINOR.MICRO, for example "8.6.1".

src/FFI.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,14 @@ private static function init(): void
791791
792792
const char* vips_foreign_find_load_source (VipsSource *source);
793793
const char* vips_foreign_find_save_target (const char* suffix);
794+
CPP;
795+
}
796+
797+
if (self::atLeast(8, 13)) {
798+
$vips_decls = $vips_decls . <<<'CPP'
799+
void vips_block_untrusted_set(int state);
800+
void vips_operation_block_set(const char *name, int state);
801+
794802
CPP;
795803
}
796804

tests/ConfigTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,56 @@ public function testVipsVersion()
3434
$version = Vips\Config::version();
3535
$this->assertEquals(preg_match("/\d+\.\d+\.\d+/", $version), 1);
3636
}
37+
38+
public function testPpmLoadBuffer()
39+
{
40+
$ppm = "P3
41+
1 1
42+
255
43+
0 0 0
44+
";
45+
46+
// the PPM loader is built in and should be available in most
47+
// libvips binaries
48+
$image = Vips\Image::ppmload_buffer($ppm);
49+
$this->assertTrue($image->width == 1);
50+
}
51+
52+
public function testBlockUntrusted()
53+
{
54+
$ppm = "P3
55+
1 1
56+
255
57+
0 0 0
58+
";
59+
60+
if (Vips\FFI::atLeast(8, 13)) {
61+
Vips\Config::setBlockUntrusted(true);
62+
63+
// should fail
64+
$this->expectException(Vips\Exception::class);
65+
$image = Vips\Image::ppmload_buffer($ppm);
66+
$this->assertTrue($image->width == 1);
67+
}
68+
}
69+
70+
public function testBlock()
71+
{
72+
$ppm = "P3
73+
1 1
74+
255
75+
0 0 0
76+
";
77+
78+
if (Vips\FFI::atLeast(8, 13)) {
79+
Vips\Config::setBlockUntrusted(true);
80+
Vips\Config::setBlock("VipsForeignLoadPpm", false);
81+
82+
// should work
83+
$image = Vips\Image::ppmload_buffer($ppm);
84+
$this->assertTrue($image->width == 1);
85+
}
86+
}
3787
}
3888

3989
/*

0 commit comments

Comments
 (0)