Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
mangeshraut712 committed Oct 28, 2026
commit 063037f57389f546fafc10f8601fe6d8262b88ad
20 changes: 14 additions & 6 deletions src/stickler/reporting/html/visualization_engine.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -66,7 +74,7 @@ def generate_field_performance_chart(self, field_metrics: Dict[str, Any], config
<div class="field-label">{html.escape(str(field_name))}</div>
<div class="bar-container">
<div class="bar-fill" style="width: {percentage}%; background-color: {color};"></div>
<span class="bar-value">{f1_score:.3f}</span>
<span class="bar-value">{html.escape(f"{f1_score:.3f}")}</span>
</div>
</div>
'''
Expand Down Expand Up @@ -123,9 +131,9 @@ def generate_field_performance_table(self, field_metrics: Dict[str, Any], config
html_string += f'''
<tr>
<td>{html.escape(str(field_name))}</td>
<td>{precision:.3f}</td>
<td>{recall:.3f}</td>
<td style="background-color: {f1_color}; color: white; font-weight: bold;">{f1:.3f}</td>
<td>{html.escape(f"{precision:.3f}")}</td>
<td>{html.escape(f"{recall:.3f}")}</td>
<td style="background-color: {f1_color}; color: white; font-weight: bold;">{html.escape(f"{f1:.3f}")}</td>
'''

html_string += f'''
Expand Down Expand Up @@ -167,9 +175,9 @@ def generate_confusion_matrix_heatmap(self, cm_data: Dict[str, Any], config: Any
<div class="cm-cell" style="border-left-color: {metric_colors[metric]}">
<div class="cm-label">{metric.upper()}</div>
<div class="cm-value">{value}</div>
<div class="cm-percentage">{percentage:.1f}%</div>
<div class="cm-percentage">{html.escape(f"{percentage:.1f}")}%</div>
</div>
'''

html_string += '</div>'
return html_string
return html_string
102 changes: 102 additions & 0 deletions tests/reporting/html/test_visualization_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<img src=x onerror=alert(1)>"

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 "<img src=x onerror=alert(1)>"

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 "<img src=x" not in out
assert "&lt;img src=x" in out

def test_chart_bar_value_escapes_format_markup(self):
eng, config = self._engine()
out = eng.generate_field_performance_chart(
{"vendor": {"f1": self.HF(0.75)}},
config,
)
assert "<img src=x" not in out
assert "&lt;img src=x" in out

def test_heatmap_percentage_escapes_format_markup(self):
eng, config = self._engine()
out = eng.generate_confusion_matrix_heatmap(
{"tp": self.HeatHostile(1), "tn": 0, "fd": 0, "fa": 0, "fn": 0},
config,
)
assert "<img src=x" not in out
assert "&lt;img src=x" in out

def test_ordinary_format_values_still_render(self):
eng, config = self._engine()
table = eng.generate_field_performance_table(
{
"vendor": {
"precision": 0.9123,
"recall": 0.8,
"f1": 0.85,
"tp": 1,
"fd": 0,
"fa": 0,
"fn": 0,
}
},
config,
)
chart = eng.generate_field_performance_chart(
{"vendor": {"f1": 0.75}},
config,
)
heat = eng.generate_confusion_matrix_heatmap(
{"tp": 1, "tn": 0, "fd": 0, "fa": 0, "fn": 0},
config,
)
assert "0.912" in table
assert "0.750" in chart
assert "100.0%" in heat
Loading