You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nextcloud-server/tests/lib/Security/CSRF/CsrfTokenManagerTest.php

146 lines
4.1 KiB

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace Test\Security\CSRF;
class CsrfTokenManagerTest extends \Test\TestCase {
/** @var \OC\Security\CSRF\CsrfTokenManager */
private $csrfTokenManager;
/** @var \OC\Security\CSRF\CsrfTokenGenerator */
private $tokenGenerator;
/** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
private $storageInterface;
protected function setUp(): void {
parent::setUp();
$this->tokenGenerator = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenGenerator')
->disableOriginalConstructor()->getMock();
$this->storageInterface = $this->getMockBuilder('\OC\Security\CSRF\TokenStorage\SessionStorage')
->disableOriginalConstructor()->getMock();
$this->csrfTokenManager = new \OC\Security\CSRF\CsrfTokenManager(
$this->tokenGenerator,
$this->storageInterface
);
}
public function testGetTokenWithExistingToken(): void {
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(true);
$this->storageInterface
->expects($this->once())
->method('getToken')
->willReturn('MyExistingToken');
$expected = new \OC\Security\CSRF\CsrfToken('MyExistingToken');
$this->assertEquals($expected, $this->csrfTokenManager->getToken());
}
public function testGetTokenWithExistingTokenKeepsOnSecondRequest(): void {
Add support for CSP nonces CSP nonces are a feature available with CSP v2. Basically instead of saying "JS resources from the same domain are ok to be served" we now say "Ressources from everywhere are allowed as long as they add a `nonce` attribute to the script tag with the right nonce. At the moment the nonce is basically just a `<?php p(base64_encode($_['requesttoken'])) ?>`, we have to decode the requesttoken since `:` is not an allowed value in the nonce. So if somebody does on their own include JS files (instead of using the `addScript` public API, they now must also include that attribute.) IE does currently not implement CSP v2, thus there is a whitelist included that delivers the new CSP v2 policy to newer browsers. Check http://caniuse.com/#feat=contentsecuritypolicy2 for the current browser support list. An alternative approach would be to just add `'unsafe-inline'` as well as `'unsafe-inline'` is ignored by CSPv2 when a nonce is set. But this would make this security feature unusable at all in IE. Not worth it at the moment IMO. Implementing this offers the following advantages: 1. **Security:** As we host resources from the same domain by design we don't have to worry about 'self' anymore being in the whitelist 2. **Performance:** We can move oc.js again to inline JS. This makes the loading way quicker as we don't have to load on every load of a new web page a blocking dynamically non-cached JavaScript file. If you want to toy with CSP see also https://csp-evaluator.withgoogle.com/ Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
9 years ago
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(true);
$this->storageInterface
->expects($this->once())
->method('getToken')
->willReturn('MyExistingToken');
$expected = new \OC\Security\CSRF\CsrfToken('MyExistingToken');
$token = $this->csrfTokenManager->getToken();
$this->assertSame($token, $this->csrfTokenManager->getToken());
$this->assertSame($token, $this->csrfTokenManager->getToken());
}
public function testGetTokenWithoutExistingToken(): void {
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(false);
$this->tokenGenerator
->expects($this->once())
->method('generateToken')
->willReturn('MyNewToken');
$this->storageInterface
->expects($this->once())
->method('setToken')
->with('MyNewToken');
$expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
$this->assertEquals($expected, $this->csrfTokenManager->getToken());
}
public function testRefreshToken(): void {
$this->tokenGenerator
->expects($this->once())
->method('generateToken')
->willReturn('MyNewToken');
$this->storageInterface
->expects($this->once())
->method('setToken')
->with('MyNewToken');
$expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
$this->assertEquals($expected, $this->csrfTokenManager->refreshToken());
}
public function testRemoveToken(): void {
$this->storageInterface
->expects($this->once())
->method('removeToken');
$this->csrfTokenManager->removeToken();
}
public function testIsTokenValidWithoutToken(): void {
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(false);
$token = new \OC\Security\CSRF\CsrfToken('Token');
$this->assertSame(false, $this->csrfTokenManager->isTokenValid($token));
}
public function testIsTokenValidWithWrongToken(): void {
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(true);
$token = new \OC\Security\CSRF\CsrfToken('Token');
$this->storageInterface
->expects($this->once())
->method('getToken')
->willReturn('MyToken');
$this->assertSame(false, $this->csrfTokenManager->isTokenValid($token));
}
public function testIsTokenValidWithValidToken(): void {
$a = 'abc';
$b = 'def';
$xorB64 = 'BQcF';
$tokenVal = sprintf('%s:%s', $xorB64, base64_encode($a));
$this->storageInterface
->expects($this->once())
->method('hasToken')
->willReturn(true);
$token = new \OC\Security\CSRF\CsrfToken($tokenVal);
$this->storageInterface
->expects($this->once())
->method('getToken')
->willReturn($b);
$this->assertSame(true, $this->csrfTokenManager->isTokenValid($token));
}
}