Skip to content

Commit

Permalink
prevent csrf to flush logs (#2930)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Nov 14, 2021
1 parent 8b0962e commit 6b49535
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Controller/DoctorController.php
Expand Up @@ -15,6 +15,8 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

/**
* @Route(path="/doctor")
Expand Down Expand Up @@ -56,11 +58,19 @@ public function __construct(string $projectDirectory, string $kernelEnvironment,
}

/**
* @Route(path="/flush-log", name="doctor_flush_log", methods={"GET"})
* @Route(path="/flush-log/{token}", name="doctor_flush_log", methods={"GET"})
* @Security("is_granted('system_configuration')")
*/
public function deleteLogfileAction(): Response
public function deleteLogfileAction(string $token, CsrfTokenManagerInterface $csrfTokenManager): Response
{
if (!$csrfTokenManager->isTokenValid(new CsrfToken('doctor.flush_log', $token))) {
$this->flashError('action.delete.error');

return $this->redirectToRoute('doctor');
}

$csrfTokenManager->refreshToken($token);

$logfile = $this->getLogFilename();

if (file_exists($logfile)) {
Expand Down
2 changes: 1 addition & 1 deletion templates/doctor/index.html.twig
Expand Up @@ -80,7 +80,7 @@
{% block box_title %}Logfile (max. {{ logLines }} last lines){% endblock %}
{% block box_tools %}
{% if log_delete %}
<a class="btn-box-tool confirmation-link" href="{{ path('doctor_flush_log') }}" data-question="confirm.delete"><i class="{{ 'delete'|icon }}"></i></a>
<a class="btn-box-tool confirmation-link" href="{{ path('doctor_flush_log', {'token': csrf_token('doctor.flush_log')}) }}" data-question="confirm.delete"><i class="{{ 'delete'|icon }}"></i></a>
{% endif %}
{% endblock %}
{% block box_body %}
Expand Down

0 comments on commit 6b49535

Please sign in to comment.