Escape root profile name in HtmlDumper

This commit is contained in:
Fabien Potencier
2026-05-23 08:42:10 +02:00
parent ead63cc824
commit f6aca309d8
4 changed files with 22 additions and 1 deletions
+1
View File
@@ -1,6 +1,7 @@
# 3.27.0 (2026-XX-XX)
* Restrict allowed classes in `Twig\Profiler\Profile::unserialize()` to prevent arbitrary class instantiation
* Escape root profile name in `HtmlDumper`
* Deprecate the `Twig\Sandbox\SourcePolicyInterface` interface with no replacement
# 3.26.0 (2026-05-20)
+6 -1
View File
@@ -31,11 +31,16 @@ abstract class BaseDumper
abstract protected function formatTime(Profile $profile, $percent): string;
protected function formatRoot(Profile $profile): string
{
return $profile->getName();
}
private function dumpProfile(Profile $profile, $prefix = '', $sibling = false): string
{
if ($profile->isRoot()) {
$this->root = $profile->getDuration();
$start = $profile->getName();
$start = $this->formatRoot($profile);
} else {
if ($profile->isTemplate()) {
$start = $this->formatTemplate($profile, $prefix);
+5
View File
@@ -30,6 +30,11 @@ final class HtmlDumper extends BaseDumper
return '<pre>'.parent::dump($profile).'</pre>';
}
protected function formatRoot(Profile $profile): string
{
return self::escape($profile->getName());
}
protected function formatTemplate(Profile $profile, $prefix): string
{
return \sprintf('%s└ <span style="background-color: %s">%s</span>', $prefix, self::$colors['template'], self::escape($profile->getTemplate()));
+10
View File
@@ -59,4 +59,14 @@ EOF, $dumper->dump($this->getProfile()));
$this->assertStringContainsString('&lt;img src=x onerror=alert(2)&gt;', $output);
$this->assertStringContainsString('&lt;img src=x onerror=alert(3)&gt;', $output);
}
public function testDumpEscapesRootProfileName()
{
$root = new Profile('template-name', Profile::ROOT, '<img src=x onerror=alert(1)>');
$output = (new HtmlDumper())->dump($root);
$this->assertStringNotContainsString('<img src=x onerror=alert(1)>', $output);
$this->assertStringContainsString('&lt;img src=x onerror=alert(1)&gt;', $output);
}
}