|
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
|
use App\Services\Cloud\CloudOperationErrorCatalog;
|
|
|
|
|
use App\Services\Cloud\CloudUpdateHelperClient;
|
|
|
|
|
|
|
|
|
|
/** Super-admin-only proxy for the root-owned local update helper. */
|
|
|
|
|
final class AdminUpdateController
|
|
|
|
|
{
|
|
|
|
|
private function actor($request): array
|
|
|
|
|
{
|
|
|
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor();
|
|
|
|
|
CloudAccess::manage($actor);
|
|
|
|
|
return $actor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function helper(): CloudUpdateHelperClient
|
|
|
|
|
{
|
|
|
|
|
return new CloudUpdateHelperClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function credentialTransportIsSecure($request): bool
|
|
|
|
|
{
|
|
|
|
|
if (strtolower((string) $request->getUri()->getScheme()) === 'https') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Permit a direct loopback request for local setup and tests. Do not
|
|
|
|
|
// trust forwarding headers here: proxy scheme handling belongs in the
|
|
|
|
|
// web server / PSR-7 request construction boundary.
|
|
|
|
|
$host = strtolower(rtrim((string) $request->getUri()->getHost(), '.'));
|
|
|
|
|
return in_array((string) $request->getServerParam('REMOTE_ADDR', ''), ['127.0.0.1', '::1'], true)
|
|
|
|
|
&& in_array($host, ['localhost', '127.0.0.1', '::1'], true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function json($response, array $result, int $status = 200)
|
|
|
|
|
{
|
|
|
|
|
return $response->withJson($this->withoutAccessToken($result), $status)
|
|
|
|
|
->withHeader('Cache-Control', 'no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Defence in depth: a secret is never returned if the helper regresses. */
|
|
|
|
|
private function withoutAccessToken(array $value): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($value as $key => $item) {
|
|
|
|
|
$name = strtolower((string) $key);
|
|
|
|
|
if (in_array($name, ['access_token', 'gitea_access_token'], true)) {
|
|
|
|
|
unset($value[$key]);
|
|
|
|
|
} elseif (CloudOperationErrorCatalog::isDiagnosticKey($name)) {
|
|
|
|
|
// Do not recursively pass arbitrary validation/error maps
|
|
|
|
|
// through: a nested non-diagnostic key can still carry a
|
|
|
|
|
// host path, command, URL, or credential. The updater UI only
|
|
|
|
|
// needs one bounded diagnostic sentence, so discard complex
|
|
|
|
|
// diagnostic payloads and retain a scalar redacted summary.
|
|
|
|
|
if (!is_scalar($item)) {
|
|
|
|
|
unset($value[$key]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$summary = CloudOperationErrorCatalog::adminSummary(null, null, $item);
|
|
|
|
|
if ($summary === null) {
|
|
|
|
|
unset($value[$key]);
|
|
|
|
|
} else {
|
|
|
|
|
$value[$key] = $summary;
|
|
|
|
|
}
|
|
|
|
|
} elseif (is_array($item)) {
|
|
|
|
|
$value[$key] = $this->withoutAccessToken($item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function settings($request, $response)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request);
|
|
|
|
|
if ($request->getMethod() === 'GET') {
|
|
|
|
|
return $this->json($response, $this->helper()->request(
|
|
|
|
|
'GET', '/v1/settings', $actor
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$body = CloudInput::object($request, 8192);
|
|
|
|
|
CloudInput::only($body, ['access_token', 'clear_token']);
|
|
|
|
|
if (array_key_exists('access_token', $body) && !is_string($body['access_token'])) {
|
|
|
|
|
throw new CloudError('访问令牌格式无效');
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('clear_token', $body) && !is_bool($body['clear_token'])) {
|
|
|
|
|
throw new CloudError('清除令牌选项无效');
|
|
|
|
|
}
|
|
|
|
|
if (($body['clear_token'] ?? false) === true && ($body['access_token'] ?? '') !== '') {
|
|
|
|
|
throw new CloudError('不能同时设置并清除访问令牌');
|
|
|
|
|
}
|
|
|
|
|
if (($body['access_token'] ?? '') !== '' && !$this->credentialTransportIsSecure($request)) {
|
|
|
|
|
throw new CloudError('访问令牌只能通过 HTTPS 或本机回环地址提交', 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, $this->helper()->request(
|
|
|
|
|
'PUT', '/v1/settings', $actor, $body
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function check($request, $response)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request);
|
|
|
|
|
$body = CloudInput::object($request, 1024);
|
|
|
|
|
CloudInput::only($body, []);
|
|
|
|
|
return $this->json($response, $this->helper()->request(
|
|
|
|
|
'POST', '/v1/check', $actor, $body
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function apply($request, $response)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request);
|
|
|
|
|
$body = CloudInput::object($request, 4096);
|
|
|
|
|
CloudInput::only($body, ['expected_current_commit', 'expected_remote_commit']);
|
|
|
|
|
foreach (['expected_current_commit', 'expected_remote_commit'] as $field) {
|
|
|
|
|
if (!isset($body[$field]) || !is_string($body[$field])
|
|
|
|
|
|| preg_match('/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/D', $body[$field]) !== 1) {
|
|
|
|
|
throw new CloudError('更新提交标识无效');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->json($response, $this->helper()->request(
|
|
|
|
|
'POST', '/v1/apply', $actor, $body
|
|
|
|
|
), 202);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function job($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$actor = $this->actor($request);
|
|
|
|
|
$jobId = (string) ($args['id'] ?? '');
|
|
|
|
|
if (preg_match('/^[A-Za-z0-9_-]{16,64}$/D', $jobId) !== 1) {
|
|
|
|
|
throw new CloudError('更新任务编号无效', 404);
|
|
|
|
|
}
|
|
|
|
|
return $this->json($response, $this->helper()->request(
|
|
|
|
|
'GET', '/v1/jobs/' . rawurlencode($jobId), $actor
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|