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.
61 lines
3.5 KiB
PHTML
61 lines
3.5 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
namespace App\Controllers;
|
||
|
|
|
||
|
|
use App\Services\RedisClient;
|
||
|
|
use App\Services\Cloud\CloudEngineClient;
|
||
|
|
use App\Services\Cloud\CloudSecrets;
|
||
|
|
use App\Services\Cloud\CloudSessionService;
|
||
|
|
|
||
|
|
final class CloudInternalController
|
||
|
|
{
|
||
|
|
public static function validSignature(string $method, string $path, string $body, array $headers, string $key, int $now): bool
|
||
|
|
{
|
||
|
|
$time = $headers['timestamp'] ?? '';
|
||
|
|
$nonce = $headers['nonce'] ?? '';
|
||
|
|
$actor = $headers['actor'] ?? '';
|
||
|
|
$signature = $headers['signature'] ?? '';
|
||
|
|
if (!is_string($time) || preg_match('/^[0-9]{1,12}$/D', $time) !== 1 || abs($now - (int) $time) > 60
|
||
|
|
|| !is_string($nonce) || preg_match('/^[A-Za-z0-9_-]{16,128}$/D', $nonce) !== 1
|
||
|
|
|| !is_string($signature) || preg_match('/^[a-f0-9]{64}$/D', $signature) !== 1
|
||
|
|
|| !is_string($actor) || strlen($actor) > 2048 || preg_match('/^[A-Za-z0-9_-]+$/D', $actor) !== 1) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$expected = hash_hmac('sha256', CloudEngineClient::canonical($method, $path, $body, $time, $nonce, $actor), $key);
|
||
|
|
return hash_equals($expected, $signature);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function introspection($request, $response)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$body = (string) $request->getBody();
|
||
|
|
$headers = ['timestamp' => $request->getHeaderLine('X-Cloud-Timestamp'),
|
||
|
|
'nonce' => $request->getHeaderLine('X-Cloud-Nonce'), 'actor' => $request->getHeaderLine('X-Cloud-Actor'),
|
||
|
|
'signature' => $request->getHeaderLine('X-Cloud-Signature')];
|
||
|
|
if (strlen($body) > 4096 || !self::validSignature($request->getMethod(), $request->getRequestTarget(),
|
||
|
|
$body, $headers, CloudSecrets::key('engine_hmac_key'), time())) {
|
||
|
|
return $response->withJson(['active' => false], 403)->withHeader('Cache-Control', 'no-store');
|
||
|
|
}
|
||
|
|
$redis = (new RedisClient())->getClient();
|
||
|
|
if ((string) $redis->set('cloud-introspection-nonce:' . $headers['nonce'], '1', 'EX', 120, 'NX') !== 'OK') {
|
||
|
|
return $response->withJson(['active' => false], 403)->withHeader('Cache-Control', 'no-store');
|
||
|
|
}
|
||
|
|
$value = json_decode($body, true);
|
||
|
|
if (!is_array($value) || !is_string($value['session_id'] ?? null)
|
||
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $value['session_id']) !== 1
|
||
|
|
|| !isset($value['user_id'], $value['host_id'], $value['vm_uuid'])) {
|
||
|
|
return $response->withJson(['active' => false], 400)->withHeader('Cache-Control', 'no-store');
|
||
|
|
}
|
||
|
|
$session = \App\Models\CloudSession::find($value['session_id']);
|
||
|
|
$active = $session !== null && (int) $session->user_id === (int) $value['user_id']
|
||
|
|
&& CloudSessionService::active($value['session_id'], (int) $value['host_id'], (string) $value['vm_uuid']);
|
||
|
|
return $response->withJson(['active' => $active])->withHeader('Cache-Control', 'no-store');
|
||
|
|
} catch (\Throwable $error) {
|
||
|
|
error_log('Cloud session introspection unavailable: ' . get_class($error) . ' at '
|
||
|
|
. basename($error->getFile()) . ':' . $error->getLine() . ' ' . json_encode(array_map(
|
||
|
|
fn ($frame) => [basename($frame['file'] ?? ''), $frame['line'] ?? 0, $frame['function'] ?? ''],
|
||
|
|
array_slice($error->getTrace(), 0, 4))));
|
||
|
|
return $response->withJson(['active' => false], 503)->withHeader('Cache-Control', 'no-store');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|