]> git.p6c8.net - jirafeau/jirafeau.git/commitdiff
Merge branch 'allow-disallow-extensions' into 'next-release'
authorBlackeye <14815724-Blackstareye@users.noreply.gitlab.com>
Sat, 31 Oct 2026 00:32:20 +0000 (01:32 +0100)
committerBlackeye <14815724-Blackstareye@users.noreply.gitlab.com>
Sat, 31 Oct 2026 00:32:20 +0000 (01:32 +0100)
Add a config option to forbid certain file extensions for uploads

See merge request jirafeau/Jirafeau!39

index.php
lib/config.original.php
lib/functions.js.php
lib/functions.php
lib/locales/en.json
script.php

index d60cb1809dfebb56b6e8f8295ca6b054d6e43269..454bc5209e2721de4b5d7dea694d7f67e386791e 100644 (file)
--- a/index.php
+++ b/index.php
@@ -136,7 +136,9 @@ elseif (true === jirafeau_challenge_upload_ip($cfg, get_ip_address($cfg))) {
             } elseif ($cfg['maximal_upload_size'] > 0) {
                 echo t('2_BIG') . ', ' . t('FILE_LIM') . " " . $cfg['maximal_upload_size'] . " MB.";
             }
-?>')"/>
+?>'); control_selected_file_extension(<?php
+            echo jirafeau_escape(json_encode(jirafeau_disallowed_extensions($cfg)));
+?>, '<?php echo t('EXT_NOT_ALLOWED'); ?>.')"/>
     </p>
 
     <div id="options">
@@ -184,6 +186,10 @@ if ($cfg['maximal_upload_size'] >= 1024) {
 } else {
     echo '<p class="config"></p>';
 }
+if (!empty($cfg['disallowed_extensions'])) {
+    // only shown when the selected file matches, see control_selected_file_extension()
+    echo '<p id="disallowed_extensions" class="config" style="display: none">' . t('EXT_LIM') . ' ' . implode(', ', jirafeau_disallowed_extensions($cfg)) . '</p>';
+}
 ?>
 
         <p id="max_file_size" class="config"></p>
index 37c87ebc573d6d6db4637d2c815c658980291c5c..54be072a05b753f468ba7c3f1e42f6caf95d7700 100644 (file)
@@ -170,6 +170,15 @@ $cfg['one_time_download_preselected'] = false;
  */
 $cfg['maximal_upload_size'] = 0;
 
