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
This commit is contained in:
Jan Böhmer 2025-12-04 23:31:42 +01:00
parent cb6da36954
commit fd7106af28
5 changed files with 95 additions and 3 deletions

1
.env
View file

@ -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. # 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/" DEFAULT_URI="https://partdb.changeme.invalid/"
################################################################################### ###################################################################################

View file

@ -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.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.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. 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.

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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',
];
}
}

View file

@ -20,7 +20,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Services; namespace App\EnvVarProcessors;
use Closure; use Closure;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;

View file

@ -0,0 +1,44 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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));
}
}