Skip to content

Commit 548c9c3

Browse files
authored
Merge pull request #729 from YMuskrat/fix/prevent-input-overwrite
fix(cli): prevent report output from overwriting the input file
2 parents 3708557 + e11148a commit 548c9c3

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎src/skillspector/cli.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,16 @@ def scan(
711711
if not input_path.startswith(("http://", "https://", "git@")):
712712
try:
713713
resolved_path = validate_local_input_path(resolved_path)
714-
except ValueError as e:
714+
if (
715+
output is not None
716+
and resolved_path.is_file()
717+
and output.exists()
718+
and output.samefile(resolved_path)
719+
):
720+
raise ValueError(
721+
"--output points to the input file. Choose a different output path."
722+
)
723+
except (OSError, ValueError) as e:
715724
err_console.print(f"[red]Error:[/red] {e}")
716725
raise typer.Exit(code=2) from e
717726
try:

‎tests/unit/test_cli.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,85 @@ def test_cli_scan_output_to_file(tmp_path: Path) -> None:
333333
assert "out-test" in content or "risk_assessment" in content
334334

335335

336+
@pytest.mark.parametrize("format", list(FormatChoice))
337+
@pytest.mark.parametrize(
338+
"alias", ["same-path", "relative-path", "parent-path", "symlink", "symlink-parent", "hard-link"]
339+
)
340+
def test_cli_scan_rejects_output_alias_of_input_file(
341+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, format: FormatChoice, alias: str
342+
) -> None:
343+
"""An output alias must not replace the original skill with its report."""
344+
source = tmp_path / "SKILL.md"
345+
original = b"---\nname: protected\ndescription: Say hello.\n---\n# Hello\n"
346+
source.write_bytes(original)
347+
output = source
348+
monkeypatch.chdir(tmp_path)
349+
if alias == "relative-path":
350+
output = Path("SKILL.md")
351+
elif alias == "parent-path":
352+
nested = tmp_path / "nested"
353+
nested.mkdir()
354+
output = nested / ".." / source.name
355+
elif alias == "symlink":
356+
output = tmp_path / "report.txt"
357+
try:
358+
output.symlink_to(source)
359+
except OSError:
360+
pytest.skip("symlinks are not supported on this filesystem")
361+
elif alias == "symlink-parent":
362+
linked = tmp_path / "linked"
363+
try:
364+
linked.symlink_to(tmp_path, target_is_directory=True)
365+
except OSError:
366+
pytest.skip("symlinks are not supported on this filesystem")
367+
output = linked / source.name
368+
elif alias == "hard-link":
369+
output = tmp_path / "report.txt"
370+
try:
371+
os.link(source, output)
372+
except OSError:
373+
pytest.skip("hard links are not supported on this filesystem")
374+
scan_skill = MagicMock(return_value={"report_body": "Scan report", "risk_score": 0})
375+
monkeypatch.setattr(cli, "_scan_skill", scan_skill)
376+
377+
result = runner.invoke(
378+
app, ["scan", str(source), "--no-llm", "--format", format.value, "--output", str(output)]
379+
)
380+
381+
assert result.exit_code == 2
382+
assert "--output points to the input file" in result.output
383+
assert source.read_bytes() == original
384+
assert output.read_bytes() == original
385+
scan_skill.assert_not_called()
386+
387+
388+
@pytest.mark.parametrize("format", list(FormatChoice))
389+
@pytest.mark.parametrize("destination", ["new", "existing", "same-name"])
390+
def test_cli_scan_preserves_input_when_writing_a_separate_report(
391+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, format: FormatChoice, destination: str
392+
) -> None:
393+
"""Separate reports still work, including existing files and matching names."""
394+
source = tmp_path / "SKILL.md"
395+
original = b"# Original skill\n"
396+
source.write_bytes(original)
397+
reports = tmp_path / "reports"
398+
reports.mkdir()
399+
output = reports / (source.name if destination == "same-name" else "report.txt")
400+
if destination == "existing":
401+
output.write_text("Previous report", encoding="utf-8")
402+
scan_skill = MagicMock(return_value={"report_body": "Scan report", "risk_score": 0})
403+
monkeypatch.setattr(cli, "_scan_skill", scan_skill)
404+
405+
result = runner.invoke(
406+
app, ["scan", str(source), "--no-llm", "--format", format.value, "--output", str(output)]
407+
)
408+
409+
assert result.exit_code == 0
410+
assert source.read_bytes() == original
411+
assert output.read_text(encoding="utf-8") == "Scan report"
412+
scan_skill.assert_called_once()
413+
414+
336415
def test_cli_scan_no_llm(tmp_path: Path) -> None:
337416
"""scan with --no-llm runs without requiring an LLM API key (uses fallback)."""
338417
(tmp_path / "SKILL.md").write_text("# No LLM test", encoding="utf-8")

0 commit comments

Comments
 (0)