CVE-2018-25061 (published 2022-12-31T20:15Z)

A vulnerability was found in rgb2hex up to 0.1.5. It has been rated as problematic. This issue affects some unknown processing. The manipulation leads to inefficient regular expression complexity. The attack may be initiated remotely. Upgrading to version 0.1.6 is able to address this issue. The patch is named 9e0c38594432edfa64136fdf7bb651835e17c34f. It is recommended to upgrade the affected component. The associated identifier of this vulnerability is VDB-217151.

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

Weakness information:
cwe_id: CWE-1333
cwe_name: Inefficient Regular Expression Complexity
description: The product uses a regular expression with an inefficient, possibly exponential worst-case computational complexity that consumes excessive CPU cycles.
extended_description: Some regular expression engines have a feature called "backtracking". If the token cannot match, the engine "backtracks" to a position that may result in a different token that can match.
                Backtracking becomes a weakness if all of these conditions are met:
                
                    The number of possible backtracking attempts are exponential relative to the length of the input.
                    The input can fail to match the regular expression.
                    The input can be long enough.
                
		
		  Attackers can create crafted inputs that
		  intentionally cause the regular expression to use
		  excessive backtracking in a way that causes the CPU
		  consumption to spike.
		
            
            

Fix information:
repo_url: https://github.com/christian-bromann/rgb2hex
commit_details: repo_url: https://github.com/christian-bromann/rgb2hex
author: christian-bromann
author_date: 2018-07-05 11:40:04+02:00
msg: prevent Regular Expression Denial of Service attacks
repository: repo_url: https://github.com/christian-bromann/rgb2hex
repo_name: christian-bromann/rgb2hex
description: a lightweight rgb/rgba to hex parser
repo_language: JavaScript
owner: christian-bromann
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -28,21 +28,21 @@ var rgb2hex = module.exports = function rgb2hex(color) {
     /**
      * parse input
      */
-    var digits = /rgba?\((\d+),(\d+),(\d+)(,(\d+)?\.?(\d+))?\);?/.exec(strippedColor);
+    var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
 
     if(!digits) {
         // or throw error if input isn't a valid rgb(a) color
         throw new Error('given color (' + color + ') isn\'t a valid rgb or rgba color');
     }
 
-    var red = parseInt(digits[1], 10);
-    var green = parseInt(digits[2], 10);
-    var blue = parseInt(digits[3], 10);
-    var alpha = digits[4] ? /([0-9\.]+)/.exec(digits[4])[0] : '1';
+    var red = parseInt(digits[3], 10);
+    var green = parseInt(digits[4], 10);
+    var blue = parseInt(digits[5], 10);
+    var alpha = digits[6] ? /([0-9\.]+)/.exec(digits[6])[0] : '1';
     var rgb = ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1);
 
     // parse alpha value into float
-    if(alpha.substr(0,1) === '.') {
+    if(alpha.substr(0,2) === ',.') {
         alpha = parseFloat('0' + alpha);
     }
 
@@ -58,4 +58,4 @@ var rgb2hex = module.exports = function rgb2hex(color) {
         hex: '#' + rgb.toString(16),
         alpha: alpha
     };
-};
+};
\ No newline at end of file

programming_language: JavaScript
filename: rgb2hex.js
change_type: ModificationType.MODIFY
diff: @@ -21,21 +21,21 @@
         /**
          * parse input
          */
-        var digits = /rgba?\((\d+),(\d+),(\d+)(,(\d+)?\.?(\d+))?\);?/.exec(strippedColor);
+        var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
 
         if(!digits) {
             // or throw error if input isn't a valid rgb(a) color
             throw new Error('given color (' + color + ') isn\'t a valid rgb or rgba color');
         }
 
