Skip to content

Commit 347513a

Browse files
authored
fix(view): prevent infinite loop with unclosed PHP or comment tags (#1282)
1 parent dde685a commit 347513a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/view/src/Parser/TempestViewLexer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ final class TempestViewLexer
1010

1111
private ?string $current;
1212

13+
private bool $eof = false;
14+
1315
public function __construct(
1416
private readonly string $html,
1517
) {
@@ -155,7 +157,7 @@ private function lexPhp(): Token
155157
{
156158
$buffer = '';
157159

158-
while ($this->seek(2) !== '?>') {
160+
while ($this->seek(2) !== '?>' && $this->current) {
159161
$buffer .= $this->consume();
160162
}
161163

@@ -175,7 +177,7 @@ private function lexComment(): Token
175177
{
176178
$buffer = '';
177179

178-
while ($this->seek(3) !== '-->') {
180+
while ($this->seek(3) !== '-->' && $this->current) {
179181
$buffer .= $this->consume();
180182
}
181183

packages/view/tests/TempestViewLexerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ public function test_attribute_with_new_line(): void
220220
], $tokens);
221221
}
222222

223+
public function test_unclosed_php_tag(): void
224+
{
225+
$tokens = new TempestViewLexer('<?php echo "hi";')->lex();
226+
227+
$this->assertTokens([
228+
new Token('<?php echo "hi";', TokenType::PHP),
229+
], $tokens);
230+
}
231+
232+
public function test_unclosed_comment_tag(): void
233+
{
234+
$tokens = new TempestViewLexer('<!-- comment')->lex();
235+
236+
$this->assertTokens([
237+
new Token('<!-- comment', TokenType::COMMENT),
238+
], $tokens);
239+
}
240+
223241
private function assertTokens(array $expected, TokenCollection $actual): void
224242
{
225243
$this->assertCount(count($expected), $actual);

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,13 @@ public function test_escape_expression_attribute(): void
709709

710710
$this->assertSnippetsMatch('<div :escaped="foo"></div>', $html);
711711
}
712+
713+
public function test_unclosed_php_tag(): void
714+
{
715+
$html = $this->render(<<<'HTML'
716+
<?php echo 'hi';
717+
HTML);
718+
719+
$this->assertSame('hi', $html);
720+
}
712721
}

0 commit comments

Comments
 (0)