Files
Investidor-V2/outputs/investmoz-staff-preparation-20261012/diagnose-promoter-mfa-readonly.php
T
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

66 lines
5.1 KiB
PHP

<?php
ini_set('zend.exception_ignore_args', '1');
try {
$portal = dirname(__DIR__, 2);
$plan = json_decode(file_get_contents($portal.'/storage/app/private/investmoz-v2/portal-audit/internal-designation-plan.json'), true, 512, JSON_THROW_ON_ERROR);
$qa = $plan['source'];
if (!str_contains($qa, 'onboarding-qa-internal-20261011_191450')) throw new RuntimeException('QA source rejected');
require $qa.'/qa-autoload.php';
$app = require $qa.'/bootstrap/app.php';
(new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app);
(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
$c = config('database.connections.'.config('database.default'));
if ($c['driver'] !== 'pgsql' || (int)$c['port'] !== 55441 || $c['database'] !== 'onboarding_internal_20261011_191450_test' || !empty($c['url']) || !in_array($c['host'], ['127.0.0.1', 'localhost'], true)) throw new RuntimeException('QA target rejected');
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$db = Illuminate\Support\Facades\DB::class;
$db::statement('BEGIN READ ONLY');
try {
if ((int)$db::selectOne('SELECT inet_server_port() AS port')->port !== 55441) throw new RuntimeException('Server rejected');
$actors = json_decode(file_get_contents($qa.'/storage/app/private/onboarding-actors-local.json'), true, 512, JSON_THROW_ON_ERROR);
$actor = App\Models\User::findOrFail($actors['promoter']['id']);
$secret = Laravel\Fortify\Fortify::currentEncrypter()->decrypt($actor->two_factor_secret);
$html = file_get_contents($qa.'/storage/app/private/promoter-qa18118-2fas-local.html');
preg_match('/<svg\b.*?<\/svg>/s', $html, $matches);
$svgMatches = isset($matches[0]) && hash_equals($actor->twoFactorQrCodeSvg(), $matches[0]);
$uri = $actor->twoFactorQrCodeUrl();
parse_str(parse_url($uri, PHP_URL_QUERY), $params);
$rows = [];
$driver = config('session.driver');
if ($driver === 'database') {
foreach ($db::table(config('session.table', 'sessions'))->where('last_activity', '>=', time()-3600)->orderByDesc('last_activity')->limit(12)->get(['payload','last_activity']) as $row) {
$rows[] = ['payload' => base64_decode($row->payload, true), 'at' => $row->last_activity];
}
} elseif ($driver === 'file') {
$sessionPath = realpath(config('session.files'));
if (!$sessionPath || !str_starts_with($sessionPath, realpath($qa).DIRECTORY_SEPARATOR)) throw new RuntimeException('QA session path rejected');
$files = glob($sessionPath.'/*');
usort($files, fn ($a, $b) => filemtime($b)<=>filemtime($a));
foreach (array_slice($files, 0, 12) as $file) if (is_file($file) && filemtime($file) >= time()-3600) $rows[] = ['payload' => file_get_contents($file), 'at' => filemtime($file)];
}
$sessions = [];
foreach ($rows as $row) {
$payload = $row['payload'];
if (config('session.encrypt')) $payload = app('encrypter')->decrypt($payload);
$data = json_decode($payload, true);
if (!is_array($data)) $data = @unserialize($payload, ['allowed_classes' => false]);
if (!is_array($data)) continue;
$id = data_get($data, 'login.id');
$role = null;
foreach ($actors as $name => $local) if ($id && (int)$local['id'] === (int)$id) $role = $name;
$sessions[] = ['at_utc' => gmdate('c', $row['at']), 'pending_mfa_user_id' => $id, 'synthetic_role' => $role,
'matches_promoter_qr' => $id ? (int)$id === (int)$actor->id : null,
'already_authenticated' => array_filter(array_keys($data), fn ($key) => str_starts_with($key, 'login_web_')) !== []];
}
$proof = ['at' => gmdate('c'), 'read_only' => true, 'target' => $c['database'].':55441', 'account_id' => $actor->id,
'account_email' => $actor->email, 'secret_readable' => true, 'QR_matches_existing_secret' => $svgMatches,
'QR_scheme' => parse_url($uri, PHP_URL_SCHEME), 'QR_type' => parse_url($uri, PHP_URL_HOST), 'QR_secret_matches_account' => hash_equals($secret, $params['secret'] ?? ''),
'algorithm' => $params['algorithm'] ?? 'SHA1', 'digits' => $params['digits'] ?? 6, 'period_seconds' => $params['period'] ?? 30,
'session_driver' => $driver, 'session_cookie' => config('session.cookie'), 'session_encrypted' => config('session.encrypt'),
'configuration_cached' => $app->configurationIsCached(), 'sessions' => $sessions,
'secret_or_code_recorded' => false, 'database_writes' => 0];
unset($secret, $uri, $params, $html, $matches, $rows, $data, $payload, $actors, $actor);
} finally { $db::statement('ROLLBACK'); }
file_put_contents(__DIR__.'/promoter-mfa-diagnosis-readonly.json', json_encode($proof, JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));
echo json_encode($proof, JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR);
} catch (Throwable $e) { echo json_encode(['result' => 'stopped', 'type' => $e::class, 'line' => $e->getLine()]); exit(1); }