Skip to content

Commit 77246ed

Browse files
feat(mock ip): Remove useless configuration
1 parent bcdc297 commit 77246ed

File tree

8 files changed

+67
-153
lines changed

8 files changed

+67
-153
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 103 deletions
This file was deleted.

docs/USER_GUIDE.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ Here is the list of available settings:
137137
- `forced_test_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used instead of the
138138
real remote ip.
139139

140-
- `forced_test_forwarded_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used instead of the real forwarded ip.
141-
142-
- `forced_test_never_use_forwarded`: Only for test or debug purpose. Default to false. Set to true if you never
143-
want to use the x-forwarded-for mechanism.
140+
- `forced_test_forwarded_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used
141+
instead of the real forwarded ip. If set to `no_forward`, the x-forwarded-for mechanism will not be used at all.
144142

145143
##### Bouncer behavior
146144

scripts/auto-prepend/settings.example.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,10 @@
4242
/** Only for test or debug purpose. Default to empty.
4343
*
4444
* If not empty, it will be used instead of the real forwarded ip.
45-
*/
46-
'forced_test_forwarded_ip' => '',
47-
48-
/** Only for test or debug purpose. Default to false.
45+
* If set to "no_forward", the x-forwarded-for mechanism will not be used at all.
4946
*
50-
* Set to true if you never want to use the x-forwarded-for mechanism.
5147
*/
52-
'forced_test_never_use_forwarded' => false,
48+
'forced_test_forwarded_ip' => '',
5349

5450
/** Select from 'bouncing_disabled', 'normal_bouncing' or 'flex_bouncing'.
5551
*

src/AbstractBounce.php

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Monolog\Formatter\LineFormatter;
1313
use Monolog\Handler\RotatingFileHandler;
1414
use Monolog\Logger;
15+
use Psr\Cache\CacheException;
1516
use Psr\Cache\InvalidArgumentException;
1617
use Psr\Log\LoggerInterface;
1718

@@ -65,7 +66,7 @@ protected function getArraySettings(string $name): array
6566
/**
6667
* Run a bounce.
6768
*
68-
* @throws Exception|InvalidArgumentException
69+
* @throws Exception|InvalidArgumentException|CacheException
6970
*/
7071
public function run(): void
7172
{
@@ -112,18 +113,55 @@ protected function initLoggerHelper(string $logDirectoryPath, string $loggerName
112113
}
113114

114115
/**
115-
* Decide if we use forward (default behavior) or if it depends on test settings
116+
* Handle X-Forwarded-For HTTP header to retrieve the IP to bounce
116117
*
117-
* @param $settings
118-
* @return bool
118+
* @param $ip
119+
* @return false|mixed
119120
*/
120-
protected function shouldUseForward($settings)
121+
protected function handleForwardedFor($ip)
121122
{
122-
return empty($settings['forced_test_never_use_forwarded']);
123+
if (empty($this->settings['forced_test_forwarded_ip'])) {
124+
$XForwardedForHeader = $this->getHttpRequestHeader('X-Forwarded-For');
125+
if (null !== $XForwardedForHeader) {
126+
$ipList = array_map('trim', array_values(array_filter(explode(',', $XForwardedForHeader))));
127+
$forwardedIp = end($ipList);
128+
if ($this->shouldTrustXforwardedFor($ip)) {
129+
$ip = $forwardedIp;
130+
} else {
131+
$this->logger->warning('', [
132+
'type' => 'NON_AUTHORIZED_X_FORWARDED_FOR_USAGE',
133+
'original_ip' => $ip,
134+
'x_forwarded_for_ip' => $forwardedIp,
135+
]);
136+
}
137+
}
138+
} else if ($this->settings['forced_test_forwarded_ip'] === Constants::X_FORWARDED_DISABLED) {
139+
$this->logger->debug('', [
140+
'type' => 'DISABLED_X_FORWARDED_FOR_USAGE',
141+
'original_ip' => $ip,
142+
]);
143+
} else {
144+
$forwardedIp = $this->settings['forced_test_forwarded_ip'];
145+
if ($this->shouldTrustXforwardedFor($ip)) {
146+
$ip = $forwardedIp;
147+
} else {
148+
$this->logger->warning('', [
149+
'type' => 'NON_AUTHORIZED_TEST_X_FORWARDED_FOR_USAGE',
150+
'original_ip' => $ip,
151+
'x_forwarded_for_ip_for_test' => $forwardedIp,
152+
]);
153+
}
154+
}
155+
156+
return $ip;
123157
}
124158

125159
/**
126-
* @throws Exception|InvalidArgumentException
160+
* Bounce process
161+
*
162+
* @return void
163+
* @throws InvalidArgumentException|CacheException
164+
* @throws Exception
127165
*/
128166
protected function bounceCurrentIp(): void
129167
{
@@ -133,38 +171,13 @@ protected function bounceCurrentIp(): void
133171
}
134172
// Retrieve the current IP (even if it is a proxy IP) or a testing IP
135173
$ip = !empty($this->settings['forced_test_ip']) ? $this->settings['forced_test_ip'] : $this->getRemoteIp();
136-
if ($this->shouldUseForward($this->settings)) {
137-
// Retrieve the forwarded IP (testing one or real)
138-
if (!empty($this->settings['forced_test_forwarded_ip'])) {
139-
$forwardedIp = $this->settings['forced_test_forwarded_ip'];
140-
} elseif ($XForwardedForHeader = $this->getHttpRequestHeader('X-Forwarded-For')) {
141-
$ipList = array_map('trim', array_values(array_filter(explode(',', $XForwardedForHeader))));
142-
$forwardedIp = end($ipList);
143-
}
144-
if (isset($forwardedIp)) {
145-
if ($this->shouldTrustXforwardedFor($ip)) {
146-
$this->logger->debug('', [
147-
'type' => 'AUTHORIZED_X_FORWARDED_FOR_USAGE',
148-
'original_ip' => $ip,
149-
]);
150-
$ip = $forwardedIp;
151-
} else {
152-
$this->logger->warning('', [
153-
'type' => 'NON_AUTHORIZED_X_FORWARDED_FOR_USAGE',
154-
'original_ip' => $ip,
155-
'x_forwarded_for_ip' => $forwardedIp ?? 'undefined',
156-
]);
157-
}
158-
} else {
159-
$this->logger->debug('', ['type' => 'X_FORWARDED_FOR_NOT_FOUND']);
160-
}
161-
}
174+
$ip = $this->handleForwardedFor($ip);
162175
$remediation = $this->bouncer->getRemediationForIp($ip);
163176
$this->handleRemediation($remediation, $ip);
164177
} catch (Exception $e) {
165178
$this->logger->warning('', [
166179
'type' => 'UNKNOWN_EXCEPTION_WHILE_BOUNCING',
167-
'ip' => $ip,
180+
'ip' => $ip ?? '',
168181
'message' => $e->getMessage(),
169182
'code' => $e->getCode(),
170183
'file' => $e->getFile(),
@@ -197,6 +210,9 @@ protected function shouldTrustXforwardedFor(string $ip): bool
197210
return false;
198211
}
199212

213+
/**
214+
* @throws InvalidArgumentException
215+
*/
200216
protected function displayCaptchaWall(string $ip): void
201217
{
202218
$options = $this->getCaptchaWallOptions();
@@ -222,7 +238,10 @@ protected function handleBanRemediation(): void
222238
}
223239

224240
/**
241+
* @param string $ip
225242
* @return void
243+
* @throws InvalidArgumentException
244+
* @throws CacheException
226245
*/
227246
protected function handleCaptchaResolutionForm(string $ip)
228247
{
@@ -305,6 +324,8 @@ protected function handleCaptchaResolutionForm(string $ip)
305324
* @param string $ip
306325
*
307326
* @return void
327+
* @throws InvalidArgumentException
328+
* @throws CacheException
308329
*/
309330
protected function handleCaptchaRemediation(string $ip)
310331
{
@@ -344,6 +365,8 @@ protected function handleCaptchaRemediation(string $ip)
344365
* @param string $remediation
345366
* @param string $ip
346367
* @return void
368+
* @throws InvalidArgumentException
369+
* @throws CacheException
347370
*/
348371
protected function handleRemediation(string $remediation, string $ip)
349372
{
@@ -368,7 +391,7 @@ protected function handleRemediation(string $remediation, string $ip)
368391
* @return array
369392
* @throws InvalidArgumentException
370393
*/
371-
public function getIpVariables(string $cacheTag, array $names, string $ip)
394+
public function getIpVariables(string $cacheTag, array $names, string $ip): array
372395
{
373396
if (!$this->bouncer) {
374397
throw new BouncerException('Bouncer must be instantiated to get cache data.');
@@ -386,7 +409,7 @@ public function getIpVariables(string $cacheTag, array $names, string $ip)
386409
* @param string $ip
387410
* @return void
388411
* @throws InvalidArgumentException
389-
* @throws \Psr\Cache\CacheException
412+
* @throws CacheException
390413
*/
391414
public function setIpVariables(string $cacheTag, array $pairs, string $ip): void
392415
{
@@ -405,7 +428,7 @@ public function setIpVariables(string $cacheTag, array $pairs, string $ip): void
405428
* @param string $ip
406429
* @return void
407430
* @throws InvalidArgumentException
408-
* @throws \Psr\Cache\CacheException
431+
* @throws CacheException
409432
*/
410433
public function unsetIpVariables(string $cacheTag, array $names, string $ip): void
411434
{

src/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function getConfigTreeBuilder(): TreeBuilder
3636
// Debug
3737
->scalarNode('forced_test_ip')->defaultValue('')->end()
3838
->scalarNode('forced_test_forwarded_ip')->defaultValue('')->end()
39-
->booleanNode('forced_test_never_use_forwarded')->defaultValue(false)->end()
4039
->booleanNode('debug_mode')->defaultValue(false)->end()
4140
->scalarNode('log_directory_path')->end()
4241
->booleanNode('display_errors')->defaultValue(false)->end()

src/Constants.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,7 @@ class Constants
9696

9797
/** @var string The Maxmind "City" database type */
9898
public const MAXMIND_CITY = 'city';
99+
100+
/** @var string The "disabled" bouncing level */
101+
public const X_FORWARDED_DISABLED = 'no_forward';
99102
}

src/StandaloneBounce.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ public function getBouncerInstance(array $settings, bool $forceReload = false):
170170
'log_directory_path' => $this->getStringSettings('log_directory_path'),
171171
'forced_test_ip' => $this->getStringSettings('forced_test_ip'),
172172
'forced_test_forwarded_ip' => $this->getStringSettings('forced_test_forwarded_ip'),
173-
'forced_test_never_use_forwarded' => $this->getBoolSettings('forced_test_never_use_forwarded'),
174173
'display_errors' => $this->getBoolSettings('display_errors'),
175174
// Bouncer
176175
'bouncing_level' => $bouncingLevel,

tests/end-to-end/settings/base.php.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ $crowdSecStandaloneBouncerConfig = [
1515
'fs_cache_path' => __DIR__.'/.cache',
1616
'forced_test_ip' => 'REPLACE_FORCED_IP',
1717
'forced_test_forwarded_ip' => 'REPLACE_FORCED_FORWARDED_IP',
18-
'forced_test_never_use_forwarded' => false,
1918
// Bouncer
2019
'bouncing_level' => Constants::BOUNCING_LEVEL_NORMAL,
2120
'stream_mode' => false,

0 commit comments

Comments
 (0)