Skip to content

Commit

Permalink
Allow image stream whitelist to be customised
Browse files Browse the repository at this point in the history
Resolves #991
  • Loading branch information
jakejackson1 authored and finwe committed Mar 29, 2019
1 parent 95dce25 commit b69be38
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Config/ConfigVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ public function __construct()
// Default dpi to output images if size not defined
// See also above "dpi"
'img_dpi' => 96,
// Specify whitelisted PHP streams to be used for images
// Useful to add custom streams like `s3`
// Note: for security reasons the `phar` stream cannot be used @see https://github.com/mpdf/mpdf/issues/949
'whitelistStreamWrappers' => ['http', 'https', 'file'],

// TEXT SPACING & JUSTIFICATION

Expand Down
3 changes: 2 additions & 1 deletion src/Image/ImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,9 @@ private function hasBlacklistedStreamWrapper($filename)
{
if (strpos($filename, '://') > 0) {
$wrappers = stream_get_wrappers();
$whitelistStreamWrappers = array_diff($this->mpdf->whitelistStreamWrappers, ['phar']); /* remove `phar` (security issue) */
foreach ($wrappers as $wrapper) {
if (in_array($wrapper, ['http', 'https', 'file'])) {
if (in_array($wrapper, $whitelistStreamWrappers)) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface
var $allow_html_optional_endtags;

var $img_dpi;
var $whitelistStreamWrappers;

var $defaultheaderfontsize;
var $defaultheaderfontstyle;
Expand Down
17 changes: 16 additions & 1 deletion tests/Mpdf/Image/ImageProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function setUp()
$mpdf->shouldIgnoreMissing();

$mpdf->img_dpi = 72;
$mpdf->whitelistStreamWrappers = ['http', 'file', 's3', 'phar'];
$mpdf->showImageErrors = true;
$mpdf->PDFAXwarnings = [];

Expand Down Expand Up @@ -82,7 +83,7 @@ public function dataProviderStreamBlacklist()

$wrappers = stream_get_wrappers();
foreach ($wrappers as $wrapper) {
if (in_array($wrapper, ['http', 'https', 'file'])) {
if (in_array($wrapper, ['http', 'file', 's3'])) {
$testData[] = [$wrapper . '://', '/does not exist on this mock object/'];
} else {
$testData[] = [$wrapper . '://', '/File contains an invalid stream./'];
Expand All @@ -92,3 +93,17 @@ public function dataProviderStreamBlacklist()
return $testData;
}
}

function stream_get_wrappers()
{
return [
'php',
'file',
'http',
'ftp',
'https',
's3',
'phar',
'compress.bzip2'
];
}

0 comments on commit b69be38

Please sign in to comment.