function escape(text) {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&")
return "&";
if (char === "<")
return "<";
if (char === ">")
return ">";
return """;
});
}
function time(at) {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function shorten(value, size = 22) {
if (value.length <= size)
return value;
return `${value.slice(0, size / 2)}…${value.slice(-size / 2)}`;
}
function checkRow(check) {
return `
${check.pass ? "PASS" : "FAIL"}
${escape(check.label)}
${escape(check.detail)}
`;
}
function navRows(items, mode) {
return items
.map((item) => `
${escape(item.label)}
${item.allowed ? "允许" : "拒绝"}
${escape(shorten(item.url, 30))}
${escape(mode === "attempt" ? item.observed : item.reason)}
`)
.join("");
}
export function mountApp(root) {
root.innerHTML = `
CSP 工作台
—
—
violation 事件 0 条
导航与窗口
—
策略判定
实际尝试
开发态 vs 生产态
同一份策略,不同来源
| 项 | 开发 | 生产 |
| 页面来源 | http://localhost | file:// |
| 样式 | 注入 <style> | 独立 CSS |
| style-src | 需 unsafe-inline | 可收紧为 self |
| connect-src | ws: localhost | 仅业务域名 |
| ASAR | 无 | 可校验完整性 |
`;
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 = null;
function renderChecks() {
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() {
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) => `
${escape(row.directive)}
${escape(shorten(row.blockedURI, 34))}
`)
.join("");
}
}
function renderNavigation() {
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() {
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 !== 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() {
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) {
state = next;
renderChecks();
renderCsp();
renderNavigation();
renderIntegrity();
renderEvents();
}
const sleep = (value) => new Promise((resolve) => setTimeout(resolve, value));
window.__runCspProbes = async () => {
const violations = [];
const onViolation = (event) => {
violations.push({
directive: event.effectiveDirective || event.violatedDirective,
blockedURI: event.blockedURI,
disposition: event.disposition,
});
};
document.addEventListener("securitypolicyviolation", onViolation);
const results = [];
const flag = window;
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["__inlineFlag"] === true;
const inlineBlocked = !inlineExecuted;
results.push({
id: "inline",
label: "内联