-        var red = parseInt(digits[1], 10);
-        var green = parseInt(digits[2], 10);
-        var blue = parseInt(digits[3], 10);
-        var alpha = digits[4] ? /([0-9\.]+)/.exec(digits[4])[0] : '1';
+        var red = parseInt(digits[3], 10);
+        var green = parseInt(digits[4], 10);
+        var blue = parseInt(digits[5], 10);
+        var alpha = digits[6] ? /([0-9\.]+)/.exec(digits[6])[0] : '1';
         var rgb = ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1);
 
         // parse alpha value into float
-        if(alpha.substr(0,1) === '.') {
+        if(alpha.substr(0,2) === ',.') {
             alpha = parseFloat('0' + alpha);
         }
 

programming_language: JavaScript
filename: rgb2hex.min.js
change_type: ModificationType.MODIFY
diff: @@ -1 +1 @@
-!function(r){var e=function(r){if("string"!=typeof r)throw new Error("color has to be type of `string`");if("#"===r.substr(0,1))return{hex:r,alpha:1};var e=r.replace(/\s+/g,""),t=/rgba?\((\d+),(\d+),(\d+)(,(\d+)?\.?(\d+))?\);?/.exec(e);if(!t)throw new Error("given color ("+r+") isn't a valid rgb or rgba color");var n=parseInt(t[1],10),o=parseInt(t[2],10),a=parseInt(t[3],10),i=t[4]?/([0-9\.]+)/.exec(t[4])[0]:"1",s=(a|o<<8|n<<16|1<<24).toString(16).slice(1);return"."===i.substr(0,1)&&(i=parseFloat("0"+i)),1<i&&(i=1),i=parseFloat(Math.round(100*i))/100,{hex:"#"+s.toString(16),alpha:i}};"function"==typeof define&&define.amd?define("rgb2hex",function(){return e}):r.rgb2hex=e}(window);
\ No newline at end of file
+!function(r){var e=function(r){if("string"!=typeof r)throw new Error("color has to be type of `string`");if("#"===r.substr(0,1))return{hex:r,alpha:1};var e=r.replace(/\s+/g,""),t=/(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(e);if(!t)throw new Error("given color ("+r+") isn't a valid rgb or rgba color");var n=parseInt(t[3],10),o=parseInt(t[4],10),a=parseInt(t[5],10),i=t[6]?/([0-9\.]+)/.exec(t[6])[0]:"1",s=(a|o<<8|n<<16|1<<24).toString(16).slice(1);return",."===i.substr(0,2)&&(i=parseFloat("0"+i)),1<i&&(i=1),i=parseFloat(Math.round(100*i))/100,{hex:"#"+s.toString(16),alpha:i}};"function"==typeof define&&define.amd?define("rgb2hex",function(){return e}):r.rgb2hex=e}(window);
\ No newline at end of file

programming_language: JavaScript
filename: rgb2hex.test.js
change_type: ModificationType.MODIFY
diff: @@ -80,10 +80,22 @@ describe('rgb2hex should', () => {
         })
 
         it('by limiting alpha value to 1', () => {
-            var input = 'rgba(12,173,22,12312.67)'
+            var input = 'rgba(12,173,22,1.67)'
             expect(rgb2hex(input).alpha).not.toBeGreaterThan(1)
         })
 
+        it('by not accepting to big values', () => {
+            var input = 'rgba(1123, 54, 4, 0.33)'
+            expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
+            input = 'rgba(113, 1154, 4, 0.33)'
+            expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
+            input = 'rgba(113, 154, 1114, 0.33)'
+            expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
+            input = 'rgba(113, 54, 4, 2.33)'
+            expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
+            input = 'rgbaaaaaa(113, 54, 4, .33)'
+            expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
+        })
     })
 
     describe('not care about', () => {
@@ -132,4 +144,4 @@ describe('rgb2hex should', () => {
             expect(rgb2hex(`${values}rgba(226,230,233,0.4)${values}`).hex)
         })
     })
-})
+})
\ No newline at end of file

programming_language: CoffeeScript