Skip to content

Commit 8372a10

Browse files
committed
Ensure title is escaped
1 parent 2d73aa8 commit 8372a10

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This release marked the addition of strict typing and return type declarations (
2929
- Fix HTML border-color parsing. @troosan #1551 #1570
3030
- Fixed specifying cell widths, background color, etc on `PhpOffice\PhpWord\Style\Cell` @0b10011 #1669
3131
- Escape arrays of replacements in `TemplateProcessor` @0b10011 #1669
32+
- Escape text provided for `<title>` when exporting to HTML @0b10011
3233

3334
### Miscellaneous
3435
-

src/PhpWord/Writer/HTML/Part/Head.php

+27-14
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,32 @@ public function write()
5454
$title = $docProps->getTitle();
5555
$title = ($title != '') ? $title : 'PHPWord';
5656

57-
$content = '';
57+
$html = '';
5858

59-
$content .= '<head>' . PHP_EOL;
60-
$content .= '<meta charset="UTF-8" />' . PHP_EOL;
61-
$content .= '<title>' . $title . '</title>' . PHP_EOL;
62-
foreach ($propertiesMapping as $key => $value) {
63-
$value = ($value == '') ? $key : $value;
59+
$html .= '<head>' . PHP_EOL;
60+
$html .= '<meta charset="UTF-8" />' . PHP_EOL;
61+
$html .= '<title>' . (Settings::isOutputEscapingEnabled() ? $this->escaper->escapeHtml($title) : $title) . '</title>' . PHP_EOL;
62+
foreach ($propertiesMapping as $key => $name) {
63+
$name = ($name == '') ? $key : $name;
6464
$method = 'get' . $key;
65-
if ($docProps->$method() != '') {
66-
$content .= '<meta name="' . $value . '"'
67-
. ' content="' . (Settings::isOutputEscapingEnabled() ? $this->escaper->escapeHtmlAttr($docProps->$method()) : $docProps->$method()) . '"'
68-
. ' />' . PHP_EOL;
65+
66+
$content = $docProps->$method();
67+
if ($content === '') {
68+
continue;
69+
}
70+
71+
if (Settings::isOutputEscapingEnabled()) {
72+
$content = $this->escaper->escapeHtmlAttr($content);
6973
}
74+
75+
$html .= '<meta name="' . $name . '"'
76+
. ' content="' . $content . '"'
77+
. ' />' . PHP_EOL;
7078
}
71-
$content .= $this->writeStyles();
72-
$content .= '</head>' . PHP_EOL;
79+
$html .= $this->writeStyles();
80+
$html .= '</head>' . PHP_EOL;
7381

74-
return $content;
82+
return $html;
7583
}
7684

7785
/**
@@ -81,7 +89,12 @@ public function write()
8189
*/
8290
private function writeStyles()
8391
{
84-
$css = '<style>' . PHP_EOL;
92+
// Stylesheets with the title "PHPWord Base Styles"
93+
// are ignored during read
94+
// so we can make the HTML document look nice
95+
// without interfering with the styles
96+
// when we import.
97+
$css = '<style title="PHPWord Base Styles">' . PHP_EOL;
8598

8699
// Default styles
87100
$defaultStyles = array(

tests/PhpWord/Writer/HTMLTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ public function testSave()
147147
unlink($file);
148148
}
149149

150+
public function testEscaping()
151+
{
152+
$file = __DIR__ . '/../_files/temp.html';
153+
154+
$phpWord = new PhpWord();
155+
Settings::setOutputEscapingEnabled(true);
156+
157+
$docProps = $phpWord->getDocInfo();
158+
$docProps->setTitle('"Test" & <hack>');
159+
160+
$writer = new HTML($phpWord);
161+
162+
$writer->save($file);
163+
$this->assertFileExists($file);
164+
$html = file_get_contents($file);
165+
166+
$this->assertInternalType('int', strpos($html, '<title>&quot;Test&quot; &amp; &lt;hack&gt;</title>'), 'Quotes, ampersand, and tag should be escaped in title value');
167+
$this->assertInternalType('int', strpos($html, '<meta name="title" content="&quot;Test&quot;&#x20;&amp;&#x20;&lt;hack&gt;" />'), 'Quotes, ampersand, and tag should be escaped in attribute');
168+
169+
unlink($file);
170+
}
171+
150172
public function testTitle()
151173
{
152174
$file = __DIR__ . '/../_files/temp.html';

0 commit comments

Comments
 (0)