Skip to content

Commit e403b5a

Browse files
style(code smell): Fix some code smelling issues
1 parent e0c23fe commit e403b5a

File tree

4 files changed

+108
-145
lines changed

4 files changed

+108
-145
lines changed

scripts/auto-prepend/settings.example.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
// Settings for ban wall
211211
'theme_text_ban_wall_tab_title' => 'Oops..',
212212
'theme_text_ban_wall_title' => '🤭 Oh!',
213-
'theme_text_ban_wall_subtitle' => 'This page is protected against cyber attacks and your IP has been banned by our system.',
213+
'theme_text_ban_wall_subtitle' =>
214+
'This page is protected against cyber attacks and your IP has been banned by our system.',
214215
'theme_text_ban_wall_footer' => '',
215216
];

src/StandaloneBounce.php

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,48 @@ public function getHttpMethod(): string
105105
return $_SERVER['REQUEST_METHOD'];
106106
}
107107

108+
private function getColorConfigs()
109+
{
110+
return [
111+
'text' => [
112+
'primary' => htmlspecialchars_decode(
113+
$this->getStringSettings('theme_color_text_primary'),
114+
\ENT_QUOTES
115+
),
116+
'secondary' => htmlspecialchars_decode(
117+
$this->getStringSettings('theme_color_text_secondary'),
118+
\ENT_QUOTES
119+
),
120+
'button' => htmlspecialchars_decode(
121+
$this->getStringSettings('theme_color_text_button'),
122+
\ENT_QUOTES
123+
),
124+
'error_message' => htmlspecialchars_decode(
125+
$this->getStringSettings('theme_color_text_error_message'),
126+
\ENT_QUOTES
127+
),
128+
],
129+
'background' => [
130+
'page' => htmlspecialchars_decode(
131+
$this->getStringSettings('theme_color_background_page'),
132+
\ENT_QUOTES
133+
),
134+
'container' => htmlspecialchars_decode(
135+
$this->getStringSettings('theme_color_background_container'),
136+
\ENT_QUOTES
137+
),
138+
'button' => htmlspecialchars_decode(
139+
$this->getStringSettings('theme_color_background_button'),
140+
\ENT_QUOTES
141+
),
142+
'button_hover' => htmlspecialchars_decode(
143+
$this->getStringSettings('theme_color_background_button_hover'),
144+
\ENT_QUOTES
145+
),
146+
],
147+
];
148+
}
149+
108150
/**
109151
* @return array ['hide_crowdsec_mentions': bool, color:[text:['primary' : string, 'secondary' : string, 'button' :
110152
* string, 'error_message : string' ...]]] (returns an array of option required to build the captcha wall
@@ -114,44 +156,7 @@ public function getCaptchaWallOptions(): array
114156
{
115157
return [
116158
'hide_crowdsec_mentions' => $this->getBoolSettings('hide_mentions'),
117-
'color' => [
118-
'text' => [
119-
'primary' => htmlspecialchars_decode(
120-
$this->getStringSettings('theme_color_text_primary'),
121-
\ENT_QUOTES
122-
),
123-
'secondary' => htmlspecialchars_decode(
124-
$this->getStringSettings('theme_color_text_secondary'),
125-
\ENT_QUOTES
126-
),
127-
'button' => htmlspecialchars_decode(
128-
$this->getStringSettings('theme_color_text_button'),
129-
\ENT_QUOTES
130-
),
131-
'error_message' => htmlspecialchars_decode(
132-
$this->getStringSettings('theme_color_text_error_message'),
133-
\ENT_QUOTES
134-
),
135-
],
136-
'background' => [
137-
'page' => htmlspecialchars_decode(
138-
$this->getStringSettings('theme_color_background_page'),
139-
\ENT_QUOTES
140-
),
141-
'container' => htmlspecialchars_decode(
142-
$this->getStringSettings('theme_color_background_container'),
143-
\ENT_QUOTES
144-
),
145-
'button' => htmlspecialchars_decode(
146-
$this->getStringSettings('theme_color_background_button'),
147-
\ENT_QUOTES
148-
),
149-
'button_hover' => htmlspecialchars_decode(
150-
$this->getStringSettings('theme_color_background_button_hover'),
151-
\ENT_QUOTES
152-
),
153-
],
154-
],
159+
'color' => $this->getColorConfigs(),
155160
'text' => [
156161
'captcha_wall' => [
157162
'tab_title' => htmlspecialchars_decode(
@@ -200,40 +205,7 @@ public function getBanWallOptions(): array
200205
{
201206
return [
202207
'hide_crowdsec_mentions' => $this->getBoolSettings('hide_mentions'),
203-
'color' => [
204-
'text' => [
205-
'primary' => htmlspecialchars_decode(
206-
$this->getStringSettings('theme_color_text_primary'),
207-
\ENT_QUOTES
208-
),
209-
'secondary' => htmlspecialchars_decode(
210-
$this->getStringSettings('theme_color_text_secondary'),
211-
\ENT_QUOTES
212-
),
213-
'error_message' => htmlspecialchars_decode(
214-
$this->getStringSettings('theme_color_text_error_message'),
215-
\ENT_QUOTES
216-
),
217-
],
218-
'background' => [
219-
'page' => htmlspecialchars_decode(
220-
$this->getStringSettings('theme_color_background_page'),
221-
\ENT_QUOTES
222-
),
223-
'container' => htmlspecialchars_decode(
224-
$this->getStringSettings('theme_color_background_container'),
225-
\ENT_QUOTES
226-
),
227-
'button' => htmlspecialchars_decode(
228-
$this->getStringSettings('theme_color_background_button'),
229-
\ENT_QUOTES
230-
),
231-
'button_hover' => htmlspecialchars_decode(
232-
$this->getStringSettings('theme_color_background_button_hover'),
233-
\ENT_QUOTES
234-
),
235-
],
236-
],
208+
'color' => $this->getColorConfigs(),
237209
'text' => [
238210
'ban_wall' => [
239211
'tab_title' => htmlspecialchars_decode(

tests/Integration/IpVerificationTest.php

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ final class IpVerificationTest extends TestCase
1717
/** @var LoggerInterface */
1818
private $logger;
1919

