Skip to content

Commit 94abd7c

Browse files
authored
Merge pull request #733 from NVIDIA/yashraj/fix-batch-markdown-output-20261005
fix(batch): prevent Markdown report injection
2 parents f3ec825 + fd0095f commit 94abd7c

3 files changed

Lines changed: 149 additions & 12 deletions

File tree

‎contrib/batch_scan/reports.py‎

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

2525
import json
26+
import re
2627
from collections import defaultdict
2728
from datetime import UTC, datetime
29+
from html import escape as escape_html
2830
from io import StringIO
2931

3032
from skillspector import __version__ as _skillspector_version
33+
from skillspector.nodes.report import _clean_text
3134

3235

3336
def sorted_results(results: list[dict[str, object]]) -> list[dict[str, object]]:
@@ -373,6 +376,29 @@ def _format_json(results: list[dict[str, object]]) -> str:
373376
# ═══════════════════════════════════════════════════════════════════
374377

375378

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+
376402
def _format_markdown(results: list[dict[str, object]]) -> str:
377403
lines: list[str] = []
378404
total = len(results)
@@ -438,9 +464,12 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
438464
lang = skill.get("language", "en")
439465

440466
if r.get("error"):
441-
lines.append(f"| `{name}` | ERR | ERROR | — | {lang} |")
467+
lines.append(f"| {_markdown_code(name, table_cell=True)} | ERR | ERROR | — | {_markdown_text(lang)} |")
442468
else:
443-
lines.append(f"| `{name}` | {score}/100 | {sev} | {issues} | {lang} |")
469+
lines.append(
470+
f"| {_markdown_code(name, table_cell=True)} | {score}/100 | {_markdown_text(sev)} | "
471+
f"{issues} | {_markdown_text(lang)} |"
472+
)
444473
lines.append("")
445474

446475
# ── Issue details for HIGH / CRITICAL ────────────────────────
@@ -458,8 +487,8 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
458487
risk = r.get("risk_assessment", {})
459488
name = skill.get("name", "?")
460489
lines.append(
461-
f"### {name} — {risk.get('score', 0)}/100 "
462-
f"{risk.get('severity', 'HIGH')}\n"
490+
f"### {_markdown_text(name)} — {risk.get('score', 0)}/100 "
491+
f"{_markdown_text(risk.get('severity', 'HIGH'))}\n"
463492
)
464493
for issue in r.get("issues", []):
465494
sev = str(issue.get("severity", "LOW")).upper()
@@ -469,26 +498,27 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
469498
loc_file = loc.get("file", "") if isinstance(loc, dict) else ""
470499
rule_id = issue.get("id", "?")
471500
explanation = issue.get("explanation", issue.get("message", ""))
472-
lines.append(f"- **{emoji} {rule_id}**: {explanation}")
501+
lines.append(f"- **{emoji} {_markdown_text(rule_id)}**: {_markdown_text(explanation)}")
473502
if loc_file:
474-
lines.append(f" - Location: `{loc_file}:{loc_start}`")
503+
lines.append(f" - Location: {_markdown_code(f'{loc_file}:{loc_start}')}")
475504
conf = issue.get("confidence", 0)
476505
lines.append(f" - Confidence: {float(conf):.0%}")
477506
rem = issue.get("remediation")
478507
if rem:
479-
lines.append(f" - Remediation: {rem}")
508+
lines.append(f" - Remediation: {_markdown_text(rem)}")
480509
lines.append("")
481510
lines.append("")
482511

483512
exception_groups = _exception_groups(results)
484513
if exception_groups:
485514
lines.append("## Ledger Exceptions\n")
486515
for skill_name, exceptions in exception_groups:
487-
lines.append(f"### {skill_name}\n")
516+
lines.append(f"### {_markdown_text(skill_name)}\n")
488517
for exception in exceptions:
489518
lines.append(
490-
f"- **{exception.get('reason_code', 'unknown')}** "
491-
f"`{exception.get('path', '')}`: {exception.get('message', '')}"
519+
f"- **{_markdown_text(exception.get('reason_code', 'unknown'))}** "
520+
f"{_markdown_code(exception.get('path', ''))}: "
521+
f"{_markdown_text(exception.get('message', ''))}"
492522
)
493523
lines.append("")
494524

‎contrib/batch_scan/tests/test_inspection_reporting.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import json
99
from pathlib import Path
1010

11+
from markdown_it import MarkdownIt
12+
1113
from contrib.batch_scan.reports import _format_json, _format_markdown, _format_terminal
1214
from contrib.batch_scan.runner import entry_from_result
1315

@@ -61,6 +63,9 @@ def test_batch_formats_preserve_every_child_ledger_exception() -> None:
6163
assert [item["reason_code"] for item in exceptions] == ["read_error", "syntax_error"]
6264
assert payload["skills"][0]["issues"][0]["finding_id"] == "finding-batch-1"
6365

