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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OC\AppFramework\Middleware\Security;
|
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
2014-05-11 19:55:59 +04:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
|
|
|
|
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
|
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\SecurityException
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* @expectedException \OC\AppFramework\Middleware\Security\SecurityException
|
|
|
|
*/
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
}
|