|
|
|
|
@ -18,6 +18,24 @@ namespace App\Services {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Cloud {
|
|
|
|
|
/** Test-only in-memory keyring; production still uses authenticated sodium ciphertext. */
|
|
|
|
|
final class CloudSecrets
|
|
|
|
|
{
|
|
|
|
|
private static array $payloads = [];
|
|
|
|
|
public static function encrypt(array $payload): string
|
|
|
|
|
{
|
|
|
|
|
$id = 'test-only:' . bin2hex(random_bytes(16));
|
|
|
|
|
self::$payloads[$id] = $payload;
|
|
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
public static function decrypt(string $ciphertext): array
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists($ciphertext, self::$payloads)) {
|
|
|
|
|
throw new CloudError('任务数据无法解密', 503);
|
|
|
|
|
}
|
|
|
|
|
return self::$payloads[$ciphertext];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final class CloudSessionService
|
|
|
|
|
{
|
|
|
|
|
public static int $calls = 0;
|
|
|
|
|
@ -252,9 +270,15 @@ namespace {
|
|
|
|
|
$log = $connection->getQueryLog(); $counts[] = count($log);
|
|
|
|
|
foreach ($log as $query) {
|
|
|
|
|
$sql = strtolower($query['query']);
|
|
|
|
|
$hasPayload = str_contains($sql, 'payload_cipher');
|
|
|
|
|
$scopedPowerPayload = str_contains($sql,
|
|
|
|
|
"case when cloud_jobs.kind = 'action' then cloud_jobs.payload_cipher "
|
|
|
|
|
. 'else null end as payload_cipher')
|
|
|
|
|
&& substr_count($sql, 'payload_cipher') === 2;
|
|
|
|
|
$assert(str_starts_with($sql, 'select ') && !str_contains($sql, 'cloud_sessions')
|
|
|
|
|
&& !str_contains($sql, 'payload_cipher') && !str_contains($sql, 'lease_token'),
|
|
|
|
|
'Read path touched a sensitive field/session or wrote state.');
|
|
|
|
|
&& (!$hasPayload || $scopedPowerPayload)
|
|
|
|
|
&& !str_contains($sql, 'lease_token'),
|
|
|
|
|
'Read path selected an unscoped secret/session field or wrote state.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$assert($counts === [3, 3], 'Per-row query amplification detected.');
|
|
|
|
|
@ -303,6 +327,55 @@ namespace {
|
|
|
|
|
$assert(Operations::detail($readonlyAdmin, '211')['job']['can_cancel'] === false,
|
|
|
|
|
'Read-only administrators received the task cancellation control.');
|
|
|
|
|
$reject(static fn () => Operations::cancel($readonlyAdmin, '211'), 403);
|
|
|
|
|
$powerPayload = \App\Services\Cloud\CloudSecrets::encrypt([
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'resource' => 'vms/11111111-1111-4111-8111-111111111110/actions',
|
|
|
|
|
'body' => ['action' => 'shutdown'],
|
|
|
|
|
]);
|
|
|
|
|
$insertJob(223, [
|
|
|
|
|
'kind' => 'action', 'status' => 'uncertain', 'dispatched_at' => $now - 5,
|
|
|
|
|
'payload_cipher' => $powerPayload,
|
|
|
|
|
]);
|
|
|
|
|
$powerDetail = Operations::detail($admin, '223')['job'];
|
|
|
|
|
$assert($powerDetail['can_cancel'] === true
|
|
|
|
|
&& $powerDetail['action'] === 'shutdown'
|
|
|
|
|
&& $powerDetail['kind_label'] === '关机',
|
|
|
|
|
'A reviewed power action did not expose its safe label and retry-stop control.');
|
|
|
|
|
$cancelledPower = Operations::cancel($admin, '223');
|
|
|
|
|
$cancelledPowerRow = Capsule::table('cloud_jobs')->where('id', 223)->first();
|
|
|
|
|
$assert(($cancelledPower['job']['status'] ?? null) === 'cancelled'
|
|
|
|
|
&& ($cancelledPower['job']['error_code'] ?? null) === 'admin_cancelled'
|
|
|
|
|
&& $cancelledPowerRow !== null && $cancelledPowerRow->lease_token === null
|
|
|
|
|
&& (int) $cancelledPowerRow->lease_until === 0
|
|
|
|
|
&& (int) $cancelledPowerRow->available_at === 0
|
|
|
|
|
&& (int) $cancelledPowerRow->dispatched_at === $now - 5,
|
|
|
|
|
'Administrator cancellation did not stop future power-task retries locally.');
|
|
|
|
|
$insertJob(224, [
|
|
|
|
|
'kind' => 'action', 'status' => 'running', 'dispatched_at' => $now - 4,
|
|
|
|
|
'lease_until' => $now + 60, 'payload_cipher' => $powerPayload,
|
|
|
|
|
]);
|
|
|
|
|
$runningPower = Operations::detail($admin, '224')['job'];
|
|
|
|
|
$assert($runningPower['can_cancel'] === false
|
|
|
|
|
&& str_contains((string) $runningPower['cancel_reason'], '正在执行'),
|
|
|
|
|
'A running power action incorrectly exposed the cancellation control.');
|
|
|
|
|
$reject(static fn () => Operations::cancel($admin, '224'), 409);
|
|
|
|
|
$assert(Capsule::table('cloud_jobs')->where('id', 224)->value('status') === 'running',
|
|
|
|
|
'Rejected running power cancellation mutated the task.');
|
|
|
|
|
$insertJob(225, [
|
|
|
|
|
'kind' => 'action', 'status' => 'uncertain', 'dispatched_at' => $now - 3,
|
|
|
|
|
'payload_cipher' => \App\Services\Cloud\CloudSecrets::encrypt([
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'resource' => 'vms/11111111-1111-4111-8111-111111111110/actions',
|
|
|
|
|
'body' => ['action' => 'destroy'],
|
|
|
|
|
]),
|
|
|
|
|
]);
|
|
|
|
|
$invalidPower = Operations::detail($admin, '225')['job'];
|
|
|
|
|
$assert($invalidPower['can_cancel'] === false
|
|
|
|
|
&& str_contains((string) $invalidPower['cancel_reason'], '无法验证'),
|
|
|
|
|
'An unverifiable action payload incorrectly exposed the cancellation control.');
|
|
|
|
|
$reject(static fn () => Operations::cancel($admin, '225'), 409);
|
|
|
|
|
$assert(Capsule::table('cloud_jobs')->where('id', 225)->value('status') === 'uncertain',
|
|
|
|
|
'Rejected forged power cancellation mutated the task.');
|
|
|
|
|
$cancelled = Operations::cancel($admin, '211');
|
|
|
|
|
$cancelledRow = Capsule::table('cloud_jobs')->where('id', 211)->first();
|
|
|
|
|
$assert(($cancelled['job']['status'] ?? null) === 'cancelled'
|
|
|
|
|
|