[BUGFIX] Catch InvalidPathException in BE & FE 12/55212/2
authorGeorg Ringer <georg.ringer@gmail.com>
Mon, 11 Dec 2017 10:09:46 +0000 (11:09 +0100)
committerBenni Mack <benni@typo3.org>
Wed, 27 Dec 2017 11:08:01 +0000 (12:08 +0100)
Catch invalid links in FE & BE to keep a working
website.

Resolves: #83283
Releases: master, 8.7
Change-Id: Id9e42dc9be896f3777210843c651f9726dc94552
Reviewed-on: https://review.typo3.org/55212
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Form/Element/InputLinkElement.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 037a7a7..384db8c 100644 (file)
@@ -20,6 +20,7 @@ use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
 use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException;
+use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\Folder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -320,6 +321,8 @@ class InputLinkElement extends AbstractFormElement
             return $data;
         } catch (UnknownLinkHandlerException $e) {
             return $data;
+        } catch (InvalidPathException $e) {
+            return $data;
         }
 
         // Resolving the TypoLink parts (class, title, params)
index 9ea27ad..6bdf321 100644 (file)
@@ -5676,7 +5676,15 @@ class ContentObjectRenderer
 
         // Detecting kind of link and resolve all necessary parameters
         $linkService = GeneralUtility::makeInstance(LinkService::class);
-        $linkDetails = $linkService->resolve($linkParameter);
+        try {
+            $linkDetails = $linkService->resolve($linkParameter);
+        } catch (Exception\InvalidPathException $exception) {
+            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
+            $logger->warning('The link could not be generated', ['exception' => $exception]);
+
+            return $linkText;
+        }
+
         $linkDetails['typoLinkParameter'] = $linkParameter;
         if (isset($linkDetails['type']) && isset($GLOBALS['TYPO3_CONF_VARS']['FE']['typolinkBuilder'][$linkDetails['type']])) {
             /** @var AbstractTypolinkBuilder $linkBuilder */
index eac11e9..353f0b7 100644 (file)
@@ -14,11 +14,15 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
 use Psr\Log\LoggerInterface;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface as CacheFrontendInterface;
 use TYPO3\CMS\Core\Core\ApplicationContext;
+use TYPO3\CMS\Core\LinkHandling\LinkService;
+use TYPO3\CMS\Core\Log\Logger;
 use TYPO3\CMS\Core\Log\LogManager;
+use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
@@ -3131,6 +3135,36 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     /**
      * @test
      */
+    public function typoLinkReturnsOnlyLinkTextIfNoLinkResolvingIsPossible()
+    {
+        $linkService = $this->prophesize(LinkService::class);
+        GeneralUtility::setSingletonInstance(LinkService::class, $linkService->reveal());
+        $linkService->resolve('foo')->willThrow(InvalidPathException::class);
+
+        $this->assertSame('foo', $this->subject->typoLink('foo', ['parameter' => 'foo']));
+    }
+
+    /**
+     * @test
+     */
+    public function typoLinkLogsErrorIfNoLinkResolvingIsPossible()
+    {
+        $linkService = $this->prophesize(LinkService::class);
+        GeneralUtility::setSingletonInstance(LinkService::class, $linkService->reveal());
+        $linkService->resolve('foo')->willThrow(InvalidPathException::class);
+
+        $logManager = $this->prophesize(LogManager::class);
+        GeneralUtility::setSingletonInstance(LogManager::class, $logManager->reveal());
+        $logger = $this->prophesize(Logger::class);
+        $logger->warning('The link could not be generated', Argument::any())->shouldBeCalled();
+        $logManager->getLogger(Argument::any())->willReturn($logger->reveal());
+
+        $this->subject->typoLink('foo', ['parameter' => 'foo']);
+    }
+
+    /**
+     * @test
+     */
     public function stdWrap_splitObjReturnsCount()
     {
         $conf = [