Skip to content

Commit b26f467

Browse files
Merge pull request #76 from julienloizelet/fix/standalone-forward-ips
Fix/standalone forward ips
2 parents 639b96b + 9d26ac2 commit b26f467

6 files changed

+32
-8
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77

8+
## [0.18.0] - 2022-03-18
9+
### Changed
10+
- *Breaking change*: Change `trust_ip_forward_array` symfony configuration node to an array of array.
11+
812
## [0.17.1] - 2022-03-17
913
### Removed
1014
- Remove testing scripts for quality gate test

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/ApiCache.php

-2
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,8 @@ private function hit(string $ip): string
694694
}
695695

696696
/**
697-
* @param string $cacheScope
698697
* @param $value
699698
*
700-
* @return string
701699
* @throws InvalidArgumentException
702700
* @throws Exception
703701
*/

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/Constants.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Constants
2020
public const DEFAULT_LAPI_URL = 'http://localhost:8080';
2121

2222
/** @var string The last version of this library */
23-
public const VERSION = 'v0.17.1';
23+
public const VERSION = 'v0.18.0';
2424

2525
/** @var string The user agent used to send request to LAPI */
2626
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/'.self::VERSION;

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)