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.
virt/web/tests/CookieAuthContractTest.php

49 lines
2.3 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$source = file_get_contents($root . '/' . $path);
if ($source === false) {
throw new RuntimeException('Unable to read ' . $path);
}
return $source;
};
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$cookie = $read('app/Services/Auth/Cookie.php');
$signed = $read('app/Services/Auth/SignedCookieSession.php');
$cloudSession = $read('app/Services/Cloud/CloudSessionService.php');
$analytics = $read('app/Services/SiteAnalyticsRecorder.php');
$revocation = $read('app/Services/LoginSessionRevocationService.php');
$hash = $read('app/Utils/Hash.php');
$config = $read('config/.config.php.example');
$deployment = $read('../docs/DEPLOYMENT.md');
$assert(str_contains($cookie, 'SignedCookieSession::issue')
&& str_contains($cookie, 'SignedCookieSession::verify')
&& str_contains($cloudSession, 'SignedCookieSession::verify')
&& str_contains($analytics, 'SignedCookieSession::COOKIE_NAME'),
'A signed cookie consumer drifted back to the fragmented credential.');
$assert(preg_match('/Cookie::get\(\s*[\'\"](?:uid|email|key|ip|expire_in|login_session_version)[\'\"]/D', $cookie) !== 1,
'The cookie auth driver reads a retired credential field.');
$assert(!str_contains($cloudSession, "'expire_in'")
&& !str_contains($cloudSession, "'login_session_version'")
&& !str_contains($cloudSession, "['uid', 'key'"),
'Cloud sessions still trust fragmented cookie fields.');
$assert(!str_contains($revocation, 'cookieKey') && !str_contains($hash, 'cookieHash'),
'Retired password-derived cookie helpers remain callable.');
$assert(str_contains($signed, "hash_hkdf('sha256'")
&& str_contains($signed, 'strlen($siteKey) < 32')
&& str_contains($config, 'openssl rand -hex 32'),
'The cookie signing key requirement is not implemented and documented together.');
$assert(str_contains($deployment, 'UPDATE cloud_sessions')
&& str_contains($deployment, 'auth_session')
&& str_contains($deployment, '需要重新登录一次'),
'The signed-cookie clean-break deployment procedure is missing.');
echo "Signed cookie runtime and clean-break contracts passed.\n";