Skip to content

Commit

Permalink
Add getShorthandValues ReDoS mitigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Aug 21, 2024
1 parent 01b9275 commit d829a01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/ckeditor5-engine/src/view/styles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @module engine/view/styles/utils
*/

import { CKEditorError } from '@ckeditor/ckeditor5-utils';
import type { BoxSides, PropertyDescriptor, StyleValue } from '../stylesmap.js';

const HEX_COLOR_REGEXP = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
Expand Down Expand Up @@ -253,6 +254,16 @@ export function getPositionShorthandNormalizer( shorthand: string ) {
* ```
*/
export function getShorthandValues( string: string ): Array<string> {
if ( string.length > 500 ) {
/**
* Error thrown when the value of a CSS shorthand property is too long.
* It's mitigation against potential reDoS attacks.
*
* @error css-shorthand-string-too-long
*/
throw new CKEditorError( 'css-shorthand-string-too-long', { string } );
}

const matches = string.matchAll( CSS_SHORTHAND_VALUE_REGEXP );

return Array.from( matches ).map( i => i[ 0 ] );
Expand Down
8 changes: 8 additions & 0 deletions packages/ckeditor5-engine/tests/view/styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import { CKEditorError } from '@ckeditor/ckeditor5-utils';
import {
getBoxSidesShorthandValue,
getBoxSidesValues,
Expand Down Expand Up @@ -299,6 +300,13 @@ describe( 'Styles utils', () => {
expect( getShorthandValues( 'foo bar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );

it( 'should raise exception when passed string is longer than 500 characters', () => {
expect( () => getShorthandValues( 'a'.repeat( 501 ) ) ).to.throw(
CKEditorError,
'css-shorthand-string-too-long'
);
} );

it( 'should split string to separate values when value contain grouping parens', () => {
expect( getShorthandValues( 'foo bar(1, 3, 5) url("example.com:foo/bar?q=b")' ) )
.to.deep.equal( [ 'foo', 'bar(1, 3, 5)', 'url("example.com:foo/bar?q=b")' ] );
Expand Down

0 comments on commit d829a01

Please sign in to comment.