2010-12-05 09:22:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-12-05 09:22:57 +01:00
|
|
|
* @subpackage security
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cross Site Request Forgery (CSRF) protection for the {@link Form} class and other GET links.
|
|
|
|
* Can be used globally (through {@link SecurityToken::inst()})
|
|
|
|
* or on a form-by-form basis {@link Form->getSecurityToken()}.
|
|
|
|
*
|
|
|
|
* <b>Usage in forms</b>
|
|
|
|
*
|
|
|
|
* This protective measure is automatically turned on for all new {@link Form} instances,
|
|
|
|
* and can be globally disabled through {@link disable()}.
|
|
|
|
*
|
|
|
|
* <b>Usage in custom controller actions</b>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* class MyController extends Controller {
|
|
|
|
* function mygetaction($request) {
|
|
|
|
* if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);
|
|
|
|
*
|
|
|
|
* // valid action logic ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @todo Make token name form specific for additional forgery protection.
|
|
|
|
*/
|
2012-02-11 03:08:39 +01:00
|
|
|
class SecurityToken extends Object implements TemplateGlobalProvider {
|
2010-12-05 09:22:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected static $default_name = 'SecurityID';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SecurityToken
|
|
|
|
*/
|
|
|
|
protected static $inst = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected static $enabled = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String $name
|
|
|
|
*/
|
|
|
|
protected $name = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null) {
|
2010-12-05 09:22:57 +01:00
|
|
|
$this->name = ($name) ? $name : self::get_default_name();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a global token (or creates one if it doesnt exist already).
|
|
|
|
*
|
|
|
|
* @return SecurityToken
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function inst() {
|
2010-12-05 09:22:57 +01:00
|
|
|
if(!self::$inst) self::$inst = new SecurityToken();
|
|
|
|
|
|
|
|
return self::$inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Globally disable the token (override with {@link NullSecurityToken})
|
|
|
|
* implementation. Note: Does not apply for
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function disable() {
|
2010-12-05 09:22:57 +01:00
|
|
|
self::$enabled = false;
|
|
|
|
self::$inst = new NullSecurityToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Globally enable tokens that have been previously disabled through {@link disable}.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function enable() {
|
2010-12-05 09:22:57 +01:00
|
|
|
self::$enabled = true;
|
|
|
|
self::$inst = new SecurityToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function is_enabled() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return self::$enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_default_name() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return self::$default_name;
|
|
|
|
}
|
2012-02-11 03:08:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of an the global SecurityToken in the current session
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function getSecurityID() {
|
2012-02-11 03:08:39 +01:00
|
|
|
$token = SecurityToken::inst();
|
|
|
|
return $token->getValue();
|
|
|
|
}
|
2010-12-05 09:22:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2010-12-05 09:22:57 +01:00
|
|
|
$val = $this->getValue();
|
|
|
|
$this->name = $name;
|
|
|
|
$this->setValue($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getName() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getValue() {
|
2012-04-26 06:43:58 +02:00
|
|
|
$value = Session::get($this->getName());
|
|
|
|
|
|
|
|
// only regenerate if the token isn't already set in the session
|
|
|
|
if(!$value) {
|
|
|
|
$value = $this->generate();
|
|
|
|
$this->setValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2010-12-05 09:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $val
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($val) {
|
2010-12-05 09:22:57 +01:00
|
|
|
Session::set($this->getName(), $val);
|
|
|
|
}
|
|
|
|
|
2012-07-01 10:53:58 +02:00
|
|
|
/**
|
|
|
|
* Reset the token to a new value.
|
|
|
|
*/
|
|
|
|
public function reset() {
|
|
|
|
$this->setValue($this->generate());
|
|
|
|
}
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
/**
|
|
|
|
* Checks for an existing CSRF token in the current users session.
|
|
|
|
* This check is automatically performed in {@link Form->httpSubmission()}
|
|
|
|
* if a form has security tokens enabled.
|
|
|
|
* This direct check is mainly used for URL actions on {@link FormField} that are not routed
|
|
|
|
* through {@link Form->httpSubmission()}.
|
|
|
|
*
|
|
|
|
* Typically you'll want to check {@link Form->securityTokenEnabled()} before calling this method.
|
|
|
|
*
|
|
|
|
* @param String $compare
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function check($compare) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return ($compare && $this->getValue() && $compare == $this->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link check()}.
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function checkRequest($request) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return $this->check($request->requestVar($this->getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: Doesn't call {@link FormField->setForm()}
|
|
|
|
* on the returned {@link HiddenField}, you'll need to take
|
|
|
|
* care of this yourself.
|
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* @param FieldList $fieldset
|
2010-12-05 09:22:57 +01:00
|
|
|
* @return HiddenField|false
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function updateFieldSet(&$fieldset) {
|
2010-12-05 09:22:57 +01:00
|
|
|
if(!$fieldset->fieldByName($this->getName())) {
|
|
|
|
$field = new HiddenField($this->getName(), null, $this->getValue());
|
|
|
|
$fieldset->push($field);
|
|
|
|
return $field;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $url
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addToUrl($url) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return Controller::join_links($url, sprintf('?%s=%s', $this->getName(), $this->getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You can't disable an existing instance, it will need to be overwritten like this:
|
|
|
|
* <code>
|
|
|
|
* $old = SecurityToken::inst(); // isEnabled() returns true
|
|
|
|
* SecurityToken::disable();
|
|
|
|
* $new = SecurityToken::inst(); // isEnabled() returns false
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isEnabled() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return !($this instanceof NullSecurityToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-05 09:45:42 +01:00
|
|
|
* @uses RandomGenerator
|
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
protected function generate() {
|
2010-12-05 09:45:42 +01:00
|
|
|
$generator = new RandomGenerator();
|
2012-11-08 04:33:19 +01:00
|
|
|
return $generator->randomToken('sha1');
|
2010-12-05 09:22:57 +01:00
|
|
|
}
|
2012-02-11 03:08:39 +01:00
|
|
|
|
2012-02-21 01:36:34 +01:00
|
|
|
public static function get_template_global_variables() {
|
2012-02-11 03:08:39 +01:00
|
|
|
return array(
|
|
|
|
'getSecurityID',
|
|
|
|
'SecurityID' => 'getSecurityID'
|
|
|
|
);
|
|
|
|
}
|
2010-12-05 09:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialized subclass for disabled security tokens - always returns
|
|
|
|
* TRUE for token checks. Use through {@link SecurityToken::disable()}.
|
|
|
|
*/
|
|
|
|
class NullSecurityToken extends SecurityToken {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function check($compare) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function checkRequest($request) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-11 09:51:54 +02:00
|
|
|
* @param FieldList $fieldset
|
2010-12-05 09:22:57 +01:00
|
|
|
* @return false
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function updateFieldSet(&$fieldset) {
|
2010-12-05 09:22:57 +01:00
|
|
|
// Remove, in case it was added beforehand
|
|
|
|
$fieldset->removeByName($this->getName());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $url
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addToUrl($url) {
|
2010-12-05 09:22:57 +01:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getValue() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $val
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($val) {
|
2010-12-05 09:22:57 +01:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function generate() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return null;
|
2012-07-01 10:53:58 +02:00
|
|
|
}
|
2012-11-08 04:33:19 +01:00
|
|
|
}
|