Skip to content
Snippets Groups Projects
Commit 0d43d024 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3524329 by jurgenhaas: Signal context

parent 81c0950a
No related branches found
No related tags found
No related merge requests found
Pipeline #497072 passed
......@@ -56,6 +56,7 @@ services:
- '@ban.ip_manager'
- '@queue'
- '@event_dispatcher'
- '@request_stack'
crowdsec.cti:
class: Drupal\crowdsec\Cti
......
......@@ -24,6 +24,8 @@ use Drupal\crowdsec\Event\CrowdSecEvents;
use Drupal\crowdsec\Event\IpBanned;
use Drupal\crowdsec\Event\IpSignalled;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Implements the CrowdSec buffer service.
......@@ -63,6 +65,13 @@ class Buffer {
*/
protected QueueInterface $queue;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected Request $request;
/**
* Constructs the storage implementation.
*/
......@@ -78,11 +87,13 @@ class Buffer {
protected BanIpManagerInterface $banIpManager,
protected QueueFactory $queueFactory,
protected EventDispatcherInterface $eventDispatcher,
protected RequestStack $requestStack,
) {
$this->keyValue = $keyValueFactory->get('crowdsec');
$this->expirableKeyValue = $keyValueExpirableFactory->get('crowdsec');
$this->config = $configFactory->get('crowdsec.settings');
$this->queue = $queueFactory->get('crowdsec', TRUE);
$this->request = $this->requestStack->getCurrentRequest();
}
/**
......@@ -114,11 +125,10 @@ class Buffer {
* The target user in case of a brute force attack.
*/
public function addCurrentContext(array &$context, int $status, ?string $targetUser = NULL): void {
$request = \Drupal::request();
$currentContext = [
'target_uri' => $request->getRequestUri(),
'user_agent' => $request->headers->get('User-Agent'),
'method' => $request->getMethod(),
'target_uri' => $this->request->getRequestUri(),
'user_agent' => $this->request->headers->get('User-Agent'),
'method' => $this->request->getMethod(),
'status' => $status,
];
if ($targetUser !== NULL) {
......
......@@ -61,6 +61,7 @@ class CrowdSec extends HttpExceptionSubscriberBase {
if (empty($ip)) {
return;
}
/** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
$exception = $event->getThrowable();
$this->buffer->addWhisperSignal($ip, $exception->getStatusCode());
}
......@@ -74,7 +75,7 @@ class CrowdSec extends HttpExceptionSubscriberBase {
public function onBlockIp(UserFloodEvent $event): void {
$context = [];
if ($event->getName() === UserEvents::FLOOD_BLOCKED_USER) {
$this->buffer->addCurrentContext($context, 403, $event->getUid());
$this->buffer->addCurrentContext($context, 403, (string) $event->getUid());
}
else {
$this->buffer->addCurrentContext($context, 403);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment