Skip to content

Commit ea35f51

Browse files
committed
fix(report): share Markdown escaping and check rendered leak assertions
Signed-off-by: yashrajbasav <[email protected]>
1 parent 28f6e93 commit ea35f51

5 files changed

Lines changed: 14 additions & 29 deletions

File tree

‎contrib/batch_scan/reports.py‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
from __future__ import annotations
2424

2525
import json
26-
import re
2726
from collections import defaultdict
2827
from datetime import UTC, datetime
29-
from html import escape as escape_html
3028
from io import StringIO
3129

3230
from skillspector import __version__ as _skillspector_version
33-
from skillspector.nodes.report import _clean_text
31+
from skillspector.nodes.report import _markdown_cell as _markdown_text
32+
from skillspector.nodes.report import _markdown_code
33+
from skillspector.nodes.report import _markdown_plain_text as _markdown_plain_text
3434

3535

3636
def sorted_results(results: list[dict[str, object]]) -> list[dict[str, object]]:
@@ -376,29 +376,6 @@ def _format_json(results: list[dict[str, object]]) -> str:
376376
# ═══════════════════════════════════════════════════════════════════
377377

378378

379-
def _markdown_plain_text(value: object) -> str:
380-
"""Remove display controls and keep a Markdown field on one line."""
381-
text = _clean_text(str(value)) or ""
382-
text = "".join(c for c in text if c.isprintable() or c.isspace())
383-
return " ".join(text.splitlines())
384-
385-
386-
def _markdown_text(value: object) -> str:
387-
"""Keep scan-derived prose within its own Markdown line."""
388-
text = escape_html(_markdown_plain_text(value), quote=False)
389-
return re.sub(r"([\\`*_{}\[\]()#+.!|>~-])", r"\\\1", text)
390-
391-
392-
def _markdown_code(value: object, *, table_cell: bool = False) -> str:
393-
"""Quote literal text without allowing code-span or table-cell breakouts."""
394-
text = _markdown_plain_text(value)
395-
if table_cell:
396-
text = text.replace("|", "\\|")
397-
delimiter = "`" * (max((len(run) for run in re.findall(r"`+", text)), default=0) + 1)
398-
padding = " " if text.startswith(("`", " ")) or text.endswith(("`", " ")) else ""
399-
return f"{delimiter}{padding}{text}{padding}{delimiter}"
400-
401-
402379
def _format_markdown(results: list[dict[str, object]]) -> str:
403380
lines: list[str] = []
404381
total = len(results)

‎tests/nodes/analyzers/test_dependency_sources.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99

1010
import pytest
11+
from markdown_it import MarkdownIt
1112

1213
from skillspector.dependency_sources import analyze_dependency_sources
1314
from skillspector.llm_analyzer_base import Batch
@@ -1204,6 +1205,8 @@ def test_url_credentials_are_redacted_from_findings_and_all_reports(output_forma
12041205
"output_format": output_format,
12051206
}
12061207
rendered = report(state)["report_body"]
1208+
if output_format == "markdown":
1209+
rendered = MarkdownIt().enable("table").render(rendered)
12071210
for secret in (username, password, query_token):
12081211
assert secret not in rendered
12091212

‎tests/nodes/test_report_sanitizer.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ def test_report_sanitizes_llm_message_copied_to_pattern(fmt: str) -> None:
151151

152152
result = report(state)
153153
rendered = result["report_body"]
154-
if fmt == "markdown":
155-
rendered = MarkdownIt().enable("table").render(rendered)
156154
serialized_findings = json.dumps([item.to_dict() for item in result["filtered_findings"]])
157155
assert "\x1b" not in rendered
158156
assert "secret" not in rendered

‎tests/nodes/test_security_end_to_end.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pathlib import Path
1010

1111
import pytest
12+
from markdown_it import MarkdownIt
1213
from typer.testing import CliRunner
1314

1415
import skillspector.nodes.build_context as build_context_module
@@ -677,7 +678,10 @@ async def test_complete_match_payload_is_bounded_across_public_surfaces(tmp_path
677678

678679
for output_format in ("terminal", "json", "markdown", "sarif"):
679680
rendered = render_report({**result, "output_format": output_format})
680-
assert payload_tail not in rendered["report_body"]
681+
body = rendered["report_body"]
682+
if output_format == "markdown":
683+
body = MarkdownIt().enable("table").render(body)
684+
assert payload_tail not in body
681685

682686
verdict = await run_scan(str(tmp_path), use_llm=False, output_format="json")
683687
assert payload_tail not in json.dumps(verdict, sort_keys=True)

‎tests/unit/test_cli.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import pytest
3737
import typer
3838
import yaml
39+
from markdown_it import MarkdownIt
3940
from rich.console import Console
4041
from typer.testing import CliRunner
4142

@@ -5280,6 +5281,8 @@ def test_scan_transitive_intrinsic_child_partial_is_not_traversal_truncation(
52805281
exceptions = cast(list[dict[str, object]], completeness["ledger_exceptions"])
52815282
assert {item["reason_code"] for item in exceptions} == {"static_parse_limit"}
52825283
report_body = cast(str, merged["report_body"])
5284+
if output_format is cli.FormatChoice.markdown:
5285+
report_body = MarkdownIt().enable("table").render(report_body)
52835286
assert "Inspection reached its configured output limit." not in report_body
52845287
assert "Transitive traversal truncated" not in report_body
52855288
if output_format is cli.FormatChoice.json:

0 commit comments

Comments
 (0)