Skip to content

Commit b333136

Browse files
authored
fix(sarif): encode literal filenames in artifact URIs
Percent-encode literal result and notification filenames while preserving path and archive separators. Signed-off-by: YMuskrat <[email protected]>
1 parent b6f0304 commit b333136

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎src/skillspector/nodes/report.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from hashlib import sha256
3030
from io import StringIO
3131
from typing import Literal, cast
32+
from urllib.parse import quote
3233

3334
from rich.console import Console
3435
from rich.markup import escape
@@ -207,7 +208,7 @@ def _sarif_artifact_location(
207208
if source_identity:
208209
uri = f"{source_identity}/{file_path}"
209210
else:
210-
uri = str(occurrence.get("file", finding.file))
211+
uri = str(occurrence.get("file", finding.file)).replace("\\", "/")
211212
properties = {
212213
{
213214
"source_identity": "sourceIdentity",
@@ -217,7 +218,7 @@ def _sarif_artifact_location(
217218
}[key]: value
218219
for key, value in provenance.items()
219220
}
220-
return SarifArtifactLocation(uri=uri, properties=properties or None)
221+
return SarifArtifactLocation(uri=quote(uri, safe="/!"), properties=properties or None)
221222

222223

223224
def _occurrence_columns(
@@ -854,7 +855,9 @@ def notification_from_exception(
854855
locations = [
855856
SarifLocation(
856857
physicalLocation=SarifPhysicalLocation(
857-
artifactLocation=SarifArtifactLocation(uri=path),
858+
artifactLocation=SarifArtifactLocation(
859+
uri=quote(path.replace("\\", "/"), safe="/!")
860+
),
858861
region=region,
859862
)
860863
)

‎tests/nodes/test_sarif_rules_and_empty_findings.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
from __future__ import annotations
1919

20+
from urllib.parse import unquote, urlsplit
21+
22+
import pytest
23+
2024
from skillspector.models import Finding
2125
from skillspector.nodes.report import _build_sarif
2226
from skillspector.sarif_models import validate_sarif_report
@@ -218,6 +222,66 @@ def test_suppressed_finding_keeps_properties_and_suppression_marker(self) -> Non
218222
assert result["properties"]["intent"] == "exposed_secret"
219223

220224

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+
221285
def test_sarif_transitive_properties_validate() -> None:
222286
"""Transitive provenance lands in SARIF properties and still validates."""
223287
finding = _make_finding("TR1", "Transitive Dependency")

0 commit comments

Comments
 (0)