From 6a01a07ffddac5dd7f0fe018523e91c2dd2ac65e Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 18:40:16 +0100 Subject: [PATCH] Type and escape Twig asset tags, improve timeago plural forms, and cover installer debug-mode installed check (#3631) * Initial plan * Harden Twig asset tags and improve installer debug test coverage * Finalize Twig escaping, pluralization, and installer debug coverage fixes --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .../Twig/Extension/FOSSBillingExtension.php | 38 +++++++++++-------- .../FOSSBilling/FOSSBillingExtensionTest.php | 25 +++++++++++- .../FOSSBilling/FOSSBilling_ConfigTest.php | 17 ++++++--- 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/library/FOSSBilling/Twig/Extension/FOSSBillingExtension.php b/src/library/FOSSBilling/Twig/Extension/FOSSBillingExtension.php index a3f251a86..66a540b35 100644 --- a/src/library/FOSSBilling/Twig/Extension/FOSSBillingExtension.php +++ b/src/library/FOSSBilling/Twig/Extension/FOSSBillingExtension.php @@ -304,7 +304,7 @@ class FOSSBillingExtension } #[AsTwigFilter('script_tag', isSafe: ['html'])] - public function scriptTag($path): string + public function scriptTag(string $path): string { if ($this->isAssetLoaded($path)) { return ''; @@ -312,11 +312,14 @@ class FOSSBillingExtension $this->markAssetAsLoaded($path); - return sprintf('', $path, $this->getCacheBuster($path)); + $escapedPath = htmlspecialchars($path, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $escapedCacheBuster = htmlspecialchars($this->getCacheBuster($path), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + + return sprintf('', $escapedPath, $escapedCacheBuster); } #[AsTwigFilter('stylesheet_tag', isSafe: ['html'])] - public function stylesheetTag($path, $media = 'screen'): string + public function stylesheetTag(string $path, string $media = 'screen'): string { if ($this->isAssetLoaded($path)) { return ''; @@ -324,7 +327,11 @@ class FOSSBillingExtension $this->markAssetAsLoaded($path); - return sprintf('', $path, $this->getCacheBuster($path), $media); + $escapedPath = htmlspecialchars($path, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $escapedCacheBuster = htmlspecialchars($this->getCacheBuster($path), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $escapedMedia = htmlspecialchars($media, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + + return sprintf('', $escapedPath, $escapedCacheBuster, $escapedMedia); } #[AsTwigFilter('timeago')] @@ -340,22 +347,23 @@ class FOSSBillingExtension return '-'; } $tokens = [ - 315_705_600 => __trans('decade'), - 31_570_560 => __trans('year'), - 2_630_880 => __trans('month'), - 604_800 => __trans('week'), - 86400 => __trans('day'), - 3600 => __trans('hour'), - 60 => __trans('minute'), - 1 => __trans('second'), + 315_705_600 => ['one' => __trans('decade'), 'other' => __trans('decades')], + 31_570_560 => ['one' => __trans('year'), 'other' => __trans('years')], + 2_630_880 => ['one' => __trans('month'), 'other' => __trans('months')], + 604_800 => ['one' => __trans('week'), 'other' => __trans('weeks')], + 86400 => ['one' => __trans('day'), 'other' => __trans('days')], + 3600 => ['one' => __trans('hour'), 'other' => __trans('hours')], + 60 => ['one' => __trans('minute'), 'other' => __trans('minutes')], + 1 => ['one' => __trans('second'), 'other' => __trans('seconds')], ]; - foreach ($tokens as $unit => $text) { + foreach ($tokens as $unit => $forms) { if ($timeAgo < $unit) { continue; } - $numberOfUnits = floor($timeAgo / $unit); + $numberOfUnits = (int) floor($timeAgo / $unit); + $text = ($numberOfUnits === 1) ? $forms['one'] : $forms['other']; - return sprintf('%d %s%s', $numberOfUnits, $text, ($numberOfUnits > 1) ? 's' : ''); + return sprintf('%d %s', $numberOfUnits, $text); } return ''; diff --git a/tests-legacy/library/FOSSBilling/FOSSBillingExtensionTest.php b/tests-legacy/library/FOSSBilling/FOSSBillingExtensionTest.php index 867c23235..61d044f15 100644 --- a/tests-legacy/library/FOSSBilling/FOSSBillingExtensionTest.php +++ b/tests-legacy/library/FOSSBilling/FOSSBillingExtensionTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use FOSSBilling\Twig\Extension\FOSSBillingExtension; +use Pimple\Container; #[PHPUnit\Framework\Attributes\Group('Core')] final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase @@ -11,7 +12,7 @@ final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase protected function setUp(): void { - $this->extension = new FOSSBillingExtension(null); + $this->extension = new FOSSBillingExtension(new Container()); } public function testTransWithoutValues(): void @@ -64,4 +65,26 @@ final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase $this->assertSame('<strong>Fallback</strong>', $result); } + + public function testScriptTagEscapesPath(): void + { + $result = $this->extension->scriptTag('js/app".js'); + + $this->assertStringContainsString('src="js/app".js?', $result); + } + + public function testStylesheetTagEscapesPathAndMedia(): void + { + $result = $this->extension->stylesheetTag('css/app".css', 'screen" media'); + + $this->assertStringContainsString('href="css/app".css?v=', $result); + $this->assertStringContainsString('media="screen" media"', $result); + } + + public function testTimeagoUsesPluralFormsWithoutSuffixingS(): void + { + $result = $this->extension->timeago(date('Y-m-d H:i:s', time() - 120)); + + $this->assertSame('2 minutes', $result); + } } diff --git a/tests-legacy/library/FOSSBilling/FOSSBilling_ConfigTest.php b/tests-legacy/library/FOSSBilling/FOSSBilling_ConfigTest.php index 277a1613f..b245b887b 100644 --- a/tests-legacy/library/FOSSBilling/FOSSBilling_ConfigTest.php +++ b/tests-legacy/library/FOSSBilling/FOSSBilling_ConfigTest.php @@ -91,6 +91,13 @@ final class FOSSBilling_ConfigTest extends PHPUnit\Framework\TestCase $this->assertSame('installed', $this->runInstallerInstalledCheck()); } + public function testInstallerTreatsExistingConfigAsNotInstalledInDebugMode(): void + { + $this->writeRawConfig(" ['debug' => true]];"); + + $this->assertSame('not-installed', $this->runInstallerInstalledCheck()); + } + public function testInstallerAllowsInstallWhenConfigFileIsMissing(): void { $this->removeConfig(); @@ -128,11 +135,11 @@ final class FOSSBilling_ConfigTest extends PHPUnit\Framework\TestCase private function runInstallerInstalledCheck(): string { $code = <<<'PHP' -require 'src/install/install.php'; -$request = Symfony\Component\HttpFoundation\Request::create('http://localhost/install/install.php', 'GET'); -$installer = new FOSSBilling_Installer($request); -echo $installer->isAlreadyInstalled() ? 'installed' : 'not-installed'; -PHP; + require 'src/install/install.php'; + $request = Symfony\Component\HttpFoundation\Request::create('http://localhost/install/install.php', 'GET'); + $installer = new FOSSBilling_Installer($request); + echo $installer->isAlreadyInstalled() ? 'installed' : 'not-installed'; + PHP; $pipes = []; $process = proc_open(