From 7002c0862175aaa0da74e07576f5ac56b5f05ab3 Mon Sep 17 00:00:00 2001
From: Susanne Moog <look@susi.dev>
Date: Thu, 5 Mar 2020 09:18:06 +0100
Subject: [PATCH] [BUGFIX] Allow "distinct" in select.selectFields
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add "distinct" into the whitelist of allowed select parts.

Resolves: #43114
Releases: master, 9.5
Change-Id: I8ae6561ff6067117b9d5acb6c976966fecaee96a
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/38372
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
---
 .../Classes/ContentObject/ContentObjectRenderer.php      | 2 +-
 .../ContentObject/ContentObjectRendererTest.php          | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index db9eaa8384da..35a5ee99d5ab 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -6707,7 +6707,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
         $matchEnd = '(\\s*,|\\s*$)/';
         $necessaryFields = ['uid', 'pid'];
         $wsFields = ['t3ver_state'];
-        if (isset($GLOBALS['TCA'][$table]) && !preg_match($matchStart . '\\*' . $matchEnd, $selectPart) && !preg_match('/(count|max|min|avg|sum)\\([^\\)]+\\)/i', $selectPart)) {
+        if (isset($GLOBALS['TCA'][$table]) && !preg_match($matchStart . '\\*' . $matchEnd, $selectPart) && !preg_match('/(count|max|min|avg|sum)\\([^\\)]+\\)|distinct/i', $selectPart)) {
             foreach ($necessaryFields as $field) {
                 $match = $matchStart . $field . $matchEnd;
                 if (!preg_match($match, $selectPart)) {
diff --git a/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
index c860e4eb06f7..57cfe1d3a964 100644
--- a/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
@@ -180,6 +180,15 @@ class ContentObjectRendererTest extends FunctionalTestCase
                 [
                     'SELECT' => 'avg(crdate)'
                 ]
+            ],
+            'single distinct, add nothing' => [
+                'tt_content',
+                [
+                    'selectFields' => 'DISTINCT crdate'
+                ],
+                [
+                    'SELECT' => 'DISTINCT crdate'
+                ]
             ]
         ];
 
-- 
GitLab