From fd7106af28147df16a56484f4994c117681e5d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Thu, 4 Dec 2025 23:31:42 +0100 Subject: [PATCH] Allow that the DEFAULT_URI does not end with a slash We normalize the url with an env var processor before passing it to the saml lib, to avoid an error. Fixes issue #1118 --- .env | 1 - config/parameters.yaml | 2 +- .../AddSlashEnvVarProcessor.php | 49 +++++++++++++++++++ .../CustomEnvVarProcessor.php | 2 +- .../AddSlashEnvVarProcessorTest.php | 44 +++++++++++++++++ 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/EnvVarProcessors/AddSlashEnvVarProcessor.php rename src/{Services => EnvVarProcessors}/CustomEnvVarProcessor.php (98%) create mode 100644 tests/EnvVarProcessors/AddSlashEnvVarProcessorTest.php diff --git a/.env b/.env index 89dc55d5..9a6ce846 100644 --- a/.env +++ b/.env @@ -32,7 +32,6 @@ DATABASE_EMULATE_NATURAL_SORT=0 ################################################################################### # The public reachable URL of this Part-DB installation. This is used for generating links in SAML and email templates or when no request context is available. -# This must end with a slash! DEFAULT_URI="https://partdb.changeme.invalid/" ################################################################################### diff --git a/config/parameters.yaml b/config/parameters.yaml index d4fe7581..b79e2b88 100644 --- a/config/parameters.yaml +++ b/config/parameters.yaml @@ -10,7 +10,7 @@ parameters: partdb.title: '%env(string:settings:customization:instanceName)%' # The title shown inside of Part-DB (e.g. in the navbar and on homepage) partdb.locale_menu: ['en', 'de', 'it', 'fr', 'ru', 'ja', 'cs', 'da', 'zh', 'pl', 'hu'] # The languages that are shown in user drop down menu - partdb.default_uri: '%env(string:DEFAULT_URI)%' # The default URI to use for the Part-DB instance (e.g. https://part-db.example.com/). This is used for generating links in emails + partdb.default_uri: '%env(addSlash:string:DEFAULT_URI)%' # The default URI to use for the Part-DB instance (e.g. https://part-db.example.com/). This is used for generating links in emails partdb.db.emulate_natural_sort: '%env(bool:DATABASE_EMULATE_NATURAL_SORT)%' # If this is set to true, natural sorting is emulated on platforms that do not support it natively. This can be slow on large datasets. diff --git a/src/EnvVarProcessors/AddSlashEnvVarProcessor.php b/src/EnvVarProcessors/AddSlashEnvVarProcessor.php new file mode 100644 index 00000000..aaf0abc9 --- /dev/null +++ b/src/EnvVarProcessors/AddSlashEnvVarProcessor.php @@ -0,0 +1,49 @@ +. + */ + +declare(strict_types=1); + + +namespace App\EnvVarProcessors; + +use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; + +/** + * Env var processor that adds a trailing slash to a string if not already present. + */ +final class AddSlashEnvVarProcessor implements EnvVarProcessorInterface +{ + + public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed + { + $env = $getEnv($name); + if (!is_string($env)) { + throw new \InvalidArgumentException(sprintf('The "addSlash" env var processor only works with strings, got %s.', gettype($env))); + } + return rtrim($env, '/') . '/'; + } + + public static function getProvidedTypes(): array + { + return [ + 'addSlash' => 'string', + ]; + } +} diff --git a/src/Services/CustomEnvVarProcessor.php b/src/EnvVarProcessors/CustomEnvVarProcessor.php similarity index 98% rename from src/Services/CustomEnvVarProcessor.php rename to src/EnvVarProcessors/CustomEnvVarProcessor.php index f269cc7d..55a6b94d 100644 --- a/src/Services/CustomEnvVarProcessor.php +++ b/src/EnvVarProcessors/CustomEnvVarProcessor.php @@ -20,7 +20,7 @@ declare(strict_types=1); -namespace App\Services; +namespace App\EnvVarProcessors; use Closure; use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; diff --git a/tests/EnvVarProcessors/AddSlashEnvVarProcessorTest.php b/tests/EnvVarProcessors/AddSlashEnvVarProcessorTest.php new file mode 100644 index 00000000..4099f0ee --- /dev/null +++ b/tests/EnvVarProcessors/AddSlashEnvVarProcessorTest.php @@ -0,0 +1,44 @@ +. + */ + +namespace App\Tests\EnvVarProcessors; + +use App\EnvVarProcessors\AddSlashEnvVarProcessor; +use PHPUnit\Framework\TestCase; + +class AddSlashEnvVarProcessorTest extends TestCase +{ + protected AddSlashEnvVarProcessor $processor; + + protected function setUp(): void + { + $this->processor = new AddSlashEnvVarProcessor(); + } + + public function testGetEnv(): void + { + $getEnv = function ($name) { + return $name; + }; + + $this->assertEquals('http://example.com/', $this->processor->getEnv('addSlash', 'http://example.com', $getEnv)); + $this->assertEquals('http://example.com/', $this->processor->getEnv('addSlash', 'http://example.com/', $getEnv)); + } +}