CVE-2023-4651 (published 2023-08-31T01:15Z)

Server-Side Request Forgery (SSRF) in GitHub repository instantsoft/icms2 prior to 2.16.1.

CVSS v3 base score: 5.4, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:L/A:N)

Weakness information:
cwe_id: CWE-918
cwe_name: Server-Side Request Forgery (SSRF)
description: The web server receives a URL or similar request from an upstream component and retrieves the contents of this URL, but it does not sufficiently ensure that the request is being sent to the expected destination.
extended_description: By providing URLs to unexpected hosts or ports, attackers can make it appear that the server is sending the request, possibly bypassing access controls such as firewalls that prevent the attackers from accessing the URLs directly. The server can be used as a proxy to conduct port scanning of hosts in internal networks, use other URLs such as that can access documents on the system (using file://), or use other protocols such as gopher:// or tftp://, which may provide greater control over the contents of requests.
         

Fix information:
repo_url: https://github.com/instantsoft/icms2
commit_details: repo_url: https://github.com/instantsoft/icms2
author: fuzegit
author_date: 2023-08-14 14:28:13+03:00
msg: Fix SSRF Blind in the image upload
repository: repo_url: https://github.com/instantsoft/icms2
repo_name: instantsoft/icms2
description: Self-hosted Site Management System
repo_language: PHP
owner: instantsoft
file_changes: filename: uploader.php
change_type: ModificationType.MODIFY
diff: @@ -341,8 +341,25 @@ class cmsUploader {
 
         $link = $file_name = trim($_POST[$post_filename]);
 
+
+        if (
+            // Валидный URL с PATH
+            filter_var($link, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) !== $link ||
+            // По IP адресу не разрешаем
+            preg_match('#^(?:(?:https?):\/\/)([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*#ui', $link)
+            ) {
+
+            return [
+                'success' => false,
+                'error'   => 'Not allowed',
+                'name'    => '',
+                'path'    => ''
+            ];
+        }
+
         // проверяем редирект и имя файла
         $curl = curl_init();
+        curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP);
         curl_setopt($curl, CURLOPT_URL, $link);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($curl, CURLOPT_HEADER, true);

programming_language: PHP
filename: files.helper.php
change_type: ModificationType.MODIFY
diff: @@ -315,6 +315,11 @@ function file_get_contents_from_url($url, $timeout = 5, $json_decode = false, $p
         return null;
     }
 
+    // По IP адресу не разрешаем
+    if (preg_match('#^(?:(?:https?):\/\/)([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*#ui', $url)) {
+        return null;
+    }
+
     $curl = curl_init();
 
     if (strpos($url, 'https') === 0) {
@@ -342,6 +347,7 @@ function file_get_contents_from_url($url, $timeout = 5, $json_decode = false, $p
             $headers[] = $key . ': ' . $value;
         }
     }
+    curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
@@ -368,17 +374,20 @@ function file_get_contents_from_url($url, $timeout = 5, $json_decode = false, $p
  * @param string $destination Полный путь куда сохраненить файл
  * @return boolean
  */
-function file_save_from_url($url, $destination){
+function file_save_from_url($url, $destination) {
 
-    if (!function_exists('curl_init')){ return false; }
+    if (!function_exists('curl_init')) {
+        return false;
+    }
 
     $dest_file = @fopen($destination, "w");
 
     $curl = curl_init();
-    if(strpos($url, 'https') === 0){
+    if (strpos($url, 'https') === 0) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     }
+    curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP);
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_FILE, $dest_file);
@@ -388,8 +397,8 @@ function file_save_from_url($url, $destination){
     fclose($dest_file);
 
     return true;
-
 }
+
 /**
  * Накладывает ваттермарк на изображение
  *

programming_language: PHP