Skip to content

Commit 5f27af6

Browse files
fix(standalone): Allow array of string for trust_ip_forward_array settings
1 parent 639b96b commit 5f27af6

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/AbstractBounce.php

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ protected function shouldTrustXforwardedFor(string $ip): bool
161161

162162
return false;
163163
}
164+
164165
foreach ($this->getTrustForwardedIpBoundsList() as $comparableIpBounds) {
165166
if ($comparableAddress >= $comparableIpBounds[0] && $comparableAddress <= $comparableIpBounds[1]) {
166167
return true;

src/Configuration.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public function getConfigTreeBuilder(): TreeBuilder
5252
->defaultValue(Constants::REMEDIATION_CAPTCHA)
5353
->end()
5454
->arrayNode('trust_ip_forward_array')
55-
->scalarPrototype()->end()
55+
->arrayPrototype()
56+
->scalarPrototype()->end()
57+
->end()
5658
->end()
5759
// Cache
5860
->booleanNode('stream_mode')->defaultValue(false)->end()

src/StandaloneBounce.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ErrorException;
66
use Exception;
7+
use IPLib\Factory;
78
use Symfony\Component\Cache\Adapter\AbstractAdapter;
89
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
910
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
@@ -44,7 +45,25 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer
4445
$this->session_name = session_name('crowdsec');
4546
session_start();
4647
}
48+
// Convert array of string to array of array with comparable IPs
49+
if(is_array(($configs['trust_ip_forward_array']))){
50+
$forwardConfigs = $configs['trust_ip_forward_array'];
51+
$finalForwardConfigs = [];
52+
foreach($forwardConfigs as $forwardConfig){
53+
if(\is_string($forwardConfig)){
54+
$parsedString = Factory::parseAddressString($forwardConfig, 3);
55+
if(!empty($parsedString)){
56+
$comparableValue = $parsedString->getComparableString();
57+
$finalForwardConfigs[] = [$comparableValue, $comparableValue];
58+
}
59+
} elseif (\is_array($forwardConfig)){
60+
$finalForwardConfigs[] = $forwardConfig;
61+
}
62+
}
63+
$configs['trust_ip_forward_array'] = $finalForwardConfigs;
64+
}
4765
$this->settings = $configs;
66+
4867
if (\is_array($forcedConfigs)) {
4968
$this->settings = array_merge($this->settings, $forcedConfigs);
5069
}
@@ -384,14 +403,13 @@ public function sendResponse(?string $body, int $statusCode = 200): void
384403
public function safelyBounce(array $configs): bool
385404
{
386405
$result = false;
406+
set_error_handler(function ($errno, $errstr) {
407+
throw new BouncerException("$errstr (Error level: $errno)");
408+
});
387409
try {
388-
set_error_handler(function ($errno, $errstr) {
389-
throw new BouncerException("$errstr (Error level: $errno)");
390-
});
391410
$this->init($configs);
392411
$this->run();
393412
$result = true;
394-
restore_error_handler();
395413
} catch (Exception $e) {
396414
$this->logger->error('', [
397415
'type' => 'EXCEPTION_WHILE_BOUNCING',
@@ -409,6 +427,7 @@ public function safelyBounce(array $configs): bool
409427
session_name($this->session_name);
410428
}
411429
}
430+
restore_error_handler();
412431

413432
return $result;
414433
}

0 commit comments

Comments
 (0)