|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import importlib.util |
| 3 | +import sys |
| 4 | +import types |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +ROOT = Path(__file__).resolve().parents[1] # repo root |
| 8 | +REPORT_RES = ROOT / "ax" / "utils" / "report" / "resources" |
| 9 | + |
| 10 | +# ---- Stub ax.plot.render so render.py can import without importing full ax package ---- |
| 11 | +ax_mod = types.ModuleType("ax") |
| 12 | +ax_plot_mod = types.ModuleType("ax.plot") |
| 13 | +ax_plot_render_mod = types.ModuleType("ax.plot.render") |
| 14 | + |
| 15 | +ax_plot_render_mod._js_requires = lambda *a, **k: "" |
| 16 | +ax_plot_render_mod._load_css_resource = lambda *a, **k: "" |
| 17 | + |
| 18 | +sys.modules["ax"] = ax_mod |
| 19 | +sys.modules["ax.plot"] = ax_plot_mod |
| 20 | +sys.modules["ax.plot.render"] = ax_plot_render_mod |
| 21 | + |
| 22 | +# ---- Load ax/utils/report/render.py by file path ---- |
| 23 | +render_path = (ROOT / "ax" / "utils" / "report" / "render.py").resolve() |
| 24 | +spec = importlib.util.spec_from_file_location("ax_utils_report_render", render_path) |
| 25 | +assert spec and spec.loader, "Failed to load module spec" |
| 26 | +mod = importlib.util.module_from_spec(spec) |
| 27 | +spec.loader.exec_module(mod) |
| 28 | + |
| 29 | +# ---- Monkeypatch ALL pkgutil-based loaders so we don't need package resources ---- |
| 30 | +mod._load_css_resource = lambda: "" |
| 31 | +mod._load_plot_css_resource = lambda: "" |
| 32 | +mod._load_html_template = lambda name: (REPORT_RES / name).read_text(encoding="utf-8") |
| 33 | + |
| 34 | +# ---- Now test escaping ---- |
| 35 | +payload = "<img src=x onerror=alert('AX_XSS_TEST')>" |
| 36 | +html = mod.render_report_elements(payload, [mod.p_html("hello")]) |
| 37 | + |
| 38 | +assert "<img" not in html, "FAIL: raw HTML injection still present" |
| 39 | +assert "<img" in html, "FAIL: expected escaped payload not found" |
| 40 | + |
| 41 | +print("[OK] experiment_name is escaped (autoescape enabled)") |
0 commit comments