You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
6.1 KiB
PHTML
150 lines
6.1 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Middleware\SiteAnalytics;
|
||
|
|
use App\Models\SiteSecurityEvent;
|
||
|
|
use App\Services\SiteAnalyticsService;
|
||
|
|
use App\Services\SiteSecurityEventService;
|
||
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||
|
|
use Slim\Http\Environment;
|
||
|
|
use Slim\Http\Request;
|
||
|
|
use Slim\Http\Response;
|
||
|
|
|
||
|
|
$System_Config = [
|
||
|
|
'timeZone' => 'Asia/Shanghai',
|
||
|
|
'key' => 'site-security-test-key',
|
||
|
|
];
|
||
|
|
date_default_timezone_set($System_Config['timeZone']);
|
||
|
|
|
||
|
|
$capsule = new Capsule();
|
||
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'log');
|
||
|
|
$capsule->setAsGlobal();
|
||
|
|
$capsule->bootEloquent();
|
||
|
|
|
||
|
|
$assertSame = function ($expected, $actual, $label) {
|
||
|
|
if ($expected !== $actual) {
|
||
|
|
fwrite(STDERR, sprintf(
|
||
|
|
"%s failed: expected %s, got %s\n",
|
||
|
|
$label,
|
||
|
|
var_export($expected, true),
|
||
|
|
var_export($actual, true)
|
||
|
|
));
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
$assertTrue = function ($actual, $label) {
|
||
|
|
if (!$actual) {
|
||
|
|
fwrite(STDERR, $label . " failed\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
$request = function ($uri, $method = 'GET', $ip = '192.0.2.10') {
|
||
|
|
return Request::createFromEnvironment(Environment::mock([
|
||
|
|
'REQUEST_METHOD' => $method,
|
||
|
|
'REQUEST_URI' => $uri,
|
||
|
|
'REMOTE_ADDR' => $ip,
|
||
|
|
'HTTP_HOST' => 'panel.example',
|
||
|
|
'HTTP_USER_AGENT' => 'Mozilla/5.0 Chrome/120.0 Windows',
|
||
|
|
]));
|
||
|
|
};
|
||
|
|
$response = function ($status, $location = '') {
|
||
|
|
$response = (new Response())->withStatus($status);
|
||
|
|
return $location === '' ? $response : $response->withHeader('Location', $location);
|
||
|
|
};
|
||
|
|
|
||
|
|
SiteAnalyticsService::ensureTables();
|
||
|
|
$schema = $capsule->getConnection('log')->getSchemaBuilder();
|
||
|
|
$assertTrue($schema->hasTable('site_security_events'), 'security table created');
|
||
|
|
|
||
|
|
$normal = SiteSecurityEventService::classify($request('/user'), $response(200));
|
||
|
|
$assertSame(null, $normal, 'normal page ignored');
|
||
|
|
$notFound = SiteSecurityEventService::classify($request('/missing-page'), $response(404));
|
||
|
|
$assertSame('not_found', $notFound['reason_code'], '404 classified');
|
||
|
|
$assertSame('low', $notFound['risk_level'], '404 risk');
|
||
|
|
$assertSame(null, SiteSecurityEventService::classify($request('/missing.css'), $response(404)), 'static 404 ignored');
|
||
|
|
$credential = SiteSecurityEventService::classify($request('/.env'), $response(404));
|
||
|
|
$assertSame('credential_probe', $credential['reason_code'], 'credential probe classified');
|
||
|
|
$assertSame('high', $credential['risk_level'], 'credential probe risk');
|
||
|
|
$traversal = SiteSecurityEventService::classify($request('/..%2F..%2Fetc/passwd'), $response(404));
|
||
|
|
$assertSame('path_traversal', $traversal['reason_code'], 'traversal classified');
|
||
|
|
$assertSame('critical', $traversal['risk_level'], 'traversal risk');
|
||
|
|
$unusual = SiteSecurityEventService::classify($request('/', 'TRACE'), $response(405));
|
||
|
|
$assertSame('unusual_method', $unusual['reason_code'], 'unusual method classified');
|
||
|
|
$protected = SiteSecurityEventService::classify(
|
||
|
|
$request('/admin/user'),
|
||
|
|
$response(302, '/user'),
|
||
|
|
['user_id' => 42, 'is_admin' => false]
|
||
|
|
);
|
||
|
|
$assertSame('protected_area', $protected['reason_code'], 'protected admin route classified');
|
||
|
|
$assertSame('medium', $protected['risk_level'], 'authenticated protected route risk');
|
||
|
|
|
||
|
|
$identity = [
|
||
|
|
'visitor_id' => str_repeat('a', 32),
|
||
|
|
'session_id' => str_repeat('b', 32),
|
||
|
|
'visitor_new' => false,
|
||
|
|
'session_new' => false,
|
||
|
|
];
|
||
|
|
$userContext = ['user_id' => 42, 'is_admin' => false];
|
||
|
|
$assertTrue(
|
||
|
|
SiteSecurityEventService::record($request('/missing-one'), $response(404), $identity, $userContext),
|
||
|
|
'first 404 recorded'
|
||
|
|
);
|
||
|
|
$assertTrue(
|
||
|
|
SiteSecurityEventService::record($request('/missing-two'), $response(404), $identity, $userContext),
|
||
|
|
'second 404 merged'
|
||
|
|
);
|
||
|
|
$assertSame(1, SiteSecurityEvent::where('reason_code', 'not_found')->count(), '404 rows merged');
|
||
|
|
$merged = SiteSecurityEvent::where('reason_code', 'not_found')->first();
|
||
|
|
$assertSame(2, (int) $merged->hit_count, '404 hit count incremented');
|
||
|
|
$assertSame('/missing-two', (string) $merged->request_path, 'latest merged path retained');
|
||
|
|
|
||
|
|
$assertTrue(
|
||
|
|
SiteSecurityEventService::record($request('/.env'), $response(404), $identity, $userContext),
|
||
|
|
'sensitive path recorded'
|
||
|
|
);
|
||
|
|
$assertTrue(
|
||
|
|
SiteSecurityEventService::record(
|
||
|
|
$request('/link/SECRET-TOKEN/config.php'),
|
||
|
|
$response(404),
|
||
|
|
$identity,
|
||
|
|
$userContext
|
||
|
|
),
|
||
|
|
'token path recorded'
|
||
|
|
);
|
||
|
|
$redacted = SiteSecurityEvent::where('request_path', 'like', '/link/%')->first();
|
||
|
|
$assertSame('/link/{redacted}/config.php', (string) $redacted->request_path, 'subscription token redacted');
|
||
|
|
|
||
|
|
$today = date('Y-m-d');
|
||
|
|
$report = SiteSecurityEventService::report($today, $today, ['actor' => 'user'], 1, 50);
|
||
|
|
$assertSame(3, $report['summary']['records'], 'security report records');
|
||
|
|
$assertSame(4, $report['summary']['total_hits'], 'security report hits');
|
||
|
|
$assertSame(1, $report['summary']['affected_users'], 'security report users');
|
||
|
|
$assertSame(2, $report['summary']['high_risk_hits'], 'security high risk hits');
|
||
|
|
$assertSame(3, count($report['events']), 'security report events');
|
||
|
|
$assertTrue(count($report['reason_ranking']) >= 2, 'reason ranking returned');
|
||
|
|
|
||
|
|
$middlewareRequest = $request('/middleware-missing', 'GET', '198.51.100.9');
|
||
|
|
$middlewareResponse = (new SiteAnalytics())(
|
||
|
|
$middlewareRequest,
|
||
|
|
new Response(),
|
||
|
|
function ($request, $response) {
|
||
|
|
return $response->withStatus(404);
|
||
|
|
}
|
||
|
|
);
|
||
|
|
$assertSame(404, $middlewareResponse->getStatusCode(), 'middleware keeps response status');
|
||
|
|
$assertSame(
|
||
|
|
1,
|
||
|
|
SiteSecurityEvent::where('reason_code', 'not_found')->where('user_id', 0)->count(),
|
||
|
|
'middleware records anonymous abnormal request'
|
||
|
|
);
|
||
|
|
|
||
|
|
$old = SiteSecurityEvent::where('reason_code', 'not_found')->first();
|
||
|
|
$old->last_seen_at = strtotime('-100 days');
|
||
|
|
$old->save();
|
||
|
|
$assertTrue(!method_exists(SiteSecurityEventService::class, 'cleanup'), 'automatic security cleanup is removed');
|
||
|
|
$assertSame(1, SiteSecurityEvent::where('id', $old->id)->count(), 'old security event remains available');
|
||
|
|
|
||
|
|
echo "Site security event service tests passed.\n";
|