]> source.dussan.org Git - nextcloud-server.git/commitdiff
add two csrf check calls. Review and lot´s of porting needed.
authorFrank Karlitschek <frank@owncloud.org>
Sat, 9 Jun 2012 13:05:14 +0000 (15:05 +0200)
committerFrank Karlitschek <frank@owncloud.org>
Sat, 9 Jun 2012 13:05:14 +0000 (15:05 +0200)
lib/public/util.php
lib/util.php

index 9b499574da1ca6b30f3f070b359ecb05147c29b9..995161e2abe67f6bd3f008c11d06d2ba0fdcfc4c 100644 (file)
@@ -248,6 +248,26 @@ class Util {
        }
 
 
+       /**
+        * Register an get/post call. This is important to prevent CSRF attacks
+        * TODO: write example
+        */
+       public static function callRegister(){
+               return(\OC_Util::callRegister());
+       }
+
+
+       /**
+        * Check an ajax get/post call if the request token is valid. exit if not.
+        * Todo: Write howto
+        */
+       public static function callCheck(){
+               return(\OC_Util::callCheck());
+       }
+
+
+
+
 }
 
 ?>
index 20888fa71f4056bec53516dd5bec1368c03f9086..ef8ba8efe72baa8d7599a333504071165499e6ec 100644 (file)
@@ -343,4 +343,60 @@ class OC_Util {
                }
                return $id;
        }
+
+       /**
+        * Register an get/post call. This is important to prevent CSRF attacks
+        * Todo: Write howto
+        */
+       public static function callRegister(){
+               // generate a random token.
+               $token=mt_rand(1000,9000).mt_rand(1000,9000).mt_rand(1000,9000);
+
+               // store the token together with a timestamp in the session.
+               $_SESSION['requesttoken-'.$token]=time();
+
+               // return the token
+               return($token);
+       }
+
+
+       /**
+        * Check an ajax get/post call if the request token is valid. exit if not.
+        * Todo: Write howto
+        */
+       public static function callCheck(){
+               //mamimum time before token exires
+               $maxtime=(60*60);  // 1 hour
+
+               // searches in the get and post arrays for the token.
+               if(isset($_GET['requesttoken'])) {
+                       $token=$_GET['requesttoken'];
+               }elseif(isset($_POST['requesttoken'])){
+                       $token=$_POST['requesttoken'];
+               }else{
+                       //no token found. exiting
+                       exit;
+               }
+
+               // check if the token is in the user session and if the timestamp is from the last hour.
+               if(isset($_SESSION['requesttoken-'.$token])) {
+                       $timestamp=$_SESSION['requesttoken-'.$token];
+                       if($timestamp+$maxtime<time){
+                               //token exired. exiting
+                               exit;
+
+                       }else{
+                               //token valid
+                               return;
+                       }
+               }else{
+                       //no token found. exiting
+                       exit;
+               }
+       }
+
+
+
+
+
 }