Files
zyt/identity-admin/.test-runtime/deploy-original-validation/tests/IdentityOperationLogRedactionTest.php
T
2026-11-11 14:22:02 +08:00

70 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
// 完全隔离的操作日志模型,只接收内存数据,不加载项目环境或数据库。
namespace app\common\model {
final class OperationLog
{
public static array $saved = [];
private array $values = [];
public function __set(string $key, mixed $value): void { $this->values[$key] = $value; }
public function save(): bool { self::$saved[] = $this->values; return true; }
}
}
namespace {
final class IdentityLogControllerFixture
{
public function isNotNeedLogin(): bool { return false; }
/** @notes 合成账号编辑 */
public function edit(): void {}
}
final class IdentityLogRequestFixture
{
public object $controllerObject;
public array $adminInfo = ['admin_id' => 11, 'name' => '合成管理员', 'account' => 'fixture-admin'];
public function __construct(public array $params) { $this->controllerObject = new IdentityLogControllerFixture(); }
public function controller(): string { return 'auth.admin'; }
public function action(): string { return 'edit'; }
public function param(): array { return $this->params; }
public function url(bool $domain): string { return 'https://fixture.invalid/adminapi/auth.admin/edit'; }
public function isGet(): bool { return false; }
public function ip(): string { return '192.0.2.10'; }
}
function request(): IdentityLogRequestFixture { return $GLOBALS['identityLogRequest']; }
require dirname(__DIR__) . '/bootstrap.php';
$secretKeys = ['password', 'password_confirm', 'old_password', 'new_password', 'password_hash',
'token', 'source_token', 'access_token', 'refresh_token', 'id_token', 'authorization',
'ticket', 'code_verifier', 'app_secret', 'client_secret'];
$secrets = [];
foreach ($secretKeys as $index => $key) { $secrets[$key] = 'synthetic-credential-' . $index; }
$params = ['id' => 21, 'name' => '合成用户', 'role_id' => [7], 'dept_id' => [52],
...$secrets, 'nested' => ['PASSWORD_CONFIRM' => 'synthetic-nested-confirm', 'rows' => [['Token' => 'synthetic-nested-token']]]];
$GLOBALS['identityLogRequest'] = new IdentityLogRequestFixture($params);
$response = new class {
public function getContent(): string {
return json_encode(['code' => 1, 'msg' => '操作成功', 'data' => [
'id' => 21, 'token' => 'synthetic-response-token', 'nested' => ['ticket' => 'synthetic-response-ticket'],
]], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
}
};
(new \app\adminapi\listener\OperationLog())->handle($response);
$record = \app\common\model\OperationLog::$saved[0] ?? throw new \RuntimeException('未记录合成操作');
$logged = json_encode($record, JSON_THROW_ON_ERROR);
foreach ([...array_values($secrets), 'synthetic-nested-confirm', 'synthetic-nested-token',
'synthetic-response-token', 'synthetic-response-ticket'] as $secret) {
if (str_contains($logged, $secret)) { throw new \RuntimeException('日志包含未脱敏的合成凭据'); }
}
$requestLog = json_decode($record['params'], true, 512, JSON_THROW_ON_ERROR);
$responseLog = json_decode($record['result'], true, 512, JSON_THROW_ON_ERROR);
if ($requestLog['id'] !== 21 || $requestLog['role_id'] !== [7] || $requestLog['dept_id'] !== [52]
|| $responseLog['code'] !== 1 || $responseLog['data']['id'] !== 21
|| $requestLog['nested']['PASSWORD_CONFIRM'] !== '******'
|| $responseLog['data']['nested']['ticket'] !== '******') {
throw new \RuntimeException('凭据脱敏破坏了普通审计信息或遗漏了嵌套字段');
}
if ($params['password'] !== $secrets['password']) { throw new \RuntimeException('日志修改了原请求数据'); }
echo "IdentityOperationLogRedactionTest passed: request/response/nested credentials masked; audit fields preserved\n";
}