CVE-2023-3393 (published 2023-06-23T19:15Z)

 Code Injection in GitHub repository fossbilling/fossbilling prior to 0.5.1.

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

Weakness information:
cwe_id: CWE-94
cwe_name: Improper Control of Generation of Code ('Code Injection')
description: The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
extended_description: 
            When a product allows a user's input to contain code syntax, it might be possible for an attacker to craft the code in such a way that it will alter the intended control flow of the product. Such an alteration could lead to arbitrary code execution.
            Injection problems encompass a wide variety of issues -- all mitigated in very different ways. For this reason, the most effective way to discuss these weaknesses is to note the distinct features which classify them as injection weaknesses. The most important issue to note is that all injection problems share one thing in common -- i.e., they allow for the injection of control plane data into the user-controlled data plane. This means that the execution of the process may be altered by sending code in through legitimate data channels, using no other mechanism. While buffer overflows, and many other flaws, involve the use of some further issue to gain execution, injection problems need only for the data to be parsed. The most classic instantiations of this category of weakness are SQL injection and format string vulnerabilities.
         
         

Fix information:
repo_url: https://github.com/fossbilling/fossbilling
commit_details: repo_url: https://github.com/fossbilling/fossbilling
author: Belle Aerni
author_date: 2023-06-20 05:31:20-07:00
msg: Prevent twig from calling unsafe functions (#1337)

* Prevent twig from calling unsafe functions

* Just prevent anything that's not a closure

* Formatting
repository: repo_url: https://github.com/fossbilling/fossbilling
repo_name: FOSSBilling/FOSSBilling
description: Empower your hosting business with FOSSBilling, the free and open-source solution for efficient billing and client management.
repo_language: PHP
owner: fossbilling
file_changes: filename: TwigExtensions.php
change_type: ModificationType.MODIFY
diff: @@ -35,17 +35,11 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
     public function getFilters()
     {
         return [
-            /**
-             * 'trans' filter rewrite same filter from Symfony\Bridge\Twig\Extension\TranslationExtension
-             * for compatible with outdated php-gettext library.
-             *
-             * TODO: Use Symfony\Component\Translation\Loader\MoFileLoader and remove php-gettext hardcoded library.
-             */
             'trans' => new TwigFilter('trans', '__trans'),
 
             'alink' => new TwigFilter('alink', [$this, 'twig_bb_admin_link_filter'], ['is_safe' => ['html']]),
-
             'link' => new TwigFilter('link', [$this, 'twig_bb_client_link_filter'], ['is_safe' => ['html']]),
+            'autolink' => new TwigFilter('autolink', [$this, 'twig_autolink_filter']),
 
             'gravatar' => new TwigFilter('gravatar', [$this, 'twig_gravatar_filter']),
 
@@ -54,7 +48,6 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
             'truncate' => new TwigFilter('truncate', [$this, 'twig_truncate_filter'], ['needs_environment' => true]),
 
             'timeago' => new TwigFilter('timeago', [$this, 'twig_timeago_filter']),
-
             'daysleft' => new TwigFilter('daysleft', [$this, 'twig_daysleft_filter']),
 
             'size' => new TwigFilter('size', [$this, 'twig_size_filter']),
@@ -65,27 +58,23 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
 
             'period_title' => new TwigFilter('period_title', [$this, 'twig_period_title'], ['needs_environment' => true, 'is_safe' => ['html']]),
 
-            'autolink' => new TwigFilter('autolink', [$this, 'twig_autolink_filter']),
-
             'img_tag' => new TwigFilter('img_tag', [$this, 'twig_img_tag'], ['needs_environment' => false, 'is_safe' => ['html']]),
-
             'script_tag' => new TwigFilter('script_tag', [$this, 'twig_script_tag'], ['needs_environment' => false, 'is_safe' => ['html']]),
-
             'stylesheet_tag' => new TwigFilter('stylesheet_tag', [$this, 'twig_stylesheet_tag'], ['needs_environment' => false, 'is_safe' => ['html']]),
-
             'mod_asset_url' => new TwigFilter('mod_asset_url', [$this, 'twig_mod_asset_url']),
-
             'asset_url' => new TwigFilter('asset_url', [$this, 'twig_asset_url'], ['needs_environment' => true, 'is_safe' => ['html']]),
-
             'library_url' => new TwigFilter('library_url', [$this, 'twig_library_url'], ['is_safe' => ['html']]),
 
             'money' => new TwigFilter('money', [$this, 'twig_money'], ['needs_environment' => true, 'is_safe' => ['html']]),
-
             'money_without_currency' => new TwigFilter('money_without_currency', [$this, 'twig_money_without_currency'], ['needs_environment' => true, 'is_safe' => ['html']]),
-
             'money_convert' => new TwigFilter('money_convert', [$this, 'twig_money_convert'], ['needs_environment' => true, 'is_safe' => ['html']]),
-
             'money_convert_without_currency' => new TwigFilter('money_convert_without_currency', [$this, 'money_convert_without_currency'], ['needs_environment' => true, 'is_safe' => ['html']]),
+
+            // We override these default twig filters so we can explicitly disable it from calling certain functions that may leak data or allow commands to be executed on the system.
+            'filter' => new TwigFilter('filter', [$this, 'filteredFilter']),
+            'map' => new TwigFilter('map', [$this, 'filteredMap']),
+            'reduce' => new TwigFilter('reduce', [$this, 'filteredReduce']),
+            'sort' => new TwigFilter('sort', [$this, 'filteredSort']),
         ];
     }
 
@@ -318,4 +307,62 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
 
         return $value;
     }
+
+    public function filteredFilter($array, $arrow)
+    {
+        if (!$arrow instanceof Closure) {
+            return;
+        }
+
+        return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
+    }
+
+    public function filteredMap($array, $arrow)
+    {
+        if (!$arrow instanceof Closure) {
+            return;
+        }
+
+        $r = [];
+        foreach ($array as $k => $v) {
+            $r[$k] = $arrow($v, $k);
+        }
+
+        return $r;
+    }
+
+    public function filteredReduce($array, $arrow, $initial = null)
+    {
+        if (!$arrow instanceof Closure) {
+            return;
+        }
+
+        $accumulator = $initial;
+        foreach ($array as $key => $value) {
+            $accumulator = $arrow($accumulator, $value, $key);
+        }
+
+        return $accumulator;
+    }
+
+    public function filteredSort($array, $arrow)
+    {
+        if (!$arrow instanceof Closure) {
+            return;
+        }
+
+        if ($array instanceof \Traversable) {
+            $array = iterator_to_array($array);
+        } elseif (!\is_array($array)) {
+            return $array;
+        }
+
+        if (null !== $arrow) {
+            uasort($array, $arrow);
+        } else {
+            asort($array);
+        }
+
+        return $array;
+    }
 }

programming_language: PHP