diff --git a/README.md b/README.md index 721949da..e8c7d8f8 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,23 @@ if outcome.status != "COMPLETED": ## Review exports +Use `review_sarif` to write a retained pull-request review as SARIF 2.1.0 for CI and code scanning. + +```python +import json +from pathlib import Path + +from proofline import ProoflineClient, review_sarif + +with ProoflineClient() as client: + outcome = client.wait_for_review(organization_id, review_id) + +report = review_sarif(outcome, organization_id) +Path("proofline.sarif").write_text(json.dumps(report), encoding="utf-8") +``` + +The review must record a full head SHA. Findings include file locations when their citations have verified provenance at that head. Check `runs[0].invocations[0]` for execution success and assessment gaps, including gaps in a CODE assessment alongside completed environment assessments. + `export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`. ```python diff --git a/proofline/__init__.py b/proofline/__init__.py index 689b454d..8d45043c 100644 --- a/proofline/__init__.py +++ b/proofline/__init__.py @@ -37,6 +37,7 @@ from .exports import ExportDataset, ExportFormat, ExportPage from .knowledge import KnowledgeResult from .outcomes import AssessmentGap, CheckGap, ReviewOutcome +from .sarif import review_sarif __all__ = [ "AgentSession", @@ -80,4 +81,5 @@ "UploadedReport", "__version__", "models", + "review_sarif", ] diff --git a/proofline/outcomes.py b/proofline/outcomes.py index fa363b32..e25cd718 100644 --- a/proofline/outcomes.py +++ b/proofline/outcomes.py @@ -77,9 +77,6 @@ def _optional_texts(value: object) -> bool: def _code_assessment(review: CustomerReviewDetail) -> CustomerCodeAssessment | None: - # A CODE result cannot replace a missing ENV result or its check receipts. - if review.assessment_count != 0 or review.assessments: - return None code = review.code_assessment if ( not isinstance(code, CustomerCodeAssessment) @@ -103,6 +100,18 @@ def _code_assessment(review: CustomerReviewDetail) -> CustomerCodeAssessment | N return code +def _code_gap(code: CustomerCodeAssessment) -> AssessmentGap | None: + kind = text(code.incompleteness_kind) + reasons = tuple(code.reason_codes) if isinstance(code.reason_codes, list) else () + if kind: + reasons = (*reasons, kind) + if code.status == "COMPLETED" and not reasons: + return None + return AssessmentGap( + code.id, code.status, reasons or ("CODE_ASSESSMENT_STATE_UNAVAILABLE",), kind + ) + + def _validate(review: CustomerReviewDetail, review_id: UUID | None) -> None: if ( not isinstance(review.id, UUID) @@ -166,23 +175,13 @@ def outcome( ) _validate(review, review_id) code_only = review.assessment_count == 0 and not review.assessments - code = _code_assessment(review) if code_only: + # A CODE result cannot replace a missing ENV result or its check receipts. + code = _code_assessment(review) if code is None: gaps.append(AssessmentGap(None, "UNKNOWN", ("CODE_ASSESSMENT_UNAVAILABLE",))) - elif code.status != "COMPLETED" or text(code.incompleteness_kind) or code.reason_codes: - kind = text(code.incompleteness_kind) - reasons = tuple(code.reason_codes) if isinstance(code.reason_codes, list) else () - if kind: - reasons = (*reasons, kind) - gaps.append( - AssessmentGap( - code.id, - code.status, - reasons or ("CODE_ASSESSMENT_STATE_UNAVAILABLE",), - kind, - ) - ) + elif (gap := _code_gap(code)) is not None: + gaps.append(gap) for assessment in review.assessments: reasons = ( tuple(assessment.reason_codes) if isinstance(assessment.reason_codes, list) else () diff --git a/proofline/sarif.py b/proofline/sarif.py new file mode 100644 index 00000000..09159d49 --- /dev/null +++ b/proofline/sarif.py @@ -0,0 +1,317 @@ +"""Project a retained pull-request review into the Proofline CLI's SARIF format. + +The projection follows https://github.com/prooflinedev/cli/blob/main/src/sarif.rs. +""" + +from __future__ import annotations + +import re +from typing import Any, TypeVar +from urllib.parse import quote +from uuid import UUID + +from ._generated.models import CustomerEvidenceRef, CustomerFinding +from ._generated.types import Unset +from ._transport import VERSION +from .outcomes import ReviewOutcome, _code_assessment, _code_gap +from .outcomes import outcome as recorded_outcome + +_SCHEMA = ( + "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" +) +# Rule indexes follow the CLI's public domain catalogs. +_ENV_DOMAINS = ( + "artifact_identity", + "deployment_workflow", + "runtime_configuration", + "network_reachability", + "iam_permissions", + "datastore_compatibility", + "resource_capacity", + "rollout_topology", + "runtime_architecture", + "environment_binding", + "dependency_reachability", + "data_integrity", + "external_side_effects", + "application_security", + "scalability", + "availability_resilience", + "observability", + "source_coverage", +) +_CODE_DOMAINS = ( + "logic", + "edge_case", + "error_handling", + "state_transition", + "concurrency", + "resource_lifecycle", + "credential_exposure", + "authorization", + "injection", + "test_quality", + "api_contract", + "prompt_instruction", + "doc_drift", + "ci_workflow", + "performance", + "maintainability", + "test_marker", + "removed_assertion", + "debug_residue", + "committed_secret", +) +_FULL_SHA = re.compile(r"(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})") +_INVALID_PATH_CHAR = re.compile(r"[\x00-\x1f\x7f-\x9f\\:\u200e\u200f\u202a-\u202e\u2066-\u2069]") +_MESSAGE_ESCAPES = str.maketrans({"{": "{{", "}": "}}", "\\": "\\\\", "[": "\\[", "]": "\\]"}) +_HEAD_GAPS = {"HEAD_COMMIT_MISMATCH", "HEAD_COMMIT_UNAVAILABLE"} +_LEVELS = {"SERIOUS": "error", "WARNING": "warning"} + + +T = TypeVar("T") + + +def _value(value: T | Unset) -> T | None: + return None if isinstance(value, Unset) else value + + +def _location(ref: CustomerEvidenceRef, head: str) -> tuple[str, tuple[str, int, int] | None]: + if ref.kind == "companion_repository_file" or isinstance(ref.companion_repository, str): + return "COMPANION_REPOSITORY", None + if ref.kind != "repository_file": + return "NON_CODE_REFERENCE", None + if ref.provenance_status != "VERIFIED_REPOSITORY_READ": + return "UNVERIFIED_PROVENANCE", None + commit = ref.commit_sha + if not isinstance(commit, str): + return "COMMIT_UNAVAILABLE", None + if not _FULL_SHA.fullmatch(commit): + return "INVALID_COMMIT", None + if commit.lower() != head: + return "DIFFERENT_COMMIT", None + path = ref.repository_path + if ( + not isinstance(path, str) + or _INVALID_PATH_CHAR.search(path) + or any(part in ("", ".", "..") for part in path.split("/")) + ): + return "INVALID_PATH", None + start = ref.start_line + if type(start) is not int: + return "INVALID_REGION", None + end = _value(ref.end_line) + if end is None: + end = start + if type(end) is not int or not 1 <= start <= end <= 2**32 - 1: + return "INVALID_REGION", None + return "LOCATED", (path, start, end) + + +def _message(text: str) -> dict[str, str]: + return {"text": text.translate(_MESSAGE_ESCAPES)} + + +def _finding_result( + finding: CustomerFinding, assessment_id: UUID, environment: str | None, head: str +) -> dict[str, Any]: + level = _LEVELS.get(finding.severity) + if level is None: + raise ValueError(f"Finding {finding.id} has an unsupported SARIF severity.") + code = environment is None + domains = _CODE_DOMAINS if code else _ENV_DOMAINS + if finding.risk_domain not in domains: + raise ValueError(f"Finding {finding.id} has an unsupported SARIF risk domain.") + rule_index = domains.index(finding.risk_domain) + (len(_ENV_DOMAINS) if code else 0) + locations = [] + seen = set() + references = [] + for ref in finding.evidence_refs: + status, location = _location(ref, head) + references.append( + { + "kind": ref.kind, + "provenanceStatus": ref.provenance_status, + "commitSha": _value(ref.commit_sha), + "companionRepository": _value(ref.companion_repository), + "repositoryPath": _value(ref.repository_path), + "startLine": _value(ref.start_line), + "endLine": _value(ref.end_line), + "locationStatus": status, + } + ) + if location is None or location in seen: + continue + seen.add(location) + path, start, end = location + locations.append( + { + "physicalLocation": { + "artifactLocation": {"uri": quote(path, safe="/"), "uriBaseId": "%SRCROOT%"}, + "region": {"startLine": start, "endLine": end}, + }, + } + ) + + properties = { + "findingId": str(finding.id), + "assessmentId": str(assessment_id), + "lane": "CODE" if code else "ENV", + "lifecycleState": finding.lifecycle_state, + "locationStatus": "LOCATED" if locations else "NO_VERIFIED_REVIEWED_LOCATION", + "evidenceReferences": references, + } + if environment is not None: + properties["environment"] = environment + if isinstance(finding.remediation, str): + properties["remediation"] = finding.remediation + + message = f"{finding.claim}\n\n{finding.impact}" + result = { + "ruleId": finding.risk_domain, + "ruleIndex": rule_index, + "level": level, + "message": _message(f"Code-only finding\n\n{message}" if code else message), + "locations": locations[:1], + "properties": properties, + } + if isinstance(finding.finding_fingerprint, str): + result["partialFingerprints"] = {"prooflineFinding/v1": finding.finding_fingerprint} + if len(locations) > 1: + result["relatedLocations"] = locations[1:] + return result + + +def _notification(subject: str, reason: str, assessment_id: UUID | None = None) -> dict[str, Any]: + properties = {"reasonCode": reason} + if assessment_id is not None: + properties["assessmentId"] = str(assessment_id) + return { + "level": "error", + "message": _message(f"{subject}: {reason}"), + "properties": properties, + } + + +def review_sarif(outcome: ReviewOutcome, organization_id: UUID | str) -> dict[str, Any]: + """Export a retained pull-request review as a JSON-serializable SARIF 2.1.0 report. + + Raises ValueError if the outcome has no pull-request review with a full, + matching head SHA, or a finding uses an unsupported severity or risk domain. + """ + review = outcome.review + if review is None or review.kind != "PULL_REQUEST": + raise ValueError("SARIF export requires a retained pull-request review.") + head = review.head_commit_sha + if ( + not isinstance(head, str) + or not _FULL_SHA.fullmatch(head) + or any(_HEAD_GAPS.intersection(gap.reason_codes) for gap in outcome.gaps) + ): + raise ValueError("SARIF export requires a full SHA for the reviewed pull-request head.") + head = head.lower() + + # Reuse the SDK's assessment checks even for outcomes constructed directly. + recorded = recorded_outcome(review, outcome.review_url) + checked = recorded or recorded_outcome(review, outcome.review_url, "TIMED_OUT") + assert checked is not None + state = recorded.status if recorded else None + if outcome.status == "TIMED_OUT" and state == "COMPLETED": + state = "INCOMPLETE" + elif outcome.status not in ("COMPLETED", "TIMED_OUT"): + state = outcome.status + + results: list[dict[str, Any]] = [] + notifications: list[dict[str, Any]] = [] + assessments: list[dict[str, Any]] = [] + rules: tuple[str, ...] = _ENV_DOMAINS + properties: dict[str, Any] = { + "reviewId": str(review.id), + "organizationId": str(UUID(str(organization_id))), + "reviewUrl": outcome.review_url, + "headCommitSha": head, + "repository": _value(review.repository_full_name), + "pullRequestNumber": _value(review.pull_request_number), + "runNumber": _value(review.run_number), + "reviewStatus": review.status, + "outcome": state, + "assessmentCount": review.assessment_count, + "assessments": assessments, + } + code = _code_assessment(review) + if code is not None: + rules = (*rules, *_CODE_DOMAINS) + results.extend(_finding_result(finding, code.id, None, head) for finding in code.findings) + gap = _code_gap(code) + reasons = list(gap.reason_codes) if gap else [] + notifications.extend(_notification("Code-only", reason, code.id) for reason in reasons) + properties["codeAssessment"] = { + "id": str(code.id), + "status": code.status, + "baseCommitSha": code.base_commit_sha, + "headCommitSha": code.head_commit_sha, + "gapReasons": reasons, + } + + for assessment in review.assessments: + environment = assessment.environment_display_name + results.extend( + _finding_result(finding, assessment.id, environment, head) + for finding in assessment.findings + ) + reasons = sorted( + { + reason + for gap in checked.gaps + if gap.assessment_id == assessment.id + for reason in gap.reason_codes + } + ) + notifications.extend( + _notification(environment, reason, assessment.id) for reason in reasons + ) + assessments.append( + { + "id": str(assessment.id), + "environment": environment, + "status": assessment.status, + "gapReasons": reasons, + } + ) + + if state != "COMPLETED": + reason = { + None: "REVIEW_IN_PROGRESS", + "FAILED": "REVIEW_FAILED", + "STOPPED": "REVIEW_STOPPED", + }.get(state, "REVIEW_RESULT_INCOMPLETE") + notifications.append(_notification(f"Review {review.id}", reason)) + if code is None and ( + _value(review.code_assessment) is not None + or (review.assessment_count == 0 and not review.assessments) + ): + notifications.append(_notification("Code-only", "CODE_ASSESSMENT_UNAVAILABLE")) + + return { + "$schema": _SCHEMA, + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "Proofline", + "version": VERSION, + "rules": [{"id": rule} for rule in rules], + } + }, + "invocations": [ + { + "executionSuccessful": state == "COMPLETED" and not notifications, + "toolExecutionNotifications": notifications, + } + ], + "results": results, + "properties": properties, + } + ], + } diff --git a/tests/test_sarif.py b/tests/test_sarif.py new file mode 100644 index 00000000..7fb5e2d6 --- /dev/null +++ b/tests/test_sarif.py @@ -0,0 +1,163 @@ +import json + +import pytest + +from proofline import CustomerReviewDetail, ReviewOutcome, review_sarif +from proofline.outcomes import outcome +from tests.fixtures import ORG, assessment, code_assessment, finding, review + +HEAD = "a" * 40 +URL = "https://proofline.sh/reviews/example" + + +def test_sarif_projects_both_lanes_and_only_verified_reviewed_locations() -> None: + citation = { + "kind": "repository_file", + "label": "private citation label", + "provenance_status": "VERIFIED_REPOSITORY_READ", + "commit_sha": HEAD.upper(), + "repository_path": "src/a #%.py", + "start_line": 3, + } + env = assessment() + env["findings"] = [ + { + **finding(), + "severity": "SERIOUS", + "risk_domain": "data_integrity", + "claim": "[claim] {0}", + "finding_fingerprint": "retained", + "evidence_refs": [ + citation, + citation, + {**citation, "repository_path": "src/b.py", "end_line": 8}, + {**citation, "commit_sha": "b" * 40}, + {**citation, "repository_path": "../outside.py"}, + {**citation, "end_line": 2}, + {**citation, "companion_repository": "owner/other"}, + {**citation, "provenance_status": "LEGACY_UNSTRUCTURED"}, + ], + } + ] + code = code_assessment("INCOMPLETE") + code["incompleteness_kind"] = "MODEL_BUDGET_EXHAUSTED" + code["findings"] = [{**finding(), "severity": "WARNING", "risk_domain": "logic"}] + detail = CustomerReviewDetail.from_dict( + { + **review(), + "kind": "PULL_REQUEST", + "head_commit_sha": HEAD.upper(), + "assessments": [env], + "code_assessment": code, + } + ) + result = outcome(detail, URL) + assert result is not None and result.status == "COMPLETED" + report = review_sarif(result, ORG) + assert report["version"] == "2.1.0" + assert "private citation label" not in json.dumps(report) + run = report["runs"][0] + assert run["properties"]["organizationId"] == str(ORG) + assert run["properties"]["headCommitSha"] == HEAD + code_result, env_result = run["results"] + assert code_result["locations"] == [] + assert code_result["properties"]["locationStatus"] == "NO_VERIFIED_REVIEWED_LOCATION" + assert "partialFingerprints" not in code_result + assert env_result["partialFingerprints"] == {"prooflineFinding/v1": "retained"} + assert env_result["message"]["text"].startswith(r"\[claim\] {{0}}") + for item in run["results"]: + assert run["tool"]["driver"]["rules"][item["ruleIndex"]]["id"] == item["ruleId"] + assert env_result["locations"] == [ + { + "physicalLocation": { + "artifactLocation": {"uri": "src/a%20%23%25.py", "uriBaseId": "%SRCROOT%"}, + "region": {"startLine": 3, "endLine": 3}, + }, + } + ] + assert len(env_result["relatedLocations"]) == 1 + assert [ref["locationStatus"] for ref in env_result["properties"]["evidenceReferences"]] == [ + "LOCATED", + "LOCATED", + "LOCATED", + "DIFFERENT_COMMIT", + "INVALID_PATH", + "INVALID_REGION", + "COMPANION_REPOSITORY", + "UNVERIFIED_PROVENANCE", + ] + assert run["invocations"] == [ + { + "executionSuccessful": False, + "toolExecutionNotifications": [ + { + "level": "error", + "message": {"text": "Code-only: MODEL_BUDGET_EXHAUSTED"}, + "properties": { + "reasonCode": "MODEL_BUDGET_EXHAUSTED", + "assessmentId": code["id"], + }, + } + ], + } + ] + + +def test_sarif_reports_timeout_and_receipt_gap() -> None: + env = assessment() + env["checks"]["receipt_recorded"] = False + detail = CustomerReviewDetail.from_dict( + {**review("RUNNING"), "kind": "PULL_REQUEST", "head_commit_sha": HEAD, "assessments": [env]} + ) + result = outcome(detail, URL, "TIMED_OUT") + assert result is not None + run = review_sarif(result, str(ORG))["runs"][0] + assert run["properties"]["outcome"] is None + invocation = run["invocations"][0] + assert invocation["executionSuccessful"] is False + assert [notice["properties"] for notice in invocation["toolExecutionNotifications"]] == [ + {"reasonCode": "CHECK_RECEIPT_ABSENT", "assessmentId": env["id"]}, + {"reasonCode": "REVIEW_IN_PROGRESS"}, + ] + + +def test_sarif_requires_a_retained_pr_at_the_expected_full_head() -> None: + with pytest.raises(ValueError, match="retained pull-request"): + review_sarif(ReviewOutcome("TIMED_OUT", URL), ORG) + detail = CustomerReviewDetail.from_dict({**review(), "head_commit_sha": HEAD}) + with pytest.raises(ValueError, match="retained pull-request"): + review_sarif(ReviewOutcome("COMPLETED", URL, detail), ORG) + detail.kind = "PULL_REQUEST" + result = outcome(detail, URL, head_commit_sha="b" * 40) + assert result is not None + with pytest.raises(ValueError, match="full SHA"): + review_sarif(result, ORG) + detail.head_commit_sha = HEAD[:7] + with pytest.raises(ValueError, match="full SHA"): + review_sarif(ReviewOutcome("COMPLETED", URL, detail), ORG) + + +def test_sarif_marks_a_complete_run_successful_and_excludes_unavailable_code() -> None: + raw = {**review(), "kind": "PULL_REQUEST", "head_commit_sha": HEAD} + detail = CustomerReviewDetail.from_dict(raw) + result = outcome(detail, URL) + assert result is not None + run = review_sarif(result, ORG)["runs"][0] + assert run["invocations"] == [ + {"executionSuccessful": True, "toolExecutionNotifications": []}, + ] + + code = code_assessment() + code["head_commit_sha"] = "b" * 40 + code["findings"] = [{**finding(), "severity": "WARNING", "risk_domain": "logic"}] + detail = CustomerReviewDetail.from_dict({**raw, "code_assessment": code}) + result = outcome(detail, URL) + assert result is not None and result.status == "COMPLETED" + run = review_sarif(result, ORG)["runs"][0] + assert run["results"] == [] + assert "codeAssessment" not in run["properties"] + invocation = run["invocations"][0] + assert invocation["executionSuccessful"] is False + assert invocation["toolExecutionNotifications"][0]["properties"] == { + "reasonCode": "CODE_ASSESSMENT_UNAVAILABLE", + }