2013-07-15 13:58:09 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring frontends
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
2013-07-24 10:56:41 +02:00
|
|
|
use Icinga\Web\Form\InvalidCSRFTokenException;
|
2013-07-23 17:09:06 +02:00
|
|
|
use Zend_Form_Exception;
|
2013-07-16 15:39:47 +02:00
|
|
|
use Zend_View_Interface;
|
2013-07-15 13:58:09 +02:00
|
|
|
|
2013-07-15 14:32:18 +02:00
|
|
|
/**
|
|
|
|
* Class Form
|
|
|
|
*
|
|
|
|
* How forms are used in Icinga 2 Web
|
|
|
|
*/
|
2013-07-15 13:58:09 +02:00
|
|
|
abstract class Form extends \Zend_Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The form's request object
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
private $request = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this form should NOT add random generated "challenge" tokens that are associated
|
|
|
|
* with the user's current session in order to prevent Cross-Site Request Forgery (CSRF).
|
|
|
|
* It is the form's responsibility to verify the existence and correctness of this token
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $tokenDisabled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the CSRF token element (used to create non-colliding hashes)
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $tokenElementName = 'CSRFToken';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time to live for the CRSF token
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $tokenTimeout = 300;
|
|
|
|
|
2013-07-18 10:32:53 +02:00
|
|
|
/**
|
|
|
|
* Flag to indicate that form is already build
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $created = false;
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
/**
|
|
|
|
* Session id required for CSRF token generation
|
|
|
|
* @var numeric|bool
|
|
|
|
*/
|
|
|
|
private $sessionId = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the session ID stored in this form instance
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getSessionId()
|
|
|
|
{
|
|
|
|
if (!$this->sessionId) {
|
|
|
|
$this->sessionId = session_id();
|
|
|
|
}
|
|
|
|
return $this->sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrites the currently set session id to a user
|
|
|
|
* provided one, helpful when testing
|
|
|
|
*
|
|
|
|
* @param $sessionId The session id to use for CSRF generation
|
|
|
|
*/
|
|
|
|
public function setSessionId($sessionId)
|
|
|
|
{
|
|
|
|
$this->sessionId = $sessionId;
|
|
|
|
}
|
|
|
|
|
2013-07-15 13:58:09 +02:00
|
|
|
/**
|
|
|
|
* @see Zend_Form::init
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
2013-07-24 10:56:41 +02:00
|
|
|
|
2013-07-16 15:39:47 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
/**
|
|
|
|
* Returns the html-element name of the CSRF token
|
|
|
|
* field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTokenElementName()
|
|
|
|
{
|
|
|
|
return $this->tokenElementName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 10:32:53 +02:00
|
|
|
/**
|
|
|
|
* Render the form to html
|
|
|
|
* @param Zend_View_Interface $view
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-07-16 15:39:47 +02:00
|
|
|
public function render(Zend_View_Interface $view = null)
|
|
|
|
{
|
2013-07-18 10:32:53 +02:00
|
|
|
// Elements must be there to render the form
|
2013-07-16 15:39:47 +02:00
|
|
|
return parent::render($view);
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add elements to this form (used by extending classes)
|
|
|
|
*/
|
2013-07-15 14:32:18 +02:00
|
|
|
abstract protected function create();
|
2013-07-15 13:58:09 +02:00
|
|
|
|
2013-07-18 13:46:12 +02:00
|
|
|
/**
|
|
|
|
* Method called before validation
|
|
|
|
*/
|
2013-07-23 17:09:06 +02:00
|
|
|
protected function preValidation(array $data)
|
2013-07-18 13:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-15 13:58:09 +02:00
|
|
|
/**
|
2013-07-15 14:32:18 +02:00
|
|
|
* Setter for request
|
|
|
|
* @param \Zend_Controller_Request_Abstract $request The request object of a session
|
2013-07-15 13:58:09 +02:00
|
|
|
*/
|
2013-07-15 14:32:18 +02:00
|
|
|
public function setRequest(\Zend_Controller_Request_Abstract $request)
|
2013-07-15 13:58:09 +02:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:32:18 +02:00
|
|
|
/**
|
|
|
|
* Getter for request
|
|
|
|
* @return \Zend_Controller_Request_Abstract
|
|
|
|
*/
|
|
|
|
public function getRequest()
|
|
|
|
{
|
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:32:53 +02:00
|
|
|
/**
|
|
|
|
* Triggers form creation
|
|
|
|
*/
|
|
|
|
public function buildForm()
|
|
|
|
{
|
|
|
|
if ($this->created === false) {
|
2013-07-24 10:56:41 +02:00
|
|
|
$this->initCsrfToken();
|
2013-07-18 10:32:53 +02:00
|
|
|
$this->create();
|
|
|
|
|
|
|
|
// Empty action if not safe
|
|
|
|
if (!$this->getAction() && $this->getRequest()) {
|
|
|
|
$this->setAction($this->getRequest()->getRequestUri());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->created = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-15 14:32:18 +02:00
|
|
|
|
2013-07-23 17:09:06 +02:00
|
|
|
/**
|
|
|
|
* Test if data from array or request is valid
|
|
|
|
*
|
|
|
|
* If $data is null, internal request is selected to test validity
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPostAndValid()
|
|
|
|
{
|
|
|
|
if ($this->getRequest()->isPost() === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-18 13:46:12 +02:00
|
|
|
|
2013-07-23 17:09:06 +02:00
|
|
|
$checkData = $this->getRequest()->getParams();
|
2013-07-24 10:56:41 +02:00
|
|
|
|
|
|
|
$this->buildForm();
|
|
|
|
$this->assertValidCsrfToken($checkData);
|
|
|
|
$this->preValidation($checkData);
|
2013-07-23 17:09:06 +02:00
|
|
|
return parent::isValid($checkData);
|
2013-07-15 14:32:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
|
2013-07-15 13:58:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable CSRF counter measure and remove its field if already added
|
|
|
|
*/
|
2013-07-24 10:56:41 +02:00
|
|
|
final public function setTokenDisabled($value)
|
2013-07-15 13:58:09 +02:00
|
|
|
{
|
2013-07-24 10:56:41 +02:00
|
|
|
$this->tokenDisabled = $value;
|
|
|
|
if ($value == true)
|
|
|
|
$this->removeElement($this->tokenElementName);
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add CSRF counter measure field to form
|
|
|
|
*/
|
|
|
|
final public function initCsrfToken()
|
|
|
|
{
|
|
|
|
if ($this->tokenDisabled || $this->getElement($this->tokenElementName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:32:18 +02:00
|
|
|
$this->addElement(
|
|
|
|
'hidden',
|
|
|
|
$this->tokenElementName,
|
|
|
|
array(
|
2013-07-24 10:56:41 +02:00
|
|
|
'value' => $this->generateCsrfTokenAsString(),
|
2013-07-15 14:32:18 +02:00
|
|
|
'decorators' => array('ViewHelper')
|
2013-07-15 13:58:09 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
/**
|
|
|
|
* Tests the submitted data for a correct CSRF token, if needed
|
|
|
|
*
|
|
|
|
* @param Array $checkData The POST data send by the user
|
|
|
|
* @throws Form\InvalidCSRFTokenException When CSRF Validation fails
|
|
|
|
*/
|
|
|
|
final public function assertValidCsrfToken(array $checkData)
|
|
|
|
{
|
|
|
|
if ($this->tokenDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($checkData[$this->tokenElementName]) || !$this->hasValidCsrfToken($checkData[$this->tokenElementName])) {
|
|
|
|
throw new InvalidCSRFTokenException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 13:58:09 +02:00
|
|
|
/**
|
|
|
|
* Check whether the form's CSRF token-field has a valid value
|
|
|
|
*
|
|
|
|
* @param int $maxAge Max allowed token age
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-24 10:56:41 +02:00
|
|
|
final private function hasValidCsrfToken($checkData)
|
2013-07-15 13:58:09 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
if ($this->getElement($this->tokenElementName) === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
$elementValue = $checkData;
|
|
|
|
if (strpos($elementValue, '|') === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list($seed, $token) = explode('|', $elementValue);
|
2013-07-15 13:58:09 +02:00
|
|
|
|
|
|
|
if (!is_numeric($seed)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
$seed -= intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
|
|
|
|
|
|
|
return $token === hash('sha256', $this->getSessionId() . $seed);
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a new (seed, token) pair
|
|
|
|
*
|
|
|
|
* @param int $maxAge Max allowed token age
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-07-24 10:56:41 +02:00
|
|
|
final public function generateCsrfToken()
|
2013-07-15 13:58:09 +02:00
|
|
|
{
|
|
|
|
$seed = mt_rand();
|
2013-07-24 10:56:41 +02:00
|
|
|
$hash = hash('sha256', $this->getSessionId() . $seed);
|
|
|
|
$seed += intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
|
|
|
|
2013-07-15 13:58:09 +02:00
|
|
|
return array($seed, $hash);
|
|
|
|
}
|
2013-07-24 10:56:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string representation of the CSRF seed/token pair
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public function generateCsrfTokenAsString()
|
|
|
|
{
|
|
|
|
list ($seed, $token) = $this->generateCsrfToken($this->getSessionId());
|
|
|
|
return sprintf('%s|%s', $seed, $token);
|
|
|
|
}
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|