Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fix for potential security risk related to file include #1261

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions Classes/Controller/Backend/NewTenantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
use Kitodo\Dlf\Domain\Repository\StructureRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Localization\LocalizationFactory;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
Expand Down Expand Up @@ -179,7 +180,7 @@
public function addFormatAction(): void
{
// Include formats definition file.
$formatsDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/FormatDefaults.php');
$formatsDefaults = $this->getRecords('Format');

$frameworkConfiguration = $this->configurationManager->getConfiguration($this->configurationManager::CONFIGURATION_TYPE_FRAMEWORK);
// tx_dlf_formats are stored on PID = 0
Expand Down Expand Up @@ -221,7 +222,7 @@
public function addMetadataAction(): void
{
// Include metadata definition file.
$metadataDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/MetadataDefaults.php');
$metadataDefaults = $this->getRecords('Metadata');

// load language file in own array
$metadataLabels = $this->languageFactory->getParsedData('EXT:dlf/Resources/Private/Language/locallang_metadata.xlf', $this->siteLanguages[0]->getTypo3Language());
Expand Down Expand Up @@ -344,7 +345,7 @@
public function addStructureAction(): void
{
// Include structure definition file.
$structureDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/StructureDefaults.php');
$structureDefaults = $this->getRecords('Structure');

// load language file in own array
$structureLabels = $this->languageFactory->getParsedData('EXT:dlf/Resources/Private/Language/locallang_structure.xlf', $this->siteLanguages[0]->getTypo3Language());
Expand Down Expand Up @@ -426,15 +427,15 @@
$this->forward('error');
}

$formatsDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/FormatDefaults.php');
$formatsDefaults = $this->getRecords('Format');
$recordInfos['formats']['numCurrent'] = $this->formatRepository->countAll();
$recordInfos['formats']['numDefault'] = count($formatsDefaults);

$structuresDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/StructureDefaults.php');
$structuresDefaults = $this->getRecords('Structure');
$recordInfos['structures']['numCurrent'] = $this->structureRepository->countByPid($this->pid);
$recordInfos['structures']['numDefault'] = count($structuresDefaults);

$metadataDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/MetadataDefaults.php');
$metadataDefaults = $this->getRecords('Metadata');
$recordInfos['metadata']['numCurrent'] = $this->metadataRepository->countByPid($this->pid);
$recordInfos['metadata']['numDefault'] = count($metadataDefaults);

Expand All @@ -459,15 +460,15 @@
/**
* Get language label for given key and language.
*
* @access protected
* @access private
*
* @param string $index
* @param string $lang
* @param array $langArray
*
* @return string
*/
protected function getLLL(string $index, string $lang, array $langArray): string
private function getLLL(string $index, string $lang, array $langArray): string

Check notice on line 471 in Classes/Controller/Backend/NewTenantController.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Controller/Backend/NewTenantController.php#L471

Private method name "NewTenantController::getLLL" is not in camel caps format
{
if (isset($langArray[$lang][$index][0]['target'])) {
return $langArray[$lang][$index][0]['target'];
Expand All @@ -477,4 +478,31 @@
return 'Missing translation for ' . $index;
}
}

/**
* Get records from file for given record type.
*
* @access private
*
* @param string $recordType
*
* @return array
*/
private function getRecords(string $recordType): array
{
$filePath = Environment::getPublicPath() . '/typo3conf/ext/dlf/Resources/Private/Data/' . $recordType . 'Defaults.json';

$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$fileObject = $resourceFactory->getFileObjectFromCombinedIdentifier($filePath);

if ($fileObject !== null) {
$fileContents = $fileObject->getContents();
$records = json_decode($fileContents, true);

if (json_last_error() === JSON_ERROR_NONE) {
return $records;
}
}
return [];
}
}
32 changes: 32 additions & 0 deletions Resources/Private/Data/FormatDefaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"MODS": {
"root": "mods",
"namespace": "http://www.loc.gov/mods/v3",
"class": "Kitodo\\Dlf\\Format\\Mods"
},
"TEIHDR": {
"root": "teiHeader",
"namespace": "http://www.tei-c.org/ns/1.0",
"class": "Kitodo\\Dlf\\Format\\TeiHeader"
},
"ALTO": {
"root": "alto",
"namespace": "http://www.loc.gov/standards/alto/ns-v2#",
"class": "Kitodo\\Dlf\\Format\\Alto"
},
"IIIF1": {
"root": "IIIF1",
"namespace": "http://www.shared-canvas.org/ns/context.json",
"class": ""
},
"IIIF2": {
"root": "IIIF2",
"namespace": "http://iiif.io/api/presentation/2/context.json",
"class": ""
},
"IIIF3": {
"root": "IIIF3",
"namespace": "http://iiif.io/api/presentation/3/context.json",
"class": ""
}
}
44 changes: 0 additions & 44 deletions Resources/Private/Data/FormatDefaults.php

This file was deleted.

Loading