]> git.p6c8.net - jirafeau/pcanterino.git/commitdiff
More validation of async ref and code strings
authorPatrick Canterino <patrick@patrick-canterino.de>
Thu, 15 Oct 2026 12:36:16 +0000 (14:36 +0200)
committerPatrick Canterino <patrick@patrick-canterino.de>
Thu, 15 Oct 2026 12:36:16 +0000 (14:36 +0200)
Ref string MUST be 32 characters, code string MUST be 4 characters.

Code string MUST be a valid hex string.

Compare code using hash_equals().

lib/functions.php
lib/settings.php

index 0676c6fdd1a996586b192b03fdf125bde9ca9f56..e9dacf1f72349c92dd5d93ce07108f4ef939c0d6 100644 (file)
@@ -1080,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);
@@ -1125,11 +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(trim($ref))) {
+    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);
 
@@ -1138,7 +1146,7 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
         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) {
@@ -1204,18 +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(trim($ref))) {
+    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)) {
index e015067bd45e9345de460ff77f6536e77478d0ac..55bf11a5ad3f0517d892bbddf23bfbe27ab85e07 100644 (file)
@@ -84,6 +84,9 @@ define('JIRAFEAU_USER_AUTH_BY_PASSWORD', 2);
 
 define('JIRAFEAU_SODIUM_CHUNKSIZE', 1024);
 
+define('JIRAFEAU_ASYNC_REF_LENGTH', 32);
+define('JIRAFEAU_ASYNC_CODE_LENGTH', 4);
+
 // Define some Sodium constants from newer PHP versions if they are not available
 
 if (extension_loaded('sodium')) {

patrick-canterino.de