diff --git a/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js b/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js index d66da0402e52ca..58e33703771fee 100644 --- a/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js +++ b/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js @@ -335,7 +335,7 @@ function createPythonTooltip(data) { const childCount = d.children ? d.children.length : 0; const source = d.data.source; - const funcname = resolveString(d.data.funcname) || resolveString(d.data.name); + const funcname = escapeHtml(resolveString(d.data.funcname) || resolveString(d.data.name) || ""); const filename = resolveString(d.data.filename) || ""; const moduleName = resolveString(d.data.module) || ""; const displayName = escapeHtml(useModuleNames ? (moduleName || filename) : filename); diff --git a/Lib/profiling/sampling/stack_collector.py b/Lib/profiling/sampling/stack_collector.py index 60061b4a2ecf11..b3fc4460792488 100644 --- a/Lib/profiling/sampling/stack_collector.py +++ b/Lib/profiling/sampling/stack_collector.py @@ -504,6 +504,11 @@ def _create_flamegraph_html(self, data): # The C encoder can exhaust the C stack independently of the # Python recursion limit. iterencode() uses the Python encoder. data_json = "".join(json.JSONEncoder().iterencode(data)) + data_json = ( + data_json.replace("<", "\\u003c") + .replace(">", "\\u003e") + .replace("&", "\\u0026") + ) template_dir = importlib.resources.files(__package__) vendor_dir = template_dir / "_vendor" diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py b/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py index dcac6f3e7dfd9e..46953eeba43021 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py @@ -596,6 +596,47 @@ def test_flamegraph_collector_export(self): self.assertIn('"sample_interval_usec": 10000', content) self.assertIn("samples * data.stats.sample_interval_usec / 1000", content) + def test_flamegraph_escapes_special_chars_in_funcname(self): + # Function names with <, >, & must be escaped in the HTML. + flamegraph_out = tempfile.NamedTemporaryFile( + suffix=".html", delete=False + ) + self.addCleanup(close_and_unlink, flamegraph_out) + + collector = FlamegraphCollector(1000) + frames = [ + MockInterpreterInfo( + 0, + [MockThreadInfo( + 1, + [MockFrameInfo("test.py", 1, "root"), + MockFrameInfo("test.py", 2, '&')] + )], + ) + ] + collector.collect(frames) + + with captured_stdout(), captured_stderr(): + export_ok = collector.export(flamegraph_out.name) + self.assertTrue(export_ok) + + with open(flamegraph_out.name, "r", encoding="utf-8") as f: + content = f.read() + + # The funcname's must be escaped in the embedded data so it + # cannot close the inline ", content) + + # The tooltip inserts the function name as HTML after resolving it. + self.assertIn( + "const funcname = escapeHtml(" + 'resolveString(d.data.funcname) || resolveString(d.data.name) || "");', + content, + ) + def test_flamegraph_collector_empty_export_fails(self): """Test empty flamegraph export reports no output.""" flamegraph_out = tempfile.NamedTemporaryFile( diff --git a/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst b/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst new file mode 100644 index 00000000000000..43316035d9b2c2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst @@ -0,0 +1,3 @@ +Escape special characters in ``profiling.sampling`` flamegraph function +names to prevent markup injection in the generated HTML report. Patch by +tonghuaroot.