diff --git a/oracletrace/reporters/html.py b/oracletrace/reporters/html.py index 1daa60a..7d3acca 100644 --- a/oracletrace/reporters/html.py +++ b/oracletrace/reporters/html.py @@ -1,5 +1,6 @@ import json from datetime import datetime +from html import escape from string import Template from typing import List from ..tracer import TracerData, FunctionData @@ -15,7 +16,7 @@ def generate_html_report(data: TracerData, output_path: str) -> None: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") html = _JSTemplate(_HTML_TEMPLATE).substitute( - root_path=metadata.root_path, + root_path=escape(metadata.root_path), total_time=f"{metadata.total_execution_time:.4f}s", total_functions=str(metadata.total_functions), timestamp=timestamp, @@ -36,7 +37,9 @@ def _serialize_functions(functions: List[FunctionData]) -> str: "avg_time": fn.avg_time * 1000, "callees": fn.callees, }) - return json.dumps(serialized) + # < keeps a literal "" in a function name from terminating + # the inline ', + total_time=1.0, + call_count=1, + avg_time=1.0, + callees=['evil.py:'], + ) + ], + ) + output = tmp_path / "report.html" + generate_html_report(data, str(output)) + html = output.read_text(encoding="utf-8") + + # a literal inside the embedded JSON would terminate the + # report's own script block at parse time + assert "" not in html + assert "" not in html + assert "<img" in html