Skip to content

Commit 96e9cca

Browse files
committed
bug symfony#14698 [2.3] Fix HTML escaping of to-source links (nicolas-grekas)
This PR was merged into the 2.3 branch. Discussion ---------- [2.3] Fix HTML escaping of to-source links | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 385a6b7 Fix HTML escaping of to-source links
2 parents ea6e3d5 + 385a6b7 commit 96e9cca

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getFilters()
4949
new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
5050
new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
5151
new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
52-
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink'), array('is_safe' => array('html'))),
52+
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
5353
);
5454
}
5555

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,25 @@ public function fileExcerpt($file, $line)
154154
*/
155155
public function formatFile($file, $line, $text = null)
156156
{
157+
if (PHP_VERSION_ID >= 50400) {
158+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
159+
} else {
160+
$flags = ENT_QUOTES;
161+
}
162+
157163
if (null === $text) {
158164
$file = trim($file);
159165
$fileStr = $file;
160166
if (0 === strpos($fileStr, $this->rootDir)) {
161167
$fileStr = str_replace($this->rootDir, '', str_replace('\\', '/', $fileStr));
162-
$fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', $this->rootDir, $fileStr);
168+
$fileStr = htmlspecialchars($fileStr, $flags, $this->charset);
169+
$fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', htmlspecialchars($this->rootDir, $flags, $this->charset), $fileStr);
163170
}
164171

165-
$text = "$fileStr at line $line";
172+
$text = sprintf('%s at line %d', $fileStr, $line);
166173
}
167174

168175
if (false !== $link = $this->getFileLink($file, $line)) {
169-
if (PHP_VERSION_ID >= 50400) {
170-
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
171-
} else {
172-
$flags = ENT_QUOTES;
173-
}
174-
175176
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
176177
}
177178

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ public function getContent(FlattenException $exception)
157157
}
158158
if (isset($trace['file']) && isset($trace['line'])) {
159159
if ($linkFormat = ini_get('xdebug.file_link_format')) {
160-
$link = str_replace(array('%f', '%l'), array($trace['file'], $trace['line']), $linkFormat);
161-
$content .= sprintf(' in <a href="%s" title="Go to source">%s line %s</a>', $link, $trace['file'], $trace['line']);
160+
$link = strtr($linkFormat, array('%f' => $trace['file'], '%l' => $trace['line']));
161+
$link = htmlspecialchars($link, $flags, $this->charset);
162+
$content .= sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $link, htmlspecialchars($trace['file'], $flags, $this->charset), $trace['line']);
162163
} else {
163-
$content .= sprintf(' in %s line %s', $trace['file'], $trace['line']);
164+
$content .= sprintf(' in %s line %d', htmlspecialchars($trace['file'], $flags, $this->charset), $trace['line']);
164165
}
165166
}
166167
$content .= "</li>\n";

0 commit comments

Comments
 (0)