+/* Forbid specific file extensions from being uploaded.
+ * Leave empty to allow every extension.
+ * Extensions are matched case-insensitively; a leading dot is optional.
+ * Compound extensions such as 'tar.gz' are supported. Note that 'gz'
+ * also covers 'tar.gz', while 'tar.gz' leaves plain '.gz' files allowed.
+ * $cfg['disallowed_extensions'] = array('exe', 'bat', 'sh', 'php', 'tar.gz');
+ */
+$cfg['disallowed_extensions'] = array();
+
 /* Proxy IP
  * If the installation is behind some reverse proxies, it is possible to set
  * the allowed proxy IP.
index cafecafcdc0f6fcd19ade0b89944ec80c86daad8..c468d181c5f8aa8330f22e95ae717a3c9880282c 100644 (file)
@@ -346,11 +346,45 @@ function control_selected_file_size(max_size, error_str)
         // display options
         document.getElementById('options').style.display = 'block';
         document.getElementById('send').style.display = 'block';
+        document.getElementById('send').disabled = false;
         document.getElementById('error_pop').style.display = 'none';
         document.getElementById('send').focus();
     }
 }
 
+// UX-only: script.php enforces disallowed_extensions independently, this
+// just warns the user before they submit a doomed upload.
+function control_selected_file_extension(disallowed_extensions, error_str)
+{
+    if (disallowed_extensions.length === 0)
+    {
+        return;
+    }
+    f_name = document.getElementById('file_select').files[0].name.replace(/[\s.]+$/, '').toLowerCase();
+    // suffix match so that compound extensions such as 'tar.gz' work too
+    if (disallowed_extensions.some(function (ext) { return f_name.endsWith('.' + ext); }))
+    {
+        // control_selected_file_size runs first: keep its error, if any, and add ours
+        var error_pop = document.getElementById('error_pop');
+        if (error_pop.style.display === 'none')
+        {
+            pop_failure(error_str);
+        }
+        else
+        {
+            error_pop.innerHTML += '<br />' + error_str;
+        }
+        // keep the button visible but disabled so the user sees it is forbidden
+        document.getElementById('send').style.display = 'block';
+        document.getElementById('send').disabled = true;
+        document.getElementById('disallowed_extensions').style.display = 'block';
+    }
+    else
+    {
+        document.getElementById('disallowed_extensions').style.display = 'none';
+    }
+}
+
 function XHRErrorHandler(e)
 {
     var text = "${e.type}: ${e.loaded} bytes transferred"
index 6c8d56db6b839b9d7cf6fc786be1d00540587eca..7f03b5f5b4ea405dafe54c3c269cfcb07d88b583 100644 (file)
@@ -229,6 +229,58 @@ function jirafeau_ini_to_bytes($value)
     return $bytes;
 }
 
+/**
+ * returns the configured disallowed extensions, lower-cased and without
+ * a leading dot
+ * @param $cfg jirafeau config
+ * @returns array of normalized extensions
+ */
+function jirafeau_disallowed_extensions($cfg)
+{
+    if (empty($cfg['disallowed_extensions'])) {
+        return array();
+    }
+
+    // tolerate a leading dot in the config, e.g. '.jpg' instead of 'jpg'
+    $extensions = array();
+    foreach ($cfg['disallowed_extensions'] as $extension) {
+        $extension = ltrim(strtolower(trim($extension)), '.');
+        if ($extension !== '') {
+            $extensions[] = $extension;
+        }
+    }
+
+    return $extensions;
+}
+
+/**
+ * checks if a file name's extension is allowed to be uploaded
+ * @param $cfg jirafeau config
+ * @param $filename the name of the file being uploaded
+ * @returns true if the extension is allowed
+ */
+function jirafeau_is_extension_allowed($cfg, $filename)
+{
+    $extensions = jirafeau_disallowed_extensions($cfg);
+    if (empty($extensions)) {
+        return true;
+    }
+
+    // trailing dots and whitespace are dropped on storage or by the client OS
+    $filename = strtolower(rtrim($filename, " \t\n\r\0\v."));
+
+    // suffix match so that compound extensions such as 'tar.gz' work too;
+    // 'gz' still covers 'archive.tar.gz'
+    foreach ($extensions as $extension) {
+        $suffix = '.' . $extension;
+        if (substr($filename, -strlen($suffix)) === $suffix) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 /**
  * gets the maximum upload size according to php.ini
  * @returns the maximum upload size in bytes
index f17bf9ac211dd04596616d4c74816f577b26f802..dfef8d5f3e41f62df0e3bc7d2181f0c13100b90c 100644 (file)
@@ -38,6 +38,8 @@
     "UP_PSW": "Upload password",
     "2_BIG": "The file is too big",
     "FILE_LIM": "File size limited to",
+    "EXT_NOT_ALLOWED": "This file extension is not allowed",
+    "EXT_LIM": "Disallowed file extensions:",
     "FILE_DIR_W": "The file directory is not writable.",
     "LINK_DIR_W": "The link directory is not writable.",
     "ASYNC_DIR_W": "The async directory is not writable.",
index bcca4f78c48236e520d7b953875b57ff733b0173..39eb9d2a54c04c7485de944b6e78afda1af1af97 100644 (file)
@@ -110,6 +110,12 @@ if (isset($_FILES['file']) && is_writable(VAR_FILES)
         exit;
     }
 
+    // Check file extension
+    if (!jirafeau_is_extension_allowed($cfg, $_FILES['file']['name'])) {
+        echo 'Error 27: This file extension is not allowed.';
+        exit;
+    }
+
     // Check if one time download is enabled
     if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
         echo 'Error 26: One time download is disabled.';
@@ -418,6 +424,12 @@ elseif (isset($_GET['init_async'])) {
         exit;
     }
 
+    // Check file extension
+    if (!jirafeau_is_extension_allowed($cfg, $_POST['filename'])) {
+        echo 'Error 28: This file extension is not allowed.';
+        exit;
+    }
+
     $type = '';
     if (isset($_POST['type'])) {
         $type = $_POST['type'];

patrick-canterino.de