Skip to content

Commit

Permalink
Added decodeMemoryLimit to Config to avoid memory leaks. (#476)
Browse files Browse the repository at this point in the history
* Added decodeMemoryLimit to Config

* decodeMemoryLimit is now an optional argument, defaulting to 0 (unlimited)

* Add type to parameter $decodeMemoryLimit in decodeFilter

* fixes cs issue in RawDataParser.php

Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
b3n-l and k00ni authored Nov 25, 2021
1 parent 768d1d6 commit e056671
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/Smalot/PdfParser/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Config
*/
private $retainImageContent = true;

/**
* Memory limit to use when de-compressing files, in bytes.
*
* @var int
*/
private $decodeMemoryLimit = 0;

public function getFontSpaceLimit()
{
return $this->fontSpaceLimit;
Expand Down Expand Up @@ -100,4 +107,14 @@ public function setRetainImageContent(bool $retainImageContent): void
{
$this->retainImageContent = $retainImageContent;
}

public function getDecodeMemoryLimit(): int
{
return $this->decodeMemoryLimit;
}

public function setDecodeMemoryLimit(int $decodeMemoryLimit): void
{
$this->decodeMemoryLimit = $decodeMemoryLimit;
}
}
11 changes: 6 additions & 5 deletions src/Smalot/PdfParser/RawData/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class FilterHelper
*
* @throws Exception if a certain decode function is not implemented yet
*/
public function decodeFilter(string $filter, string $data): string
public function decodeFilter(string $filter, string $data, int $decodeMemoryLimit = 0): string
{
switch ($filter) {
case 'ASCIIHexDecode':
Expand All @@ -69,7 +69,7 @@ public function decodeFilter(string $filter, string $data): string
return $this->decodeFilterLZWDecode($data);

case 'FlateDecode':
return $this->decodeFilterFlateDecode($data);
return $this->decodeFilterFlateDecode($data, $decodeMemoryLimit);

case 'RunLengthDecode':
return $this->decodeFilterRunLengthDecode($data);
Expand Down Expand Up @@ -224,13 +224,14 @@ protected function decodeFilterASCII85Decode(string $data): string
*
* Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data.
*
* @param string $data Data to decode
* @param string $data Data to decode
* @param int $decodeMemoryLimit Memory limit on deflation
*
* @return string data string
*
* @throws Exception
*/
protected function decodeFilterFlateDecode(string $data): ?string
protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string
{
/*
* gzuncompress may throw a not catchable E_WARNING in case of an error (like $data is empty)
Expand All @@ -249,7 +250,7 @@ protected function decodeFilterFlateDecode(string $data): ?string

// initialize string to return
try {
$decoded = gzuncompress($data);
$decoded = gzuncompress($data, $decodeMemoryLimit);
if (false === $decoded) {
throw new Exception('decodeFilterFlateDecode: invalid code');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function decodeStream(string $pdfData, array $xref, array $sdic, strin
foreach ($filters as $filter) {
if (\in_array($filter, $this->filterHelper->getAvailableFilters())) {
try {
$stream = $this->filterHelper->decodeFilter($filter, $stream);
$stream = $this->filterHelper->decodeFilter($filter, $stream, $this->config->getDecodeMemoryLimit());
} catch (Exception $e) {
$emsg = $e->getMessage();
if ((('~' == $emsg[0]) && !$this->cfg['ignore_missing_filter_decoders'])
Expand Down

0 comments on commit e056671

Please sign in to comment.