‪TYPO3CMS  9.5
FileDumpController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
27 
32 {
44  public function ‪dumpAction(ServerRequestInterface $request): ResponseInterface
45  {
46  $parameters = ['eID' => 'dumpFile'];
47  $t = $this->‪getGetOrPost($request, 't');
48  if ($t) {
49  $parameters['t'] = $t;
50  }
51  $f = $this->‪getGetOrPost($request, 'f');
52  if ($f) {
53  $parameters['f'] = $f;
54  }
55  $p = $this->‪getGetOrPost($request, 'p');
56  if ($p) {
57  $parameters['p'] = $p;
58  }
59 
60  if (hash_equals(GeneralUtility::hmac(implode('|', $parameters), 'resourceStorageDumpFile'), $this->‪getGetOrPost($request, 'token'))) {
61  if (isset($parameters['f'])) {
62  try {
63  $file = ‪ResourceFactory::getInstance()->‪getFileObject($parameters['f']);
64  if ($file->isDeleted() || $file->isMissing() || !$this->isFileValid($file)) {
65  $file = null;
66  }
67  } catch (\‪Exception $e) {
68  $file = null;
69  }
70  } else {
71  $file = GeneralUtility::makeInstance(ProcessedFileRepository::class)->findByUid($parameters['p']);
72  if (!$file || $file->isDeleted() || !$this->isFileValid($file->getOriginalFile())) {
73  $file = null;
74  }
75  }
76 
77  if ($file === null) {
78  return (new ‪Response)->withStatus(404);
79  }
80 
81  // Hook: allow some other process to do some security/access checks. Hook should return 403 response if access is rejected, void otherwise
82  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess'] ?? [] as $className) {
83  $hookObject = GeneralUtility::makeInstance($className);
84  if (!$hookObject instanceof ‪FileDumpEIDHookInterface) {
85  throw new \UnexpectedValueException($className . ' must implement interface ' . FileDumpEIDHookInterface::class, 1394442417);
86  }
87  $response = $hookObject->checkFileAccess($file);
88  if ($response instanceof ResponseInterface) {
89  return $response;
90  }
91  }
92 
93  return $file->getStorage()->streamFile($file);
94  }
95  return (new ‪Response)->withStatus(403);
96  }
97 
103  protected function ‪getGetOrPost(ServerRequestInterface $request, string $parameter): string
104  {
105  return (string)($request->getParsedBody()[$parameter] ?? $request->getQueryParams()[$parameter] ?? '');
106  }
107 
108  protected function ‪isFileValid(‪FileInterface $file): bool
109  {
110  return $file->‪getStorage()->‪getDriverType() !== 'Local'
111  || GeneralUtility::verifyFilenameAgainstDenyPattern(basename($file->‪getIdentifier()));
112  }
113 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:29
‪TYPO3\CMS\Core\Resource\Hook\FileDumpEIDHookInterface
Definition: FileDumpEIDHookInterface.php:24
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Controller\FileDumpController\getGetOrPost
‪string getGetOrPost(ServerRequestInterface $request, string $parameter)
Definition: FileDumpController.php:103
‪TYPO3\CMS\Core\Resource\ResourceStorage\getDriverType
‪string getDriverType()
Definition: ResourceStorage.php:3133
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Controller\FileDumpController\isFileValid
‪isFileValid(FileInterface $file)
Definition: FileDumpController.php:108
‪TYPO3\CMS\Core\Controller
Definition: ErrorPageController.php:3
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\ResourceInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Resource\ResourceInterface\getStorage
‪ResourceStorage getStorage()
‪TYPO3\CMS\Core\Controller\FileDumpController\dumpAction
‪ResponseInterface dumpAction(ServerRequestInterface $request)
Definition: FileDumpController.php:44
‪TYPO3\CMS\Core\Controller\FileDumpController
Definition: FileDumpController.php:32
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:399
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45