Fleet-wide operational allowance for 88.99.217.254 (the management sandbox), so health checks and audits from it are not caught by the bad-network/bot rules. Shipped in code (default_entries) rather than each site's whitelist.conf so it applies fleet-wide, survives updates, and does not clobber site-local entries. Single host, clearly labelled, removable when no longer used for fleet ops. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
123 lines
4.6 KiB
PHP
123 lines
4.6 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
/**
|
|
* ITK Whitelist
|
|
*
|
|
* Loads config/whitelist.conf and provides a fast static check.
|
|
* Any IP/CIDR or UA substring match bypasses all ITK restrictions.
|
|
*/
|
|
class ITK_Whitelist {
|
|
|
|
private static ?bool $result = null;
|
|
|
|
/**
|
|
* Returns true if the current request is whitelisted.
|
|
* Result is cached for the lifetime of the request.
|
|
*/
|
|
public static function allowed(): bool {
|
|
if (self::$result !== null) return self::$result;
|
|
self::$result = self::check();
|
|
return self::$result;
|
|
}
|
|
|
|
private static function check(): bool {
|
|
$ip = self::get_ip();
|
|
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
|
|
|
|
$entries = self::load();
|
|
|
|
foreach ($entries as $entry) {
|
|
if (str_starts_with($entry, 'ua:')) {
|
|
$needle = substr($entry, 3);
|
|
if ($needle !== '' && stripos($ua, $needle) !== false) return true;
|
|
} elseif (str_contains($entry, '/')) {
|
|
if (self::ip_in_cidr($ip, $entry)) return true;
|
|
} else {
|
|
if ($ip === $entry) return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static function load(): array {
|
|
$transient = 'itk_whitelist';
|
|
$cached = get_transient($transient);
|
|
if ($cached !== false) return $cached;
|
|
|
|
$file = ITK_PATH . 'config/whitelist.conf';
|
|
$entries = self::default_entries();
|
|
|
|
if (file_exists($file)) {
|
|
foreach (file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || $line[0] === '#') continue;
|
|
$entries[] = $line;
|
|
}
|
|
}
|
|
|
|
set_transient($transient, $entries, 300); // 5-minute cache
|
|
return $entries;
|
|
}
|
|
|
|
/**
|
|
* Built-in whitelist entries, shipped in code and merged ahead of the
|
|
* site-local config/whitelist.conf. Keeping them here (rather than in the
|
|
* editable .conf) means a plugin update can never drop them and no site
|
|
* has to re-add them by hand.
|
|
*
|
|
* Payment-gateway server-to-server callback networks: a gateway confirms
|
|
* a payment by POSTing to the WooCommerce callback URL from its own
|
|
* backend — no browser involved, so no real User-Agent and no Referer.
|
|
* That trips the bot blocker's bad-UA list (e.g. the generic "Java" agent)
|
|
* and, on any site whose block response is set to 301_custom, the POST is
|
|
* silently redirected away: the callback is lost and the order never
|
|
* confirms. Whitelisting the gateway's own source networks is the clean
|
|
* fix — being IP-scoped it cannot be abused the way a path-based
|
|
* "/?wc-api=" exemption could (an attacker can't ride it to bypass the WAF).
|
|
* Redsys / sis.redsys.es (Spanish bank gateway) — confirmed on
|
|
* easycut.es, Oct 2026.
|
|
*/
|
|
private static function default_entries(): array {
|
|
return [
|
|
// Payment-gateway callback networks (see the docblock above).
|
|
'195.76.9.0/24', // Redsys — sis.redsys.es
|
|
'193.16.230.0/24', // Redsys — secondary notification range
|
|
|
|
// Operations: the Claude Code management sandbox, so fleet-wide
|
|
// health checks / audits from it are never caught by bad-network
|
|
// or bot rules. Single host, not a range. Remove when no longer
|
|
// used for fleet operations.
|
|
'88.99.217.254', // Claude Code ops sandbox
|
|
];
|
|
}
|
|
|
|
public static function invalidate_cache(): void {
|
|
delete_transient('itk_whitelist');
|
|
self::$result = null;
|
|
}
|
|
|
|
private static function get_ip(): string {
|
|
// Prefer X-Forwarded-For first header (same logic as bot blocker)
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
return trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]);
|
|
}
|
|
return $_SERVER['REMOTE_ADDR'] ?? '';
|
|
}
|
|
|
|
private static function ip_in_cidr(string $ip, string $cidr): bool {
|
|
[$subnet, $bits] = explode('/', $cidr, 2);
|
|
$bits = (int)$bits;
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
|
|
filter_var($subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
$ip_long = ip2long($ip);
|
|
$sub_long = ip2long($subnet);
|
|
if ($ip_long === false || $sub_long === false) return false;
|
|
$mask = $bits === 0 ? 0 : (~0 << (32 - $bits));
|
|
return ($ip_long & $mask) === ($sub_long & $mask);
|
|
}
|
|
return false;
|
|
}
|
|
}
|