diff --git a/core/lib/Drupal/Core/Asset/JsOptimizer.php b/core/lib/Drupal/Core/Asset/JsOptimizer.php index 83d48ee1a2d3f47667f0d0d8333066d24bc332ee..c09a854230ca48a296ce6b7b8dccc7b4c337a3f3 100644 --- a/core/lib/Drupal/Core/Asset/JsOptimizer.php +++ b/core/lib/Drupal/Core/Asset/JsOptimizer.php @@ -79,7 +79,7 @@ public function optimize(array $js_asset) { */ public function clean($contents) { // Remove JS source and source mapping URLs or these may cause 404 errors. - $contents = preg_replace('/\/\/(#|@)\s(sourceURL|sourceMappingURL)=\s*(\S*?)\s*$/m', '', $contents); + $contents = preg_replace('~//[#@]\s(source(?:Mapping)?URL)=\s*(\S+)\s*~', '', $contents); return $contents; } diff --git a/core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php index 6a6b3f80e8a7b9c3aac23779cdfdf277e563d2fc..517277f434560e05e3aba771cb6be03d4f347987 100644 --- a/core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php @@ -77,6 +77,35 @@ public function testClean($js_asset, $expected): void { $this->assertEquals($expected, $this->optimizer->clean($js_asset)); } + /** + * Tests that the javascript may be cleaned without backtracking. + */ + public function testCleanWithRecursionLimit() { + // Specify a hard backtracking limit. + ini_set('pcre.backtrack_limit', 100); + $backtrack_limit = (int) ini_get('pcre.backtrack_limit'); + $this->assertEquals(100, $backtrack_limit); + $script = <<<JS +(function($) { "use strict"; }) +//# sourceMappingURL=data:application/json;charset=utf-8;base64, +JS; + // Generate a source map URL that would exceed the backtrack limit. + $script .= str_repeat('x', $backtrack_limit); + // Add an empty space after the sourcemap, followed by other + // miscellaneous code. + $script .= <<<JS + // I appear after the sourcemap URL. +(function($) { "use strict"; console.log('Hello'); }) +JS; + $expected_script = <<<JS +(function($) { "use strict"; }) +// I appear after the sourcemap URL. +(function($) { "use strict"; console.log('Hello'); }) +JS; + + $this->assertEquals($expected_script, $this->optimizer->clean($script)); + } + /** * Provides data for the JS asset optimize test. *