Spaces:
Sleeping
Sleeping
File size: 5,404 Bytes
9f0c00b 5f82f13 1302ebc 5f82f13 1302ebc 76e92fa cac73cf 1302ebc cac73cf 1302ebc cac73cf 1302ebc 5f82f13 9f0c00b 5f82f13 9f0c00b 5f82f13 9f0c00b 5f82f13 0c167c4 9f0c00b 1302ebc 9f0c00b f608a7c 9f0c00b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | /**
* 鬼影服務:上傳形態驗證 + 以 battle-core 重播反作弊驗證(04 §5、synthesis B-4/B-5)。
*/
import { replay, RULE_VERSION, CARDS_HASH, type GhostRecord } from "@wcg/battle-core";
/** 有界整數檢查(擋 NaN/Infinity/非整數/超界)。 */
export function intInRange(v: unknown, lo: number, hi: number): boolean {
return typeof v === "number" && Number.isInteger(v) && v >= lo && v <= hi;
}
/** 驗證動作序列(鬼影上傳與統計回報共用);回錯誤訊息或 null。逐型驗欄位,擋 NaN/物件混入引擎。 */
export function validateActions(value: unknown): string | null {
if (!Array.isArray(value)) return "actions 必須是陣列";
if (value.length > 1500) return "actions 過長"; // 防重播 DoS(真實 11 關約數百個動作;1500 仍給長場充足餘裕,又砍掉惡意上傳最壞放大)
let lastElapsedSec = 0;
const validTypes = new Set(["PLAY_CARD", "END_TURN", "PICK_REWARD", "CHOOSE_CARDS", "REWARD_HEAL", "REWARD_DRAFT", "SKIP_REWARD", "SURRENDER"]);
for (const a of value) {
if (typeof a !== "object" || a === null) return "actions 元素必須是物件";
const aa = a as Record<string, unknown>;
if (typeof aa.type !== "string") return "actions 元素缺 type";
if (!validTypes.has(aa.type)) return "actions type 非法";
if (aa.type === "PLAY_CARD" && !intInRange(aa.handIndex, 0, 63)) return "PLAY_CARD handIndex 非法";
if ((aa.type === "PLAY_CARD" || aa.type === "END_TURN") && aa.elapsedSec !== undefined) {
if (!intInRange(aa.elapsedSec, 0, 24 * 60 * 60)) return `${aa.type} elapsedSec 非法`;
if ((aa.elapsedSec as number) < lastElapsedSec) return "elapsedSec 不可倒退";
lastElapsedSec = aa.elapsedSec as number;
}
if (aa.type === "PICK_REWARD" && !intInRange(aa.choiceIndex, 0, 9)) return "PICK_REWARD choiceIndex 非法";
if (aa.type === "CHOOSE_CARDS") {
if (!Array.isArray(aa.indices) || aa.indices.length > 16 || !aa.indices.every((x) => intInRange(x, 0, 63))) return "CHOOSE_CARDS indices 非法";
}
}
return null;
}
/** 檢查上傳 body 的形態;回傳錯誤訊息或 null(通過)。全面用 Number.isInteger 擋 NaN/Infinity,並逐一驗證 actions 元素。 */
export function validateGhostShape(body: unknown): string | null {
if (typeof body !== "object" || body === null) return "body 必須是物件";
const r = body as Record<string, unknown>;
if (!intInRange(r.playerMaxHp, 1, 100000)) return "playerMaxHp 超出範圍";
if (!Array.isArray(r.turns)) return "turns 必須是陣列";
if (r.turns.length > 2000) return "turns 過長"; // 防 DoS(11 關正常數十回合)
for (const t of r.turns) {
if (typeof t !== "object" || t === null) return "turns 元素必須是物件";
const tt = t as Record<string, unknown>;
if (!intInRange(tt.atk, -100000, 100000) || !intInRange(tt.def, -100000, 100000)) return "turns 元素需含整數 atk/def";
}
if (!intInRange(r.seed, -0x80000000, 0xffffffff)) return "seed 必須是整數";
if (typeof r.ruleVersion !== "string" || r.ruleVersion.length > 32) return "缺/非法 ruleVersion";
if (typeof r.cardsHash !== "string" || r.cardsHash.length > 64) return "缺/非法 cardsHash";
if (r.meta !== undefined) {
if (typeof r.meta !== "object" || r.meta === null) return "meta 必須是物件";
const hn = (r.meta as Record<string, unknown>).heroName;
// heroName 若有則必為字串、長度有界(防 DoS;實際內容由 cleanHeroName 截長/過濾)。
if (hn !== undefined && (typeof hn !== "string" || hn.length > 1000)) return "meta.heroName 非法";
}
if (r.actions !== undefined) {
const e = validateActions(r.actions);
if (e) return e;
}
return null;
}
export interface VerifyResult {
ok: boolean;
reason: string;
}
/**
* 以 seed + actions 重播,驗證 turns / playerMaxHp / 確實破關。
* 版本或卡表指紋不符 → 降級為「不校驗」(B-4,避免舊紀錄被誤判作弊)。
*/
export function verifyGhost(record: GhostRecord): VerifyResult {
if (record.ruleVersion !== RULE_VERSION || record.cardsHash !== CARDS_HASH) {
return { ok: false, reason: "version-mismatch(降級不校驗)" };
}
if (!record.actions || record.actions.length === 0) {
return { ok: false, reason: "no-actions(無法重播)" };
}
let final;
try {
// 帶職業/路線/困難模式才能忠實重播魔法師/野蠻人/Sanctus(maxLevel 省略→createGame 依 campaign 取正式關數)。
final = replay(record.seed, record.actions, {
classType: record.meta?.classType ?? "WARRIOR",
campaign: record.campaign ?? "slime",
hardMode: record.hardMode,
});
} catch (e) {
return { ok: false, reason: `replay-error: ${(e as Error).message}` };
}
if (final.phase !== "VICTORY") return { ok: false, reason: "未達破關(phase 非 VICTORY)" };
if (final.player.maxHp !== record.playerMaxHp) return { ok: false, reason: "playerMaxHp 不符" };
if (final.turnLog.length !== record.turns.length) return { ok: false, reason: "turns 長度不符" };
for (let i = 0; i < final.turnLog.length; i++) {
const a = final.turnLog[i]!;
const b = record.turns[i]!;
if (a.atk !== b.atk || a.def !== b.def) return { ok: false, reason: `第 ${i + 1} 回合 atk/def 不符` };
}
return { ok: true, reason: "verified" };
}
|