import os from html import escape as html_escape from analyzer import HTMLJSAnalyzer, classify_functions, SIDE_EFFECT_ICONS from mermaid_gen import generate_mermaid_flowchart from sbom import SEVERITY_ICONS, get_db_metadata def _build_caller_index(func_dict): index = {} for f in func_dict.values(): for call in f.calls: if call not in index: index[call] = [] index[call].append(f.name) return index def _generate_func_cards_html(func_dict, icons, caller_index): html = '
' for func in func_dict.values(): is_risk = len(func.side_effects) > 0 card_class = "func-card has-risk" if is_risk else "func-card" icon_str = "".join([icons.get(se, "") for se in sorted(func.side_effects)]) safe_name = html_escape(func.name) calls = ", ".join(html_escape(c) for c in sorted(func.calls)) if func.calls else "無" builtins = ", ".join(html_escape(b) for b in sorted(func.builtins)) if func.builtins else "無" callers = caller_index.get(func.name, []) caller_str = ", ".join(html_escape(c) for c in callers) if callers else "無" html += f'''
{icon_str} {safe_name}() { '含風險' if is_risk else ''}
位置:第 {func.start_line} - {func.end_line} 行
呼叫函式: {calls}
內建方法: {builtins}
被誰呼叫: {caller_str}
副作用: { "".join(f'{html_escape(se)}' for se in sorted(func.side_effects)) if func.side_effects else '無' }
''' html += '
' return html def _generate_sbom_html(dependencies): if not dependencies: return "

未偵測到外部相依套件(檔案僅含內嵌程式碼)。

" html = '
' for dep in dependencies: is_risk = len(dep.vulnerabilities) > 0 card_class = "func-card has-risk" if is_risk else "func-card" name = html_escape(dep.name) version = html_escape(dep.version) if dep.version else "未知" source = html_escape(dep.source) risk_badge = '含已知漏洞' if is_risk else '' if dep.vulnerabilities: vuln_html = "" for v in dep.vulnerabilities: icon = SEVERITY_ICONS.get(v["severity"], "") vuln_html += ( f'
{icon} {html_escape(v["id"])} ' f'({html_escape(v["severity"])}) — {html_escape(v["desc"])}' f',修復版本:{html_escape(v["fixed"])}
' ) else: vuln_html = '無比對到' html += f'''
{name} {version} {risk_badge}
類型:{html_escape(dep.kind)}
來源: {source}
已知漏洞: {vuln_html}
''' html += '
' return html def generate_html_report(html_path, output_dir=None, report=None): if report is None: analyzer = HTMLJSAnalyzer(html_path) report = analyzer.analyze() main_funcs, ui_funcs = classify_functions(report.functions) base_name = os.path.splitext(os.path.basename(html_path))[0] if not output_dir: output_dir = os.path.dirname(os.path.abspath(html_path)) output_path = os.path.join(output_dir, f"{base_name}_report.html") mermaid_str, has_edges = generate_mermaid_flowchart(main_funcs) icons = SIDE_EFFECT_ICONS main_caller_index = _build_caller_index(main_funcs) ui_caller_index = _build_caller_index(ui_funcs) safe_html_file = html_escape(report.html_file) main_cards_html = _generate_func_cards_html(main_funcs, icons, main_caller_index) if main_funcs else "

未偵測到主函式。所有函式皆歸類為常規腳本 (見「常規腳本」頁籤)。

" ui_cards_html = _generate_func_cards_html(ui_funcs, icons, ui_caller_index) if ui_funcs else "

未偵測到純常規 UI 腳本。

" no_edges_hint = '

所有函式皆獨立運作,無互相呼叫或共享變數關係。

' if main_funcs and not has_edges else '' if main_funcs: flowchart_html = f'
{no_edges_hint}' else: flowchart_html = '

無主函式呼叫關係可顯示。

' has_side_effects = any(f.side_effects for f in main_funcs.values()) side_effect_count = sum(1 for f in main_funcs.values() if f.side_effects) sbom_html = _generate_sbom_html(report.dependencies) vuln_dep_count = sum(1 for d in report.dependencies if d.vulnerabilities) sbom_meta = get_db_metadata() sbom_meta_text = f"漏洞資料來源:{sbom_meta['source']}" if sbom_meta["updated_at"]: sbom_meta_text += f"|更新時間:{sbom_meta['updated_at']}" safe_sbom_meta = html_escape(sbom_meta_text) html_content = f""" 分析報告: {safe_html_file}

🔍 分析報告:{safe_html_file}

主函式總數
{len(main_funcs)}
含副作用函式
{side_effect_count}
整檔警告
{len(report.warnings)}

主函式詳細清單

{main_cards_html}

常規腳本 (UI/狀態操作)

此區塊列出僅包含內建方法 (如 forEach, addEventListener),且無危險副作用的輔助函式。

{ui_cards_html}

相依套件 SBOM

外部相依套件清單與已知漏洞比對,涵蓋常見前端函式庫的 CVE,非完整掃描。

{safe_sbom_meta}

外部相依套件
{len(report.dependencies)}
含已知漏洞套件
{vuln_dep_count}
{sbom_html}
{flowchart_html}
""" with open(output_path, 'w', encoding='utf-8') as f: f.write(html_content) return output_path