Skip to content

Commit 94b4edf

Browse files
committed
Prevent syntax issues if docs contains "*/"
1 parent 98aff4d commit 94b4edf

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Node/BlockNode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ public function __construct(string $name, Node $body, int $lineno)
3030

3131
public function compile(Compiler $compiler): void
3232
{
33+
$docs = '';
34+
if ($this->hasAttribute('docs')) {
35+
// Escape */ to prevent closing the PHPDoc comment prematurely
36+
$docsContent = str_replace('*/', '* /', $this->getAttribute('docs'));
37+
$docs = ' * '.str_replace("\n", "\n * ", $docsContent)."\n";
38+
}
39+
3340
$compiler
3441
->addDebugInfo($this)
3542
->write("/**\n")
36-
->write($this->hasAttribute('docs') ? ' * '.str_replace("\n", "\n * ", $this->getAttribute('docs'))."\n" : '')
43+
->write($docs)
3744
->write(" * @return iterable<null|scalar|\Stringable>\n")
3845
->write(" */\n")
3946
->write(\sprintf("public function block_%s(array \$context, array \$blocks = []): iterable\n", $this->getAttribute('name')), "{\n")

tests/Node/BlockTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ public function block_foo(array \$context, array \$blocks = []): iterable
9393
EOF, new Environment(new ArrayLoader()),
9494
];
9595

96+
// block with docs containing */ (should be escaped)
97+
$blockWithDocsContainingCommentEnd = new BlockNode('foo', new TextNode('foo', 1), 1);
98+
$blockWithDocsContainingCommentEnd->setAttribute('docs', 'This contains */ which could break things');
99+
$tests[] = [$blockWithDocsContainingCommentEnd, <<<EOF
100+
// line 1
101+
/**
102+
* This contains * / which could break things
103+
* @return iterable<null|scalar|\Stringable>
104+
*/
105+
public function block_foo(array \$context, array \$blocks = []): iterable
106+
{
107+
\$macros = \$this->macros;
108+
yield "foo";
109+
yield from [];
110+
}
111+
EOF, new Environment(new ArrayLoader()),
112+
];
113+
96114
return $tests;
97115
}
98116
}

0 commit comments

Comments
 (0)