Skip to content

Commit

Permalink
Add support for empty tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Jun 28, 2024
1 parent 3dcd51c commit 6fcc50c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Test/Unit/Util/ScriptFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function getHtmlSamples(): array
return [
["<div>\n</div>", 0],
["<div>\n<script>\nalert(true);</script>\n</div>", 1],
["<div>\n<script></script>\n</div>", 0],
["<div>\n<script nonce='foobar'>\nalert(true);</script>\n</div>", 0],
['<div><script type="text/javascript">foobar</script></div>', 1],
['<div><script type="text/javascript" nonce="foobar">foobar</script></div>', 0],
['<div><script type="application/javascript">foobar</script></div>', 1],
['<div><script type="text/x-magento-init">foobar</script></div>', 0],
['<div><script>test1</script><script>test2</script></div>', 2],
];
}
}
8 changes: 6 additions & 2 deletions Util/ScriptFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function find(string $html): array
return [];
}

if (false === preg_match_all('#<script([^>]*)>(.*)</script>#msi', $html, $matches)) {
if (false === preg_match_all('#<script([^>]*)>(.*)</script>#msUi', $html, $matches)) {
return [];
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 6fcc50c

Please sign in to comment.