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.
88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Controllers\CloudAdminAbuseMonitoringController;
|
|
use App\Services\Cloud\CloudError;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
ob_start();
|
|
require __DIR__ . '/CloudAdminAbuseMonitoringServiceTest.php';
|
|
ob_end_clean();
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$request = static function (string $path, array $actor,
|
|
array $query = []): Request {
|
|
$uri = $path;
|
|
if ($query !== []) {
|
|
$uri .= '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
|
}
|
|
return Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'GET',
|
|
'REQUEST_URI' => $uri,
|
|
'QUERY_STRING' => parse_url($uri, PHP_URL_QUERY) ?: '',
|
|
]))->withAttribute('cloud_actor', $actor);
|
|
};
|
|
|
|
$controller = new CloudAdminAbuseMonitoringController();
|
|
$readonly = [
|
|
'user_id' => 88,
|
|
'is_admin' => true,
|
|
'is_super_admin' => false,
|
|
'can_manage' => false,
|
|
];
|
|
|
|
$index = $controller->index(
|
|
$request('/api/v1/admin/abuse-monitoring', $readonly, [
|
|
'per_page' => '2',
|
|
]),
|
|
new Response()
|
|
);
|
|
$listed = json_decode((string) $index->getBody(), true,
|
|
flags: JSON_THROW_ON_ERROR);
|
|
$assert($index->getStatusCode() === 200
|
|
&& $index->getHeaderLine('Cache-Control') === 'private, no-store'
|
|
&& is_array($listed['items'] ?? null)
|
|
&& count($listed['items']) <= 2,
|
|
'Read-only administrator list lost its response or cache boundary.');
|
|
|
|
$detail = $controller->detail(
|
|
$request('/api/v1/admin/abuse-monitoring/instances/2', $readonly),
|
|
new Response(),
|
|
['id' => '2']
|
|
);
|
|
$detailBody = (string) $detail->getBody();
|
|
$decoded = json_decode($detailBody, true, flags: JSON_THROW_ON_ERROR);
|
|
$assert($detail->getStatusCode() === 200
|
|
&& $detail->getHeaderLine('Cache-Control') === 'private, no-store'
|
|
&& ($decoded['item']['instance_id'] ?? '') === '2'
|
|
&& count($decoded['series'] ?? []) <= 60
|
|
&& !str_contains($detailBody, 'metrics_json')
|
|
&& !str_contains($detailBody, 'evidence_json')
|
|
&& !str_contains($detailBody, '"remote_addresses"')
|
|
&& !str_contains($detailBody, '"remote_ports"'),
|
|
'Detail API leaked raw observation data or lost its bounded contract.');
|
|
|
|
try {
|
|
$controller->index(
|
|
$request('/api/v1/admin/abuse-monitoring', [
|
|
'user_id' => 89,
|
|
'is_admin' => false,
|
|
]),
|
|
new Response()
|
|
);
|
|
throw new RuntimeException('Non-administrator monitoring access succeeded.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 403,
|
|
'Non-administrator monitoring access returned the wrong status.');
|
|
}
|
|
|
|
echo "Administrator abuse-monitoring API behavior passed.\n";
|