|
17 | 17 |
|
18 | 18 | from __future__ import annotations |
19 | 19 |
|
| 20 | +from urllib.parse import unquote, urlsplit |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
20 | 24 | from skillspector.models import Finding |
21 | 25 | from skillspector.nodes.report import _build_sarif |
22 | 26 | from skillspector.sarif_models import validate_sarif_report |
@@ -218,6 +222,66 @@ def test_suppressed_finding_keeps_properties_and_suppression_marker(self) -> Non |
218 | 222 | assert result["properties"]["intent"] == "exposed_secret" |
219 | 223 |
|
220 | 224 |
|
| 225 | +@pytest.mark.parametrize("suppressed", [False, True]) |
| 226 | +@pytest.mark.parametrize("scoped", [False, True]) |
| 227 | +@pytest.mark.parametrize( |
| 228 | + "filename,encoded", |
| 229 | + [ |
| 230 | + ("scripts/a#b.py", "scripts/a%23b.py"), |
| 231 | + ("scripts/a?b.py", "scripts/a%3Fb.py"), |
| 232 | + ("scripts/a b.py", "scripts/a%20b.py"), |
| 233 | + ("scripts/a%20b.py", "scripts/a%2520b.py"), |
| 234 | + ("scripts/café.py", "scripts/caf%C3%A9.py"), |
| 235 | + ("scripts\\a#b.py", "scripts/a%23b.py"), |
| 236 | + ("outer.zip!/a#b.py", "outer.zip!/a%23b.py"), |
| 237 | + ("scripts/plain.py", "scripts/plain.py"), |
| 238 | + ], |
| 239 | +) |
| 240 | +def test_sarif_artifact_uri_preserves_literal_filename( |
| 241 | + filename: str, encoded: str, suppressed: bool, scoped: bool |
| 242 | +) -> None: |
| 243 | + finding = _make_finding(file=filename) |
| 244 | + prefix = f"external/{'a' * 64}/" if scoped else "" |
| 245 | + if scoped: |
| 246 | + finding.source_identity = prefix.rstrip("/") |
| 247 | + sarif = ( |
| 248 | + _build_sarif([], [SuppressedFinding(finding=finding, reason="reviewed")]) |
| 249 | + if suppressed |
| 250 | + else _build_sarif([finding]) |
| 251 | + ) |
| 252 | + validate_sarif_report(sarif) |
| 253 | + uri = sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["artifactLocation"][ |
| 254 | + "uri" |
| 255 | + ] |
| 256 | + assert uri == prefix + encoded |
| 257 | + parsed = urlsplit(uri) |
| 258 | + assert parsed.query == parsed.fragment == "" |
| 259 | + assert unquote(parsed.path) == prefix + filename.replace("\\", "/") |
| 260 | + assert finding.file == filename |
| 261 | + |
| 262 | + |
| 263 | +def test_sarif_occurrence_uri_preserves_literal_filename() -> None: |
| 264 | + finding = _make_finding() |
| 265 | + finding.occurrences = [{"file": "nested/a#b.py", "start_line": 7}] |
| 266 | + result = _build_sarif([finding])["runs"][0]["results"][0] |
| 267 | + artifact = result["locations"][0]["physicalLocation"]["artifactLocation"] |
| 268 | + assert artifact["uri"] == "nested/a%23b.py" |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.parametrize("category", ["ledger_exceptions", "scope_exclusions"]) |
| 272 | +def test_sarif_notification_uri_preserves_literal_filename(category: str) -> None: |
| 273 | + sarif = _build_sarif( |
| 274 | + [], |
| 275 | + analysis_completeness={ |
| 276 | + category: [{"path": "nested/a#b?.py", "message": "File skipped", "start_line": 1}] |
| 277 | + }, |
| 278 | + ) |
| 279 | + validate_sarif_report(sarif) |
| 280 | + notification = sarif["runs"][0]["invocations"][0]["toolExecutionNotifications"][0] |
| 281 | + artifact = notification["locations"][0]["physicalLocation"]["artifactLocation"] |
| 282 | + assert artifact["uri"] == "nested/a%23b%3F.py" |
| 283 | + |
| 284 | + |
221 | 285 | def test_sarif_transitive_properties_validate() -> None: |
222 | 286 | """Transitive provenance lands in SARIF properties and still validates.""" |
223 | 287 | finding = _make_finding("TR1", "Transitive Dependency") |
|
0 commit comments