Skip to content

Commit 48d67e1

Browse files
GlombergCopilot
andauthored
Fix. SFW. User agents priority fixed. (#874)
* Fix. SFW. Priority fixed. * Fix. SFW. AC UA checking fixed. * Fix. Code. Copilot suggestion applied. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix. Code. Copilot suggestion applied. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix. Code. Docblock fixed. * Fix. Code. Code review fixed #1. * Fix. Code. Code error fixed. --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 2b4771b commit 48d67e1

4 files changed

Lines changed: 68 additions & 7 deletions

File tree

lib/Cleantalk/ApbctWP/Firewall/AntiCrawler.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@ private function runLogSearchForIpPool($ip_array)
251251
*
252252
* @param string $ip
253253
* @param string $status
254+
* @param bool $is_personal
254255
* @return array
255256
*/
256-
private function makeResult($ip, $status)
257+
private function makeResult($ip, $status, $is_personal = false)
257258
{
258-
return array('ip' => $ip, 'is_personal' => false, 'status' => $status);
259+
return array('ip' => $ip, 'is_personal' => $is_personal, 'status' => $status);
259260
}
260261

261262
/**
@@ -294,9 +295,12 @@ private function performUaCheck($current_ip)
294295
}
295296

296297
// Blacklisted — record but continue to cookie check
298+
// HardCode - write AC dined by UA as personal: blacklisted user-agent may be only personally
299+
// `is_personal` makes priority bigger, but we don't have a personal flag in the AC module yet, so this fix is needed
300+
$is_personal = true;
297301
return array(
298-
'entries' => array($this->makeResult($current_ip, 'DENY_ANTICRAWLER_UA')),
299-
'early_return' => false,
302+
'entries' => array($this->makeResult($current_ip, 'DENY_ANTICRAWLER_UA', $is_personal)),
303+
'early_return' => true,
300304
);
301305
}
302306
}

lib/Cleantalk/Common/Firewall.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Cleantalk\Common;
44

55
use Cleantalk\ApbctWP\Variables\Cookie;
6+
use Cleantalk\ApbctWP\Variables\Server;
67
use Cleantalk\Common\Firewall\FirewallModule;
78
use Cleantalk\ApbctWP\Variables\Get;
89

@@ -26,6 +27,11 @@ class Firewall
2627
{
2728
public $ip_array = array();
2829

30+
/**
31+
* @var array example array ( 'ua', 'ua_id', 'ua_status')
32+
*/
33+
public $user_agent_data = array();
34+
2935
// Database
3036
protected $db;
3137

@@ -64,6 +70,7 @@ public function __construct($db)
6470
$this->db = $db;
6571
$this->debug = (bool)Get::get('debug');
6672
$this->ip_array = $this->ipGet();
73+
$this->user_agent_data = $this->getUserAgentData();
6774
}
6875

6976
/**
@@ -81,6 +88,42 @@ public function ipGet($ips_input = 'real', $v4_only = true)
8188
return ! empty($result) ? array('real' => $result) : array();
8289
}
8390

91+
public function getUserAgentData()
92+
{
93+
$server_ua = Server::getString('HTTP_USER_AGENT');
94+
$ua_table = defined('APBCT_TBL_AC_UA_BL') ? APBCT_TBL_AC_UA_BL : null;
95+
96+
if ( $ua_table ) {
97+
$ua_bl_query = "SELECT * FROM $ua_table ORDER BY `ua_status` DESC;";
98+
$ua_bl_results = $this->db->fetchAll($ua_bl_query);
99+
foreach ( $ua_bl_results as $ua_bl_result ) {
100+
if ( ! empty($ua_bl_result['ua_template']) ) {
101+
$pattern = '%' . str_replace(array('"', '%'), array('', '\%'), $ua_bl_result['ua_template']) . '%i';
102+
$match = preg_match($pattern, $server_ua);
103+
104+
if ( $match === false && in_array(preg_last_error(), array(PREG_BACKTRACK_LIMIT_ERROR, PREG_RECURSION_LIMIT_ERROR), true) ) {
105+
continue;
106+
}
107+
108+
if ( $match === 1 ) {
109+
$ua_id = TT::getArrayValueAsString($ua_bl_result, 'id');
110+
111+
return array(
112+
'ua' => $server_ua,
113+
'ua_id' => $ua_id,
114+
'ua_status' => TT::getArrayValueAsString($ua_bl_result, 'ua_status'),
115+
);
116+
}
117+
}
118+
}
119+
}
120+
return array(
121+
'ua' => $server_ua,
122+
'ua_id' => null,
123+
'ua_status' => null,
124+
);
125+
}
126+
84127
/**
85128
* Loads the FireWall module to the array.
86129
* For inner usage only.
@@ -90,11 +133,12 @@ public function ipGet($ips_input = 'real', $v4_only = true)
90133
*/
91134
public function loadFwModule(FirewallModule $module)
92135
{
93-
if ( ! in_array($module, $this->fw_modules)) {
136+
if ( ! in_array($module, $this->fw_modules) ) {
94137
$module->setDb($this->db);
95138
$module->ipAppendAdditional($this->ip_array);
96139
$this->fw_modules[$module->module_name] = $module;
97140
$module->setIpArray($this->ip_array);
141+
$module->setUserAgentData($this->user_agent_data);
98142
}
99143
}
100144

@@ -120,8 +164,11 @@ public function run()
120164
$results[$module->module_name] = $module_results;
121165
}
122166

123-
if ($this->isWhitelisted($results)) {
124-
// Break protection logic if it whitelisted or trusted network.
167+
if (
168+
$this->isWhitelisted($results) &&
169+
( ! isset($this->user_agent_data['ua_status']) || (int) $this->user_agent_data['ua_status'] !== 0 )
170+
) {
171+
// Break protection logic if it whitelisted, or trusted network, or user-agent not blocked.
125172
break;
126173
}
127174
}

lib/Cleantalk/Common/Firewall/FirewallModule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public function setIpArray($ip_array)
7979
$this->ip_array = $ip_array;
8080
}
8181

82+
/**
83+
* @param array $user_agent_data
84+
*/
85+
public function setUserAgentData($user_agent_data)
86+
{
87+
$this->user_agent_data = $user_agent_data;
88+
}
89+
8290
/**
8391
* @param $result
8492
*

lib/Cleantalk/Common/Firewall/FirewallModuleAbstract.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ abstract class FirewallModuleAbstract
4040

4141
protected $ip_array = array();
4242

43+
protected $user_agent_data = array();
44+
4345
protected $test_ip;
4446

4547
protected $passed_ip;

0 commit comments

Comments
 (0)