Files
codex-security/plugins/codex-security/tests/test_report_projection.py
ea617a4c09 fix(workbench): retain shared history across worktree changes (#456)
* fix(workbench): share scan history across linked worktrees

* fix(workbench): verify repository generation across platforms

* fix(workbench): harden and simplify worktree history

* fix(workbench): preserve recreated repository subdirectories

* fix(workbench): refresh the bundled history plugin

Advance the paired standalone plugin version so existing staged marketplaces load the linked-worktree history helpers. Leave the npm package release version unchanged.

* fix(workbench): verify saved history candidates

Use recorded checkout ownership before falling back to live repository identities. Preserve matching persisted identities for removed worktrees and retain verified same-origin comparisons.

* fix(workbench): unify verified repository history

* fix(workbench): preserve scan history ownership and replay

* fix(workbench): quarantine unproved prerelease identities

* fix(workbench): order automatic history comparisons

* fix(workbench): persist completion visibility order

* fix(workbench): preserve findings and Linux identities

* fix(workbench): preserve unbound legacy history

* Bind repository history to each scan generation

* Preserve archived scans across registration failures

* Bound repository identity maintenance to selected targets

* Repair legacy identity upgrades and precompute repository counts

* test(workbench): isolate directory metadata on Python 3.14

* fix(sdk): exclude internal helpers from public declarations

* test(workbench): send identity fixture through stdin

* fix(workbench): verify ownership before rejoining scans

* test(workbench): retain recreated fixture inodes

* test: align shared history fixtures with current schema

* test: include current migrations in repository identity upgrade checks

* Preserve owned legacy history and current rerun prompts

* Preserve repository history through validated saved matches

* Preserve timestamp and custom plugin history compatibility

* Preserve grouped history matching and mock output recovery

* Preserve grouped history matching and mock output recovery

* Preserve portable repository ownership and indexed history lookups

* Preserve legacy Git history and mixed filesystem comparisons

* Preserve legacy triage and registered linked-worktree comparison

* Keep legacy comparisons scoped and reuse repository history indexing

* Rank same-target feedback across legacy repository generations

* Keep legacy history local when matching current repository targets

* Preserve saved legacy aliases in linked comparison history

* Order combined feedback fixture imports

* Update completed findings fixture for combined migration history

* Align migration expectations and batch workflow condition fixtures

* Adapt archived-output callbacks to current observer helper

* Format archived-output observer callback

* Use the current historical migration fixture helper

* Preserve requested repository aliases through findings lookup

* Preserve local legacy decisions and requested scan aliases

* Support absolute paths in the Windows history fixture

* test: preserve Windows fixture identity and shell behavior

* fix(history): preserve accepted completion timestamp precision

* fix(workbench): replace reversed scan comparisons

* docs: make finding detail example explicitly synthetic

* test(workbench): follow shared scan allocation in identity fixture

* fix(workbench): preserve history across filesystem remounts

* test(workbench): align identity fixtures with migration semantics

* fix(workbench): preserve history across supported Python runtimes

* perf(workbench): paginate repository projections

* test(workbench): type repository page expectations

* fix(workbench): retain verified history after device changes

* test(workbench): supply archive defaults in lineage fixture

* fix(workbench): preserve composed remediation and writer fixtures

* Preserve caller scope and valid history during saved scan bootstrap

* Retain bound history after worktree paths are reused

* Use scan generations for history and live Git roots for interpreter protection

* Align schema and archive rollback fixtures with registration ordering

* Exclude saved checkouts from ambient Python discovery

* Isolate trusted Python from Git in history fixture

* Preserve base Python and packages in isolated history fixture

* test(findings): expect the requested repository alias

* fix(identity): avoid external stat and synchronous fixture timeouts

* fix(workbench): preserve historical target registration and migration

* fix(history): compare saved scan timestamps chronologically

* fix(sdk): trim saved scan timestamp fractions linearly

* Fix platform coverage in history migration fixtures

* fix(workbench): retain lexical roots during migration discovery

* test: preserve shared history across receipt migration

* test: align repository generation fixtures with merged migrations

* test(workbench): align identity migrations with issue receipts

* test(workbench): align history fixtures with current contracts

* test: run migration probes through Python runtime helpers

* Update generation migration fixtures for current schema

* test(workbench): align coordinator fixture with generation migration

* fix(workbench): keep Git discovery scoped across saved history

* fix(workbench): cover new and unavailable Git targets

* fix(workbench): expand incoming Git target paths

* docs: format shared scan history guidance

* fix(workbench): retain evidence of vanished Git targets

* test: exercise current repository generation migration

* fix: preserve Git discovery after incomplete workspace setup

* test: bind host Git in direct saved-history helpers

* fix(workbench): retain all scan feedback and register fixture targets

* fix(workbench): preserve saved checkout boundaries during tool discovery

* test(plugin): restore checkout root column in legacy migration fixture

* fix(plugin): require recorded checkout roots for legacy Git discovery

* fix(sdk): require saved checkout roots before tool discovery

* test(workbench): install the lifecycle view in the repository alias fixture

* fix(sdk): exclude rejected workspace paths from executable discovery

* fix(workbench): retain trusted Git for deleted bounded targets

* fix(workbench): disable ambient lookup for unanchored workspace paths

* fix(workbench): retain checkout boundaries for rejected workspaces

* test(workbench): use the complete schema in Python migration fixtures

* Retain rejected checkout exclusions after workspace correction

* Classify the quota error in the comparison stop test

* Update workbench identity fixtures with the object-store birth time

* Exercise repository generation upgrades and Windows owner rejection after renumbering

* Drop the stale retry assignment after archive acknowledgement

* Start the Windows archive attempt when the real patch write opens

* Exercise repository history with the current registration and schema

* Keep workbench identity test outputs in private temporary directories

* test: verify current and stale checkout ownership independently

* fix(history): validate native parent traversal before lookup

* Cover partially sequenced completion history on migration replay

---------

Co-authored-by: mldangelo-oai <269034524+mldangelo-oai@users.noreply.github.com>
Co-authored-by: Codex <codex@users.noreply.github.com>
2026-11-11 04:46:42 -08:00

1083 lines
42 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Check how structured scan records and legacy evidence become readable Markdown reports."""
from __future__ import annotations
import copy
from pathlib import Path
import pytest
from workbench_test_support import load_script
pytestmark = pytest.mark.cross_platform
PROJECTION = load_script("report_projection")
def canonical_documents() -> tuple[dict[str, object], dict[str, object], dict[str, object]]:
manifest = {
"scan": {
"target": {"displayName": "example/repo"},
"scope": {
"includePaths": ["src/"],
"excludePaths": [],
"summary": "## Injected scope\n- nested item",
},
"threatModel": {"summary": "# Queue boundaries\n\nThreat details"},
}
}
findings = {
"findings": [
{
"occurrenceId": "occ_1",
"title": "Parser | boundary\n## Injected finding heading",
"summary": "```\ncode fence\n```",
"severity": {"level": "high"},
"confidence": {"level": "high", "rationale": "Direct trace."},
"taxonomy": {"category": "parser | injection", "cwe": ["CWE-20"]},
"locations": [{"path": "src/parser.py", "startLine": 10}],
"remediation": "## Injected remediation\n- unsafe instruction",
}
]
}
coverage = {
"mode": "repository",
"inventoryStrategy": "repository",
"includePaths": ["src/"],
"excludePaths": [],
"surfaces": [],
"explicitExclusions": [],
"deferred": [],
}
return manifest, findings, coverage
def test_report_points_to_distinct_worker_models_in_the_manifest(monkeypatch) -> None:
monkeypatch.syspath_prepend(Path(PROJECTION.__file__).parent)
manifest, findings, coverage = canonical_documents()
manifest["scan"].update(
review={},
reviewState={"reviews": [{"workerId": "worker", "threatModel": {"summary": "Job state"}}]},
)
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "scan.reviewState.reviews" in markdown
def report_with_finding(**fields: object) -> str:
manifest, findings, coverage = canonical_documents()
findings["findings"][0].update(fields)
return PROJECTION.build_report_markdown(manifest, findings, coverage)
def test_projection_normalizes_structured_fields() -> None:
markdown = PROJECTION.build_report_markdown(*canonical_documents())
assert "\n## Injected" not in markdown
assert "\n# Injected" not in markdown
assert "\n```" not in markdown
assert "Text: ## Injected scope - nested item" in markdown
assert "Text: # Queue boundaries Threat details" in markdown
assert "\n# Queue boundaries" not in markdown
assert "Text: \\`\\`\\` code fence \\`\\`\\`" in markdown
assert "Parser \\| boundary ## Injected finding heading" in markdown
assert "Text: ## Injected remediation - unsafe instruction" in markdown
def test_projection_normalizes_block_syntax_and_preserves_inline_tildes() -> None:
manifest, findings, coverage = canonical_documents()
finding = findings["findings"][0]
finding["summary"] = "~~~\ncode fence\n~~~"
instruction = (
"1) Retry 3–5 times, then 5 to 10. See https://example.test/~docs?version=~1. "
"Use chmod 0~777 or chmod 0~600 key.pem."
)
finding["remediation"] = instruction
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "\n~~~" not in markdown
assert "Text: ~~~ code fence ~~~" in markdown
assert f"Text: {instruction}" in markdown
@pytest.mark.parametrize("linked_writeup", [False, True], ids=["inline", "linked"])
def test_projection_retains_distinct_source_fixes(linked_writeup: bool) -> None:
manifest, findings, coverage = canonical_documents()
finding = findings["findings"][0]
if linked_writeup:
finding["writeup"] = {"reportPath": "findings/parser/parser.md"}
finding["remediation"] = "Validate the record length."
finding["remediationTests"] = ["Reject a record longer than the allowed size."]
finding["preventiveControls"] = ["Centralize record validation."]
finding["provenance"] = {
"sourceFindings": [
{"id": "review-1:0", "finding": {"remediation": "Validate the record length."}},
{
"id": "review-2:0",
"finding": {
"remediation": "Reject duplicate record keys.",
"remediationTests": [
"Reject a record longer than the allowed size.",
"Cover duplicate keys in parser tests.",
],
"preventiveControls": [
"Centralize record validation.",
"Track keys while parsing a record.",
],
},
},
{
"id": "review-3:0",
"finding": {
"remediation": "Reject duplicate record keys.",
"remediationTests": [
"Cover duplicate keys in parser tests.",
"Reject case-variant duplicate keys.",
],
"preventiveControls": ["Track keys while parsing a record."],
},
},
]
}
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
if linked_writeup:
assert "findings/parser/parser.md" in markdown
assert "Source review-2:0: Reject duplicate record keys." in markdown
for text in (
"Validate the record length.",
"Reject duplicate record keys.",
"Reject a record longer than the allowed size.",
"Cover duplicate keys in parser tests.",
"Reject case-variant duplicate keys.",
"Centralize record validation.",
"Track keys while parsing a record.",
):
assert markdown.count(text) == 1
def test_projection_renders_inline_code_and_section_code_evidence() -> None:
manifest, findings, coverage = canonical_documents()
finding = findings["findings"][0]
finding["summary"] = "The `route/set` RPC forwards `routeName` to `RouteTable::set_route()`."
finding["codeEvidence"] = [
{
"id": "runtime-upsert",
"label": "Runtime upsert omits the reserved-ID check",
"path": "src/routes/table.rs",
"startLine": 25,
"endLine": 29,
"language": "rust",
"code": "self.routes.insert(route_name, destination);",
"explanation": "The runtime path inserts `owner` without reusing the startup check.",
}
]
finding["rootCause"] = {
"summary": "`owner` is reserved, but `set_route()` accepts it.",
"evidenceRefs": ["runtime-upsert"],
}
finding["validation"] = {
"summary": "The source trace confirmed the unchecked insert.",
"evidenceRefs": ["runtime-upsert"],
}
finding["attackPath"] = {
"dataflow": {"summary": "`route/set` -> shared route map"},
"evidenceRefs": ["runtime-upsert"],
}
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "The `route/set` RPC forwards `routeName`" in markdown
assert "#### Root Cause" in markdown
assert "**Runtime upsert omits the reserved-ID check**" in markdown
assert "`src/routes/table.rs:25-29`" in markdown
assert "```rust" in markdown
assert "self.routes.insert(route_name, destination);" in markdown
assert "The runtime path inserts `owner` without reusing the startup check." in markdown
@pytest.mark.parametrize("reference_key", ["evidenceRefs", "evidence_refs"])
def test_projection_renders_nested_attack_path_code_evidence(reference_key: str) -> None:
markdown = report_with_finding(
codeEvidence=[
{
"id": "archive-source",
"label": "Attacker-controlled archive path",
"path": "src/archive.py",
"startLine": 20,
"code": "entry_path = archive_entry.name",
"explanation": "The archive controls the path.",
},
{
"id": "archive-sink",
"label": "Unchecked filesystem write",
"path": "src/archive.py",
"startLine": 41,
"code": "destination.write_bytes(entry.read())",
"explanation": "The unchecked path reaches the write.",
},
],
attackPath={
"dataflow": {
"summary": "An archive entry path reaches a filesystem write.",
"evidenceRefs": [],
"evidence_refs": ["archive-source"],
},
"reachability": {
"summary": "An authenticated uploader can trigger extraction.",
reference_key: ["archive-sink"],
},
},
)
assert "entry_path = archive_entry.name" in markdown
assert "destination.write_bytes(entry.read())" in markdown
def test_projection_normalizes_scalar_evidence_references() -> None:
markdown = report_with_finding(
codeEvidence=[
{"id": "root-source", "code": "root_source()"},
{"id": "dataflow-source", "code": "dataflow_source()"},
{"id": "reachability-source", "code": "reachability_source()"},
],
root_cause={
"summary": "The authorization check occurs after the write.",
"evidence_refs": "root-source",
},
attackPath={
"dataflow": {
"summary": "An attacker-controlled value reaches the write.",
"evidence_refs": "dataflow-source",
},
"reachability": {
"summary": "An authenticated caller can reach the handler.",
"evidence_refs": "reachability-source",
},
},
)
assert "root_source()" in markdown
assert "dataflow_source()" in markdown
assert "reachability_source()" in markdown
def test_projection_merges_transformations_across_data_flow_aliases() -> None:
markdown = report_with_finding(
attackPath={
"dataFlow": {"transformations": ["decode archive entry", "parse *input*"]},
"dataflow": {
"summary": "request -> archive extraction -> filesystem write",
"transformations": ["dispatch extraction", "decode archive entry"],
},
"data_flow": {"transformations": None},
},
)
assert markdown.count("- decode archive entry") == 1
assert "- dispatch extraction" in markdown
assert "- parse \\*input\\*" in markdown.splitlines()
def test_projection_merges_top_level_and_reachability_preconditions() -> None:
markdown = report_with_finding(
attackPath={
"preconditions": [
"The service processes uploaded archives.",
"The attacker can upload an archive.",
],
"reachability": {
"summary": "An authenticated uploader can trigger extraction.",
"preconditions": [
"The attacker can upload an archive.",
"Automatic extraction is enabled.",
],
},
},
)
assert markdown.count("- The service processes uploaded archives.") == 1
assert markdown.count("- The attacker can upload an archive.") == 1
assert markdown.count("- Automatic extraction is enabled.") == 1
def test_projection_uses_top_level_attack_path_summary_as_reachability_fallback() -> None:
markdown = report_with_finding(
attackPath={
"summary": "An authenticated uploader can trigger parse_file to extract an archive."
},
)
reachability = markdown.split("#### Reachability", 1)[1].split("#### Severity", 1)[0]
assert (
reachability.strip()
== r"An authenticated uploader can trigger parse\_file to extract an archive."
)
assert "Reachability was not recorded" not in reachability
def test_projection_renders_typed_attack_path_steps() -> None:
markdown = report_with_finding(
attackPath={
"steps": [
"Upload an archive with a traversal entry.",
"Trigger automatic extraction.",
]
},
)
dataflow = markdown.split("#### Dataflow", 1)[1].split("#### Reachability", 1)[0]
assert "Attack steps:" in dataflow
assert "- Upload an archive with a traversal entry." in dataflow
assert "- Trigger automatic extraction." in dataflow
def test_projection_renders_typed_assessments_and_validation_outcomes() -> None:
markdown = report_with_finding(
validation={
"status": "validated",
"disposition": "reported",
"result": "The traversal write was confirmed.",
},
attackPath={
"impact": {
"level": "high",
"rationale": "The write can overwrite application files.",
"why": "The destination escapes the extraction root.",
},
"likelihood": "Likely for authenticated uploaders.",
"reachability": {
"source": "Attacker-controlled archive entry.",
"sink": "Unchecked filesystem write.",
},
},
)
assert "- **Status:** validated" in markdown
assert "- **Disposition:** reported" in markdown
assert "- **Result:** The traversal write was confirmed." in markdown
assert "- **Source:** Attacker-controlled archive entry." in markdown
assert "- **Sink:** Unchecked filesystem write." in markdown
assert "Impact assessment:" in markdown
assert "- **Level:** high" in markdown
assert "- **Rationale:** The write can overwrite application files." in markdown
assert "- **Why:** The destination escapes the extraction root." in markdown
assert "**Likelihood assessment:** Likely for authenticated uploaders." in markdown
def test_projection_renders_attack_path_context_lists() -> None:
markdown = report_with_finding(
attackPath={
"assumptions": ["Automatic extraction is enabled."],
"blindspots": ["A downstream sandbox was not exercised."],
"controls": ["Archive uploads require authentication."],
"limitations": ["The exploit was validated statically."],
},
)
reachability = markdown.split("#### Reachability", 1)[1].split("#### Severity", 1)[0]
assert "Assumptions:" in reachability
assert "- Automatic extraction is enabled." in reachability
assert "Existing controls:" in reachability
assert "- Archive uploads require authentication." in reachability
assert "Blind spots:" in reachability
assert "- A downstream sandbox was not exercised." in reachability
assert "Limitations:" in reachability
assert "- The exploit was validated statically." in reachability
def test_projection_preserves_heading_like_source_evidence() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["codeEvidence"] = [
{
"id": "markdown-source",
"label": "Reviewed Markdown source",
"path": "docs/security.md",
"startLine": 10,
"language": "markdown",
"code": "### [2] Legitimate heading inside reviewed source",
"explanation": "The source contains a heading that resembles a report finding.",
}
]
findings["findings"][0]["rootCause"] = {
"summary": "The reviewed source contains security-relevant Markdown.",
"evidenceRefs": ["markdown-source"],
}
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "```markdown\n### [2] Legitimate heading inside reviewed source\n```" in markdown
assert "Reviewed Markdown source" in markdown
def test_projection_renders_string_root_cause() -> None:
markdown = report_with_finding(
rootCause="The authorization check runs after the privileged `write`.",
)
assert (
"#### Root Cause\n\nThe authorization check runs after the privileged `write`." in markdown
)
def test_projection_merges_root_cause_aliases() -> None:
markdown = report_with_finding(
rootCause={"summary": ""},
root_cause={
"summary": "The destination is not contained before the write.",
"code": "destination.write_bytes(payload)",
"language": "python",
},
)
assert "The destination is not contained before the write." in markdown
assert "```python\ndestination.write_bytes(payload)\n```" in markdown
def test_projection_ignores_malformed_root_cause_alias_fields() -> None:
markdown = report_with_finding(
rootCause={"summary": 42},
root_cause={
"summary": "The valid legacy root cause.",
"evidence_refs": ["legacy-root"],
},
code_evidence=[{"id": "legacy-root", "code": "legacy_root()"}],
)
assert "The valid legacy root cause." in markdown
assert "legacy_root()" in markdown
assert "42" not in markdown
def test_projection_merges_scalar_and_list_root_cause_evidence_references() -> None:
markdown = report_with_finding(
rootCause={
"summary": "The source reaches the write.",
"evidenceRefs": ["canonical-root-source"],
},
root_cause={"evidence_refs": "legacy-root-source"},
codeEvidence=[{"id": "canonical-root-source", "code": "canonical_source()"}],
code_evidence=[{"id": "legacy-root-source", "code": "legacy_source()"}],
)
assert "canonical_source()" in markdown
assert "legacy_source()" in markdown
def test_projection_merges_embedded_evidence_across_root_cause_aliases() -> None:
markdown = report_with_finding(
rootCause={
"summary": "The canonical root cause.",
"codeEvidence": [{"id": "canonical-root", "code": "canonical_evidence()"}],
},
root_cause={"codeEvidence": [{"id": "legacy-root", "code": "legacy_evidence()"}]},
)
assert "canonical_evidence()" in markdown
assert "legacy_evidence()" in markdown
def test_projection_treats_whitespace_root_cause_fields_as_unpopulated() -> None:
markdown = report_with_finding(
rootCause={
"summary": " ",
"code": "\t",
"language": "text",
},
root_cause={
"summary": "The destination is not contained before the write.",
"code": "destination.write_bytes(payload)",
"language": "python",
},
)
assert "The destination is not contained before the write." in markdown
assert "```python\ndestination.write_bytes(payload)\n```" in markdown
def test_projection_ignores_malformed_data_flow_alias_values() -> None:
markdown = report_with_finding(
attackPath={
"dataFlow": {"summary": ["invalid canonical value"]},
"dataflow": {"summary": "request -> validated sink"},
},
)
assert "request -\\> validated sink" in markdown
def test_projection_links_detailed_writeup_without_repeating_inline_finding() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["writeup"] = {
"reportPath": "findings/parser-boundary/parser-boundary.md"
}
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "[Open report](findings/parser-boundary/parser-boundary.md)" in markdown
assert "### [1] Parser" in markdown
assert "See the [detailed technical write-up]" in markdown
assert "## Injected remediation" not in markdown
@pytest.mark.parametrize("coverage_mode", ["deep_repository", "scoped_path"])
def test_projection_groups_deep_reports_by_candidate_id(coverage_mode: str) -> None:
manifest, findings, coverage = canonical_documents()
coverage["mode"] = coverage_mode
if coverage_mode == "scoped_path":
coverage["inventoryStrategy"] = "scoped_path"
finding = findings["findings"][0]
finding["title"] = (
"Render clients can reconfigure device-global firmware logging [DSS-079-RGXFWDBG-rogue]"
)
finding["extensions"] = {
"candidateId": "DSS-079",
"ledgerRowId": "R07W01-COV-RGXFWDBG",
"reportId": "DSS-079-RGXFWDBG-rogue",
}
finding["severity"]["level"] = "high"
finding["writeup"] = {"reportPath": "findings/dss-079-rogue/dss-079-rogue.md"}
sibling = copy.deepcopy(finding)
sibling["occurrenceId"] = "occ_2"
sibling["title"] = (
"Render clients can reconfigure device-global firmware logging [DSS-079-RGXFWDBG-volcanic]"
)
sibling["extensions"]["reportId"] = "DSS-079-RGXFWDBG-volcanic"
sibling["severity"]["level"] = "medium"
sibling["writeup"] = {"reportPath": "findings/dss-079-volcanic/dss-079-volcanic.md"}
findings["findings"].append(sibling)
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "| Reportable DSS findings | 1 |" in markdown
assert "This report omits details" not in markdown
assert "| Report instances | 2 |" in markdown
assert "| Findings | Reports | Severity | Confidence | Detailed write-up |" in markdown
assert (
"| Render clients can reconfigure device-global firmware logging "
"| [DSS-079-RGXFWDBG-rogue](#finding-1)"
"<br>[DSS-079-RGXFWDBG-volcanic](#finding-2) | high<br>medium | high "
"| [Open DSS-079-RGXFWDBG-rogue]"
"(findings/dss-079-rogue/dss-079-rogue.md)"
"<br>[Open DSS-079-RGXFWDBG-volcanic]"
"(findings/dss-079-volcanic/dss-079-volcanic.md) |" in markdown
)
assert markdown.count("| Render clients can reconfigure device-global firmware logging |") == 1
assert '<a id="finding-1"></a>' in markdown
assert '<a id="finding-2"></a>' in markdown
def test_projection_moves_legacy_deep_title_annotations_to_reports() -> None:
manifest, findings, coverage = canonical_documents()
coverage["mode"] = "deep_repository"
finding = findings["findings"][0]
finding["title"] = (
"Render clients can dump device-global firmware trace and assertion buffers into "
"PDump output [R07W01-COV-RGXPDUMP-TRACE; COV-PDUMP-004; "
"new-rgx-pdump-trace-authz:rogue-fw]"
)
finding["extensions"] = {
"candidateId": "DSS-145",
"ledgerRowId": "R07W01-COV-RGXPDUMP-TRACE; COV-PDUMP-004",
}
sibling = copy.deepcopy(finding)
sibling["occurrenceId"] = "occ_2"
sibling["title"] = (
"Render clients can dump device-global firmware trace and assertion buffers into "
"PDump output [R07W01-COV-RGXPDUMP-TRACE; COV-PDUMP-004; "
"new-rgx-pdump-trace-authz:rogue-mips]"
)
sibling["identity"] = {"instance": "dss-145-rogue-mips"}
findings["findings"].append(sibling)
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
finding_title = (
"Render clients can dump device-global firmware trace and assertion buffers into "
"PDump output"
)
assert markdown.count(f"| {finding_title} |") == 1
assert (
"[R07W01-COV-RGXPDUMP-TRACE; COV-PDUMP-004; "
"new-rgx-pdump-trace-authz:rogue-fw](#finding-1)" in markdown
)
assert (
"[R07W01-COV-RGXPDUMP-TRACE; COV-PDUMP-004; "
"new-rgx-pdump-trace-authz:rogue-mips](#finding-2)" in markdown
)
def test_projection_keeps_standard_findings_table_unchanged() -> None:
manifest, findings, coverage = canonical_documents()
coverage["mode"] = "scoped_path"
coverage["inventoryStrategy"] = "scoped_path"
finding = findings["findings"][0]
finding["title"] = "Parser boundary [SCAN-001-parser]"
finding["extensions"] = {"ledgerRowId": "SCAN-001-parser"}
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "| Finding | Severity | Confidence | Detailed write-up |" in markdown
assert "| Findings | Reports | Severity | Confidence | Detailed write-up |" not in markdown
assert "| Reportable findings | 1 |" in markdown
assert "| Report instances |" not in markdown
assert "[Parser boundary \\[SCAN-001-parser\\]](#finding-1)" in markdown
def test_projection_rejects_unsafe_detailed_writeup_path() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["writeup"] = {"reportPath": "../outside.md"}
with pytest.raises(PROJECTION.ReportProjectionError, match="invalid reportPath"):
PROJECTION.build_report_markdown(manifest, findings, coverage)
findings["findings"][0]["writeup"] = {"reportPath": "findings/one/two.md"}
with pytest.raises(PROJECTION.ReportProjectionError, match="invalid reportPath"):
PROJECTION.build_report_markdown(manifest, findings, coverage)
def test_projection_rejects_duplicate_detailed_writeup_paths() -> None:
manifest, findings, coverage = canonical_documents()
report_path = "findings/parser-boundary/parser-boundary.md"
findings["findings"][0]["writeup"] = {"reportPath": report_path}
duplicate = copy.deepcopy(findings["findings"][0])
duplicate["title"] = "Second parser boundary"
findings["findings"].append(duplicate)
with pytest.raises(PROJECTION.ReportProjectionError, match="duplicate writeup reportPath"):
PROJECTION.build_report_markdown(manifest, findings, coverage)
duplicate["writeup"] = {"reportPath": "findings/second-boundary/second-boundary.md"}
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert f"[Open report]({report_path})" in markdown
assert "[Open report](findings/second-boundary/second-boundary.md)" in markdown
def test_projection_links_structural_hardening_portfolio() -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["hardening"] = {"portfolioPath": "hardening/hardening.md"}
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "## Structural Hardening" in markdown
assert "[Open the structural hardening portfolio](hardening/hardening.md)" in markdown
assert "do not indicate that any finding has been remediated" in markdown
def test_projection_omits_absent_structural_hardening_portfolio() -> None:
markdown = PROJECTION.generate_report_markdown(*canonical_documents()).decode()
assert "## Structural Hardening" not in markdown
def test_projection_rejects_unsafe_structural_hardening_portfolio() -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["hardening"] = {"portfolioPath": "../hardening.md"}
with pytest.raises(PROJECTION.ReportProjectionError, match="invalid portfolioPath"):
PROJECTION.build_report_markdown(manifest, findings, coverage)
def test_projection_escapes_markdown_link_syntax_in_finding_title() -> None:
markdown = report_with_finding(
title="Parser ](https://example.com) [boundary",
)
assert "[Parser \\](https://example.com) \\[boundary](#finding-1)" in markdown
assert "[Parser ](https://example.com) [boundary](" not in markdown
def test_projection_normalizes_target_and_quotes_scope_control_characters() -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["target"]["displayName"] = "repo\n## Injected target heading"
manifest["scan"]["scope"]["includePaths"] = ["src\n## Injected path heading"]
coverage["includePaths"] = ["src\n## Injected path heading"]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "\n## Injected target heading" not in markdown
assert "\n## Injected path heading" not in markdown
assert "# Security Review: repo ## Injected target heading" in markdown
assert '- Included paths: `"src\\n## Injected path heading"`' in markdown
@pytest.mark.parametrize(
("path", "marker"),
[
(" ", "`"),
(" ", "`"),
(" service ", "`"),
("a,b.ts", "`"),
("a b", "`"),
(" a`b ", "``"),
("a``b,c", "```"),
],
)
def test_projection_preserves_ambiguous_literal_paths(path: str, marker: str) -> None:
manifest, findings, coverage = canonical_documents()
coverage["includePaths"] = [path]
coverage["excludePaths"] = [path]
findings["findings"][0]["locations"] = [{"path": path, "startLine": 1}]
findings["findings"][0]["codeEvidence"] = [
{"id": "source", "label": "Source", "path": path, "startLine": 1, "code": "source()"}
]
findings["findings"][0]["rootCause"] = {"evidenceRefs": ["source"]}
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert f'- Included paths: {marker}"{path}"{marker}' in markdown
assert f'- Excluded paths: {marker}"{path}"{marker}' in markdown
assert f'| Affected lines | {marker}"{path}"{marker}:1 |' in markdown
assert f'{marker}"{path}":1{marker}' in markdown
def test_projection_distinguishes_repeated_spaces_from_ordinary_path_spacing() -> None:
manifest, findings, coverage = canonical_documents()
coverage["includePaths"] = ["a b", "a b", " ", " "]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert '- Included paths: `"a b"`, a b, `" "`, `" "`' in markdown
def test_projection_keeps_printable_non_ascii_in_quoted_paths() -> None:
manifest, findings, coverage = canonical_documents()
coverage["includePaths"] = ["docs/Résumé, final.md", "docs/a\u202eb, c.md"]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert '- Included paths: `"docs/Résumé, final.md"`, `"docs/a\\u202eb, c.md"`' in markdown
def test_projection_includes_exact_target_identity() -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["target"].update(
{
"kind": "git_diff",
"targetId": "repo-1",
"baseRevision": "base-sha",
"headRevision": "head-sha",
"snapshotDigest": "codex-security-snapshot/v1:sha256:" + "a" * 64,
}
)
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "- Target kind: git\\_diff" in markdown
assert "- Target ID: repo-1" in markdown
assert "- Revision range: base-sha...head-sha" in markdown
assert "- Snapshot digest: codex-security-snapshot/v1:sha256:" in markdown
def test_generate_report_markdown_accepts_escaped_pipes_in_metadata() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["confidence"]["rationale"] = "Direct | trace."
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage)
assert "| Confidence rationale | Direct \\| trace. |" in markdown.decode()
def test_projection_treats_escaped_canonical_markdown_as_text() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["title"] = "Parser ](https://example.com) [boundary"
findings["findings"][0]["remediation"] = "[Open report](file:///tmp/report.md)"
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "[Parser ](https://example.com) [boundary](" not in markdown
assert "\\[Open report\\](file:///tmp/report.md)" in markdown
def test_projection_escapes_raw_html_in_markdown() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["summary"] = '<img src="x" onerror="alert(1)">'
markdown_text = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert '<img src="x" onerror="alert(1)">' not in markdown_text
assert r'\<img src="x" onerror="alert(1)"\>' in markdown_text
def test_projection_keeps_html_entities_literal_in_markdown() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["summary"] = "Sanitizer decodes &#x3C;script&#x3E; and &amp;lt;"
coverage["includePaths"] = ["docs/&lt;b&gt;.md"]
markdown_text = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert r"Sanitizer decodes \&#x3C;script\&#x3E; and \&amp;lt;" in markdown_text
assert "- Included paths: `docs/&lt;b&gt;.md`" in markdown_text
@pytest.mark.parametrize(
"url",
[
"https://example.com/?first=1&second=2",
"https://example.test/~docs?version=~1&retry=3",
"http://example.com/?payload=&#x3C;tag&#x3E;&mode=1",
"www.example.com/?payload=&lt;tag&gt;&mode=1",
"WWW.example.com/?first=1&second=2",
"WwW.example.com/?first=1&second=2",
"ftp://example.com/?first=1&second=2",
"https://example.com/`a`?first=1&second=2",
"www.example.com/`a`?first=1&second=2",
"example.com/?first=1&second=2",
"//example.com/?first=1&second=2",
'"example.com/?first=1&second=2"',
"éxample.com/?first=1&second=2",
"例子.com/?first=1&second=2",
"example.com/?payload=&lt;tag&gt;&mode=1",
"example.com/?first=1&word;=2",
"//example.com/?first=1&word;=2",
"user&tag@example.com",
"mailto:user&tag@example.com",
"//localhost:8080/?first=1&word;=2",
"{//example.com/?first=1&word;=2}",
"?example.com/?first=1&word;=2",
"mailto:user&tag@localhost",
],
)
def test_projection_preserves_bare_url_ampersands_in_prose_and_tables(url: str) -> None:
manifest, findings, coverage = canonical_documents()
finding = findings["findings"][0]
finding["summary"] = f"R&D decoded &#60;tag&#62;. Reproduce with {url} then compare &amp;."
finding["confidence"]["rationale"] = f"Request: {url}"
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert f"Reproduce with {url} then compare \\&amp;." in markdown
assert f"| Confidence rationale | Request: {url} |" in markdown
assert r"R&D decoded \&#60;tag\&#62;." in markdown
def test_projection_preserves_inline_code_and_existing_url_escapes() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["summary"] = (
"Compare `&amp;` and `https://example.com/?a=1&b=2 &amp;` with "
"https://example.com/a_b?first=1&second=2 and &amp;. "
"Keep https://example.com/`a _b_` as written."
)
coverage["includePaths"] = [
"docs/www.example.com/&lt;tag&gt;.md",
"docs/`&lt;tag&gt;`.md",
"dir&lt;tag&gt;.example/file.md",
"dir.example/file&lt;tag&gt;.md",
"www.example.com/file&amp;.md",
"`&amp;`",
"`&amp;.md",
"file&amp;`",
]
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "Compare `&amp;` and `https://example.com/?a=1&b=2 &amp;`" in markdown
assert r"https://example.com/a\_b?first=1&second=2 and \&amp;." in markdown
assert "Keep https://example.com/`a _b_` as written." in markdown
assert "- Included paths: `docs/www.example.com/&lt;tag&gt;.md`" in markdown
assert "``docs/`&lt;tag&gt;`.md``" in markdown
assert "`dir&lt;tag&gt;.example/file.md`" in markdown
assert "`dir.example/file&lt;tag&gt;.md`" in markdown
assert "`www.example.com/file&amp;.md`" in markdown
assert "`` `&amp;` ``" in markdown
assert "`` `&amp;.md ``" in markdown
assert "`` file&amp;` ``" in markdown
@pytest.mark.parametrize("status", ["completed", "interrupted"])
def test_projection_explains_informational_only_findings(status: str) -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["status"] = status
findings["findings"][0]["severity"]["level"] = "informational"
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
section = markdown.split("## Findings", 1)[1]
assert "| Reportable findings | 0 |" in markdown
assert "### No reportable findings" in section
assert "This report omits details for 1 informational finding." in section
assert "`findings.json`" in section and "SARIF" in section and "CSV" in section
assert "No findings" not in section
assert ("No vulnerability conclusion can be drawn." in section) == (status == "interrupted")
assert r"Parser \| boundary" not in markdown
def test_deep_projection_counts_omitted_informational_findings() -> None:
manifest, findings, coverage = canonical_documents()
coverage["mode"] = "deep_repository"
reportable = findings["findings"][0]
for index in range(2):
findings["findings"].append(
{
**reportable,
"occurrenceId": f"info-{index}",
"title": f"Informational detail {index}",
"severity": {"level": "informational"},
}
)
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert "| Reportable DSS findings | 1 |" in markdown
assert "This report omits details for 2 informational findings." in markdown
assert r"Parser \| boundary" in markdown
assert "Informational detail" not in markdown
def test_projection_does_not_claim_completed_gates_for_stopped_scan() -> None:
manifest, findings, coverage = canonical_documents()
manifest["scan"]["status"] = "failed"
findings["findings"] = []
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "No findings were retained before the scan stopped." in markdown
assert "survived the canonical discovery, validation" not in markdown
assert "No vulnerability conclusion can be drawn" in markdown
@pytest.mark.parametrize(
"reason",
[
"Validation was deferred because the scan reached its cost limit.",
(
"The configured discovery time limit elapsed before any source review completed. "
"Saved findings and pending review were preserved without a completed reduction."
),
"The parser runtime could not be inspected in this environment.",
],
)
def test_projection_preserves_follow_up_without_classifying_its_reason(reason: str) -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"] = []
coverage["deferred"] = [
{
"id": "candidate-parser",
"reason": reason,
"paths": ["src/parser.py"],
}
]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "No findings were reported." in markdown
assert "This report omits details" not in markdown
assert "## Deferred Requested Work" in markdown
assert "## Open Questions And Follow Up" not in markdown
assert reason in markdown
assert "Review deferred unit candidate-parser" in markdown
assert "Paths: src/parser.py." in markdown
def test_finding_summary_link_uses_stable_markdown_anchor() -> None:
manifest, findings, coverage = canonical_documents()
findings["findings"][0]["title"] = "Missing <tenant_id> check"
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
assert '<a id="finding-1"></a>' in markdown
def test_projection_separates_deferred_work_from_open_questions() -> None:
manifest, findings, coverage = canonical_documents()
coverage["openQuestions"] = [{"question": "Is production authentication enabled?"}]
coverage["deferred"] = [
{
"id": "parser-review",
"reason": "Parser review incomplete.",
"paths": ["src/parser.py"],
"surfaceIds": ["parser-surface"],
}
]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "Is production authentication enabled?" in markdown
assert "Parser review incomplete." in markdown
assert "Review deferred unit parser-review" in markdown
assert "Paths: src/parser.py." in markdown
assert "Surfaces: parser-surface." in markdown
deferred_section, optional_section = markdown.split("## Open Questions And Follow Up")
assert "## Deferred Requested Work" in deferred_section
assert "Parser review incomplete." in deferred_section
assert "Is production authentication enabled?" not in deferred_section
assert "Is production authentication enabled?" in optional_section
assert "Parser review incomplete." not in optional_section
def test_projection_includes_surface_evidence_receipts() -> None:
manifest, findings, coverage = canonical_documents()
coverage["surfaces"] = [
{
"id": "parser-surface",
"label": "Parser",
"disposition": "no_issue_found",
"receiptRefs": ["artifacts/receipts/parser.jsonl"],
"notes": "Reviewed parser entrypoints.",
}
]
markdown = PROJECTION.build_report_markdown(manifest, findings, coverage)
assert "Reviewed parser entrypoints. Evidence: artifacts/receipts/parser.jsonl" in markdown
@pytest.mark.parametrize(
("source_path", "rendered_location"),
[
("app/api/[id]/route.ts", "`app/api/[id]/route.ts:7`"),
("app/api/`id`/route.ts", "``app/api/`id`/route.ts:7``"),
("`route`.ts", "`` `route`.ts:7 ``"),
],
)
def test_projection_preserves_identifier_text_and_code_path_spelling(
source_path: str, rendered_location: str
) -> None:
markdown = report_with_finding(
title="__proto__ pollution",
validation={"evidenceRefs": ["source"]},
codeEvidence=[
{
"id": "source",
"label": "Source control",
"path": source_path,
"startLine": 7,
"code": "handle(request)",
"explanation": "A source-backed operation.",
}
],
)
assert "\\_\\_proto\\_\\_ pollution" in markdown
assert rendered_location in markdown
@pytest.mark.parametrize(
("bounds", "rendered_location"),
[
({"startLine": 37, "endLine": None}, "`src/evidence.py:37`"),
({"startLine": 48, "endLine": 47}, "`src/evidence.py:48`"),
({"startLine": 59, "endLine": "60"}, "`src/evidence.py:59`"),
({"startLine": 37, "endLine": True}, "`src/evidence.py:37`"),
({"startLine": True, "endLine": 4}, "`src/evidence.py`"),
({"startLine": 0}, "`src/evidence.py`"),
({"startLine": -1}, "`src/evidence.py`"),
],
)
def test_projection_normalizes_invalid_legacy_code_evidence_bounds(
bounds: dict[str, object], rendered_location: str
) -> None:
markdown = report_with_finding(
validation={"evidenceRefs": ["source"]},
code_evidence=[
{
"id": "source",
"label": "Source control",
"path": "src/evidence.py",
**bounds,
"code": "handle(request)",
}
],
)
assert rendered_location in markdown