import type { Check, CspViolation, NavAttempt, Snapshot } 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 shorten(value: string, size = 22): string { if (value.length <= size) return value; return `${value.slice(0, size / 2)}…${value.slice(-size / 2)}`; } function checkRow(check: Check): string { return `
  • ${check.pass ? "PASS" : "FAIL"} ${escape(check.label)} ${escape(check.detail)}
  • `; } function navRows(items: NavAttempt[], mode: "policy" | "attempt"): string { return items .map( (item) => ` `, ) .join(""); } export function mountApp(root: HTMLElement): void { root.innerHTML = `

    CSP 与导航完整性实验室

    CSP · 导航白名单 · 新窗口拒绝 · 资源哈希 · 供应链
    sandbox: true —

    CSP 工作台

    —
    —
      violation 事件 0 条

        资源完整性

        —

          防线检查

          —

            开发态 vs 生产态

            同一份策略,不同来源
            项开发生产
            页面来源http://localhostfile://
            样式注入 <style>独立 CSS
            style-src需 unsafe-inline可收紧为 self
            connect-srcws: localhost仅业务域名
            ASAR无可校验完整性

            打包态信息

            如实标注

              活动

              CSP / 导航 / 哈希
                `; const score = root.querySelector('[data-testid="score"]'); const checkCount = root.querySelector( '[data-testid="check-count"]', ); const checksEl = root.querySelector('[data-testid="checks"]'); const cspState = root.querySelector('[data-testid="csp-state"]'); const cspPolicy = root.querySelector( '[data-testid="csp-policy"]', ); const cspResults = root.querySelector( '[data-testid="csp-results"]', ); const violationCount = root.querySelector( '[data-testid="violation-count"]', ); const violationsEl = root.querySelector( '[data-testid="violations"]', ); const currentUrl = root.querySelector( '[data-testid="current-url"]', ); const navPolicy = root.querySelector( '[data-testid="nav-policy"]', ); const navAttempts = root.querySelector( '[data-testid="nav-attempts"]', ); const manifestVersion = root.querySelector( '[data-testid="manifest-version"]', ); const integrityEl = root.querySelector( '[data-testid="integrity"]', ); const packagingEl = root.querySelector( '[data-testid="packaging"]', ); const eventsEl = root.querySelector('[data-testid="events"]'); let state: Snapshot | null = null; function renderChecks(): void { if (!state) return; if (checksEl) 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 ${state.score === 100 ? "ok-pill" : "bad-pill"}`; } } function renderCsp(): void { if (!state) return; const csp = state.csp; if (cspState) { cspState.textContent = csp.policy ? `${csp.violations.length} 条违规` : "尚未探测"; } if (cspPolicy) cspPolicy.textContent = csp.policy || "—"; if (cspResults) { cspResults.innerHTML = csp.results.length === 0 ? `
              • 尚未运行探针
              • ` : csp.results .map( (row) => `
              • ${row.blocked ? "已拦截" : "未拦截"} ${escape(row.label)} ${escape(shorten(row.evidence, 46))}
              • `, ) .join(""); } if (violationCount) { violationCount.textContent = String(csp.violations.length); } if (violationsEl) { violationsEl.innerHTML = csp.violations.length === 0 ? `
              • 无
              • ` : csp.violations .slice(0, 6) .map( (row: CspViolation) => `
              • ${escape(row.directive)} ${escape(shorten(row.blockedURI, 34))}
              • `, ) .join(""); } } function renderNavigation(): void { if (!state) return; if (currentUrl) currentUrl.textContent = shorten(location.href, 38); if (navPolicy) navPolicy.innerHTML = navRows(state.navigation.policy, "policy"); if (navAttempts) { navAttempts.innerHTML = state.navigation.attempts.length === 0 ? `
              • 尚无实际尝试
              • ` : navRows(state.navigation.attempts, "attempt"); } } function renderIntegrity(): void { if (!state) return; const integrity = state.integrity; if (manifestVersion) { manifestVersion.textContent = `manifest v${integrity.manifestVersion}`; } if (integrityEl) { const rows = [integrity.good, integrity.tampered].filter( (row): row is NonNullable => row !== null, ); integrityEl.innerHTML = rows .map( (row) => `
              • ${row.ok ? "一致" : "不匹配"} ${escape(row.fileName)} ${escape(row.actual.slice(0, 16))}…
              • `, ) .join(""); } if (packagingEl) { const pack = state.packaging; packagingEl.innerHTML = `
              • isPackaged${pack.isPackaged}
              • ASAR${pack.asar ? "yes" : "no"}
              • fuses${pack.fusesVerified ? "verified" : "dev-only"}
              • ${escape(pack.note)}
              • `; } } function renderEvents(): void { if (!eventsEl || !state) return; eventsEl.innerHTML = state.events.length === 0 ? `
              • 暂无活动
              • ` : state.events .slice(0, 8) .map( (row) => `
              • ${time(row.at)} ${escape(row.kind)} ${escape(row.detail)}
              • `, ) .join(""); } function render(next: Snapshot): void { state = next; renderChecks(); renderCsp(); renderNavigation(); renderIntegrity(); renderEvents(); } const sleep = (value: number): Promise => new Promise((resolve) => setTimeout(resolve, value)); window.__runCspProbes = async () => { const violations: CspViolation[] = []; const onViolation = (event: SecurityPolicyViolationEvent): void => { violations.push({ directive: event.effectiveDirective || event.violatedDirective, blockedURI: event.blockedURI, disposition: event.disposition, }); }; document.addEventListener("securitypolicyviolation", onViolation); const results: Snapshot["csp"]["results"] = []; const flag = window as unknown as { __inlineFlag?: boolean }; flag.__inlineFlag = false; const script = document.createElement("script"); script.textContent = "window.__inlineFlag = true;"; document.head.appendChild(script); script.remove(); await sleep(150); const inlineExecuted = (window as unknown as Record)["__inlineFlag"] === true; const inlineBlocked = !inlineExecuted; results.push({ id: "inline", label: "内联