Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Only allow requesting new CSRF tokens if it passes the SameSite Cooki…
…e test

Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
rullzer committed Jan 3, 2020
commit da81b71f9337621a60def04c304cb301321163b7
5 changes: 5 additions & 0 deletions core/Controller/CSRFTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use OC\Security\CSRF\CsrfTokenManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

Expand All @@ -54,6 +55,10 @@ public function __construct(string $appName, IRequest $request,
* @return JSONResponse
*/
public function index(): JSONResponse {
if (!$this->request->passesStrictCookieCheck()) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$requestToken = $this->tokenManager->getToken();

return new JSONResponse([
Expand Down
13 changes: 12 additions & 1 deletion tests/Core/Controller/CSRFTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ protected function setUp(): void {
$this->tokenManager);
}

public function testGetToken() {
public function testGetToken(): void {
$this->request->method('passesStrictCookieCheck')->willReturn(true);

$token = $this->createMock(CsrfToken::class);
$this->tokenManager->method('getToken')->willReturn($token);
$token->method('getEncryptedValue')->willReturn('toktok123');
Expand All @@ -68,4 +70,13 @@ public function testGetToken() {
], $response->getData());
}

public function testGetTokenNoStrictSameSiteCookie(): void {
$this->request->method('passesStrictCookieCheck')->willReturn(false);

$response = $this->controller->index();

$this->assertInstanceOf(JSONResponse::class, $response);
$this->assertSame(Http::STATUS_FORBIDDEN, $response->getStatus());
}

}