From 30f7fd25796baf570dfb0f739016d6c1a4c6fa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tonghuaroot=20=28=E7=AB=A5=E8=AF=9D=29?= Date: Tue, 24 Nov 2026 20:30:50 +0800 Subject: [PATCH] gh-153840: Escape special characters in flamegraph function names (GH-153841) (cherry picked from commit 2aa6d1d29b730163f6ee257abde16cc1210e3713) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: tonghuaroot (童话) --- .../sampling/_flamegraph_assets/flamegraph.js | 2 +- Lib/profiling/sampling/stack_collector.py | 5 +++ .../test_sampling_profiler/test_collectors.py | 41 +++++++++++++++++++ ...-07-17-14-00-00.gh-issue-153840.FgXs3e.rst | 3 ++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst diff --git a/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js b/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js index d66da0402e52ca1..58e33703771fee4 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 60061b4a2ecf111..b3fc44607924884 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 dcac6f3e7dfd9e7..46953eeba43021a 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 000000000000000..43316035d9b2c27 --- /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.