示例工程 48-project-security-auditor

48-project-security-auditor/app/.tsc/shared/rules.js

javascript · 4.3 KB

const IGNORED_PREFIX = ["//", "*", "/*", "<!--", "#"];
export const RULES = [
    {
        id: "R01",
        severity: "high",
        label: "nodeIntegration 开启",
        hint: "改为 false;渲染进程需要的能力走 preload + IPC。",
        test: (line) => /nodeIntegration\s*:\s*true/.test(line),
    },
    {
        id: "R02",
        severity: "high",
        label: "contextIsolation 关闭",
        hint: "改为 true;页面与 preload 必须隔离。",
        test: (line) => /contextIsolation\s*:\s*false/.test(line),
    },
    {
        id: "R03",
        severity: "high",
        label: "sandbox 关闭",
        hint: "改为 true;不要让渲染进程直接持有 Node。",
        test: (line) => /sandbox\s*:\s*false/.test(line),
    },
    {
        id: "R04",
        severity: "high",
        label: "webSecurity 关闭",
        hint: "改为 true;关闭同源策略等于放弃页面边界。",
        test: (line) => /webSecurity\s*:\s*false/.test(line),
    },
    {
        id: "R05",
        severity: "medium",
        label: "webviewTag 开启",
        hint: "改为 false;需要 webview 时单独收紧权限与导航。",
        test: (line) => /webviewTag\s*:\s*true/.test(line),
    },
    {
        id: "R06",
        severity: "high",
        label: "preload 暴露 ipcRenderer/require/process",
        hint: "只暴露具体方法,不暴露通信本体。",
        test: (line) => /exposeInMainWorld\(\s*["'](ipcRenderer|require|process)["']/.test(line),
    },
    {
        id: "R07",
        severity: "high",
        label: "CSP 允许 unsafe-eval",
        hint: "移除 unsafe-eval,改用预编译模板。",
        test: (line) => /unsafe-eval/.test(line),
    },
    {
        id: "R08",
        severity: "high",
        label: "script-src 允许 unsafe-inline",
        hint: "移除内联脚本;需要时用 nonce/hash。",
        test: (line) => /script-src[^;]*unsafe-inline/.test(line),
    },
    {
        id: "R09",
        severity: "medium",
        label: "CSP 来源通配",
        hint: "把 * 换成明确的域名与协议。",
        test: (line) => /(?:default-src|connect-src|script-src|img-src)\s+\*/.test(line),
    },
    {
        id: "R10",
        severity: "high",
        label: "openExternal 未校验 URL",
        hint: "先解析 URL,只允许 https 与白名单主机。",
        test: (line) => /openExternal\(\s*(url|req|request|input|raw|link|userUrl)\b/.test(line),
    },
    {
        id: "R11",
        severity: "high",
        label: "path.join 拼接用户输入",
        hint: "先 resolve,再用 relative 判断是否越出工作区。",
        test: (line) => /path\.join\([^)]*\b(req|request|input|userPath|rawPath)\b/.test(line),
    },
];
export function scanText(file, text) {
    const findings = [];
    const seen = new Set();
    const lines = text.split(/\r?\n/);
    lines.forEach((raw, index) => {
        const line = raw.trim();
        if (!line)
            return;
        if (IGNORED_PREFIX.some((prefix) => line.startsWith(prefix)))
            return;
        for (const rule of RULES) {
            if (!rule.test(line))
                continue;
            const key = `${rule.id}:${index + 1}`;
            if (seen.has(key))
                continue;
            seen.add(key);
            findings.push({
                ruleId: rule.id,
                severity: rule.severity,
                file,
                line: index + 1,
                evidence: line.slice(0, 140),
                message: rule.label,
                hint: rule.hint,
            });
        }
    });
    return findings;
}
export function scoreFindings(findings) {
    const penalty = findings.reduce((sum, finding) => sum + (finding.severity === "high" ? 8 : 4), 0);
    return Math.max(0, 100 - penalty);
}
export function summarize(findings) {
    return RULES.map((rule) => ({
        id: rule.id,
        severity: rule.severity,
        label: rule.label,
        hits: findings.filter((finding) => finding.ruleId === rule.id).length,
    }));
}
export function escapeHtml(text) {
    return text.replace(/[&<>"]/g, (char) => {
        if (char === "&")
            return "&amp;";
        if (char === "<")
            return "&lt;";
        if (char === ">")
            return "&gt;";
        return "&quot;";
    });
}
//# sourceMappingURL=rules.js.map