Skip to content

Commit fd7106a

Browse files
committed
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
1 parent cb6da36 commit fd7106a

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ DATABASE_EMULATE_NATURAL_SORT=0
3232
###################################################################################
3333

3434
# 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.
35-
# This must end with a slash!
3635
DEFAULT_URI="https://partdb.changeme.invalid/"
3736

3837
###################################################################################

config/parameters.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ parameters:
1010
partdb.title: '%env(string:settings:customization:instanceName)%' # The title shown inside of Part-DB (e.g. in the navbar and on homepage)
1111
partdb.locale_menu: ['en', 'de', 'it', 'fr', 'ru', 'ja', 'cs', 'da', 'zh', 'pl', 'hu'] # The languages that are shown in user drop down menu
1212

13-
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
13+
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
1414

1515
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.
1616

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\EnvVarProcessors;
25+
26+
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
27+
28+
/**
29+
* Env var processor that adds a trailing slash to a string if not already present.
30+
*/
31+
final class AddSlashEnvVarProcessor implements EnvVarProcessorInterface
32+
{
33+
34+
public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
35+
{
36+
$env = $getEnv($name);
37+
if (!is_string($env)) {
38+
throw new \InvalidArgumentException(sprintf('The "addSlash" env var processor only works with strings, got %s.', gettype($env)));
39+
}
40+
return rtrim($env, '/') . '/';
41+
}
42+
43+
public static function getProvidedTypes(): array
44+
{
45+
return [
46+
'addSlash' => 'string',
47+
];
48+
}
49+
}

src/Services/CustomEnvVarProcessor.php renamed to src/EnvVarProcessors/CustomEnvVarProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace App\Services;
23+
namespace App\EnvVarProcessors;
2424

2525
use Closure;
2626
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace App\Tests\EnvVarProcessors;
22+
23+
use App\EnvVarProcessors\AddSlashEnvVarProcessor;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class AddSlashEnvVarProcessorTest extends TestCase
27+
{
28+
protected AddSlashEnvVarProcessor $processor;
29+
30+
protected function setUp(): void
31+
{
32+
$this->processor = new AddSlashEnvVarProcessor();
33+
}
34+
35+
public function testGetEnv(): void
36+
{
37+
$getEnv = function ($name) {
38+
return $name;
39+
};
40+
41+
$this->assertEquals('http://example.com/', $this->processor->getEnv('addSlash', 'http://example.com', $getEnv));
42+
$this->assertEquals('http://example.com/', $this->processor->getEnv('addSlash', 'http://example.com/', $getEnv));
43+
}
44+
}

0 commit comments

Comments
 (0)