64-
for rendered in (_format_terminal([entry]), _format_markdown([entry])):
66+
for rendered in (
67+
_format_terminal([entry]),
68+
MarkdownIt().enable("table").render(_format_markdown([entry])),
69+
):
6570
assert "read_error" in rendered
6671
assert "syntax_error" in rendered

‎tests/test_batch_scan_reports.py‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
from __future__ import annotations
77

88
import json
9+
from copy import deepcopy
910

10-
from contrib.batch_scan.reports import _format_json
11+
import pytest
12+
from markdown_it import MarkdownIt
13+
14+
from contrib.batch_scan.reports import _format_json, _format_markdown
1115

1216

1317
def test_json_marks_error_entries_as_unsuccessful() -> None:
@@ -23,3 +27,101 @@ def test_json_marks_error_entries_as_unsuccessful() -> None:
2327

2428
assert payload["skills"][0]["error"] == "scan crashed"
2529
assert payload["skills"][0]["execution_successful"] is False
30+
31+
32+
@pytest.mark.parametrize(
33+
"payload",
34+
[
35+
"safe` | 0/100 | LOW | 0 | en |\n<!--",
36+
"\r\n## Issues (0)\rNo security issues detected.\n<!--",
37+
"` `` ``` <script>alert(1)</script> [safe](https://example.invalid)",
38+
"\\| **safe** &lt;!--",
39+
" leading and trailing spaces ",
40+
"~~hidden~~",
41+
"\x1b[2J\x00\u202eLOW\u202c\x9b\u2066safe\u2069",
42+
],
43+
)
44+
@pytest.mark.parametrize(
45+
"field",
46+
[
47+
"name",
48+
"language",
49+
"id",
50+
"message",
51+
"explanation",
52+
"remediation",
53+
"file",
54+
"reason_code",
55+
"path",
56+
"ledger_message",
57+
],
58+
)
59+
def test_batch_markdown_treats_scan_content_as_literal_text(payload: str, field: str) -> None:
60+
entry = {
61+
"skill": {"name": "sample", "language": "zh"},
62+
"risk_assessment": {"score": 90, "severity": "CRITICAL"},
63+
"issues": [
64+
{
65+
"id": "P1",
66+
"message": "finding",
67+
"remediation": "review",
68+
"location": {"file": "SKILL.md", "start_line": 1},
69+
}
70+
],
71+
"analysis_completeness": {
72+
"ledger_exceptions": [
73+
{"reason_code": "partial", "path": "run.py", "message": "inspect"}
74+
]
75+
},
76+
}
77+
parser = MarkdownIt("commonmark", {"html": True}).enable(["table", "strikethrough"])
78+
original_blocks = [token.type for token in parser.parse(_format_markdown([entry]))]
79+
if field in {"name", "language"}:
80+
entry["skill"][field] = payload
81+
elif field == "file":
82+
entry["issues"][0]["location"][field] = payload
83+
elif field in {"reason_code", "path", "ledger_message"}:
84+
entry["analysis_completeness"]["ledger_exceptions"][0][
85+
"message" if field == "ledger_message" else field
86+
] = payload
87+
else:
88+
entry["issues"][0][field] = payload
89+
original = deepcopy(entry)
90+
91+
report = _format_markdown([entry])
92+
tokens = parser.parse(report)
93+
94+
assert all(character.isprintable() or character in "\n\t" for character in report)
95+
assert [token.type for token in tokens] == original_blocks
96+
for token in tokens:
97+
assert token.type not in {"html_block", "fence", "code_block"}
98+
assert not any(
99+
child.type in {"html_inline", "link_open", "image", "s_open"}
100+
for child in token.children or []
101+
)
102+
assert entry == original
103+
assert json.loads(_format_json([entry]))["skills"][0]["skill"]["name"] == entry["skill"]["name"]
104+
105+
106+
@pytest.mark.parametrize("value", ["a|b", r"a\|b", "`a`", "a``b`", "<script> & value"])
107+
@pytest.mark.parametrize("table_cell", [False, True])
108+
def test_batch_markdown_code_preserves_literal_values(value: str, table_cell: bool) -> None:
109+
from contrib.batch_scan.reports import _markdown_code
110+
111+
source = _markdown_code(value, table_cell=table_cell)
112+
if table_cell:
113+
source = f"| Path |\n|---|\n| {source} |"
114+
tokens = MarkdownIt("commonmark").enable("table").parse(source)
115+
code = [
116+
child.content
117+
for token in tokens
118+
for child in token.children or []
119+
if child.type == "code_inline"
120+
]
121+
assert code == [value]
122+
123+
124+
def test_batch_markdown_strips_complete_ansi_sequences() -> None:
125+
from contrib.batch_scan.reports import _markdown_plain_text
126+
127+
assert _markdown_plain_text("a\x1b[2Jb\x1b[31mc\x1b[0m") == "abc"

0 commit comments

Comments
 (0)