Skip to content

Commit ecf5194

Browse files
authored
Merge pull request #52 from crowdsecurity/handle-invalid-ip-format
handle invalid ip format
2 parents bd0f1a1 + af065bd commit ecf5194

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/AbstractBounce.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ protected function bounceCurrentIp()
126126
protected function shouldTrustXforwardedFor(string $ip): bool
127127
{
128128
$comparableAddress = Factory::addressFromString($ip)->getComparableString();
129+
if (null === $comparableAddress) {
130+
$this->logger->warning('', [
131+
'type' => 'INVALID_INPUT_IP',
132+
'ip' => $ip,
133+
]);
134+
135+
return false;
136+
}
129137
foreach ($this->getTrustForwardedIpBoundsList() as $comparableIpBounds) {
130138
if ($comparableAddress >= $comparableIpBounds[0] && $comparableAddress <= $comparableIpBounds[1]) {
131139
return true;

src/ApiCache.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ private function saveRemediations(array $decisions): array
308308

309309
if ('Ip' === $decision['scope']) {
310310
$address = Factory::addressFromString($decision['value']);
311+
if (null === $address) {
312+
$this->logger->warning('', [
313+
'type' => 'INVALID_IP_TO_ADD_FROM_REMEDIATION',
314+
'decision' => $decision,
315+
]);
316+
continue;
317+
}
311318
$this->addRemediationToCacheItem($address->toString(), $type, $exp, $id);
312319
} elseif ('Range' === $decision['scope']) {
313320
$range = Subnet::fromString($decision['value']);
@@ -347,6 +354,13 @@ private function removeRemediations(array $decisions): array
347354
foreach ($decisions as $decision) {
348355
if ('Ip' === $decision['scope']) {
349356
$address = Factory::addressFromString($decision['value']);
357+
if (null === $address) {
358+
$this->logger->warning('', [
359+
'type' => 'INVALID_IP_TO_REMOVE_FROM_REMEDIATION',
360+
'decision' => $decision,
361+
]);
362+
continue;
363+
}
350364
if (!$this->removeDecisionFromRemediationItem($address->toString(), $decision['id'])) {
351365
$this->logger->debug('', ['type' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE', 'decision' => $decision['id']]);
352366
} else {

0 commit comments

Comments
 (0)