Skip to content

Commit 02d7e1f

Browse files
committed
fallback remediation
1 parent 0c4380e commit 02d7e1f

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

docs/configuration-reference.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// Optional. Cap the remediation to the selected one. Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation). Defaults to 'ban'.
2121
'max_remediation_level'=> 'ban',
2222

23+
// Optional. Handle unknown remediations as. Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation). Defaults to 'captcha'.
24+
'fallback_remediation'=> 'captcha',
2325

2426
// Optional. Set the duration we keep in cache the fact that an IP is clean. In seconds. Defaults to 60 (1 minute).
2527
'cache_expiration_for_clean_ip'=> '60',

src/Bouncer.php

+14
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ private function capRemediationLevel(string $remediation): string
8383
return $remediation;
8484
}
8585

86+
/**
87+
* If the CrowdSec remediation is not handled by this library,
88+
* replace it with the value of the configuration "fallback_remediation".
89+
*/
90+
private function handleUnknownRemediation(string $remediation): string
91+
{
92+
// TODO P3 test this
93+
if (!in_array($remediation, Constants::ORDERED_REMEDIATIONS)) {
94+
return $this->config['fallback_remediation'];
95+
}
96+
return $remediation;
97+
}
98+
8699
/**
87100
* Get the remediation for the specified IP. This method use the cache layer.
88101
* In live mode, when no remediation was found in cache,
@@ -97,6 +110,7 @@ public function getRemediationForIp(string $ip): string
97110
throw new BouncerException("IP $ip should looks like x.x.x.x, with x in 0-255. Ex: 1.2.3.4");
98111
}
99112
$remediation = $this->apiCache->get(long2ip($intIp));
113+
$remediation = $this->handleUnknownRemediation($remediation);
100114
$remediation = $this->capRemediationLevel($remediation);
101115
return $remediation;
102116
}

src/Configuration.php

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function getConfigTreeBuilder()
3737
->values(Constants::ORDERED_REMEDIATIONS)
3838
->defaultValue(Constants::REMEDIATION_BAN)
3939
->end()
40+
->enumNode('fallback_remediation')
41+
->values(Constants::ORDERED_REMEDIATIONS)
42+
->defaultValue(Constants::REMEDIATION_CAPTCHA)
43+
->end()
4044
->integerNode('cache_expiration_for_clean_ip')
4145
->defaultValue(Constants::CACHE_EXPIRATION_FOR_CLEAN_IP)
4246
->end()

0 commit comments

Comments
 (0)