CVE-2020-13756 (published 2020-06-03T14:15Z)

Sabberworm PHP CSS Parser before 8.3.1 calls eval on uncontrolled data, possibly leading to remote code execution if the function allSelectors() or getSelectorsBySpecificity() is called with input from an attacker.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/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/sabberworm/PHP-CSS-Parser
commit_details: repo_url: https://github.com/sabberworm/PHP-CSS-Parser
author: Raphael Schweikert
author_date: 2020-06-01 11:03:20+02:00
msg: Don’t use eval
repository: repo_url: https://github.com/sabberworm/PHP-CSS-Parser
repo_name: MyIntervals/PHP-CSS-Parser
description: A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS
repo_language: PHP
owner: sabberworm
file_changes: filename: CSSBlockList.php
change_type: ModificationType.MODIFY
diff: @@ -65,9 +65,34 @@ abstract class CSSBlockList extends CSSList {
 				if ($sSpecificitySearch === null) {
 					$aResult[] = $oSelector;
 				} else {
-					$sComparison = "\$bRes = {$oSelector->getSpecificity()} $sSpecificitySearch;";
-					eval($sComparison);
-					if ($bRes) {
+					$sComparator = '===';
+					$aSpecificitySearch = explode(' ', $sSpecificitySearch);
+					$iTargetSpecificity = $aSpecificitySearch[0];
+					if(count($aSpecificitySearch) > 1) {
+						$sComparator = $aSpecificitySearch[0];
+						$iTargetSpecificity = $aSpecificitySearch[1];
+					}
+					$iTargetSpecificity = (int)$iTargetSpecificity;
+					$iSelectorSpecificity = $oSelector->getSpecificity();
+					$bMatches = false;
+					switch($sComparator) {
+						case '<=':
+							$bMatches = $iSelectorSpecificity <= $iTargetSpecificity;
+						break;
+						case '<':
+							$bMatches = $iSelectorSpecificity < $iTargetSpecificity;
+						break;
+						case '>=':
+							$bMatches = $iSelectorSpecificity >= $iTargetSpecificity;
+						break;
+						case '>':
+							$bMatches = $iSelectorSpecificity > $iTargetSpecificity;
+						break;
+						default:
+							$bMatches = $iSelectorSpecificity === $iTargetSpecificity;
+						break;
+					}
+					if ($bMatches) {
 						$aResult[] = $oSelector;
 					}
 				}

programming_language: PowerShell
filename: Document.php
change_type: ModificationType.MODIFY
diff: @@ -57,9 +57,6 @@ class Document extends CSSBlockList {
      * @example getSelectorsBySpecificity('>= 100')
      */
     public function getSelectorsBySpecificity($sSpecificitySearch = null) {
-        if (is_numeric($sSpecificitySearch) || is_numeric($sSpecificitySearch[0])) {
-            $sSpecificitySearch = "== $sSpecificitySearch";
-        }
         $aResult = array();
         $this->allSelectors($aResult, $sSpecificitySearch);
         return $aResult;

programming_language: PHP
filename: ParserTest.php
change_type: ModificationType.MODIFY
diff: @@ -146,6 +146,13 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
 			}
 		}
 		$this->assertEquals(array(new Selector('#test .help', true)), $oDoc->getSelectorsBySpecificity('> 100'));
+		$this->assertEquals(array(new Selector('#test .help', true), new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('>= 100'));
+		$this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('=== 100'));
+		$this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('== 100'));
+		$this->assertEquals(array(new Selector('#file', true), new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('<= 100'));
+		$this->assertEquals(array(new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('< 100'));
+		$this->assertEquals(array(new Selector('li.green', true)), $oDoc->getSelectorsBySpecificity('11'));
+		$this->assertEquals(array(new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity(3));
 	}
 
 	function testManipulation() {

programming_language: PHP