Skip to content

Commit 2aa6d1d

Browse files
authored
gh-153840: Escape special characters in flamegraph function names (#153841)
1 parent e69658b commit 2aa6d1d

4 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎Lib/profiling/sampling/_flamegraph_assets/flamegraph.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ function createPythonTooltip(data) {
335335
const childCount = d.children ? d.children.length : 0;
336336
const source = d.data.source;
337337

338-
const funcname = resolveString(d.data.funcname) || resolveString(d.data.name);
338+
const funcname = escapeHtml(resolveString(d.data.funcname) || resolveString(d.data.name) || "");
339339
const filename = resolveString(d.data.filename) || "";
340340
const moduleName = resolveString(d.data.module) || "";
341341
const displayName = escapeHtml(useModuleNames ? (moduleName || filename) : filename);

‎Lib/profiling/sampling/stack_collector.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ def _create_flamegraph_html(self, data):
504504
# The C encoder can exhaust the C stack independently of the
505505
# Python recursion limit. iterencode() uses the Python encoder.
506506
data_json = "".join(json.JSONEncoder().iterencode(data))
507+
data_json = (
508+
data_json.replace("<", "\\u003c")
509+
.replace(">", "\\u003e")
510+
.replace("&", "\\u0026")
511+
)
507512

508513
template_dir = importlib.resources.files(__package__)
509514
vendor_dir = template_dir / "_vendor"

‎Lib/test/test_profiling/test_sampling_profiler/test_collectors.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,47 @@ def test_flamegraph_collector_export(self):
596596
self.assertIn('"sample_interval_usec": 10000', content)
597597
self.assertIn("samples * data.stats.sample_interval_usec / 1000", content)
598598

599+
def test_flamegraph_escapes_special_chars_in_funcname(self):
600+
# Function names with <, >, & must be escaped in the HTML.
601+
flamegraph_out = tempfile.NamedTemporaryFile(
602+
suffix=".html", delete=False
603+
)
604+
self.addCleanup(close_and_unlink, flamegraph_out)
605+
606+
collector = FlamegraphCollector(1000)
607+
frames = [
608+
MockInterpreterInfo(
609+
0,
610+
[MockThreadInfo(
611+
1,
612+
[MockFrameInfo("test.py", 1, "root"),
613+
MockFrameInfo("test.py", 2, '</script><lambda>&')]
614+
)],
615+
)
616+
]
617+
collector.collect(frames)
618+
619+
with captured_stdout(), captured_stderr():
620+
export_ok = collector.export(flamegraph_out.name)
621+
self.assertTrue(export_ok)
622+
623+
with open(flamegraph_out.name, "r", encoding="utf-8") as f:
624+
content = f.read()
625+
626+
# The funcname's </script> must be escaped in the embedded data so it
627+
# cannot close the inline <script> block; the raw form must be absent.
628+
self.assertIn(
629+
r"\u003c/script\u003e\u003clambda\u003e\u0026", content
630+
)
631+
self.assertNotIn("</script><lambda>", content)
632+
633+
# The tooltip inserts the function name as HTML after resolving it.
634+
self.assertIn(
635+
"const funcname = escapeHtml("
636+
'resolveString(d.data.funcname) || resolveString(d.data.name) || "");',
637+
content,
638+
)
639+
599640
def test_flamegraph_collector_empty_export_fails(self):
600641
"""Test empty flamegraph export reports no output."""
601642
flamegraph_out = tempfile.NamedTemporaryFile(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Escape special characters in ``profiling.sampling`` flamegraph function
2+
names to prevent markup injection in the generated HTML report. Patch by
3+
tonghuaroot.

0 commit comments

Comments
 (0)