You are here

class PerimeterSubscriber in Drupal Perimeter Defence 8

Same name and namespace in other branches
  1. 2.0.x src/EventSubscriber/PerimeterSubscriber.php \Drupal\perimeter\EventSubscriber\PerimeterSubscriber

On page not found events, ban the IP if the request is suspicious.

Hierarchy

  • class \Drupal\perimeter\EventSubscriber\PerimeterSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PerimeterSubscriber

1 string reference to 'PerimeterSubscriber'
perimeter.services.yml in ./perimeter.services.yml
perimeter.services.yml
1 service uses PerimeterSubscriber
perimeter.perimeter in ./perimeter.services.yml
Drupal\perimeter\EventSubscriber\PerimeterSubscriber

File

src/EventSubscriber/PerimeterSubscriber.php, line 18

Namespace

Drupal\perimeter\EventSubscriber
View source
class PerimeterSubscriber implements EventSubscriberInterface {
  protected $loggerFactory;
  protected $config;

  /**
   * {@inheritdoc}
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory) {
    $this->loggerFactory = $logger_factory;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['kernel.exception'][] = [
      'handleBannedUrls',
    ];
    return $events;
  }

  /**
   * On page not found events, ban the IP if the request is suspicious.
   */
  public function handleBannedUrls(Event $event) {
    $exception = $event
      ->getException();
    if ($exception instanceof NotFoundHttpException) {
      $request_path = $event
        ->getRequest()
        ->getPathInfo();
      $bannedPatterns = $this->configFactory
        ->get('perimeter.settings')
        ->get('not_found_exception_patterns');
      foreach ($bannedPatterns as $pattern) {
        $pattern = trim($pattern);
        if (preg_match($pattern, $request_path)) {
          $connection = Database::getConnection();
          $banManager = new BanIpManager($connection);
          $banManager
            ->banIp($event
            ->getRequest()
            ->getClientIp());
          $this->loggerFactory
            ->get('Perimeter')
            ->notice('Banned: %ip for requesting %pattern <br />Source: %source <br /> User Agent: %browser', [
            '%ip' => $event
              ->getRequest()
              ->getClientIp(),
            '%pattern' => Xss::filter($request_path),
            '%source' => isset($_SERVER['HTTP_REFERER']) ? Xss::filter($_SERVER['HTTP_REFERER']) : '',
            '%browser' => isset($_SERVER['HTTP_USER_AGENT']) ? Xss::filter($_SERVER['HTTP_USER_AGENT']) : '',
          ]);
          break;
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PerimeterSubscriber::$config protected property
PerimeterSubscriber::$loggerFactory protected property
PerimeterSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PerimeterSubscriber::handleBannedUrls public function On page not found events, ban the IP if the request is suspicious.
PerimeterSubscriber::__construct public function