]> git.p6c8.net - jirafeau/pcanterino.git/commitdiff
Add disallowed_extensions config with server and client validation
authorThePeGaSuS <25697531+TehPeGaSuS@users.noreply.github.com>
Mon, 7 Sep 2026 11:02:50 +0000 (13:02 +0200)
committerThePeGaSuS <25697531+TehPeGaSuS@users.noreply.github.com>
Mon, 7 Sep 2026 11:03:42 +0000 (13:03 +0200)
Add $cfg['disallowed_extensions'] as a single-purpose blacklist field
(supersedes MR !39's allowed/forbidden approach per maintainer
feedback). Server-side validation runs in both the direct-upload and
async-init branches of script.php, covering browser and CLI/curl
uploads alike. Client-side JS mirrors the existing file-size check,
warning the user and hiding the send button before they submit an
upload with a disallowed extension, and the disallowed list is shown
next to the file-size hint on the upload form.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MGzCMiwvovH9jLpoAJZvkF

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

index d60cb1809dfebb56b6e8f8295ca6b054d6e43269..c96657c15be93f186f4bf3f4962d587a1ac2526e 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 json_encode(array_map('strtolower', $cfg['disallowed_extensions']));
+?>, '<?php echo t('EXT_NOT_ALLOWED'); ?>.')"/>
     </p>
 
     <div id="options">
@@ -184,6 +186,9 @@ if ($cfg['maximal_upload_size'] >= 1024) {
 } else {
     echo '<p class="config"></p>';
 }
+if (!empty($cfg['disallowed_extensions'])) {
+    echo '<p class="config">' . t('EXT_LIM') . ' ' . implode(', ', $cfg['disallowed_extensions']) . '</p>';
+}
 ?>
 
         <p id="max_file_size" class="config"></p>
index 37c87ebc573d6d6db4637d2c815c658980291c5c..2be4e52bdb0d51476494f1d6f9d0a938cb8dce32 100644 (file)
@@ -170,6 +170,13 @@ $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 and without the leading dot.
+ * $cfg['disallowed_extensions'] = array('exe', 'bat', 'sh', 'php');
+ */
+$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..b178f4f96468ca88dbdfdfdab2c27d822ec17d47 100644 (file)
@@ -351,6 +351,19 @@ function control_selected_file_size(max_size, error_str)
     }
 }
 
+// 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)
+{
+    f_name = document.getElementById('file_select').files[0].name;
+    f_ext = f_name.includes('.') ? f_name.split('.').pop().toLowerCase() : '';
+    if (disallowed_extensions.includes(f_ext))
+    {
+        pop_failure(error_str);
+        document.getElementById('send').style.display = 'none';
+    }
+}
+
 function XHRErrorHandler(e)
 {
     var text = "${e.type}: ${e.loaded} bytes transferred"
index 6c8d56db6b839b9d7cf6fc786be1d00540587eca..8bfa61bb3482d6993ab0dee22c8f7643a9923a17 100644 (file)
@@ -229,6 +229,24 @@ function jirafeau_ini_to_bytes($value)
     return $bytes;
 }
 
+/**
+ * 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)
+{
+    if (empty($cfg['disallowed_extensions'])) {
+        return true;
+    }
+
+    $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
+    $disallowed = array_map('strtolower', $cfg['disallowed_extensions']);
+
+    return !in_array($extension, $disallowed);
+}
+
 /**
  * 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 2fc297f3412302a0933ef91d3d85f82560c49df9..671aeef331d9908dc527103358cb03437a9a2b24 100644 (file)
@@ -108,6 +108,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.';
@@ -416,6 +422,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