@@ -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 (
0 commit comments