Skip to content

Commit 1d40157

Browse files
[BUGFIX] Ignore scalar plugin arguments instead of throwing a TypeError
A request like "?tx_powermail_pi1=foo" on a page with a powermail form resulted in an HTTP 500, because FrontendUtility::getArgumentsFromGetOrPostRequest() returned the string although its return type is array. The same value reached getArgumentsFromTyposcriptFrontendController() via the routing arguments and was cast to [0 => 'foo']. Plugin arguments are now only accepted if they are arrays. In addition, CaptchaValidator no longer crashes on a scalar or missing tx_powermail_pi1 query argument. Related: https://projekte.in2code.de/issues/85254
1 parent 1e66d18 commit 1d40157

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎Classes/Domain/Validator/CaptchaValidator.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function setCaptchaArgument(bool $captchaArgument): void
136136
public function __construct()
137137
{
138138
// clear captcha only on create action
139-
$pluginVariables = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_powermail_pi1'];
140-
$this->setClearSession($pluginVariables['action'] === 'create');
139+
$pluginVariables = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_powermail_pi1'] ?? [];
140+
$this->setClearSession(is_array($pluginVariables) && ($pluginVariables['action'] ?? '') === 'create');
141141
}
142142
}

‎Classes/Utility/FrontendUtility.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,28 @@ public static function getArguments(string $key = 'tx_powermail_pi1'): array
264264
);
265265
}
266266

267+
/**
268+
* Arguments are user input and could be scalar (e.g. "?tx_powermail_pi1=foo"), so only accept arrays
269+
*/
267270
protected static function getArgumentsFromGetOrPostRequest(string $key): array
268271
{
269-
return $GLOBALS['TYPO3_REQUEST']->getParsedBody()[$key] ?? $GLOBALS['TYPO3_REQUEST']->getQueryParams()[$key] ?? [];
272+
$arguments = $GLOBALS['TYPO3_REQUEST']->getParsedBody()[$key]
273+
?? $GLOBALS['TYPO3_REQUEST']->getQueryParams()[$key]
274+
?? [];
275+
return is_array($arguments) ? $arguments : [];
270276
}
271277

278+
/**
279+
* Remaining query arguments are part of the routing arguments, so they could be scalar as well
280+
*/
272281
protected static function getArgumentsFromTyposcriptFrontendController(string $key): array
273282
{
274283
$request = self::getRequest();
275284
if ($request !== null) {
276285
$pageArguments = $request->getAttribute('routing');
277286
$arguments = $pageArguments->getArguments();
278-
if (array_key_exists($key, $arguments)) {
279-
return (array)$arguments[$key];
287+
if (is_array($arguments[$key] ?? null)) {
288+
return $arguments[$key];
280289
}
281290
}
282291
return [];

‎Tests/Unit/Utility/FrontendUtilityTest.php‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use In2code\Powermail\Tests\Helper\TestingHelper;
66
use In2code\Powermail\Utility\FrontendUtility;
7+
use TYPO3\CMS\Core\Http\ServerRequest;
8+
use TYPO3\CMS\Core\Routing\PageArguments;
79
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
810

911
/**
@@ -18,6 +20,12 @@ public function setUp(): void
1820
TestingHelper::setDefaultConstants();
1921
}
2022

23+
public function tearDown(): void
24+
{
25+
unset($GLOBALS['TYPO3_REQUEST']);
26+
parent::tearDown();
27+
}
28+
2129
/**
2230
* Data Provider for getDomainFromUriReturnsString()
2331
*/
@@ -172,4 +180,55 @@ public function getSubFolderOfCurrentUrlReturnsString($leadingSlash, $trailingSl
172180
$result = FrontendUtility::getSubFolderOfCurrentUrl($leadingSlash, $trailingSlash, $host, $url);
173181
self::assertSame($expectedResult, $result);
174182
}
183+
184+
/**
185+
* Data Provider for getArgumentsReturnsArray()
186+
*/
187+
public static function getArgumentsReturnsArrayDataProvider(): array
188+
{
189+
return [
190+
'no arguments' => [
191+
[],
192+
null,
193+
[],
194+
],
195+
'array in query' => [
196+
['tx_powermail_pi1' => ['field' => ['name' => 'Alex']]],
197+
null,
198+
['field' => ['name' => 'Alex']],
199+
],
200+
'string in query' => [
201+
['tx_powermail_pi1' => 'foo'],
202+
null,
203+
[],
204+
],
205+
'array in body' => [
206+
[],
207+
['tx_powermail_pi1' => ['field' => ['name' => 'Alex']]],
208+
['field' => ['name' => 'Alex']],
209+
],
210+
'string in body' => [
211+
[],
212+
['tx_powermail_pi1' => 'foo'],
213+
[],
214+
],
215+
];
216+
}
217+
218+
/**
219+
* @param array $queryParams
220+
* @param array|null $parsedBody
221+
* @param array $expectedResult
222+
* @dataProvider getArgumentsReturnsArrayDataProvider
223+
* @test
224+
* @covers ::getArguments
225+
*/
226+
public function getArgumentsReturnsArray(array $queryParams, ?array $parsedBody, array $expectedResult): void
227+
{
228+
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())
229+
->withQueryParams($queryParams)
230+
->withParsedBody($parsedBody)
231+
->withAttribute('routing', new PageArguments(1, '0', [], [], $queryParams));
232+
self::assertSame($expectedResult, FrontendUtility::getArguments());
233+
}
175234
}

0 commit comments

Comments
 (0)