Skip to content

Commit 28a3e04

Browse files
committed
extend heredoc end detection
1 parent 8f113e2 commit 28a3e04

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Languages/Php/Injections/PhpHeredocInjection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
2222
// First we'll search for all Heredoc open tags,
2323
// which we need in order to find the close tag, and so the whole Heredoc block
2424
foreach ($languageMatches['language'] as $language) {
25-
preg_match_all('/<<<' . $language . '(?<match>(.|\n)*?)' . $language . ';/', $content, $matches);
25+
preg_match_all('/<<<' . $language . '(?<match>(.|\n)*?)' . $language . '(?:;|\s|\))/', $content, $matches);
2626

2727
foreach ($matches['match'] as $key => $match) {
2828
$fullMatch = $matches[0][$key];

tests/Languages/Php/Injections/HeredocInjectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,34 @@ public function test_injection(): void
4444
currentLanguage: new PhpLanguage(),
4545
);
4646
}
47+
48+
#[Test]
49+
public function sql_injection(): void
50+
{
51+
52+
$content = '
53+
$books = map(new Query(<<<SQL
54+
SELECT *
55+
FROM Book
56+
LEFT JOIN …
57+
HAVING …
58+
SQL))->collection()->to(Book::class);
59+
';
60+
61+
$expected = '
62+
$books = map(new Query(&lt;&lt;&lt;<span class="hl-property">SQL</span>
63+
<span class="hl-keyword">SELECT</span> *
64+
<span class="hl-keyword">FROM</span> <span class="hl-type">Book</span>
65+
<span class="hl-keyword">LEFT JOIN</span> …
66+
<span class="hl-keyword">HAVING</span> …
67+
SQL))-&gt;collection()-&gt;to(Book::class);
68+
';
69+
70+
$this->assertMatches(
71+
injection: new PhpHeredocInjection(),
72+
content: $content,
73+
expectedContent: $expected,
74+
currentLanguage: new PhpLanguage(),
75+
);
76+
}
4777
}

0 commit comments

Comments
 (0)