2014-05-08 13:47:18 +04: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 19:40:34 +03:00
|
|
|
namespace Test\AppFramework\Middleware\Security;
|
2014-05-08 13:47:18 +04:00
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
2016-05-18 19:40:34 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\CORSMiddleware;
|
2014-05-11 19:55:59 +04:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2015-11-28 13:06:46 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
|
2015-07-20 13:54:22 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-05-08 13:47:18 +04:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class CORSMiddlewareTest extends \Test\TestCase {
|
2014-05-08 13:47:18 +04:00
|
|
|
|
2014-05-11 19:55:59 +04:00
|
|
|
private $reflector;
|
2015-05-22 14:17:27 +03:00
|
|
|
private $session;
|
2014-05-11 19:55:59 +04:00
|
|
|
|
|
|
|
protected function setUp() {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector = new ControllerMethodReflector();
|
2015-05-22 14:17:27 +03:00
|
|
|
$this->session = $this->getMock('\OCP\IUserSession');
|
2014-05-11 19:55:59 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
*/
|
|
|
|
public function testSetCORSAPIHeader() {
|
|
|
|
$request = new Request(
|
2015-02-09 13:41:48 +03:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2015-02-10 15:02:48 +03:00
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
2014-05-08 13:47:18 +04:00
|
|
|
);
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2015-05-22 14:17:27 +03:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
2014-05-08 13:47:18 +04: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 13:41:48 +03:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2015-02-10 15:02:48 +03:00
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
2014-05-08 13:47:18 +04:00
|
|
|
);
|
2015-05-22 14:17:27 +03:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
2014-05-08 13:47:18 +04: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 15:02:48 +03:00
|
|
|
$request = new Request(
|
|
|
|
[],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2015-05-22 14:17:27 +03:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
2014-05-08 13:47:18 +04: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 13:06:46 +03:00
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
|
2014-05-08 13:47:18 +04:00
|
|
|
*/
|
|
|
|
public function testCorsIgnoredIfWithCredentialsHeaderPresent() {
|
|
|
|
$request = new Request(
|
2015-02-09 13:41:48 +03:00
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'HTTP_ORIGIN' => 'test'
|
|
|
|
]
|
|
|
|
],
|
2015-02-10 15:02:48 +03:00
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
2014-05-08 13:47:18 +04:00
|
|
|
);
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2015-05-22 14:17:27 +03:00
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
2014-05-08 13:47:18 +04:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->addHeader('AcCess-control-Allow-Credentials ', 'TRUE');
|
2015-02-10 15:02:48 +03:00
|
|
|
$middleware->afterController($this, __FUNCTION__, $response);
|
2014-05-08 13:47:18 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:17:27 +03:00
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function testNoCORSShouldAllowCookieAuth() {
|
|
|
|
$request = new Request(
|
|
|
|
[],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
2015-06-03 13:56:50 +03:00
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('login')
|
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
2015-05-22 14:17:27 +03:00
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @CORS
|
|
|
|
*/
|
|
|
|
public function testCORSShouldRelogin() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('login')
|
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @CORS
|
2015-11-28 13:06:46 +03:00
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
|
2015-05-22 14:17:27 +03:00
|
|
|
*/
|
|
|
|
public function testCORSShouldNotAllowCookieAuth() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('login')
|
|
|
|
->with($this->equalTo('user'), $this->equalTo('pass'))
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->reflector->reflect($this, __FUNCTION__);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
|
|
|
|
|
|
|
$middleware->beforeController($this, __FUNCTION__, new Response());
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:54:22 +03:00
|
|
|
public function testAfterExceptionWithSecurityExceptionNoStatus() {
|
|
|
|
$request = new Request(
|
|
|
|
['server' => [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'pass'
|
|
|
|
]],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
|
|
|
$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'
|
|
|
|
]],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
|
|
|
$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'
|
|
|
|
]],
|
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
|
|
|
);
|
|
|
|
$middleware = new CORSMiddleware($request, $this->reflector, $this->session);
|
|
|
|
$middleware->afterException($this, __FUNCTION__, new \Exception('A regular exception'));
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
}
|