Log AccessDeniedExceptions to event log.

This commit is contained in:
Jan Böhmer 2020-04-10 12:28:15 +02:00
parent 26737f4b46
commit f116c2f09e
4 changed files with 98 additions and 3 deletions

View file

@ -46,6 +46,7 @@ use App\Entity\Attachments\Attachment;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\NamedElementInterface; use App\Entity\Contracts\NamedElementInterface;
use App\Entity\LogSystem\AbstractLogEntry; use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\LogSystem\UserNotAllowedLogEntry;
use App\Entity\Parameters\AbstractParameter; use App\Entity\Parameters\AbstractParameter;
use App\Entity\Parts\PartLot; use App\Entity\Parts\PartLot;
use App\Entity\PriceInformations\Orderdetail; use App\Entity\PriceInformations\Orderdetail;
@ -86,12 +87,17 @@ class LogEntryTargetColumn extends AbstractColumn
{ {
parent::configureOptions($resolver); parent::configureOptions($resolver);
$resolver->setDefault('show_associated', true); $resolver->setDefault('show_associated', true);
$resolver->setDefault('showAccessDeniedPath', true);
return $this; return $this;
} }
public function render($value, $context) public function render($value, $context)
{ {
if ($context instanceof UserNotAllowedLogEntry && $this->options['showAccessDeniedPath']) {
return htmlspecialchars($context->getPath());
}
/** @var AbstractLogEntry $context */ /** @var AbstractLogEntry $context */
$target = $this->entryRepository->getTargetElement($context); $target = $this->entryRepository->getTargetElement($context);

View file

@ -52,11 +52,21 @@ class UserNotAllowedLogEntry extends AbstractLogEntry
{ {
protected $typeString = 'user_not_allowed'; protected $typeString = 'user_not_allowed';
public function __construct() public function __construct(string $path)
{ {
parent::__construct(); parent::__construct();
//Obsolete, use server log now. $this->level = static::LEVEL_WARNING;
throw new LogEntryObsoleteException();
$this->extra['a'] = $path;
}
/**
* Returns the path the user tried to accessed and what was denied.
* @return string
*/
public function getPath(): string
{
return $this->extra['a'] ?? 'legacy';
} }
public function getMessage(): string public function getMessage(): string

View file

@ -0,0 +1,67 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\EventSubscriber\LogSystem;
use App\Entity\LogSystem\UserNotAllowedLogEntry;
use App\Services\LogSystem\EventLogger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Write to event log when a user tries to access an forbidden page and recevies an 403 Access Denied message.
* @package App\EventSubscriber\LogSystem
*/
class LogAccessDeniedSubscriber implements EventSubscriberInterface
{
private $logger;
public function __construct(EventLogger $logger)
{
$this->logger = $logger;
}
public function onKernelException(ExceptionEvent $event)
{
$throwable = $event->getThrowable();
if ($throwable instanceof AccessDeniedHttpException) {
$throwable = $throwable->getPrevious();
}
//Ignore everything except AccessDeniedExceptions
if (!$throwable instanceof AccessDeniedException) {
return;
}
$path = $event->getRequest()->getPathInfo();
$log_entry = new UserNotAllowedLogEntry($path);
$this->logger->logAndFlush($log_entry);
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return ['kernel.exception' => 'onKernelException'];
}
}

View file

@ -8244,5 +8244,17 @@ Element 3</target>
<target>Duplicate element</target> <target>Duplicate element</target>
</segment> </segment>
</unit> </unit>
<unit id="bJ.EAvM" name="log.type.user_not_allowed">
<segment>
<source>log.type.user_not_allowed</source>
<target>Unauthorised access attempt</target>
</segment>
</unit>
<unit id="Z28oTau" name="log.database_updated.success">
<segment>
<source>log.database_updated.success</source>
<target>Sucess</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>