#!/usr/bin/env tsx /** * 虎牙账号 UID / 平台加密 roleId / 客户游戏角色 UID 只读审计。 * * 用法: * npm run audit:huya-binding-identity * npm run audit:huya-binding-identity -- --strict */ import { closeDb, getDb } from '../src/db/client.js' const strict = process.argv.includes('--strict') type AuditRow = { task_id: number order_no: string account_uid: string binding_status: string current_role_name: string current_role_token: string platform_role_uid: string pending_role_name: string pending_role_token: string pending_platform_role_uid: string customer_role_uid: string legacy_bind_game_role_id: string role_identity_source: string verification_basis: string latest_outer_role_name: string latest_outer_role_token: string latest_inner_role_name: string latest_inner_role_token: string } async function main() { const client = await getDb().connect() try { await client.query('BEGIN TRANSACTION READ ONLY') const result = await client.query(` SELECT task.id AS task_id, order_record.platform_order_id AS order_no, task.huya_uid AS account_uid, task.binding_status, task.bind_game_role AS current_role_name, task.bind_game_role_token AS current_role_token, task.platform_game_role_uid, task.pending_bind_game_role AS pending_role_name, task.pending_bind_game_role_token AS pending_role_token, task.pending_platform_game_role_uid AS pending_platform_role_uid, task.customer_game_role_id AS customer_role_uid, task.legacy_bind_game_role_id, task.role_identity_source, task.verification_basis, COALESCE(outer_trace.role_name, '') AS latest_outer_role_name, COALESCE(outer_trace.role_token, '') AS latest_outer_role_token, COALESCE(inner_trace.role_name, '') AS latest_inner_role_name, COALESCE(inner_trace.role_token, '') AS latest_inner_role_token FROM semi_auto_tasks task JOIN orders order_record ON order_record.id = task.order_id LEFT JOIN LATERAL ( SELECT trace.response_body::jsonb #>> '{accountData,gameRole,roleName}' AS role_name, trace.response_body::jsonb #>> '{accountData,gameRole,roleId}' AS role_token FROM semi_auto_trace_logs trace WHERE trace.task_id = task.id AND trace.action = 'webActUI.checkUserBindGameAccount' AND trace.status = 'ok' AND COALESCE(trace.request_summary->>'outerActId', '') <> '' ORDER BY trace.id DESC LIMIT 1 ) outer_trace ON TRUE LEFT JOIN LATERAL ( SELECT trace.response_body::jsonb #>> '{accountData,gameRole,roleName}' AS role_name, trace.response_body::jsonb #>> '{accountData,gameRole,roleId}' AS role_token FROM semi_auto_trace_logs trace WHERE trace.task_id = task.id AND trace.action = 'webActUI.checkUserBindGameAccount' AND trace.status = 'ok' AND COALESCE(trace.request_summary->>'outerActId', '') = '' ORDER BY trace.id DESC LIMIT 1 ) inner_trace ON TRUE WHERE task.platform = 'huya' ORDER BY task.updated_at DESC, task.id DESC `) await client.query('COMMIT') const rows = result.rows const historicalLegacyFallback = rows.filter( (row) => row.legacy_bind_game_role_id && row.legacy_bind_game_role_id === row.account_uid, ) const activeAccountUidReuse = rows.filter( (row) => Boolean(row.account_uid) && (row.current_role_token === row.account_uid || row.pending_role_token === row.account_uid), ) const historicalTraceAccountUidReuse = rows.filter( (row) => Boolean(row.account_uid) && (row.latest_outer_role_token === row.account_uid || row.latest_inner_role_token === row.account_uid), ) const invalidActiveRoleTokens = rows.filter( (row) => (row.current_role_token && !/^[0-9a-f]{40}$/i.test(row.current_role_token)) || (row.pending_role_token && !/^[0-9a-f]{40}$/i.test(row.pending_role_token)), ) const invalidHistoricalTraceRoleTokens = rows.filter( (row) => (row.latest_outer_role_token && !/^[0-9a-f]{40}$/i.test(row.latest_outer_role_token)) || (row.latest_inner_role_token && !/^[0-9a-f]{40}$/i.test(row.latest_inner_role_token)), ) const invalidPlatformRoleUids = rows.filter( (row) => (row.platform_role_uid && !/^\d+$/.test(row.platform_role_uid)) || (row.pending_platform_role_uid && !/^\d+$/.test(row.pending_platform_role_uid)), ) const platformRoleUidReusedFromAccountUid = rows.filter( (row) => Boolean(row.account_uid) && ((Boolean(row.platform_role_uid) && row.platform_role_uid === row.account_uid) || (Boolean(row.pending_platform_role_uid) && row.pending_platform_role_uid === row.account_uid)), ) console.log( JSON.stringify( { generatedAt: new Date().toISOString(), summary: { huyaTasks: rows.length, historicalLegacyAccountUidFallback: historicalLegacyFallback.length, accountUidReusedAsActiveRoleToken: activeAccountUidReuse.length, accountUidReusedInHistoricalTrace: historicalTraceAccountUidReuse.length, invalidActiveRoleTokens: invalidActiveRoleTokens.length, invalidHistoricalTraceRoleTokens: invalidHistoricalTraceRoleTokens.length, invalidPlatformRoleUids: invalidPlatformRoleUids.length, platformRoleUidReusedFromAccountUid: platformRoleUidReusedFromAccountUid.length, }, historicalLegacyFallbackTasks: historicalLegacyFallback.map((row) => ({ taskId: row.task_id, orderNo: row.order_no, accountUid: row.account_uid, bindingStatus: row.binding_status, legacyBindGameRoleId: row.legacy_bind_game_role_id, roleIdentitySource: row.role_identity_source, })), activeAccountUidReuseTasks: activeAccountUidReuse.map((row) => ({ taskId: row.task_id, orderNo: row.order_no, accountUid: row.account_uid, currentRoleToken: row.current_role_token, pendingRoleToken: row.pending_role_token, })), invalidActiveRoleTokenTasks: invalidActiveRoleTokens.map((row) => ({ taskId: row.task_id, orderNo: row.order_no, currentRoleToken: row.current_role_token, pendingRoleToken: row.pending_role_token, })), invalidPlatformRoleUidTasks: invalidPlatformRoleUids.map((row) => ({ taskId: row.task_id, orderNo: row.order_no, platformRoleUid: row.platform_role_uid, pendingPlatformRoleUid: row.pending_platform_role_uid, })), platformRoleUidReusedFromAccountUidTasks: platformRoleUidReusedFromAccountUid.map( (row) => ({ taskId: row.task_id, orderNo: row.order_no, accountUid: row.account_uid, platformRoleUid: row.platform_role_uid, }), ), }, null, 2, ), ) if ( strict && (activeAccountUidReuse.length > 0 || invalidActiveRoleTokens.length > 0 || invalidPlatformRoleUids.length > 0 || platformRoleUidReusedFromAccountUid.length > 0) ) { process.exitCode = 1 } } catch (error) { await client.query('ROLLBACK').catch(() => undefined) throw error } finally { client.release() await closeDb() } } main().catch((error) => { console.error('[audit:huya-binding-identity]', error) process.exitCode = 1 })