Skip to content

Commit

Permalink
Merge pull request #155 from nuernbergerA/fix-heredoc-issue
Browse files Browse the repository at this point in the history
Extend heredoc end detection
  • Loading branch information
brendt authored Oct 18, 2024
2 parents 8f113e2 + 28a3e04 commit 6ea5573
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Languages/Php/Injections/PhpHeredocInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
// First we'll search for all Heredoc open tags,
// which we need in order to find the close tag, and so the whole Heredoc block
foreach ($languageMatches['language'] as $language) {
preg_match_all('/<<<' . $language . '(?<match>(.|\n)*?)' . $language . ';/', $content, $matches);
preg_match_all('/<<<' . $language . '(?<match>(.|\n)*?)' . $language . '(?:;|\s|\))/', $content, $matches);

foreach ($matches['match'] as $key => $match) {
$fullMatch = $matches[0][$key];
Expand Down
30 changes: 30 additions & 0 deletions tests/Languages/Php/Injections/HeredocInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,34 @@ public function test_injection(): void
currentLanguage: new PhpLanguage(),
);
}

#[Test]
public function sql_injection(): void
{

$content = '
$books = map(new Query(<<<SQL
SELECT *
FROM Book
LEFT JOIN …
HAVING …
SQL))->collection()->to(Book::class);
';

$expected = '
$books = map(new Query(&lt;&lt;&lt;<span class="hl-property">SQL</span>
<span class="hl-keyword">SELECT</span> *
<span class="hl-keyword">FROM</span> <span class="hl-type">Book</span>
<span class="hl-keyword">LEFT JOIN</span> …
<span class="hl-keyword">HAVING</span> …
SQL))-&gt;collection()-&gt;to(Book::class);
';

$this->assertMatches(
injection: new PhpHeredocInjection(),
content: $content,
expectedContent: $expected,
currentLanguage: new PhpLanguage(),
);
}
}

0 comments on commit 6ea5573

Please sign in to comment.