import type { Check, Snapshot, Verdict } from "../shared/ipc";
function escape(text: string): string {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&") return "&";
if (char === "<") return "<";
if (char === ">") return ">";
return """;
});
}
function time(at: number): string {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function verdictRows(items: Verdict[]): string {
return items
.map(
(item) => `
${escape(item.label)}
${item.allowed ? "允许" : "拒绝"}
${escape(item.reason)}
`,
)
.join("");
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
信任边界
数据流与防线
运行时探针
—
尚未尝试
`;
const score = root.querySelector('[data-testid="score"]');
const versions = root.querySelector('[data-testid="versions"]');
const blockedCount = root.querySelector(
'[data-testid="blocked-count"]',
);
const scenariosEl = root.querySelector(
'[data-testid="scenarios"]',
);
const checkCount = root.querySelector(
'[data-testid="check-count"]',
);
const checksEl = root.querySelector('[data-testid="checks"]');
const navAttacks = root.querySelector(
'[data-testid="nav-attacks"]',
);
const pathAttacks = root.querySelector(
'[data-testid="path-attacks"]',
);
const updateAttacks = root.querySelector(
'[data-testid="update-attacks"]',
);
const pluginAttacks = root.querySelector(
'[data-testid="plugin-attacks"]',
);
const eventsEl = root.querySelector('[data-testid="events"]');
const cspState = root.querySelector('[data-testid="csp-state"]');
const cspEl = root.querySelector('[data-testid="csp"]');
const requireOutput = root.querySelector(
'[data-testid="require-output"]',
);
const status = root.querySelector('[data-testid="score"]');
let state: Snapshot | null = null;
function checkRow(check: Check): string {
return `
${check.pass ? "PASS" : "FAIL"}
${escape(check.label)}
${escape(check.detail)}
`;
}
function renderChecks(): void {
if (!state || !checksEl) return;
checksEl.innerHTML = state.checks.map(checkRow).join("");
if (checkCount) {
const passed = state.checks.filter((check) => check.pass).length;
checkCount.textContent = `${passed}/${state.checks.length} 通过`;
}
if (score) {
score.textContent = `${state.score} / 100`;
score.className = `pill score-pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`;
}
}
function renderScenarios(): void {
if (!state || !scenariosEl) return;
const blocked = state.scenarios.filter(
(scenario) => scenario.status === "blocked",
).length;
if (blockedCount) blockedCount.textContent = `${blocked} 类已挡住`;
scenariosEl.innerHTML = state.scenarios
.map(
(scenario) => `
${escape(scenario.title)}
${
scenario.status === "blocked" ? "默认挡住" : "残余风险"
}
${escape(scenario.controls.join(" · "))}
`,
)
.join("");
}
function renderAttacks(): void {
if (!state) return;
if (navAttacks)
navAttacks.innerHTML = verdictRows(state.attacks.navigation);
if (pathAttacks) pathAttacks.innerHTML = verdictRows(state.attacks.path);
if (updateAttacks)
updateAttacks.innerHTML = verdictRows(state.attacks.update);
if (pluginAttacks)
pluginAttacks.innerHTML = verdictRows(state.attacks.plugin);
}
function renderRuntime(): void {
if (!state) return;
if (cspState) {
cspState.textContent = state.runtime.csp ? "CSP 已启用" : "CSP 缺失";
cspState.className = `muted small ${state.runtime.csp ? "ok" : "bad"}`;
}
if (cspEl) cspEl.textContent = state.runtime.csp;
}
function renderEvents(): void {
if (!eventsEl || !state) return;
eventsEl.innerHTML =
state.events.length === 0
? `暂无活动`
: state.events
.slice(0, 7)
.map(
(row) => `
${time(row.at)}
${escape(row.kind)}
${escape(row.detail)}
`,
)
.join("");
}
function render(next: Snapshot): void {
state = next;
if (versions) {
versions.textContent = `Electron ${next.electronVersion} · Node ${next.nodeVersion}`;
}
renderChecks();
renderScenarios();
renderAttacks();
renderRuntime();
renderEvents();
}
const probeRequire = (): string => {
try {
(window as unknown as { require: (name: string) => unknown }).require(
"node:fs",
);
return "UNEXPECTED: require is available";
} catch (error) {
return error instanceof Error ? error.message : String(error);
}
};
const showRequire = (): void => {
const message = probeRequire();
if (requireOutput) {
requireOutput.textContent = message;
requireOutput.className = `probe-output ${message.includes("not a function") ? "ok" : "bad"}`;
}
};
void window.api.threat.snapshot().then(render);
const unsubscribe = window.api.threat.onEvent(() => {
void window.api.threat.snapshot().then(render);
});
root
.querySelector('[data-testid="probe"]')
?.addEventListener("click", async () => {
render(await window.api.threat.probe());
if (status) status.textContent = "已重新检查";
});
root
.querySelector('[data-testid="require-probe"]')
?.addEventListener("click", showRequire);
const sleep = (value: number): Promise =>
new Promise((resolve) => setTimeout(resolve, value));
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
render(await window.api.threat.probe());
if (stage === "attacks") {
document
.querySelector(".attacks-card")
?.scrollIntoView({ block: "start" });
} else if (stage === "checklist") {
showRequire();
document
.querySelector(".checklist-card")
?.scrollIntoView({ block: "start" });
}
await sleep(100);
};
window.__smoke = async () => {
const api = window.api.threat;
const first = await api.probe();
const refreshed = await api.refresh();
const requireError = probeRequire();
unsubscribe();
const passed = first.checks.filter((check) => check.pass).length;
const blocked = first.scenarios.filter(
(scenario) => scenario.status === "blocked",
).length;
return {
score: first.score,
checks: first.checks.length,
passed,
scenarios: first.scenarios.length,
blocked,
residual: first.scenarios.length - blocked,
navAllowed: first.attacks.navigation.filter((item) => item.allowed)
.length,
navBlocked: first.attacks.navigation.filter((item) => !item.allowed)
.length,
pathAllowed: first.attacks.path.filter((item) => item.allowed).length,
pathBlocked: first.attacks.path.filter((item) => !item.allowed).length,
updateAllowed: first.attacks.update.filter((item) => item.allowed).length,
updateBlocked: first.attacks.update.filter((item) => !item.allowed)
.length,
pluginAllowed: first.attacks.plugin.filter((item) => item.allowed).length,
pluginBlocked: first.attacks.plugin.filter((item) => !item.allowed)
.length,
requireType: first.runtime.requireType,
processType: first.runtime.processType,
requireError,
csp: first.runtime.csp,
events: refreshed.events.length,
};
};
}