Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Controller/Security/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Signalement;
use App\Entity\User;
use App\Manager\UserManager;
use App\Repository\FileRepository;
use App\Repository\SignalementRepository;
use App\Security\User\SignalementBailleur;
use App\Security\Voter\SignalementFoVoter;
Expand Down Expand Up @@ -162,14 +163,25 @@ public function loginBailleur(
public function showUploadedFile(
LoggerInterface $logger,
ImageVariantProvider $imageVariantProvider,
FileRepository $fileRepository,
string $filename,
?Signalement $signalement = null,
): Response {
if (!$signalement) {
Comment thread
hmeneuvrier marked this conversation as resolved.
Resolved
throw $this->createAccessDeniedException();
}
$request = Request::createFromGlobals();
if (
!$this->isCsrfTokenValid('suivi_signalement_ext_file_view', $request->query->get('t'))
&& !$this->isGranted(SignalementVoter::SIGN_VIEW, $signalement)
) {
$isFileOfSignalement = null !== $fileRepository->findOneBy(['filename' => $filename, 'signalement' => $signalement]);

// Usager : liens des anciens suivis, token lié au signalement généré à l'affichage
$canViewWithToken = $isFileOfSignalement
&& $this->isCsrfTokenValid('suivi_signalement_ext_file_view_'.$signalement->getUuid(), $request->query->get('t'));

// Partenaire : liens des anciens suivis et export PDF (sans entité File, uuid dans le nom)
$canViewAsPartner = ($isFileOfSignalement || str_contains($filename, $signalement->getUuid()))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Penser à simplifier quand le #6789 sera fait :

Stocker dans la table file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'ai mis un commentaire, et j'ajoute un truc dans l'autre ticket

&& $this->isGranted(SignalementVoter::SIGN_VIEW, $signalement);

if (!$canViewWithToken && !$canViewAsPartner) {
throw $this->createAccessDeniedException();
}
try {
Expand Down
4 changes: 3 additions & 1 deletion templates/front/_partials/_suivi_signalement_suivi.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
{% endif %}
<div class="message-box-message">
{% set recipient = forUsager ? constant('App\\Service\\Signalement\\Suivi\\SuiviRecipient::USAGER') : constant('App\\Service\\Signalement\\Suivi\\SuiviRecipient::DEFAULT') %}
{{ suivi.description(recipient: recipient)|replace({'___TOKEN___':csrf_token('suivi_signalement_ext_file_view')})|raw }}
{% set signalementUuid = suivi.signalement.uuid %}
{% set fileLinkSuffix = '/' ~ signalementUuid ~ '?t&#61;' ~ csrf_token('suivi_signalement_ext_file_view_' ~ signalementUuid) %}
{{ suivi.description(recipient: recipient)|replace({'?t&#61;___TOKEN___': fileLinkSuffix, '&amp;t&#61;___TOKEN___': fileLinkSuffix})|raw }}
Comment thread
hmeneuvrier marked this conversation as resolved.
Resolved
</div>
</li>
18 changes: 16 additions & 2 deletions tests/Functional/Controller/SecurityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@
$client = static::createClient();
/** @var UserRepository $userRepository */
$userRepository = static::getContainer()->get(UserRepository::class);
$user = $userRepository->findOneBy(['email' => 'admin-01@signal-logement.fr']);

Check failure on line 158 in tests/Functional/Controller/SecurityControllerTest.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "admin-01@signal-logement.fr" 3 times.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_histologe&issues=AaIwp5RFjIZAHAoHww25&open=AaIwp5RFjIZAHAoHww25&pullRequest=6795
$client->loginUser($user);
$client->request('GET', '/_up/check.png/00000000-0000-0000-2022-000000000001');
$client->request('GET', '/_up/blank-2022-1.jpg/00000000-0000-0000-2022-000000000001');
/** @var BinaryFileResponse $response */
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
Expand All @@ -173,14 +173,28 @@
$user = $userRepository->findOneBy(['email' => 'admin-01@signal-logement.fr']);
$client->loginUser($user);

$client->request('GET', '/_up/file_not_exist.txt/00000000-0000-0000-2022-000000000001');
$client->request('GET', '/_up/export-pdf-signalement-00000000-0000-0000-2022-000000000001.pdf/00000000-0000-0000-2022-000000000001');
/** @var BinaryFileResponse $response */
$response = $client->getResponse();
$this->assertEquals('image-404.png', $response->getFile()->getFilename());
$this->assertTrue($response->isSuccessful());
$this->assertInstanceOf(BinaryFileResponse::class, $response);
}

public function testShowUploadedFileFromAnotherSignalementIsDenied(): void
{
self::ensureKernelShutdown();
$client = static::createClient();
/** @var UserRepository $userRepository */
$userRepository = static::getContainer()->get(UserRepository::class);
$user = $userRepository->findOneBy(['email' => 'admin-01@signal-logement.fr']);
$client->loginUser($user);

$client->request('GET', '/_up/blank-2022-2.jpg/00000000-0000-0000-2022-000000000001');

$this->assertResponseStatusCodeSame(403);
}

public function testShowUploadedWithInvalidToken(): void
{
self::ensureKernelShutdown();
Expand Down
Loading