2014-05-08 09:47:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
|
|
|
* @copyright Bernhard Posselt 2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-05-18 16:40:34 +00:00
|
|
|
namespace Test\AppFramework\Middleware\Security;
|
2014-05-08 09:47:18 +00:00
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
2016-05-18 16:40:34 +00:00
|
|
|
use OC\AppFramework\Middleware\Security\CORSMiddleware;
|
2014-05-11 15:55:59 +00:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2015-11-28 10:06:46 +00:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
|
2016-07-20 16:36:15 +00:00
|
|
|
use OC\Security\Bruteforce\Throttler;
|
2015-07-20 10:54:22 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-05-08 09:47:18 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
class CORSMiddlewareTest extends \Test\TestCase {
|
2014-05-08 09:47:18 +00:00
|
|
|
|
2014-05-11 15:55:59 +00:00
|
|
|
private $reflector;
|
2015-05-22 11:17:27 +00:00
|
|
|
private $session;
|
2016-07-20 16:36:15 +00:00
|
|
|
/** @var Throttler */
|
|
|
|
private $throttler;
|
2014-05-11 15:55:59 +00:00
|
|
|
|
|
|
|
protected function setUp() {
|
2014-11-10 22:30:38 +00:00
|
|
|
parent::setUp();
|
2014-05-11 15:55:59 +00:00
|
|
|
$this->reflector = new ControllerMethodReflector();
|
2016-05-25 07:58:01 +00:00
|
|
|
$this->session = $this->getMockBuilder('\OC\User\Session')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-07-20 16:36:15 +00:00
|
|
|
$this->throttler = $this->getMockBuilder('\OC\Security\Bruteforce\Throttler')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2014-05-11 15:55:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 09:47:18 +00:00
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
*/
|
|
|
|
public function testSetCORSAPIHeader() {
|
|
|
|
$request = new Request(
|
2015-02-09 10:41:48 +00:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2014-05-08 09:47:18 +00:00
|
|
|
);
|
2014-05-11 15:55:59 +00:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2014-05-08 09:47:18 +00:00
|
|
|
|
|
|
|
$response = $middleware->afterController($this, __FUNCTION__, new Response());
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testNoAnnotationNoCORSHEADER() {
|
|
|
|
$request = new Request(
|
2015-02-09 10:41:48 +00:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2014-05-08 09:47:18 +00:00
|
|
|
);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2014-05-08 09:47:18 +00:00
|
|
|
|
|
|
|
$response = $middleware->afterController($this, __FUNCTION__, new Response());
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
*/
|
|
|
|
public function testNoOriginHeaderNoCORSHEADER() {
|
2015-02-10 12:02:48 +00:00
|
|
|
$request = new Request(
|
|
|
|
[],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-02-10 12:02:48 +00:00
|
|
|
);
|
2014-05-11 15:55:59 +00:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2014-05-08 09:47:18 +00:00
|
|
|
|
|
|
|
$response = $middleware->afterController($this, __FUNCTION__, new Response());
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @CORS
|
2015-11-28 10:06:46 +00:00
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
|
2014-05-08 09:47:18 +00:00
|
|
|
*/
|
|
|
|
public function testCorsIgnoredIfWithCredentialsHeaderPresent() {
|
|
|
|
$request = new Request(
|
2015-02-09 10:41:48 +00:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2014-05-08 09:47:18 +00:00
|
|
|
);
|
2014-05-11 15:55:59 +00:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2014-05-08 09:47:18 +00:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->addHeader('AcCess-control-Allow-Credentials ', 'TRUE');
|
2015-02-10 12:02:48 +00:00
|
|
|
$middleware->afterController($this, __FUNCTION__, $response);
|
2014-05-08 09:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:17:27 +00:00
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function testNoCORSShouldAllowCookieAuth() {
|
|
|
|
$request = new Request(
|
|
|
|
[],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-05-22 11:17:27 +00:00
|
|
|
);
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-06-03 10:56:50 +00:00
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->never())
|
2016-05-25 07:58:01 +00:00
|
|
|
->method('logClientIn')
|
2015-06-03 10:56:50 +00:00
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2015-05-22 11:17:27 +00:00
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
*/
|
|
|
|
public function testCORSShouldRelogin() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-05-22 11:17:27 +00:00
|
|
|
);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->once())
|
2016-05-25 07:58:01 +00:00
|
|
|
->method('logClientIn')
|
2015-05-22 11:17:27 +00:00
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-05-22 11:17:27 +00:00
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
2016-06-17 09:01:35 +00:00
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
|
|
|
|
*/
|
|
|
|
public function testCORSShouldFailIfPasswordLoginIsForbidden() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2016-06-17 09:01:35 +00:00
|
|
|
);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logClientIn')
|
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->throwException(new \OC\Authentication\Exceptions\PasswordLoginForbiddenException));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2016-06-17 09:01:35 +00:00
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
2015-05-22 11:17:27 +00:00
|
|
|
/**
|
|
|
|
* @CORS
|
2015-11-28 10:06:46 +00:00
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
|
2015-05-22 11:17:27 +00:00
|
|
|
*/
|
|
|
|
public function testCORSShouldNotAllowCookieAuth() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-05-22 11:17:27 +00:00
|
|
|
);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->once())
|
2016-05-25 07:58:01 +00:00
|
|
|
->method('logClientIn')
|
2015-05-22 11:17:27 +00:00
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-05-22 11:17:27 +00:00
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
2015-07-20 10:54:22 +00:00
|
|
|
public function testAfterExceptionWithSecurityExceptionNoStatus() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-07-20 10:54:22 +00:00
|
|
|
);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-07-20 10:54:22 +00:00
|
|
|
$response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception'));
|
|
|
|
|
|
|
|
$expected = new JSONResponse(['message' => 'A security exception'], 500);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterExceptionWithSecurityExceptionWithStatus() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-07-20 10:54:22 +00:00
|
|
|
);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-07-20 10:54:22 +00:00
|
|
|
$response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception', 501));
|
|
|
|
|
|
|
|
$expected = new JSONResponse(['message' => 'A security exception'], 501);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage A regular exception
|
|
|
|
*/
|
|
|
|
public function testAfterExceptionWithRegularException() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
2016-07-10 12:17:26 +00:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
|
|
|
$this->getMockBuilder('\OCP\IConfig')->getMock()
|
2015-07-20 10:54:22 +00:00
|
|
|
);
|
2016-07-20 16:36:15 +00:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
|
2015-07-20 10:54:22 +00:00
|
|
|
$middleware->afterException($this, __FUNCTION__, new \Exception('A regular exception'));
|
|
|
|
}
|
|
|
|
|
2014-05-08 09:47:18 +00:00
|
|
|
}
|