[TikiWiki-commits] [Git][tikiwiki/tiki][27.x] [BP][FIX] Use cryptographically secure temp files in metadata processing

"Elifeleti Mukisa Dan \(@Danelif\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <6a10d1e550167_381926fc13098@gitlab-sidekiq-low-urgency-cpu-bound-v2-d7f87744c-64znb.mail>

Elifeleti Mukisa Dan pushed to branch 27.x at Tiki Wiki CMS Groupware / Tiki


Commits:
1d499894 by Elifeleti Mukisa Dan at 2026-05-22T21:53:56+00:00
[BP][FIX] Use cryptographically secure temp files in metadata processing
---
* [BP][FIX] Use cryptographically secure temp files in metadata processing
---
* [BP][FIX] Use cryptographically secure temp files in metadata processing
---
* [FIX] Use cryptographically secure temp files in metadata processing
---
* [FIX] Use cryptographically secure temp files in metadata processing


(cherry picked from commit d6dd57d3f4a67d53d22f8b7e9ae7ccaee734b7ef)

d6dd57d3 [FIX] Use cryptographically secure temp files in metadata processing

Co-authored-by: Danelif <[email protected]>

See merge request tikiwiki/tiki!10286


(cherry picked from commit bed3b1ac303352149baa980cfd0f3abc5f9dda6c)

5a57e3fc [FIX] Use cryptographically secure temp files in metadata processing

Co-authored-by: Elifeleti Mukisa Dan <[email protected]>

See merge request tikiwiki/tiki!10301


(cherry picked from commit 9aea07dfeef31426402a752b0db38ec2e0efa09c)

91c54a91 [FIX] Use cryptographically secure temp files in metadata processing

Co-authored-by: Elifeleti Mukisa Dan <[email protected]>

See merge request tikiwiki/tiki!10313

See merge request tikiwiki/tiki!10335

- - - - -


1 changed file:

- lib/metadata/metadatalib.php


Changes:

=====================================
lib/metadata/metadatalib.php
=====================================
@@ -203,6 +203,8 @@ class FileMetadata
     /**
      * Used to create a temporary path to a file when only the contents are available
      * Necessary because some php functions used to extract metadata require a file path
+     * Creates a temporary file in a secure location with a cryptographically secure random name
+     *
      * @param       string          $content        contents of a file
      *
      * @return      bool|string     $temppath       path to a temporary file in the temp directory or false if $content is
@@ -210,19 +212,56 @@ class FileMetadata
      */
     private function temppathFromContent($content)
     {
-        if (! empty($content)) {
-            $cwd = getcwd();
-            $temppath = tempnam("$cwd/temp", 'temp_file_');
-            if (! is_writeable($temppath)) {
+        if (empty($content)) {
+            return false;
+        }
+
+        // Always prefer Tiki's temp directory
+        $tempDir = TIKI_PATH . '/' . TEMP_PATH;
+
+        // Only fall back to system temp directory if Tiki's temp is not available
+        if (! is_dir($tempDir) || ! is_writable($tempDir)) {
+            $tempDir = sys_get_temp_dir();
+
+            if (! is_dir($tempDir) || ! is_writable($tempDir)) {
                 return false;
             }
-            $temphandle = fopen($temppath, 'w');
-            fwrite($temphandle, $content);
-            fclose($temphandle);
-            return $temppath;
-        } else {
+        }
+
+        // Generate a cryptographically secure random filename
+        $randomName = 'temp_file_' . bin2hex(random_bytes(16));
+
+        $temppath = $tempDir . DIRECTORY_SEPARATOR . $randomName;
+
+        // Ensure the generated filename doesn't already exist
+        $counter = 0;
+        while (file_exists($temppath) && $counter < 100) {
+            $randomName = 'temp_file_' . bin2hex(random_bytes(16));
+            $temppath = $tempDir . DIRECTORY_SEPARATOR . $randomName;
+            $counter++;
+        }
+
+        if (file_exists($temppath)) {
+            // Could not generate unique filename after 100 attempts
+            return false;
+        }
+
+        // Write content to the temporary file
+        $temphandle = fopen($temppath, 'w');
+        if ($temphandle === false) {
             return false;
         }
+
+        $writeResult = fwrite($temphandle, $content);
+        fclose($temphandle);
+
+        if ($writeResult === false) {
+            // Clean up failed file
+            unlink($temppath);
+            return false;
+        }
+
+        return $temppath;
     }
 
     /**



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1d499894b3deeb72adfc3647e8051ac6c79d5545

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1d499894b3deeb72adfc3647e8051ac6c79d5545
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.