Skip to content

Commit 2c9139f

Browse files
authored
Security: Fix unauthenticated Template creation via /template/document-templates/create
Refs GHSA-hg2v-955j-35c3
1 parent 3de0d06 commit 2c9139f

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/CoreBundle/Controller/TemplateController.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
1515
use Chamilo\CoreBundle\Repository\SystemTemplateRepository;
1616
use Chamilo\CoreBundle\Repository\TemplatesRepository;
17+
use Chamilo\CoreBundle\Security\Authorization\Voter\CourseVoter;
1718
use Chamilo\CoreBundle\Settings\SettingsManager;
1819
use Chamilo\CourseBundle\Entity\CDocument;
1920
use Chamilo\CourseBundle\Repository\CDocumentRepository;
@@ -40,6 +41,18 @@ public function createDocumentTemplate(Request $request, EntityManagerInterface
4041
return $this->json(['error' => 'No image provided.'], Response::HTTP_BAD_REQUEST);
4142
}
4243

44+
$document = $entityManager->getRepository(CDocument::class)->find($documentId);
45+
if (!$document) {
46+
return $this->json(['error' => 'Document not found.'], Response::HTTP_NOT_FOUND);
47+
}
48+
49+
$documentCourse = $document->getFirstResourceLink()?->getCourse();
50+
if (!$documentCourse instanceof Course) {
51+
throw $this->createAccessDeniedException();
52+
}
53+
54+
$this->denyAccessUnlessGranted(CourseVoter::EDIT, $documentCourse);
55+
4356
$user = $userHelper->getCurrent();
4457
$course = null;
4558
if ($cid) {
@@ -62,13 +75,8 @@ public function createDocumentTemplate(Request $request, EntityManagerInterface
6275
$template->setImage($asset);
6376
$entityManager->persist($template);
6477

65-
$document = $entityManager->getRepository(CDocument::class)->find($documentId);
66-
if ($document) {
67-
$document->setTemplate(true);
68-
$entityManager->persist($document);
69-
} else {
70-
return $this->json(['error' => 'Document not found.'], Response::HTTP_NOT_FOUND);
71-
}
78+
$document->setTemplate(true);
79+
$entityManager->persist($document);
7280

7381
$entityManager->flush();
7482

0 commit comments

Comments
 (0)