Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Oct 26, 2024
1 parent 1a25184 commit 136eb71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
44 changes: 25 additions & 19 deletions src/EventSubscriber/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
Expand All @@ -34,6 +34,8 @@ class Challenge implements EventSubscriberInterface {
protected $currentUser;

/**
* The flood service.
*
* @var \Drupal\Core\Flood\FloodInterface
*/
protected $flood;
Expand All @@ -48,6 +50,10 @@ class Challenge implements EventSubscriberInterface {
/**
* Constructs the event subscriber.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The watchdog service.
* @param \Drupal\Core\Flood\FloodInterface $flood
* The flood service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
Expand Down Expand Up @@ -113,22 +119,22 @@ protected function applies(Request $request): bool {
return $config->get('protect_parameters') ? count($_GET) > 0 : FALSE;
}

// don't check the rate limit if it's not set
// don't check the rate limit if it's not set.
if (!$config->get("rate_limit")) {
return TRUE;
}

// check if we're rate limited
// Check if we're rate limited.
$threshold = $config->get("threshold");
$window = $config->get("window");

// base the rate limit identifier on /16 for ipv4
// and /64 for ipv6
// Base the rate limit identifier on /16 for ipv4
// and /64 for ipv6.
$delimiter = strpos($clientIp, ":") ? ":" : ".";
$components = explode($delimiter, $clientIp);
// ipv6
// ipv6.
if ($delimiter == ':') {
$components = self::expandIPv6($clientIp);
$components = self::expandIpv6($clientIp);
$components = array_slice($components, 0, 4);
}
else {
Expand All @@ -144,8 +150,8 @@ protected function applies(Request $request): bool {
);
$this->flood->register($event_name, $window, $identifier);

// if we haven't been flooded by this ip range
// do not present a challenge
// If we haven't been flooded by this ip range
// do not present a challenge.
return !$allowed;
}

Expand All @@ -161,15 +167,15 @@ public function protect(RequestEvent $event) {
return;
}

// only allow five attempts at passing a challenge
// Only allow five attempts at passing a challenge.
$session = $request->getSession();
$submission_count = $session->get('turnstile_protect_submission_count', 0);
$submission_count++;
$session->set('turnstile_protect_submission_count', $submission_count);
if ($submission_count > 5) {
$response = new Response('Too many requests', 429);
$event->setResponse($response);
// log every ten failures
// Log every ten failures.
if (($submission_count % 10) == 0) {
$this->logger->notice('@failures attempts by @ip', [
'@failures' => $submission_count,
Expand All @@ -194,22 +200,22 @@ public function protect(RequestEvent $event) {
* @param string $ip
* The ipv6 address to expand.
*/
public static function expandIPv6($ip) {
public static function expandIpv6($ip) {
$hextets = explode(':', $ip);
$expanded = [];

// Find the index of an empty hextet (indicating :: compression)
$emptyIndex = array_search('', $hextets, true);
$emptyIndex = array_search('', $hextets, TRUE);
if ($emptyIndex !== FALSE) {
// Calculate how many hextets are missing
$missingCount = 8 - count($hextets) + 1;
// Fill in zeros for the missing hextets
array_splice($hextets, $emptyIndex, 1, array_fill(0, $missingCount, '0'));
// Calculate how many hextets are missing.
$missingCount = 8 - count($hextets) + 1;
// Fill in zeros for the missing hextets.
array_splice($hextets, $emptyIndex, 1, array_fill(0, $missingCount, '0'));
}

// Pad each hextet to 4 digits
// Pad each hextet to 4 digits.
foreach ($hextets as $hextet) {
$expanded[] = str_pad($hextet, 4, '0', STR_PAD_LEFT);
$expanded[] = str_pad($hextet, 4, '0', STR_PAD_LEFT);
}

return $expanded;
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public function getFormId() {
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory->get('captcha.settings');

// if captcha's globally adding turnstile to all forms
// no need to add it here
// If captcha's globally adding turnstile to all forms
// no need to add it here.
if (!$config->get('enable_globally')) {
$form['turnstile'] = [
'#type' => 'captcha',
Expand Down

0 comments on commit 136eb71

Please sign in to comment.