Skip to content

Commit 5231dbd

Browse files
committed
Remove project path in twig label error messages to prevent information leakage
1 parent 7767155 commit 5231dbd

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/Controller/AdminPages/BaseAdminController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ protected function _edit(AbstractNamedDBElement $entity, Request $request, Entit
217217
try {
218218
$pdf_data = $this->labelGenerator->generateLabel($entity->getOptions(), $example);
219219
} catch (TwigModeException $exception) {
220-
$form->get('options')->get('lines')->addError(new FormError($exception->getMessage()));
220+
$form->get('options')->get('lines')->addError(new FormError($exception->getSafeMessage()));
221221
}
222222
}
223223

src/Controller/LabelController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function generator(Request $request, ?LabelProfile $profile = null): Resp
117117
$pdf_data = $this->labelGenerator->generateLabel($form_options, $targets);
118118
$filename = $this->getLabelName($targets[0], $profile);
119119
} catch (TwigModeException $exception) {
120-
$form->get('options')->get('lines')->addError(new FormError($exception->getMessage()));
120+
$form->get('options')->get('lines')->addError(new FormError($exception->getSafeMessage()));
121121
}
122122
} else {
123123
//$this->addFlash('warning', 'label_generator.no_entities_found');

src/Exceptions/TwigModeException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,23 @@
4646

4747
class TwigModeException extends RuntimeException
4848
{
49+
private const PROJECT_PATH = __DIR__ . '/../../';
50+
4951
public function __construct(?Error $previous = null)
5052
{
5153
parent::__construct($previous->getMessage(), 0, $previous);
5254
}
55+
56+
/**
57+
* Returns the message of this exception, where it is tried to remove any sensitive information (like filepaths).
58+
* @return string
59+
*/
60+
public function getSafeMessage(): string
61+
{
62+
//Resolve project root path
63+
$projectPath = realpath(self::PROJECT_PATH);
64+
65+
//Remove occurrences of the project path from the message
66+
return str_replace($projectPath, '[Part-DB Root Folder]', $this->getMessage());
67+
}
5368
}
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 - 2024 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\Exceptions;
22+
23+
use App\Exceptions\TwigModeException;
24+
use PHPUnit\Framework\TestCase;
25+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
26+
use Twig\Error\Error;
27+
28+
class TwigModeExceptionTest extends KernelTestCase
29+
{
30+
31+
private string $projectPath;
32+
33+
public function setUp(): void
34+
{
35+
self::bootKernel();
36+
37+
$this->projectPath = self::getContainer()->getParameter('kernel.project_dir');
38+
}
39+
40+
public function testGetSafeMessage(): void
41+
{
42+
$testException = new Error("Error at : " . $this->projectPath . "/src/dir/path/file.php");
43+
44+
$twigModeException = new TwigModeException($testException);
45+
46+
$this->assertSame("Error at : " . $this->projectPath . "/src/dir/path/file.php", $testException->getMessage());
47+
$this->assertSame("Error at : [Part-DB Root Folder]/src/dir/path/file.php", $twigModeException->getSafeMessage());
48+
}
49+
}

0 commit comments

Comments
 (0)