Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Allow multiple referrer types in backend main route
With TYPO3-CORE-SA-2020-006 (SSRF via XSS) a strict referrer handling has been introduced to avoid the TYPO3 backend being called from other non same-origin locations. In case a HTTP referrer header was empty the system tried to refresh the view - otherwise the request was denied completely. It turned out that this scenario was probably too strict, disabling feature `security.backend.enforceReferrer` was the only work-around for site administrators. This change adds new options for handling referrers in backend routes: * refresh-empty (existed already): refresh in case referrer is empty * refresh-same-site: refresh in case referrer is on same site, like `https://example.org/?eID=auth` calling `https://example.org/typo3/` * refresh-always: refresh always in case there is not valid referrer TYPO3's main backend route is using `refresh-always` now to be more relaxed on handling same-site and cross-site referrers as well. The term "refreshing" relates to trigger a reload in the browser to get the referrer of the current location. This still block direct CSRF/SSRF requests since the refreshing HTML instructions are delivered back to the client. Besides that, cross-site requests are covered by the `same-site` cookie policy, and existing CSRF tokens. Resolves: #91396 Releases: master, 9.5 Change-Id: Ib3756671fa60c6f41ba992d0e645f03da1730d19 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64492 Tested-by: Susanne Moog <look@susi.dev> Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Richard Haeser <richard@maxserv.com> Reviewed-by: Susanne Moog <look@susi.dev> Reviewed-by: Richard Haeser <richard@maxserv.com>
- Loading branch information
Showing
3 changed files
with
238 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
typo3/sysext/core/Tests/Unit/Http/Security/ReferrerEnforcerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?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\Tests\Unit\Http\Security; | ||
|
||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use TYPO3\CMS\Core\Http\NormalizedParams; | ||
use TYPO3\CMS\Core\Http\Security\InvalidReferrerException; | ||
use TYPO3\CMS\Core\Http\Security\MissingReferrerException; | ||
use TYPO3\CMS\Core\Http\Security\ReferrerEnforcer; | ||
use TYPO3\CMS\Core\Http\Uri; | ||
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; | ||
|
||
class ReferrerEnforcerTest extends UnitTestCase | ||
{ | ||
private static function buildRefreshContentPattern(string $uri): string | ||
{ | ||
return sprintf( | ||
'#.+href="%s\d+" id="referrer-refresh".+#', | ||
preg_quote(htmlspecialchars($uri . '&referrer-refresh='), '#') | ||
); | ||
} | ||
|
||
public function validReferrerIsHandledDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', // requestUri | ||
'https://example.org/typo3/index.php', // referrer | ||
null, // options | ||
null, // response | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'', | ||
['flags' => ['refresh-empty']], | ||
self::buildRefreshContentPattern( | ||
'https://example.org/typo3/index.php?route=%2Flogin' | ||
), | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'https://example.org/?eID=handler', | ||
['flags' => ['refresh-same-site']], | ||
self::buildRefreshContentPattern( | ||
'https://example.org/typo3/index.php?route=%2Flogin' | ||
), | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'https://other-example.site/security/', | ||
['flags' => ['refresh-always']], | ||
self::buildRefreshContentPattern( | ||
'https://example.org/typo3/index.php?route=%2Flogin' | ||
), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $requestUri | ||
* @param string $referrer | ||
* @param string[]|null $options | ||
* @param string|null $expectedResponse | ||
* | ||
* @test | ||
* @dataProvider validReferrerIsHandledDataProvider | ||
*/ | ||
public function validReferrerIsHandled(string $requestUri, string $referrer, ?array $options, ?string $expectedResponse): void | ||
{ | ||
$subject = $this->buildSubject($requestUri, $referrer); | ||
$response = $subject->handle($options); | ||
|
||
if ($expectedResponse === null) { | ||
self::assertNull($response); | ||
} else { | ||
self::assertRegExp($expectedResponse, (string)$response->getBody()); | ||
} | ||
} | ||
|
||
public function invalidReferrerIsHandledDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', // requestUri | ||
'https://example.org/?eID=handler', // referrer | ||
null, // options | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'https://example.org/?eID=handler', | ||
['flags' => ['refresh-empty']], | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'https://example.org.security/?eID=handler', | ||
['flags' => ['refresh-same-site']], | ||
], | ||
[ | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'https://other-example.site/security/', | ||
null, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $requestUri | ||
* @param string $referrer | ||
* @param string[]|null $options | ||
* | ||
* @test | ||
* @dataProvider invalidReferrerIsHandledDataProvider | ||
*/ | ||
public function invalidReferrerIsHandled(string $requestUri, string $referrer, ?array $options): void | ||
{ | ||
$this->expectException(InvalidReferrerException::class); | ||
$this->expectExceptionCode(1588095936); | ||
$subject = $this->buildSubject($requestUri, $referrer); | ||
$subject->handle($options); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function missingReferrerIsHandled(): void | ||
{ | ||
$this->expectException(MissingReferrerException::class); | ||
$this->expectExceptionCode(1588095935); | ||
$subject = $this->buildSubject( | ||
'https://example.org/typo3/index.php?route=%2Flogin', | ||
'' | ||
); | ||
$subject->handle(); | ||
} | ||
|
||
private function buildSubject(string $requestUri, string $referrer): ReferrerEnforcer | ||
{ | ||
$requestUriInstance = new Uri($requestUri); | ||
$host = sprintf( | ||
'%s://%s', | ||
$requestUriInstance->getScheme(), | ||
$requestUriInstance->getHost() | ||
); | ||
$dir = $host . rtrim(dirname($requestUriInstance->getPath()), '/') . '/'; | ||
parse_str($requestUriInstance->getQuery(), $queryParams); | ||
|
||
/** @var NormalizedParams|ObjectProphecy $normalizedParams */ | ||
$normalizedParams = $this->prophesize(NormalizedParams::class); | ||
$normalizedParams->getRequestHost()->willReturn($host); | ||
$normalizedParams->getRequestDir()->willReturn($dir); | ||
/** @var ServerRequestInterface|ObjectProphecy $request */ | ||
$request = $this->prophesize(ServerRequestInterface::class); | ||
$request->getAttribute('normalizedParams')->willReturn($normalizedParams); | ||
$request->getServerParams()->willReturn(['HTTP_REFERER' => $referrer]); | ||
$request->getUri()->willReturn($requestUriInstance); | ||
$request->getQueryParams()->willReturn($queryParams); | ||
|
||
$subject = $this->getMockBuilder(ReferrerEnforcer::class) | ||
->setConstructorArgs([$request->reveal()]) | ||
->onlyMethods(['resolveAbsoluteWebPath']) | ||
->getMock(); | ||
$subject->method('resolveAbsoluteWebPath') | ||
->with('EXT:core/Resources/Public/JavaScript/ReferrerRefresh.js') | ||
->willReturn('/typo3/sysext/core/Resources/Public/JavaScript/ReferrerRefresh.js'); | ||
return $subject; | ||
} | ||
} |