File size: 9,432 Bytes
27cd9bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | diff --git a/src/Attachment.php b/src/Attachment.php
index ca99f5f22..b54f97a22 100644
--- a/src/Attachment.php
+++ b/src/Attachment.php
@@ -2,6 +2,7 @@
namespace Timber;
+use InvalidArgumentException;
use Timber\Factory\PostFactory;
/**
@@ -245,6 +246,10 @@ public function size(): ?int
return $this->size = (int) $size;
}
+ if (!ImageHelper::is_protocol_allowed($this->file_loc())) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
/**
* Filesize wasn't found in the metadata, so we'll try to get it from the file itself.
*
diff --git a/src/Image/Operation/ToJpg.php b/src/Image/Operation/ToJpg.php
index 6f768e8b0..e45e9e4c3 100644
--- a/src/Image/Operation/ToJpg.php
+++ b/src/Image/Operation/ToJpg.php
@@ -2,6 +2,7 @@
namespace Timber\Image\Operation;
+use InvalidArgumentException;
use Timber\Image\Operation as ImageOperation;
use Timber\ImageHelper;
@@ -44,6 +45,10 @@ public function filename($src_filename, $src_extension = 'jpg')
*/
public function run($load_filename, $save_filename)
{
+ if (!ImageHelper::is_protocol_allowed($load_filename)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
if (!\file_exists($load_filename)) {
return false;
}
diff --git a/src/Image/Operation/ToWebp.php b/src/Image/Operation/ToWebp.php
index 11b4ba5a9..abd573ae8 100644
--- a/src/Image/Operation/ToWebp.php
+++ b/src/Image/Operation/ToWebp.php
@@ -2,6 +2,7 @@
namespace Timber\Image\Operation;
+use InvalidArgumentException;
use Timber\Helper;
use Timber\Image\Operation as ImageOperation;
use Timber\ImageHelper;
@@ -45,6 +46,10 @@ public function filename($src_filename, $src_extension = 'webp')
*/
public function run($load_filename, $save_filename)
{
+ if (!ImageHelper::is_protocol_allowed($load_filename)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
if (!\is_file($load_filename)) {
return false;
}
diff --git a/src/ImageDimensions.php b/src/ImageDimensions.php
index ee2babcb7..c92bf8dab 100644
--- a/src/ImageDimensions.php
+++ b/src/ImageDimensions.php
@@ -2,6 +2,8 @@
namespace Timber;
+use InvalidArgumentException;
+
/**
* Class FileSize
*
@@ -117,6 +119,10 @@ public function get_dimension($dimension): ?int
return $this->get_dimension_loaded($dimension);
}
+ if (!ImageHelper::is_protocol_allowed($this->file_loc)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
// Load dimensions.
if (\file_exists($this->file_loc) && \filesize($this->file_loc)) {
if (ImageHelper::is_svg($this->file_loc)) {
diff --git a/src/ImageHelper.php b/src/ImageHelper.php
index 90945a55b..a6abad014 100644
--- a/src/ImageHelper.php
+++ b/src/ImageHelper.php
@@ -2,6 +2,7 @@
namespace Timber;
+use InvalidArgumentException;
use Timber\Image\Operation;
/**
@@ -29,6 +30,10 @@ class ImageHelper
public static $home_url;
+ protected const ALLOWED_PROTOCOLS = ['file', 'http', 'https'];
+
+ protected const WINDOWS_LOCAL_FILENAME_REGEX = '/^[a-z]:(?:[\\\\\/]?(?:[\w\s!#()-]+|[\.]{1,2})+)*[\\\\\/]?/i';
+
/**
* Inits the object.
*/
@@ -140,6 +145,11 @@ public static function is_animated_gif($file)
//doesn't have .gif, bail
return false;
}
+
+ if (!ImageHelper::is_protocol_allowed($file)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
// Its a gif so test
if (!($fh = @\fopen($file, 'rb'))) {
return false;
@@ -169,7 +179,15 @@ public static function is_animated_gif($file)
*/
public static function is_svg($file_path)
{
- if ('' === $file_path || !\file_exists($file_path)) {
+ if ('' === $file_path) {
+ return false;
+ }
+
+ if (!ImageHelper::is_protocol_allowed($file_path)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
+ if (!\file_exists($file_path)) {
return false;
}
@@ -431,6 +449,10 @@ public static function get_sideloaded_file_loc($file)
*/
public static function sideload_image($file)
{
+ if (!ImageHelper::is_protocol_allowed($file)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
/**
* Adds a filter to change the upload folder temporarily.
*
@@ -444,6 +466,7 @@ public static function sideload_image($file)
\add_filter('upload_dir', [__CLASS__, 'set_sideload_image_upload_dir']);
$loc = self::get_sideloaded_file_loc($file);
+
if (\file_exists($loc)) {
$url = URLHelper::file_system_to_url($loc);
@@ -806,6 +829,10 @@ private static function _operate($src, $op, $force = false)
return '';
}
+ if (!ImageHelper::is_protocol_allowed($src)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
$allow_fs_write = \apply_filters('timber/allow_fs_write', true);
if ($allow_fs_write === false) {
@@ -949,4 +976,36 @@ public static function get_resize_file_path($url, $w, $h, $crop)
);
return $new_path;
}
+
+ /**
+ * Checks if the protocol of the given filename is allowed.
+ *
+ * This fixes a security issue with a PHAR deserialization vulnerability
+ * with file_exists() in PHP < 8.0.0.
+ *
+ * @param string $filepath File path.
+ * @return bool
+ */
+ public static function is_protocol_allowed($filepath)
+ {
+ $parsed_url = \parse_url($filepath);
+
+ if (false === $parsed_url) {
+ throw new InvalidArgumentException('The filename is not valid.');
+ }
+
+ $protocol = isset($parsed_url['scheme'])
+ ? \mb_strtolower($parsed_url['scheme'])
+ : 'file';
+
+ if (
+ \PHP_OS_FAMILY === 'Windows'
+ && \strlen($protocol) === 1
+ && \preg_match(self::WINDOWS_LOCAL_FILENAME_REGEX, $filepath)
+ ) {
+ $protocol = 'file';
+ }
+
+ return \in_array($protocol, self::ALLOWED_PROTOCOLS, true);
+ }
}
diff --git a/src/Timber.php b/src/Timber.php
index eded41497..3d01fd154 100644
--- a/src/Timber.php
+++ b/src/Timber.php
@@ -2,6 +2,7 @@
namespace Timber;
+use InvalidArgumentException;
use Timber\Factory\CommentFactory;
use Timber\Factory\MenuFactory;
use Timber\Factory\PagesMenuFactory;
@@ -659,6 +660,10 @@ public static function get_attachment_by(string $field_or_ident, string $ident =
return null;
}
+ if (!ImageHelper::is_protocol_allowed($ident)) {
+ throw new InvalidArgumentException('The output file scheme is not supported.');
+ }
+
if (!\file_exists($ident)) {
// Deal with a relative path.
$ident = URLHelper::get_full_path($ident);
diff --git a/tests/test-timber-image.php b/tests/test-timber-image.php
index 8c4d09786..822f721ec 100644
--- a/tests/test-timber-image.php
+++ b/tests/test-timber-image.php
@@ -1185,4 +1185,54 @@ public function testSVGDimensions()
$this->assertSame(23, $image->width());
$this->assertSame(20, $image->height());
}
+
+ public function testPharProtocolIsNotAllowedwithResize()
+ {
+ $object = new ImageOperation\Resize(400, 300, 'center');
+ $load_filename = 'phar://test.jpg';
+ $save_filename = 'test-new.jpg';
+
+ $this->expectException(InvalidArgumentException::class);
+ $object->run($load_filename, $save_filename);
+ }
+
+ public function testPharProtocolIsNotAllowedwithLetterbox()
+ {
+ $object = new ImageOperation\Letterbox(400, 300, '#FFFFFF');
+ $load_filename = 'phar://test.jpg';
+ $save_filename = 'test-new.jpg';
+
+ $this->expectException(InvalidArgumentException::class);
+ $object->run($load_filename, $save_filename);
+ }
+
+ public function testPharProtocolIsNotAllowedwithRetina()
+ {
+ $object = new ImageOperation\Retina(2);
+ $load_filename = 'phar://test.jpg';
+ $save_filename = 'test-new.jpg';
+
+ $this->expectException(InvalidArgumentException::class);
+ $object->run($load_filename, $save_filename);
+ }
+
+ public function testPharProtocolIsNotAllowedwithToJpg()
+ {
+ $object = new ImageOperation\ToJpg('#FFFFFF');
+ $load_filename = 'phar://test.svg';
+ $save_filename = 'test-new.svg';
+
+ $this->expectException(InvalidArgumentException::class);
+ $object->run($load_filename, $save_filename);
+ }
+
+ public function testPharProtocolIsNotAllowedwithToWebp()
+ {
+ $object = new ImageOperation\ToWebp(80);
+ $load_filename = 'phar://test.png';
+ $save_filename = 'test-new.png';
+
+ $this->expectException(InvalidArgumentException::class);
+ $object->run($load_filename, $save_filename);
+ }
}
|