Fix HTML escaping of to-source links

This commit is contained in:
Nicolas Grekas 2015-05-19 17:07:30 -07:00
parent af0e02c35e
commit 385a6b799f
3 changed files with 14 additions and 12 deletions

View File

@ -49,7 +49,7 @@ class CodeExtension extends \Twig_Extension
new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
); );
} }

View File

@ -154,24 +154,25 @@ class CodeHelper extends Helper
*/ */
public function formatFile($file, $line, $text = null) public function formatFile($file, $line, $text = null)
{ {
if (PHP_VERSION_ID >= 50400) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
if (null === $text) { if (null === $text) {
$file = trim($file); $file = trim($file);
$fileStr = $file; $fileStr = $file;
if (0 === strpos($fileStr, $this->rootDir)) { if (0 === strpos($fileStr, $this->rootDir)) {
$fileStr = str_replace($this->rootDir, '', str_replace('\\', '/', $fileStr)); $fileStr = str_replace($this->rootDir, '', str_replace('\\', '/', $fileStr));
$fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', $this->rootDir, $fileStr); $fileStr = htmlspecialchars($fileStr, $flags, $this->charset);
$fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', htmlspecialchars($this->rootDir, $flags, $this->charset), $fileStr);
} }
$text = "$fileStr at line $line"; $text = sprintf('%s at line %d', $fileStr, $line);
} }
if (false !== $link = $this->getFileLink($file, $line)) { if (false !== $link = $this->getFileLink($file, $line)) {
if (PHP_VERSION_ID >= 50400) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text); return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
} }

View File

@ -157,10 +157,11 @@ EOF
} }
if (isset($trace['file']) && isset($trace['line'])) { if (isset($trace['file']) && isset($trace['line'])) {
if ($linkFormat = ini_get('xdebug.file_link_format')) { if ($linkFormat = ini_get('xdebug.file_link_format')) {
$link = str_replace(array('%f', '%l'), array($trace['file'], $trace['line']), $linkFormat); $link = strtr($linkFormat, array('%f' => $trace['file'], '%l' => $trace['line']));
$content .= sprintf(' in <a href="%s" title="Go to source">%s line %s</a>', $link, $trace['file'], $trace['line']); $link = htmlspecialchars($link, $flags, $this->charset);
$content .= sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $link, htmlspecialchars($trace['file'], $flags, $this->charset), $trace['line']);
} else { } else {
$content .= sprintf(' in %s line %s', $trace['file'], $trace['line']); $content .= sprintf(' in %s line %d', htmlspecialchars($trace['file'], $flags, $this->charset), $trace['line']);
} }
} }
$content .= "</li>\n"; $content .= "</li>\n";