Merge branch 'bugfix/cookie-support-7383'

fixes #7383
This commit is contained in:
Alexander Fuhr 2015-08-13 11:24:25 +02:00
commit 474fd7b352
3 changed files with 113 additions and 0 deletions

View File

@ -6,6 +6,7 @@
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Forms\Authentication\LoginForm; use Icinga\Forms\Authentication\LoginForm;
use Icinga\Web\Controller; use Icinga\Web\Controller;
use Icinga\Web\Cookie;
use Icinga\Web\Url; use Icinga\Web\Url;
/** /**
@ -37,6 +38,11 @@ class AuthenticationController extends Controller
$this->redirectNow($form->getRedirectUrl()); $this->redirectNow($form->getRedirectUrl());
} }
if (! $requiresSetup) { if (! $requiresSetup) {
if (! $this->getRequest()->hasCookieSupport()) {
echo $this->translate("Cookies must be enabled to run this application.\n");
$this->getResponse()->setHttpResponseCode(403)->sendHeaders();
exit();
}
$form->handleRequest(); $form->handleRequest();
} }
$this->view->form = $form; $this->view->form = $form;

View File

@ -0,0 +1,79 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web;
/**
* Helper Class Cookie
*/
class Cookie
{
/**
* The name of the control cookie
*/
const CHECK_COOKIE = '_chc';
/**
* The request
*
* @var Request
*/
protected $request;
/**
* Create a new cookie
*
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Check whether cookies are supported or not
*
* @return bool
*/
public function isSupported()
{
if (! empty($_COOKIE)) {
$this->cleanupCheck();
return true;
}
$url = $this->request->getUrl();
if ($url->hasParam('_checkCookie') && empty($_COOKIE)) {
return false;
}
if (! $url->hasParam('_checkCookie')) {
$this->provideCheck();
}
return false;
}
/**
* Prepare check to detect cookie support
*/
public function provideCheck()
{
setcookie(self::CHECK_COOKIE, '1');
$requestUri = $this->request->getUrl()->addParams(array('_checkCookie' => 1));
$this->request->getResponse()->redirectAndExit($requestUri);
}
/**
* Cleanup the cookie support check
*/
public function cleanupCheck()
{
if ($this->request->getUrl()->hasParam('_checkCookie') && isset($_COOKIE[self::CHECK_COOKIE])) {
$requestUri =$this->request->getUrl()->without('_checkCookie');
$this->request->getResponse()->redirectAndExit($requestUri);
}
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Web; namespace Icinga\Web;
use Icinga\Application\Icinga;
use Zend_Controller_Request_Http; use Zend_Controller_Request_Http;
use Icinga\User; use Icinga\User;
@ -32,6 +33,13 @@ class Request extends Zend_Controller_Request_Http
*/ */
protected $url; protected $url;
/**
* Response
*
* @var Response
*/
protected $response;
/** /**
* Get whether the request seems to be an API request * Get whether the request seems to be an API request
* *
@ -78,6 +86,20 @@ class Request extends Zend_Controller_Request_Http
return $this; return $this;
} }
/**
* Get the response
*
* @return Response
*/
public function getResponse()
{
if ($this->response === null) {
$this->response = Icinga::app()->getResponse();
}
return $this->response;
}
/** /**
* Makes an ID unique to this request, to prevent id collisions in different containers * Makes an ID unique to this request, to prevent id collisions in different containers
* *
@ -96,4 +118,10 @@ class Request extends Zend_Controller_Request_Http
} }
return $id . '-' . $this->uniqueId; return $id . '-' . $this->uniqueId;
} }
public function hasCookieSupport()
{
$cookie = new Cookie($this);
return $cookie->isSupported();
}
} }