65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
#!/usr/bin/env php
|
||
<?php
|
||
/**
|
||
* 二次风控 Session 缓存回归(CLI 输出 JSON)
|
||
*/
|
||
if (PHP_SAPI !== 'cli') {
|
||
exit(1);
|
||
}
|
||
|
||
$root = dirname(__DIR__);
|
||
require_once $root . '/cong.php';
|
||
require_once $root . '/lib/CloakHttpHelpers.php';
|
||
require_once $root . '/lib/Cloak/ClientIpResolver.php';
|
||
require_once $root . '/lib/Cloak/RiskSecondaryCache.php';
|
||
|
||
$_SESSION = [];
|
||
|
||
$tests = [];
|
||
|
||
RiskSecondaryCache::store([
|
||
'ip' => '203.0.113.10',
|
||
'result' => 'true',
|
||
'reason' => 'ok',
|
||
'fp_url' => 'https://fp.example/a',
|
||
]);
|
||
$hit = RiskSecondaryCache::get('203.0.113.10');
|
||
$tests['same_ip_hit'] = is_array($hit) && ($hit['result'] ?? '') === 'true';
|
||
|
||
RiskSecondaryCache::applyToSession($hit);
|
||
$tests['apply_session'] = ($_SESSION['visit_to_3'] ?? '') == 3 && ($_SESSION['check_result'] ?? '') === 'true';
|
||
|
||
$miss = RiskSecondaryCache::get('198.51.100.1');
|
||
$tests['ip_change_miss'] = $miss === null && empty($_SESSION[RiskSecondaryCache::SESSION_KEY]);
|
||
$tests['ip_change_resets_visit_to_3'] = ($_SESSION['visit_to_3'] ?? '') === '1';
|
||
|
||
RiskSecondaryCache::store([
|
||
'ip' => '203.0.113.10',
|
||
'result' => 'false',
|
||
'reason' => 'deny',
|
||
'fp_url' => 'https://zp.example/',
|
||
]);
|
||
$api = RiskSecondaryCache::toApiResponse(RiskSecondaryCache::get('203.0.113.10'));
|
||
$tests['to_api_response'] = $api['result'] === false && $api['reason'] === RiskSecondaryCache::hitReason();
|
||
|
||
RiskSecondaryCache::clear();
|
||
$tests['clear'] = RiskSecondaryCache::get('203.0.113.10') === null;
|
||
|
||
$originOk = false;
|
||
$_SERVER['HTTP_HOST'] = 'cloak.example.com';
|
||
$_SERVER['HTTP_ORIGIN'] = 'https://www.example.com';
|
||
require_once $root . '/lib/Cloak/CloakSession.php';
|
||
$ref = new ReflectionClass('CloakFcheckCors');
|
||
$method = $ref->getMethod('isAllowedOrigin');
|
||
$method->setAccessible(true);
|
||
$originOk = (bool) $method->invoke(null, 'https://www.example.com');
|
||
$tests['cors_sibling_subdomain'] = $originOk;
|
||
|
||
$badOrigin = !(bool) $method->invoke(null, 'https://evil.other.com');
|
||
$tests['cors_reject_foreign'] = $badOrigin;
|
||
|
||
$ok = !in_array(false, $tests, true);
|
||
|
||
echo json_encode(['ok' => $ok, 'tests' => $tests], JSON_UNESCAPED_UNICODE) . "\n";
|
||
exit($ok ? 0 : 1);
|