mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
fixes potential XSS issue with TOTP exception lists (which need escaping, see also #1185). refactors Template escaping to use TemplateEscape enum (rather than the previous boolean). thanks @TrapoSAMA
70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class PFASmartyEscapingTest extends TestCase
|
|
{
|
|
public static function modes(): array
|
|
{
|
|
return [
|
|
'default' => [null, true],
|
|
'explicit HTML' => [TemplateEscape::HTML, true],
|
|
'explicit RAW' => [TemplateEscape::RAW, false],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('modes')]
|
|
public function testAssignmentEscapingAndRawCompatibility(?TemplateEscape $mode, bool $escaped): void
|
|
{
|
|
$smarty = PFASmarty::getInstance();
|
|
$value = ['nested' => ['text' => '<b>R&D</b>']];
|
|
if ($mode === null) {
|
|
$smarty->assign('escaping_test', $value);
|
|
} else {
|
|
$smarty->assign('escaping_test', $value, $mode);
|
|
}
|
|
self::assertSame(($escaped ? '<b>R&D</b>' : '<b>R&D</b>') . '|<b>R&D</b>', $this->renderValue());
|
|
}
|
|
|
|
public static function invalidModes(): array
|
|
{
|
|
return [
|
|
'false' => [false],
|
|
'true' => [true],
|
|
'null' => [null],
|
|
'zero' => [0],
|
|
'empty string' => [''],
|
|
'HTML string' => ['html'],
|
|
'RAW string' => ['raw'],
|
|
'old constant value' => ['no_escaping'],
|
|
'array' => [[]],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('invalidModes')]
|
|
public function testInvalidModeIsRejectedBeforeAssignment(mixed $mode): void
|
|
{
|
|
$smarty = PFASmarty::getInstance();
|
|
$smarty->assign('escaping_test', ['nested' => ['text' => '<b>original</b>']]);
|
|
try {
|
|
$smarty->assign('escaping_test', ['nested' => ['text' => 'replacement']], $mode);
|
|
self::fail('An invalid mode must throw TypeError');
|
|
} catch (TypeError $error) {
|
|
self::assertStringContainsString('TemplateEscape', $error->getMessage());
|
|
}
|
|
self::assertSame('<b>original</b>|<b>original</b>', $this->renderValue());
|
|
}
|
|
|
|
private function renderValue(): string
|
|
{
|
|
ob_start();
|
|
try {
|
|
PFASmarty::getInstance()->display('string:{$escaping_test.nested.text}|{$RAW_escaping_test.nested.text}');
|
|
return (string)ob_get_contents();
|
|
} finally {
|
|
ob_end_clean();
|
|
}
|
|
}
|
|
}
|