]> git.p6c8.net - jirafeau/pcanterino.git/blobdiff - lib/functions.php
More validation of async ref and code strings
[jirafeau/pcanterino.git] / lib / functions.php
index 38115442d237ff843eb3a9930dcd35ef897d7c6e..e9dacf1f72349c92dd5d93ce07108f4ef939c0d6 100644 (file)
@@ -464,9 +464,9 @@ function jirafeau_handle_add_file_encryption($crypt_module_enabled, $file_path)
         error_log("PHP extension sodium not loaded, won't encrypt in Jirafeau");
     }
     if ($crypt_module_enabled == true && extension_loaded('sodium') == true) {
-        $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'crypt');
+        $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'.crypt');
         if (strlen($crypt_key) > 0) {
-            if (rename($file_path.'crypt', $file_path) === true) {
+            if (rename($file_path.'.crypt', $file_path) === true) {
                 $crypted = true;
             }
         }
@@ -544,7 +544,7 @@ function jirafeau_add_file($file, $one_time_download, $key, $time, $ip, $crypt,
     /* hash password or empty. */
     $password = '';
     if (!empty($key)) {
-        $password = hash('sha256', $key);
+        $password = '[SHA256]' . hash('sha256', $key);
     }
 
     /* create link file */
@@ -554,7 +554,7 @@ function jirafeau_add_file($file, $one_time_download, $key, $time, $ip, $crypt,
         $handle,
         $name . NL. $mime_type . NL. $size . NL. $password . NL. $time .
             NL . $hash. NL . ($one_time_download ? 'O' : 'R') . NL . time() .
-            NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O')
+            NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C2' : 'O')
     );
     fclose($handle);
     $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length);
@@ -947,6 +947,7 @@ function jirafeau_admin_bug_report($cfg)
     $out .= "- version: " . JIRAFEAU_VERSION . "<br/>";
     $jirafeau_options = [
         'debug',
+        'debug_enforce_classic_upload',
         'file_hash',
         'litespeed_workaround',
         'store_uploader_ip',
@@ -1021,11 +1022,16 @@ function jirafeau_get_async_ref($ref)
     $out = array();
     $refinfos = VAR_ASYNC . s2p("$ref") . "$ref";
 
-    if (!file_exists($refinfos)) {
+    if (!is_file($refinfos)) {
         return $out;
     }
 
     $c = file($refinfos);
+
+    if(empty($c)) {
+        return $out;
+    }
+
     $out['file_name'] = trim($c[0]);
     $out['mime_type'] = trim($c[1]);
     $out['key'] = trim($c[2], NL);
@@ -1074,9 +1080,9 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
     /* Create temporary folder. */
     $ref = '';
     $p = '';
-    $code = jirafeau_gen_random(4);
+    $code = jirafeau_gen_random(JIRAFEAU_ASYNC_CODE_LENGTH);
     do {
-        $ref = jirafeau_gen_random(32);
+        $ref = jirafeau_gen_random(JIRAFEAU_ASYNC_REF_LENGTH);
         $p = VAR_ASYNC . s2p($ref);
     } while (file_exists($p));
     @mkdir($p, 0755, true);
@@ -1091,7 +1097,7 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
     /* sha256 password or empty */
     $password = '';
     if (!empty($key)) {
-        $password = hash('sha256', $key);
+        $password = '[SHA256]' . hash('sha256', $key);
     }
 
     /* Store information. */
@@ -1119,6 +1125,19 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
   */
 function jirafeau_async_push($ref, $data, $code, $max_file_size)
 {
+    $ref = trim($ref);
+    $code = trim($code);
+
+    /* Validate ref string. */
+    if (!ctype_xdigit($ref) || strlen($ref) != JIRAFEAU_ASYNC_REF_LENGTH) {
+        return "Error: Malformed ref string";
+    }
+
+    /* Validate code string. */
+    if (!ctype_xdigit($code) || strlen($code) != JIRAFEAU_ASYNC_CODE_LENGTH) {
+        return "Error: Malformed code string";
+    }
+
     /* Get async infos. */
     $a = jirafeau_get_async_ref($ref);
 
@@ -1126,7 +1145,8 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
     if (count($a) == 0) {
         return "Error: cannot find transfer";
     }
-    if ($a['next_code'] != "$code") {
+
+    if (!hash_equals($a['next_code'], "$code")) {
         return "Error: bad transfer code";
     }
     if ($data['error'] != UPLOAD_ERR_OK) {
@@ -1192,13 +1212,30 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
   */
 function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_method)
 {
+    $ref = trim($ref);
+    $code = trim($code);
+
+    /* Validate ref string. */
+    if (!ctype_xdigit($ref) || strlen($ref) != JIRAFEAU_ASYNC_REF_LENGTH) {
+        return "Error: Malformed ref string";
+    }
+
+    /* Validate code string. */
+    if (!ctype_xdigit($code) || strlen($code) != JIRAFEAU_ASYNC_CODE_LENGTH) {
+        return "Error: Malformed code string";
+    }
+
     /* Get async infos. */
     $a = jirafeau_get_async_ref($ref);
-    if (count($a) == 0
-        || $a['next_code'] != "$code") {
-        return "Error: bad code for ending transfer";
+
+    /* Check some errors. */
+    if (count($a) == 0) {
+        return "Error: cannot find transfer";
     }
 
+    if (!hash_equals($a['next_code'], "$code")) {
+        return "Error: bad code for ending transfer";
+    }
     /* Generate link infos. */
     $p = VAR_ASYNC . s2p($ref) . $ref . "_data";
     if (!file_exists($p)) {
@@ -1379,7 +1416,7 @@ function jirafeau_decrypt_file_legacy($fp_src, $fp_dst, $k)
     $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
     /* Extract key and iv. */
     $crypt_key = $k;
-    $hash_key = hash('sha256', $crypt_key);
+    $hash_key = md5($crypt_key);
     $iv = jirafeau_crypt_create_iv($hash_key, mcrypt_enc_get_iv_size($m));
     /* Init module. */
     mcrypt_generic_init($m, $hash_key, $iv);

patrick-canterino.de