Skip to content

Commit dbd346c

Browse files
Merge pull request #134 from julienloizelet/fix/empty-raw-config
Fix/empty raw config
2 parents 66d2cc2 + 2d9f9ba commit dbd346c

File tree

5 files changed

+114
-35
lines changed

5 files changed

+114
-35
lines changed

CHANGELOG.md

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

77

8+
## [2.0.4](https://github.com/crowdsecurity/cs-wordpress-bouncer/releases/tag/v2.0.4) - 2023-03-09
9+
[_Compare with previous release_](https://github.com/crowdsecurity/cs-wordpress-bouncer/compare/v2.0.3...v2.0.4)
10+
11+
### Fixed
12+
13+
- If a database option is empty, we add the default value to avoid configuration PHP error
14+
15+
---
16+
17+
818
## [2.0.3](https://github.com/crowdsecurity/cs-wordpress-bouncer/releases/tag/v2.0.3) - 2023-02-16
919
[_Compare with previous release_](https://github.com/crowdsecurity/cs-wordpress-bouncer/compare/v2.0.2...v2.0.3)
1020

crowdsec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin URI: https://github.com/crowdsecurity/cs-wordpress-bouncer
55
* Description: Safer Together. Protect your WordPress application with CrowdSec.
66
* Tags: crowdsec-bouncer, wordpress, security, firewall, captcha, ip-scanner, ip-blocker, ip-blocking, ip-address, ip-database, ip-range-check, crowdsec, ban-hosts, ban-management, anti-hacking, hacker-protection, captcha-image, captcha-generator, captcha-generation, captcha-service
7-
* Version: 2.0.3
7+
* Version: 2.0.4
88
* Author: CrowdSec
99
* Author URI: https://www.crowdsec.net/
1010
* Github: https://github.com/crowdsecurity/cs-wordpress-bouncer
@@ -13,7 +13,7 @@
1313
* Requires PHP: 7.2
1414
* Requires at least: 4.9
1515
* Tested up to: 6.1
16-
* Stable tag: 2.0.3
16+
* Stable tag: 2.0.4
1717
* Text Domain: crowdsec-wp
1818
* First release: 2021.
1919
*/

inc/Bouncer.php

Lines changed: 100 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Bouncer extends AbstractBouncer
3131
*/
3232
public function __construct(array $configs, LoggerInterface $logger = null)
3333
{
34-
$this->shouldNotBounceWpAdmin = (bool)($configs['crowdsec_public_website_only']??true);
34+
$this->shouldNotBounceWpAdmin = (bool)($configs['crowdsec_public_website_only'] ?? true);
3535
$configs = $this->handleRawConfigs($configs);
3636
$this->logger = $logger ?: new FileLog($configs, 'wordpress_bouncer');
3737
$configs['user_agent_version'] = Constants::VERSION;
@@ -63,55 +63,114 @@ public function handleRawConfigs(array $rawConfigs): array
6363
return [
6464
// LAPI connection
6565
'api_key' => $this->escape((string)$rawConfigs['crowdsec_api_key'] ?? ''),
66-
'auth_type' => (string)($rawConfigs['crowdsec_auth_type'] ?? Constants::AUTH_KEY),
67-
'tls_cert_path' => Constants::TLS_DIR . '/' . ltrim((string)$rawConfigs['crowdsec_tls_cert_path'] ?? '', '/'),
68-
'tls_key_path' => Constants::TLS_DIR . '/' . ltrim((string)$rawConfigs['crowdsec_tls_key_path'] ?? '', '/'),
69-
'tls_verify_peer' => (bool)($rawConfigs['crowdsec_tls_verify_peer'] ?? false),
66+
'auth_type' => (string)($this->handleRawConfig(
67+
$rawConfigs,
68+
'crowdsec_auth_type',
69+
Constants::AUTH_KEY
70+
)),
71+
'tls_cert_path' => Constants::TLS_DIR . '/' .
72+
ltrim(
73+
(string)($this->handleRawConfig(
74+
$rawConfigs, 'crowdsec_tls_cert_path', '')
75+
),
76+
'/'
77+
),
78+
'tls_key_path' => Constants::TLS_DIR . '/' .
79+
ltrim(
80+
(string)($this->handleRawConfig(
81+
$rawConfigs, 'crowdsec_tls_key_path', '')
82+
),
83+
'/'
84+
),
85+
'tls_verify_peer' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_tls_verify_peer', false)),
7086
'tls_ca_cert_path' => Constants::TLS_DIR . '/' .
71-
ltrim((string)$rawConfigs['crowdsec_tls_ca_cert_path'], '/'),
87+
ltrim(
88+
(string)($this->handleRawConfig(
89+
$rawConfigs, 'crowdsec_tls_ca_cert_path', '')
90+
),
91+
'/'
92+
),
7293
'api_url' => $this->escape((string)$rawConfigs['crowdsec_api_url'] ?? ''),
73-
'use_curl' => (bool)($rawConfigs['crowdsec_use_curl'] ?? false),
74-
'api_timeout' => (int)($rawConfigs['crowdsec_api_timeout'] ?? Constants::API_TIMEOUT),
94+
'use_curl' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_use_curl', false)),
95+
'api_timeout' => (int)($this->handleRawConfig(
96+
$rawConfigs,
97+
'crowdsec_api_timeout',
98+
Constants::API_TIMEOUT
99+
)),
75100
// Debug
76-
'debug_mode' => (bool)($rawConfigs['crowdsec_debug_mode'] ?? false),
77-
'disable_prod_log' => (bool)($rawConfigs['crowdsec_disable_prod_log'] ?? false),
101+
'debug_mode' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_debug_mode', false)),
102+
'disable_prod_log' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_disable_prod_log', false)),
78103
'log_directory_path' => Constants::LOG_BASE_PATH,
79104
'forced_test_ip' => (string)($rawConfigs['crowdsec_forced_test_ip'] ?? ''),
80105
'forced_test_forwarded_ip' => (string)($rawConfigs['crowdsec_forced_test_forwarded_ip'] ?? ''),
81-
'display_errors' => (bool)($rawConfigs['crowdsec_display_errors'] ?? false),
106+
'display_errors' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_display_errors', false)),
82107
// Bouncer
83-
'bouncing_level' => (string)($rawConfigs['crowdsec_bouncing_level'] ?? Constants::BOUNCING_LEVEL_DISABLED),
108+
'bouncing_level' => (string)($this->handleRawConfig(
109+
$rawConfigs,
110+
'crowdsec_bouncing_level',
111+
Constants::BOUNCING_LEVEL_DISABLED
112+
)),
84113
'trust_ip_forward_array' => (array)($rawConfigs['crowdsec_trust_ip_forward_array'] ?? []),
85-
'fallback_remediation' => (string)($rawConfigs['crowdsec_fallback_remediation'] ??
86-
Constants::REMEDIATION_BYPASS),
114+
'fallback_remediation' => (string)($this->handleRawConfig(
115+
$rawConfigs,
116+
'crowdsec_fallback_remediation',
117+
Constants::REMEDIATION_BYPASS
118+
)),
87119
// Cache settings
88-
'stream_mode' => (bool)($rawConfigs['crowdsec_stream_mode'] ?? false),
89-
'cache_system' => $this->escape((string)$rawConfigs['crowdsec_cache_system'] ?? Constants::CACHE_SYSTEM_PHPFS),
120+
'stream_mode' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_stream_mode', false)),
121+
'cache_system' => $this->escape((string)($this->handleRawConfig(
122+
$rawConfigs,
123+
'crowdsec_cache_system',
124+
Constants::CACHE_SYSTEM_PHPFS
125+
))),
90126
'fs_cache_path' => Constants::CACHE_PATH,
91127
'redis_dsn' => $this->escape((string)$rawConfigs['crowdsec_redis_dsn'] ?? ''),
92128
'memcached_dsn' => $this->escape((string)$rawConfigs['crowdsec_memcached_dsn'] ?? ''),
93-
'clean_ip_cache_duration' => (int)($rawConfigs['crowdsec_clean_ip_cache_duration'] ??
94-
Constants::CACHE_EXPIRATION_FOR_CLEAN_IP),
95-
'bad_ip_cache_duration' => (int)($rawConfigs['crowdsec_bad_ip_cache_duration'] ??
96-
Constants::CACHE_EXPIRATION_FOR_BAD_IP),
97-
'captcha_cache_duration' => (int)($rawConfigs['crowdsec_captcha_cache_duration'] ??
98-
Constants::CACHE_EXPIRATION_FOR_CAPTCHA),
129+
'clean_ip_cache_duration' => (int)($this->handleRawConfig(
130+
$rawConfigs,
131+
'crowdsec_clean_ip_cache_duration',
132+
Constants::CACHE_EXPIRATION_FOR_CLEAN_IP
133+
)),
134+
'bad_ip_cache_duration' => (int)($this->handleRawConfig(
135+
$rawConfigs,
136+
'crowdsec_bad_ip_cache_duration',
137+
Constants::CACHE_EXPIRATION_FOR_BAD_IP
138+
)),
139+
'captcha_cache_duration' => (int)($this->handleRawConfig(
140+
$rawConfigs,
141+
'crowdsec_captcha_cache_duration',
142+
Constants::CACHE_EXPIRATION_FOR_CAPTCHA
143+
)),
99144
// Geolocation
100145
'geolocation' => [
101-
'enabled' => (bool)($rawConfigs['crowdsec_geolocation_enabled'] ?? false),
102-
'type' => (string)($rawConfigs['crowdsec_geolocation_type'] ?? Constants::GEOLOCATION_TYPE_MAXMIND),
103-
'cache_duration' => (int)($rawConfigs['crowdsec_geolocation_cache_duration']
104-
?? Constants::CACHE_EXPIRATION_FOR_GEO),
146+
'enabled' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_geolocation_enabled', false)),
147+
'type' => (string)($this->handleRawConfig(
148+
$rawConfigs,
149+
'crowdsec_geolocation_type',
150+
Constants::GEOLOCATION_TYPE_MAXMIND
151+
)),
152+
'cache_duration' => (int)($this->handleRawConfig(
153+
$rawConfigs,
154+
'crowdsec_geolocation_cache_duration',
155+
Constants::CACHE_EXPIRATION_FOR_GEO
156+
)),
105157
'maxmind' => [
106-
'database_type' => (string)($rawConfigs['crowdsec_geolocation_maxmind_database_type'] ??
107-
Constants::MAXMIND_COUNTRY),
158+
'database_type' => (string)($this->handleRawConfig(
159+
$rawConfigs,
160+
'crowdsec_geolocation_maxmind_database_type',
161+
Constants::MAXMIND_COUNTRY)
162+
),
108163
'database_path' => Constants::GEOLOCATION_DIR . '/' .
109-
ltrim((string)($rawConfigs['crowdsec_geolocation_maxmind_database_path'] ?? ''),
110-
'/'),
164+
ltrim(
165+
(string)($this->handleRawConfig(
166+
$rawConfigs, 'crowdsec_geolocation_maxmind_database_path', '')
167+
),
168+
'/'
169+
),
111170
]
112171
],
113172
// Ban and Captcha walls
114-
'hide_mentions' => (bool)($rawConfigs['crowdsec_hide_mentions'] ?? false),
173+
'hide_mentions' => (bool)($this->handleRawConfig($rawConfigs, 'crowdsec_hide_mentions', false)),
115174
'custom_css' => $this->specialcharsDecodeEntQuotes(
116175
(string)($rawConfigs['crowdsec_theme_custom_css'] ?? '')
117176
),
@@ -191,6 +250,15 @@ public function handleRawConfigs(array $rawConfigs): array
191250
];
192251
}
193252

253+
private function handleRawConfig(array $rawConfigs, string $key, $defaultValue)
254+
{
255+
if (!empty($rawConfigs[$key])) {
256+
return $rawConfigs[$key];
257+
}
258+
259+
return $defaultValue;
260+
}
261+
194262
/**
195263
* @return string Ex: "X-Forwarded-For"
196264
*/
@@ -295,6 +363,7 @@ public function shouldBounceCurrentIp(): bool
295363
}
296364
}
297365
}
366+
298367
return true;
299368
}
300369
}

inc/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Constants extends LibConstants
2020
public const LOG_BASE_PATH = __DIR__ . '/../logs/';
2121
public const CACHE_PATH = __DIR__ . '/../.cache';
2222
public const CONFIG_PATH = __DIR__ . '/standalone-settings.php';
23-
public const VERSION = 'v2.0.3';
23+
public const VERSION = 'v2.0.4';
2424
public const GEOLOCATION_DIR = __DIR__ . '/../geolocation';
2525
public const TLS_DIR = __DIR__ . '/../tls';
2626
}

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://crowdsec.net/
44
Tags: crowdsec-bouncer, wordpress, security, firewall, captcha, ip-scanner, ip-blocker, ip-blocking, ip-address, ip-database, ip-range-check, crowdsec, ban-hosts, ban-management, anti-hacking, hacker-protection, captcha-image, captcha-generator, captcha-generation, captcha-service
55
Requires at least: 4.9
66
Tested up to: 6.1
7-
Stable tag: 2.0.3
7+
Stable tag: 2.0.4
88
Requires PHP: 7.2
99
License: MIT
1010
License URI: https://opensource.org/licenses/MIT

0 commit comments

Comments
 (0)