Skip to content

Commit

Permalink
[BUGFIX] Throw dedicated exception for invalid request URL on CLI
Browse files Browse the repository at this point in the history
Usage of ServerRequestFactory::fromGlobals() on
CLI is discouraged as the request url, used to
create the request is invalid, except the
$_SERVER variable is adjusted beforehand.

To properly inform the developer, a dedicated
exception is now being thrown, if in CLI context.

Resolves: #102533
Releases: main, 12.4
Change-Id: Ie84fdf9c0555eeeaf5438a2ed29acabbe2755965
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82015
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
  • Loading branch information
o-ba committed Nov 29, 2023
1 parent 054600d commit dffc2cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
25 changes: 25 additions & 0 deletions typo3/sysext/core/Classes/Http/InvalidRequestUrlOnCliException.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Core\Http;

/**
* Exception is thrown in case an invalid request url is used in CLI context
*
* @internal
*/
class InvalidRequestUrlOnCliException extends \InvalidArgumentException {}
13 changes: 12 additions & 1 deletion typo3/sysext/core/Classes/Http/ServerRequestFactory.php
Expand Up @@ -21,6 +21,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand Down Expand Up @@ -61,7 +62,17 @@ public static function fromGlobals()
$headers = static::prepareHeaders($serverParameters);

$method = $serverParameters['REQUEST_METHOD'] ?? 'GET';
$uri = new Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
try {
$uri = new Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
} catch (\InvalidArgumentException $e) {
if (Environment::isCli()) {
throw new InvalidRequestUrlOnCliException(
'Usage of ' . __METHOD__ . ' on CLI is discouraged. In case you rely on the method, you have to fake a valid request URL using $_SERVER.',
1701105725
);
}
throw $e;
}

$request = new ServerRequest(
$uri,
Expand Down

0 comments on commit dffc2cf

Please sign in to comment.