|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | +namespace In2code\Powermail\Fluid; |
| 4 | + |
| 5 | +use In2code\Powermail\Fluid\Cache\NullFluidCache; |
| 6 | +use In2code\Powermail\Fluid\Parser\NeutralizeModifiersTemplateProcessor; |
| 7 | +use In2code\Powermail\Fluid\ViewHelper\RestrictedViewHelperInvoker; |
| 8 | +use In2code\Powermail\Fluid\ViewHelper\RestrictedViewHelperResolver; |
| 9 | +use In2code\Powermail\Fluid\ViewHelper\ViewHelperPolicy; |
| 10 | +use In2code\Powermail\Utility\ObjectUtility; |
| 11 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 12 | +use TYPO3\CMS\Fluid\View\StandaloneView; |
| 13 | +use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessor\NamespaceDetectionTemplateProcessor; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class RestrictedStringRenderer |
| 17 | + * |
| 18 | + * Renders a string with Fluid so that "{marker}" placeholders are replaced, but only allowlisted |
| 19 | + * ViewHelpers may execute. |
| 20 | + * |
| 21 | + * Background: powermail parses several configured values with Fluid - the mail subject, the receiver |
| 22 | + * name and email, field titles and select options - so that editors can write "{firstname}" or |
| 23 | + * "{f:cObject(typoscriptObjectPath:'lib.x')}" into them. Some of those values can, depending on the |
| 24 | + * mail type, carry data submitted by a website visitor, which turns the whole string into a template |
| 25 | + * an attacker controls. Without a restriction, that allows arbitrary ViewHelper execution from an |
| 26 | + * unauthenticated request. |
| 27 | + * |
| 28 | + * @see ViewHelperPolicy for the allowlist |
| 29 | + * @see RestrictedViewHelperResolver for the parse time gate |
| 30 | + */ |
| 31 | +class RestrictedStringRenderer |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Controller name of the rendering context, and therefore part of the identifier of the compiled |
| 35 | + * template. Keeps compiled templates of restricted renderings separate from the permissive ones. |
| 36 | + */ |
| 37 | + const CACHE_SCOPE_CONTROLLER = 'PowermailRestrictedString'; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string $string |
| 41 | + * @param array $variables |
| 42 | + * @return string |
| 43 | + */ |
| 44 | + public function render(string $string, array $variables = []): string |
| 45 | + { |
| 46 | + if (strpos($string, '{') === false && strpos($string, '<') === false) { |
| 47 | + return $string; |
| 48 | + } |
| 49 | + |
| 50 | + $policy = ViewHelperPolicy::fromExtensionConfiguration(); |
| 51 | + $view = ObjectUtility::getObjectManager()->get(StandaloneView::class); |
| 52 | + if (!$view instanceof StandaloneView) { |
| 53 | + // Another view implementation was registered, so the restriction cannot be installed. |
| 54 | + // Replacing markers without Fluid is the only safe thing left to do. |
| 55 | + return $this->replaceMarkers($string, $variables); |
| 56 | + } |
| 57 | + |
| 58 | + $renderingContext = $view->getRenderingContext(); |
| 59 | + $resolver = new RestrictedViewHelperResolver($renderingContext->getViewHelperResolver(), $policy); |
| 60 | + $renderingContext->setViewHelperResolver($resolver); |
| 61 | + $renderingContext->setViewHelperInvoker(new RestrictedViewHelperInvoker($policy, $resolver)); |
| 62 | + $renderingContext->setTemplateProcessors([ |
| 63 | + GeneralUtility::makeInstance(NeutralizeModifiersTemplateProcessor::class), |
| 64 | + GeneralUtility::makeInstance(NamespaceDetectionTemplateProcessor::class), |
| 65 | + ]); |
| 66 | + $renderingContext->setCache(GeneralUtility::makeInstance(NullFluidCache::class)); |
| 67 | + $renderingContext->setControllerName(self::CACHE_SCOPE_CONTROLLER); |
| 68 | + // the fingerprint invalidates compiled templates when the allowlist changes |
| 69 | + $renderingContext->setControllerAction('Parse' . $policy->getFingerprint()); |
| 70 | + $view->setTemplateSource($string); |
| 71 | + $view->assignMultiple($variables); |
| 72 | + |
| 73 | + try { |
| 74 | + return $view->render() ?? ''; |
| 75 | + } catch (\Throwable $throwable) { |
| 76 | + ObjectUtility::getLogger(self::class)->warning( |
| 77 | + 'powermail could not parse a string with Fluid, Fluid syntax was removed from it', |
| 78 | + [ |
| 79 | + 'exception' => $throwable->getMessage(), |
| 80 | + 'string' => mb_substr($string, 0, 200), |
| 81 | + ] |
| 82 | + ); |
| 83 | + |
| 84 | + return $this->removeFluidSyntax($string); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Fallback that mirrors what Fluid does to a "{marker}" - including the escaping the Escape |
| 90 | + * interceptor would apply - without handing the string to the parser. |
| 91 | + * |
| 92 | + * @param string $string |
| 93 | + * @param array $variables |
| 94 | + * @return string |
| 95 | + */ |
| 96 | + protected function replaceMarkers(string $string, array $variables): string |
| 97 | + { |
| 98 | + $replacements = []; |
| 99 | + foreach ($variables as $marker => $value) { |
| 100 | + if (is_array($value) || is_object($value)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + $replacements['{' . $marker . '}'] = htmlspecialchars((string)$value, ENT_QUOTES); |
| 105 | + } |
| 106 | + |
| 107 | + return strtr($string, $replacements); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Used when parsing failed. Returning the string unchanged would hand the payload straight to the |
| 112 | + * output, and returning an empty string would silently drop mails (an empty subject makes |
| 113 | + * SendMailService::sendMail() return early), so every active Fluid construct is removed instead. |
| 114 | + * |
| 115 | + * @param string $string |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + protected function removeFluidSyntax(string $string): string |
| 119 | + { |
| 120 | + $string = (string)preg_replace('#</?[a-zA-Z0-9]+:[^>]*>#', '', $string); |
| 121 | + // twice, to also catch one level of nested curly braces |
| 122 | + $string = (string)preg_replace('#\{[^{}]*\}#', '', $string); |
| 123 | + $string = (string)preg_replace('#\{[^{}]*\}#', '', $string); |
| 124 | + |
| 125 | + return trim($string); |
| 126 | + } |
| 127 | +} |
0 commit comments