[TikiWiki-commits] [Git][tikiwiki/tiki][30.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 | <6a10a63be568a_381924e016614@gitlab-sidekiq-low-urgency-cpu-bound-v2-5755d7f9f9-bqnw4.mail> |
Elifeleti Mukisa Dan pushed to branch 30.x at Tiki Wiki CMS Groupware / Tiki Commits: 9aea07df by Elifeleti Mukisa Dan at 2026-05-22T18:46:42+00:00 [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 - - - - - 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/9aea07dfeef31426402a752b0db38ec2e0efa09c -- View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/9aea07dfeef31426402a752b0db38ec2e0efa09c 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