Html Writer Validate Hyperlink Protocols

This commit is contained in:
oleibman
2024-09-14 05:27:25 -07:00
parent fa657fd99b
commit 8b9b378ecd
2 changed files with 41 additions and 1 deletions
+8 -1
View File
@@ -1512,7 +1512,14 @@ class Html extends BaseWriter
// Hyperlink?
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) {
$cellData = '<a href="' . htmlspecialchars($worksheet->getHyperlink($coordinate)->getUrl(), Settings::htmlEntityFlags()) . '" title="' . htmlspecialchars($worksheet->getHyperlink($coordinate)->getTooltip(), Settings::htmlEntityFlags()) . '">' . $cellData . '</a>';
$url = $worksheet->getHyperlink($coordinate)->getUrl();
$urldecode = strtolower(html_entity_decode(trim($url), encoding: 'UTF-8'));
$parseScheme = preg_match('/^(\\w+):/', $urldecode, $matches);
if ($parseScheme === 1 && !in_array($matches[1], ['http', 'https', 'file', 'ftp', 's3'], true)) {
$cellData = htmlspecialchars($url, Settings::htmlEntityFlags());
} else {
$cellData = '<a href="' . htmlspecialchars($url, Settings::htmlEntityFlags()) . '" title="' . htmlspecialchars($worksheet->getHyperlink($coordinate)->getTooltip(), Settings::htmlEntityFlags()) . '">' . $cellData . '</a>';
}
}
// Should the cell be written or is it swallowed by a rowspan or colspan?
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PHPUnit\Framework\TestCase;
class NoJavascriptLinksTest extends TestCase
{
public function testNoJavascriptLinks(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('Click me');
$hyperlink = new Hyperlink('http://www.example.com');
$sheet->getCell('A1')->setHyperlink($hyperlink);
$sheet->getCell('A2')->setValue('JS link');
$hyperlink2 = new Hyperlink('javascript:alert(\'hello1\')');
$sheet->getCell('A2')->setHyperlink($hyperlink2);
$sheet->getCell('A3')->setValue('=HYPERLINK("javascript:alert(\'hello2\')", "jsfunc click")');
$writer = new Html($spreadsheet);
$html = $writer->generateHTMLAll();
self::assertStringContainsString('<td class="column0 style0 s"><a href="http://www.example.com" title="">Click me</a></td>', $html, 'http hyperlink retained');
self::assertStringContainsString('<td class="column0 style0 s">javascript:alert(\'hello1\')</td>', $html, 'javascript hyperlink dropped');
self::assertStringContainsString('<td class="column0 style0 f">javascript:alert(\'hello2\')</td>', $html, 'javascript hyperlink function dropped');
$spreadsheet->disconnectWorksheets();
}
}