Skip to content

Commit

Permalink
[WIP][BUGFIX] Prevent possible disk-filler
Browse files Browse the repository at this point in the history
* As it was possible to modify the url and generate pdf files via script
  to fill up the fiel system, we needed to provide a whitelist for
  parameter to limit valid urls

Resolves: #1
  • Loading branch information
Daniel Siepmann committed Oct 31, 2015
1 parent 423710d commit ad85d3e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
49 changes: 48 additions & 1 deletion Classes/Generator/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Pdf
public function main($content, array $conf)
{
$this->processConfiguration($conf);

if(!$this->pageAvailable()) {
$GLOBALS['TSFE']->pageNotFoundAndExit();
}
$this->generatePdf();

// Redirect to PDF in file system
Expand Down Expand Up @@ -109,6 +113,45 @@ protected function getPdfUrl()
return $this->getDomain() . str_replace(PATH_site, '', $this->getFileName());
}

protected function pageAvailable()
{
$responseHeader = array();
\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(
$this->getUrlForGeneration(),
2,
array(),
$responseHeader
);
return ($responseHeader['http_code'] === '200');
}

protected function filterUrl($urlToFilter)
{
$filteredUrl = '';
$parsedUrl = parse_url($urlToFilter);
if(!isset($parsedUrl['query'])) {
return $urlToFilter;
}

$filteredUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
$urlQuery = array_filter(
explode('&', $parsedUrl['query']),
function ($queryParameter) {
list($parameterName) = explode('=', $queryParameter);
if(!in_array($parameterName, $this->configuration['parameterWhitelist'])) {
return false;
}
return true;
}
);

if(count($urlQuery) > 0) {
$filteredUrl .= '?' . implode('&', $urlQuery);
}

return $filteredUrl;
}

/**
* Get url to use for PDF generation.
*
Expand All @@ -118,7 +161,7 @@ protected function getPdfUrl()
*/
protected function getUrlForGeneration()
{
$urlToConvert = $this->getDomain() . $GLOBALS['TSFE']->siteScript;
$urlToConvert = $this->filterUrl($this->getDomain() . $GLOBALS['TSFE']->siteScript);

// Remove type parameter to generate the real url, not the PDF (= endless loop)
$urlToConvert = str_ireplace('type=' . $this->configuration['typeNum'], '', $urlToConvert);
Expand Down Expand Up @@ -163,6 +206,10 @@ protected function processConfiguration(array $configuration)
}

$this->configuration[$key] = $value;

if($key === 'parameterWhitelist') {
$this->configuration[$key] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $value);
}
}

// Process only the cli parameter configuration
Expand Down
3 changes: 3 additions & 0 deletions Configuration/TypoScript/constants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ plugin.wv_pdfgen {
type = 100
# cat=plugin.tx_wv_pdfgen; type=string; label=LLL:EXT:wv_pdfgen/Resources/Private/Language/Backend.xlf:option.urlExtension
urlExtension = .pdf

#
parameterWhitelist = cHash, type, L, id
}
4 changes: 2 additions & 2 deletions Configuration/TypoScript/setup.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pdf = PAGE
pdf {

# Configure type for PDF output
typeNum = {$plugin.wv_pdfgen.type}

Expand All @@ -16,6 +15,7 @@ pdf {
typeNum < pdf.typeNum
# The extension to remove from url, e.g. you can configure realurl to hide the type and how ".pdf".
urlExtension = {$plugin.wv_pdfgen.urlExtension}
}

parameterWhitelist = {$plugin.wv_pdfgen.parameterWhitelist}
}
}

0 comments on commit ad85d3e

Please sign in to comment.