From 063037f57389f546fafc10f8601fe6d8262b88ad Mon Sep 17 00:00:00 2001 From: Mangesh Raut <95149253+mangeshraut712@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:29:50 +0000 Subject: [PATCH] fix(453): escape :.3f/:1f cells after formatting A numeric subclass whose __format__ returns markup could land in the field-performance table/chart and the confusion-matrix percentage cells. Escape the formatted string at each of the five sites. Gauge stays unescaped: int(score * 100) forces a plain int. --- .../reporting/html/visualization_engine.py | 20 ++-- .../html/test_visualization_engine.py | 102 ++++++++++++++++++ 2 files changed, 116 insertions(+), 6 deletions(-) diff --git a/src/stickler/reporting/html/visualization_engine.py b/src/stickler/reporting/html/visualization_engine.py index 8bbc13a9..31a105e9 100644 --- a/src/stickler/reporting/html/visualization_engine.py +++ b/src/stickler/reporting/html/visualization_engine.py @@ -1,5 +1,13 @@ """ Simple visualization engine for HTML reports - v0. + +Numeric cells that use a format spec (``:.3f`` / ``:.1f``) are rendered through +``html.escape(f"{{value:.Nf}}")`` so a caller-supplied numeric subclass whose +``__format__`` returns markup cannot land in the page (#453). + +``generate_performance_gauge`` is excluded on purpose: ``int(score * 100)`` +forces a plain ``int``, so ``__format__`` / ``__str__`` / ``__mul__`` on a +subclass cannot reach the template. """ import html from typing import Any, Dict @@ -66,7 +74,7 @@ def generate_field_performance_chart(self, field_metrics: Dict[str, Any], config
{html.escape(str(field_name))}
- {f1_score:.3f} + {html.escape(f"{f1_score:.3f}")}
''' @@ -123,9 +131,9 @@ def generate_field_performance_table(self, field_metrics: Dict[str, Any], config html_string += f''' {html.escape(str(field_name))} - {precision:.3f} - {recall:.3f} - {f1:.3f} + {html.escape(f"{precision:.3f}")} + {html.escape(f"{recall:.3f}")} + {html.escape(f"{f1:.3f}")} ''' html_string += f''' @@ -167,9 +175,9 @@ def generate_confusion_matrix_heatmap(self, cm_data: Dict[str, Any], config: Any
{metric.upper()}
{value}
-
{percentage:.1f}%
+
{html.escape(f"{percentage:.1f}")}%
''' html_string += '' - return html_string + return html_string \ No newline at end of file diff --git a/tests/reporting/html/test_visualization_engine.py b/tests/reporting/html/test_visualization_engine.py index f726a7ac..ea0f8027 100644 --- a/tests/reporting/html/test_visualization_engine.py +++ b/tests/reporting/html/test_visualization_engine.py @@ -296,3 +296,105 @@ def test_generate_confusion_matrix_heatmap_missing_metrics(self, mock_color_util assert '57.1%' in result # 20/35 * 100 assert '42.9%' in result # 15/35 * 100 assert '0.0%' in result # 0/35 * 100 + + +class TestFormatSpecCellsAreEscaped: + """``:.3f`` / ``:.1f`` cells must escape after formatting (#453). + + A numeric subclass whose ``__format__`` returns markup reaches the page + unless the formatted string is escaped. Chart/table cells need only + ``__format__``; the heatmap percentage needs ``__truediv__`` + ``__mul__`` + + ``__format__`` because the template formats ``(value / total) * 100``. + """ + + class HF(float): + def __format__(self, spec): + return "" + + class HeatHostile(float): + def __truediv__(self, other): + return TestFormatSpecCellsAreEscaped.HeatCarrier(float(self) / float(other)) + + def __rtruediv__(self, other): + return TestFormatSpecCellsAreEscaped.HeatCarrier(float(other) / float(self)) + + class HeatCarrier(float): + def __mul__(self, other): + return TestFormatSpecCellsAreEscaped.HeatHostileResult(float(self) * float(other)) + + def __rmul__(self, other): + return TestFormatSpecCellsAreEscaped.HeatHostileResult(float(other) * float(self)) + + class HeatHostileResult(float): + def __format__(self, spec): + return "" + + def _engine(self): + from stickler.reporting.html.report_config import ReportConfig + from stickler.reporting.html.visualization_engine import VisualizationEngine + return VisualizationEngine(), ReportConfig() + + def test_table_precision_recall_f1_escape_format_markup(self): + eng, config = self._engine() + out = eng.generate_field_performance_table( + { + "vendor": { + "precision": self.HF(0.9), + "recall": self.HF(0.8), + "f1": self.HF(0.85), + "tp": 1, + "fd": 0, + "fa": 0, + "fn": 0, + } + }, + config, + ) + assert "