From 6fcc50ca18c4171087545c39b287ea69bfe69149 Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Fri, 28 Jun 2024 12:06:22 +0200 Subject: [PATCH] Add support for empty tags --- Test/Unit/Util/ScriptFinderTest.php | 2 ++ Util/ScriptFinder.php | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Test/Unit/Util/ScriptFinderTest.php b/Test/Unit/Util/ScriptFinderTest.php index ef102f1..0d5673e 100644 --- a/Test/Unit/Util/ScriptFinderTest.php +++ b/Test/Unit/Util/ScriptFinderTest.php @@ -24,11 +24,13 @@ public function getHtmlSamples(): array return [ ["
\n
", 0], ["
\n\n
", 1], + ["
\n\n
", 0], ["
\n\n
", 0], ['
', 1], ['
', 0], ['
', 1], ['
', 0], + ['
', 2], ]; } } diff --git a/Util/ScriptFinder.php b/Util/ScriptFinder.php index 941b290..69a4985 100644 --- a/Util/ScriptFinder.php +++ b/Util/ScriptFinder.php @@ -15,7 +15,7 @@ public function find(string $html): array return []; } - if (false === preg_match_all('#]*)>(.*)#msi', $html, $matches)) { + if (false === preg_match_all('#]*)>(.*)#msUi', $html, $matches)) { return []; } @@ -27,7 +27,11 @@ public function find(string $html): array foreach ($matches[0] as $matchIndex => $match) { $fullScript = $matches[0][$matchIndex]; $scriptAttributes = $matches[1][$matchIndex]; - $inlineJs = $matches[2][$matchIndex]; + $inlineJs = trim($matches[2][$matchIndex]); + if (empty($inlineJs)) { + continue; + } + if (false === $this->needsCsp($scriptAttributes)) { continue; }