This commit is contained in:
RAJVARDHAN 2026-09-24 14:53:06 +01:00 • committed by GitHub
commit 26e6c3d9b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 3 deletions

View file

@ -136,15 +136,64 @@ def write_run_record(run_dir: Path, run_record: dict[str, Any]) -> None:
)
def write_executive_report(run_dir: Path, final_scan_result: str) -> None:
def deduplicate_markdown_headings(content: str) -> str:
"""Removes duplicate markdown headings appearing consecutively, ignoring fenced code blocks."""
lines = content.splitlines()
cleaned: list[str] = []
prev_heading: str | None = None
in_fence = False
fence_char = ""
fence_len = 0
for line in lines:
stripped = line.strip()
# Check for code fence start or end (``` or ~~~)
if stripped.startswith("```") or stripped.startswith("~~~"):
char = stripped[0]
count = len(stripped) - len(stripped.lstrip(char))
if not in_fence:
in_fence = True
fence_char = char
fence_len = count
prev_heading = None
cleaned.append(line)
continue
elif char == fence_char and count >= fence_len:
in_fence = False
prev_heading = None
cleaned.append(line)
continue
if in_fence:
cleaned.append(line)
continue
match = re.match(r"^(#{1,6})\s+(.*)$", stripped)
if match:
heading_key = f"{match.group(1)} {match.group(2).strip().lower()}"
if heading_key == prev_heading:
continue
prev_heading = heading_key
else:
if stripped:
prev_heading = None
cleaned.append(line)
return "\n".join(cleaned)
def write_execution_report(run_dir: Path, final_scan_result: str) -> None:
path = run_dir / "penetration_test_report.md"
cleaned_result = deduplicate_markdown_headings(final_scan_result)
with path.open("w", encoding="utf-8") as f:
f.write("# Security Penetration Test Report\n\n")
f.write(f"**Generated:** {datetime.now(UTC).strftime('%Y-%m-%d %H:%M:%S UTC')}\n\n")
f.write(f"{final_scan_result}\n")
f.write(f"{cleaned_result}\n")
logger.info("Saved final penetration test report to: %s", path)
def write_vulnerabilities(
run_dir: Path,
vulnerability_reports: list[dict[str, Any]],

View file

@ -10,9 +10,10 @@ import pytest
from strix.report.writer import (
atomic_write_text,
deduplicate_markdown_headings,
read_run_record,
render_vulnerability_md,
write_executive_report,
write_execution_report,
write_run_record,
write_vulnerabilities,
)
@ -240,6 +241,46 @@ def test_write_executive_report_writes_markdown(tmp_path: Path) -> None:
assert "Scan complete. No critical issues." in content
def test_deduplicate_markdown_headings() -> None:
raw_markdown = (
"# Executive Summary\n"
"# Executive Summary\n"
"This is the summary content.\n\n"
"```bash\n"
"# bash comment inside fence\n"
"# bash comment inside fence\n"
"echo test\n"
"```\n\n"
"## Methodology\n"
"## Methodology\n"
"Testing approach details."
)
expected = (
"# Executive Summary\n"
"This is the summary content.\n\n"
"```bash\n"
"# bash comment inside fence\n"
"# bash comment inside fence\n"
"echo test\n"
"```\n\n"
"## Methodology\n"
"Testing approach details."
)
assert deduplicate_markdown_headings(raw_markdown) == expected
def test_write_execution_report_deduplicates_headings(tmp_path: Path) -> None:
duplicate_scan_output = (
"# Executive Summary\n"
"# Executive Summary\n"
"Scan complete. No critical issues.\n"
)
write_execution_report(tmp_path, duplicate_scan_output)
content = (tmp_path / "penetration_test_report.md").read_text(encoding="utf-8")
assert content.count("# Executive Summary") == 1
assert "Scan complete. No critical issues." in content
def test_render_vulnerability_md_surfaces_calibration_metadata() -> None:
"""Confidence, the case against the finding, and retest status are part of
the deliverable — storing them without rendering hides the reasoning."""