summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLukas Reschke <lukas@statuscode.ch>2012-09-28 10:54:11 -0700
committerLukas Reschke <lukas@statuscode.ch>2012-09-28 10:54:11 -0700
commitcfc98398120065d33659f36573f81bcea9a3e97d (patch)
treedfb19e6ecabf61e14eef01b646825c1685233045 /lib
parent35357f3afb90dec1d0a9755ab0e0504a916c5e5a (diff)
parent71454b1bca0accd1ab5d7628169d4714bb682030 (diff)
downloadnextcloud-server-cfc98398120065d33659f36573f81bcea9a3e97d.tar.gz
nextcloud-server-cfc98398120065d33659f36573f81bcea9a3e97d.zip
Merge pull request #19 from arkascha/master
Reimplementation of CSRF protection strategy
Diffstat (limited to 'lib')
-rw-r--r--lib/base.php2
-rw-r--r--lib/template.php2
-rwxr-xr-xlib/util.php36
3 files changed, 30 insertions, 10 deletions
diff --git a/lib/base.php b/lib/base.php
index f6afc8fe2fe..5a2decc6f63 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -240,6 +240,8 @@ class OC{
OC_Util::addScript( "jquery-tipsy" );
OC_Util::addScript( "oc-dialogs" );
OC_Util::addScript( "js" );
+ // request protection token MUST be defined after the jquery library but before any $('document').ready()
+ OC_Util::addScript( "requesttoken" );
OC_Util::addScript( "eventsource" );
OC_Util::addScript( "config" );
//OC_Util::addScript( "multiselect" );
diff --git a/lib/template.php b/lib/template.php
index 0987d6f0d88..681b3f0b140 100644
--- a/lib/template.php
+++ b/lib/template.php
@@ -157,6 +157,7 @@ class OC_Template{
$this->vars = array();
if($renderas == 'user') {
$this->vars['requesttoken'] = OC_Util::callRegister();
+ $this->vars['requestlifespan'] = OC_Util::$callLifespan;
}
$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
$this->l10n = OC_L10N::get($parts[0]);
@@ -374,6 +375,7 @@ class OC_Template{
$page = new OC_TemplateLayout($this->renderas);
if($this->renderas == 'user') {
$page->assign('requesttoken', $this->vars['requesttoken']);
+ $page->assign('requestlifespan', $this->vars['requestlifespan']);
}
// Add custom headers
diff --git a/lib/util.php b/lib/util.php
index c89c4d8c7c1..777cb7a28fc 100755
--- a/lib/util.php
+++ b/lib/util.php
@@ -416,14 +416,29 @@ class OC_Util {
}
/**
- * @brief Register an get/post call. This is important to prevent CSRF attacks
- * Todo: Write howto
+ * @brief Static lifespan (in seconds) when a request token expires.
+ * @see OC_Util::callRegister()
+ * @see OC_Util::isCallRegistered()
+ * @description
+ * Also required for the client side to compute the piont in time when to
+ * request a fresh token. The client will do so when nearly 97% of the
+ * timespan coded here has expired.
+ */
+ public static $callLifespan = 3600; // 3600 secs = 1 hour
+
+ /**
+ * @brief Register an get/post call. Important to prevent CSRF attacks.
+ * @todo Write howto: CSRF protection guide
* @return $token Generated token.
+ * @description
+ * Creates a 'request token' (random) and stores it inside the session.
+ * Ever subsequent (ajax) request must use such a valid token to succeed,
+ * otherwise the request will be denied as a protection against CSRF.
+ * The tokens expire after a fixed lifespan.
+ * @see OC_Util::$callLifespan
+ * @see OC_Util::isCallRegistered()
*/
public static function callRegister() {
- //mamimum time before token exires
- $maxtime=(60*60); // 1 hour
-
// generate a random token.
$token=mt_rand(1000,9000).mt_rand(1000,9000).mt_rand(1000,9000);
@@ -436,7 +451,8 @@ class OC_Util {
foreach($_SESSION as $key=>$value) {
// search all tokens in the session
if(substr($key,0,12)=='requesttoken') {
- if($value+$maxtime<time()) {
+ // check if static lifespan has expired
+ if($value+self::$callLifespan<time()) {
// remove outdated tokens
unset($_SESSION[$key]);
}
@@ -447,14 +463,13 @@ class OC_Util {
return($token);
}
-
/**
* @brief Check an ajax get/post call if the request token is valid.
* @return boolean False if request token is not set or is invalid.
+ * @see OC_Util::$callLifespan
+ * @see OC_Util::calLRegister()
*/
public static function isCallRegistered() {
- //mamimum time before token exires
- $maxtime=(60*60); // 1 hour
if(isset($_GET['requesttoken'])) {
$token=$_GET['requesttoken'];
}elseif(isset($_POST['requesttoken'])) {
@@ -467,7 +482,8 @@ class OC_Util {
}
if(isset($_SESSION['requesttoken-'.$token])) {
$timestamp=$_SESSION['requesttoken-'.$token];
- if($timestamp+$maxtime<time()) {
+ // check if static lifespan has expired
+ if($timestamp+self::$callLifespan<time()) {
return false;
}else{
//token valid