20-
/** @var bool */
20+
/** @var bool */
2121
private $useCurl;
2222

23-
/** @var bool */
23+
/** @var bool */
2424
private $useTls;
2525

2626
protected function setUp(): void
2727
{
2828
$this->logger = TestHelpers::createLogger();
29-
$this->useCurl = (bool) getenv('USE_CURL');
30-
$this->useTls = (string) getenv('BOUNCER_TLS_PATH');
29+
$this->useCurl = (bool)getenv('USE_CURL');
30+
$this->useTls = (string)getenv('BOUNCER_TLS_PATH');
3131
$this->watcherClient = new WatcherClient(['use_curl' => $this->useCurl], $this->logger);
3232
}
3333

@@ -36,6 +36,43 @@ public function cacheAdapterConfigProvider(): array
3636
return TestHelpers::cacheAdapterConfigProvider();
3737
}
3838

39+
private function cacheAdapterCheck($cacheAdapter, $origCacheName)
40+
{
41+
switch ($origCacheName) {
42+
case 'PhpFilesAdapter':
43+
$this->assertEquals(
44+
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
45+
get_class($cacheAdapter),
46+
'Tested adapter should be correct'
47+
);
48+
break;
49+
case 'MemcachedAdapter':
50+
$this->assertEquals(
51+
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
52+
get_class($cacheAdapter),
53+
'Tested adapter should be correct'
54+
);
55+
break;
56+
case 'RedisAdapter':
57+
$this->assertEquals(
58+
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
59+
get_class($cacheAdapter),
60+
'Tested adapter should be correct'
61+
);
62+
break;
63+
default:
64+
break;
65+
}
66+
}
67+
68+
private function addTlsConfig(&$bouncerConfigs, $tlsPath)
69+
{
70+
$bouncerConfigs['tls_cert_path'] = $tlsPath . '/bouncer.pem';
71+
$bouncerConfigs['tls_key_path'] = $tlsPath . '/bouncer-key.pem';
72+
$bouncerConfigs['tls_ca_cert_path'] = $tlsPath . '/ca-chain.pem';
73+
$bouncerConfigs['tls_verify_peer'] = true;
74+
}
75+
3976
/**
4077
* @group integration
4178
* @dataProvider cacheAdapterConfigProvider
@@ -54,47 +91,19 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
5491
'api_user_agent' => TestHelpers::UNIT_TEST_AGENT_PREFIX . '/' . Constants::BASE_USER_AGENT,
5592
'cache_system' => $cacheAdapterName,
5693
'redis_dsn' => getenv('REDIS_DSN'),
57-
'memcached_dsn' => getenv('MEMCACHED_DSN'),
94+
'memcached_dsn' => getenv('MEMCACHED_DSN'),
5895
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
5996
];
6097
if ($this->useTls) {
61-
$bouncerConfigs['tls_cert_path'] = $this->useTls . '/bouncer.pem';
62-
$bouncerConfigs['tls_key_path'] = $this->useTls . '/bouncer-key.pem';
63-
$bouncerConfigs['tls_ca_cert_path'] = $this->useTls . '/ca-chain.pem';
64-
$bouncerConfigs['tls_verify_peer'] = true;
98+
$this->addTlsConfig($bouncerConfigs, $this->useTls);
6599
}
66100

67101
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
68102

69103
// Test cache adapter
70104
$cacheAdapter = $bouncer->getCacheAdapter();
71105
$cacheAdapter->clear();
72-
73-
switch ($origCacheName) {
74-
case 'PhpFilesAdapter':
75-
$this->assertEquals(
76-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
77-
get_class($cacheAdapter),
78-
'Tested adapter should be correct'
79-
);
80-
break;
81-
case 'MemcachedAdapter':
82-
$this->assertEquals(
83-
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
84-
get_class($cacheAdapter),
85-
'Tested adapter should be correct'
86-
);
87-
break;
88-
case 'RedisAdapter':
89-
$this->assertEquals(
90-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
91-
get_class($cacheAdapter),
92-
'Tested adapter should be correct'
93-
);
94-
break;
95-
default:
96-
break;
97-
}
106+
$this->cacheAdapterCheck($cacheAdapter, $origCacheName);
98107

99108
$this->assertEquals(
100109
'ban',
@@ -143,8 +152,13 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
143152

144153
$this->logger->info('', ['message' => 'set "Large IPV4 range banned" state']);
145154
$this->watcherClient->deleteAllDecisions();
146-
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IP . '/'
147-
. TestHelpers::LARGE_IPV4_RANGE, 'ban');
155+
$this->watcherClient->addDecision(
156+
new \DateTime(),
157+
'24h',
158+
WatcherClient::HOURS24,
159+
TestHelpers::BAD_IP . '/' . TestHelpers::LARGE_IPV4_RANGE,
160+
'ban'
161+
);
148162
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
149163
$this->assertEquals(
150164
'ban',
@@ -187,46 +201,18 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
187201
'use_curl' => $this->useCurl,
188202
'cache_system' => $cacheAdapterName,
189203
'redis_dsn' => getenv('REDIS_DSN'),
190-
'memcached_dsn' => getenv('MEMCACHED_DSN'),
204+
'memcached_dsn' => getenv('MEMCACHED_DSN'),
191205
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
192206
];
193207
if ($this->useTls) {
194-
$bouncerConfigs['tls_cert_path'] = $this->useTls . '/bouncer.pem';
195-
$bouncerConfigs['tls_key_path'] = $this->useTls . '/bouncer-key.pem';
196-
$bouncerConfigs['tls_ca_cert_path'] = $this->useTls . '/ca-chain.pem';
197-
$bouncerConfigs['tls_verify_peer'] = true;
208+
$this->addTlsConfig($bouncerConfigs, $this->useTls);
198209
}
199210

200211
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
201212
// Test cache adapter
202213
$cacheAdapter = $bouncer->getCacheAdapter();
203214
$cacheAdapter->clear();
204-
205-
switch ($origCacheName) {
206-
case 'PhpFilesAdapter':
207-
$this->assertEquals(
208-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
209-
get_class($cacheAdapter),
210-
'Tested adapter should be correct'
211-
);
212-
break;
213-
case 'MemcachedAdapter':
214-
$this->assertEquals(
215-
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
216-
get_class($cacheAdapter),
217-
'Tested adapter should be correct'
218-
);
219-
break;
220-
case 'RedisAdapter':
221-
$this->assertEquals(
222-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
223-
get_class($cacheAdapter),
224-
'Tested adapter should be correct'
225-
);
226-
break;
227-
default:
228-
break;
229-
}
215+
$this->cacheAdapterCheck($cacheAdapter, $origCacheName);
230216
// As we are in stream mode, no live call should be done to the API.
231217
// Warm BlockList cache up
232218

@@ -270,7 +256,6 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
270256
$this->logger->debug('', ['message' => 'Refresh 2nd time the cache. Nothing should append.']);
271257
$bouncer->refreshBlocklistCache();
272258

273-
274259
$this->assertEquals(
275260
'ban',
276261
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
@@ -293,7 +278,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
293278
'api_user_agent' => TestHelpers::UNIT_TEST_AGENT_PREFIX . '/' . Constants::BASE_USER_AGENT,
294279
'cache_system' => $cacheAdapterName,
295280
'redis_dsn' => getenv('REDIS_DSN'),
296-
'memcached_dsn' => getenv('MEMCACHED_DSN'),
281+
'memcached_dsn' => getenv('MEMCACHED_DSN'),
297282
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
298283
];
299284
if ($this->useTls) {

tests/Integration/WatcherClient.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ public function setSecondState(): void
7878
$this->deleteAllDecisions();
7979
$now = new \DateTime();
8080
$this->addDecision($now, '36h', '+36 hours', TestHelpers::NEWLY_BAD_IP, 'ban');
81-
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP . '/' .
82-
TestHelpers::IP_RANGE, 'captcha');
81+
$this->addDecision(
82+
$now,
83+
'48h',
84+
'+48 hours',
85+
TestHelpers::NEWLY_BAD_IP . '/' . TestHelpers::IP_RANGE,
86+
'captcha'
87+
);
8388
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
8489
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_JAPAN, 'ban');
8590
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_FRANCE, 'ban');

0 commit comments

Comments
 (0)