|
46 | 46 | from skillspector import __version__, transitive |
47 | 47 | from skillspector.cleanup import TempDirTracker, cleanup_result |
48 | 48 | from skillspector.constants import RISK_THRESHOLD |
| 49 | +from skillspector.file_output import require_secure_file_output, write_text_no_follow |
49 | 50 | from skillspector.graph_proxy import graph |
50 | 51 | from skillspector.input_handler import validate_local_input_path |
51 | 52 | from skillspector.inspection_ledger import ( |
|
83 | 84 | dump_baseline, |
84 | 85 | effective_findings, |
85 | 86 | load_baseline, |
| 87 | + serialize_baseline, |
86 | 88 | source_content_key, |
87 | 89 | ) |
88 | 90 | from skillspector.terminal_text import visible_terminal_text as _visible_terminal_text |
@@ -373,7 +375,7 @@ def _write_result( |
373 | 375 | """Write report_body to file or stdout. Uses sarif_report if report_body missing.""" |
374 | 376 | report_body = _result_body(result) |
375 | 377 | if output: |
376 | | - Path(output).write_text(report_body, encoding="utf-8") |
| 378 | + write_text_no_follow(output, report_body) |
377 | 379 | if format == FormatChoice.terminal: |
378 | 380 | console.print(f"\n[green]Report saved to:[/green] {output}") |
379 | 381 | else: |
@@ -690,6 +692,13 @@ def scan( |
690 | 692 | ) |
691 | 693 | raise typer.Exit(code=2) |
692 | 694 |
|
| 695 | + if output is not None: |
| 696 | + try: |
| 697 | + require_secure_file_output() |
| 698 | + except ValueError as exc: |
| 699 | + err_console.print(f"[red]Error:[/red] {exc}") |
| 700 | + raise typer.Exit(code=2) from exc |
| 701 | + |
693 | 702 | if mcp_registry: |
694 | 703 | if ( |
695 | 704 | recursive |
@@ -720,7 +729,7 @@ def scan( |
720 | 729 | ) |
721 | 730 | report = json.dumps(result, indent=2) |
722 | 731 | if output: |
723 | | - output.write_text(report, encoding="utf-8") |
| 732 | + write_text_no_follow(output, report) |
724 | 733 | console.print(f"Report saved to: {output}") |
725 | 734 | else: |
726 | 735 | print(report) |
@@ -3276,7 +3285,7 @@ def _scan_multi_skill( |
3276 | 3285 | rendered = json.dumps(combined, indent=2) |
3277 | 3286 | _ensure_recursive_output_bound(rendered) |
3278 | 3287 | if output is not None: |
3279 | | - Path(output).write_text(rendered, encoding="utf-8") |
| 3288 | + write_text_no_follow(output, rendered) |
3280 | 3289 | progress_console.print(f"[green]Combined report saved to:[/green] {output}") |
3281 | 3290 | else: |
3282 | 3291 | sys.stdout.write(rendered) |
@@ -3304,7 +3313,7 @@ def _scan_multi_skill( |
3304 | 3313 | rendered = json.dumps(merged_sarif, indent=2) |
3305 | 3314 | _ensure_recursive_output_bound(rendered) |
3306 | 3315 | if output is not None: |
3307 | | - Path(output).write_text(rendered, encoding="utf-8") |
| 3316 | + write_text_no_follow(output, rendered) |
3308 | 3317 | progress_console.print(f"[green]Combined report saved to:[/green] {output}") |
3309 | 3318 | else: |
3310 | 3319 | sys.stdout.write(rendered) |
@@ -3337,7 +3346,7 @@ def _scan_multi_skill( |
3337 | 3346 | ) |
3338 | 3347 | _ensure_recursive_output_bound(rendered) |
3339 | 3348 | if output is not None: |
3340 | | - Path(output).write_text(rendered, encoding="utf-8") |
| 3349 | + write_text_no_follow(output, rendered) |
3341 | 3350 | progress_console.print(f"[green]Combined report saved to:[/green] {output}") |
3342 | 3351 | elif format is FormatChoice.terminal: |
3343 | 3352 | console.print(rendered, markup=False, emoji=False) |
@@ -3418,7 +3427,7 @@ def baseline( |
3418 | 3427 | typer.Option( |
3419 | 3428 | "--output", |
3420 | 3429 | "-o", |
3421 | | - help="Where to write the baseline file (YAML; .json extension writes JSON).", |
| 3430 | + help="Baseline file (YAML; .json writes JSON), or - for JSON on stdout.", |
3422 | 3431 | ), |
3423 | 3432 | ] = Path(".skillspector-baseline.yaml"), |
3424 | 3433 | no_llm: Annotated[ |
@@ -3455,12 +3464,16 @@ def baseline( |
3455 | 3464 | _exit_on_unsupported_llm_controls(no_llm) |
3456 | 3465 | result = None |
3457 | 3466 | try: |
| 3467 | + to_stdout = str(output) == "-" |
| 3468 | + if not to_stdout: |
| 3469 | + require_secure_file_output() |
3458 | 3470 | if verbose: |
3459 | 3471 | set_level("DEBUG") |
3460 | | - console.print("[dim]Scanning to build baseline...[/dim]") |
| 3472 | + err_console.print("[dim]Scanning to build baseline...[/dim]") |
3461 | 3473 | # output_format is irrelevant here; we consume findings, not report_body. |
3462 | 3474 | state = _scan_state(input_path, FormatChoice.json, no_llm) |
3463 | | - state["baseline_path"] = os.path.abspath(output.expanduser()) |
| 3475 | + if not to_stdout: |
| 3476 | + state["baseline_path"] = os.path.abspath(output.expanduser()) |
3464 | 3477 | result = graph.invoke(state) |
3465 | 3478 | completeness_value = result.get("analysis_completeness") |
3466 | 3479 | completeness = completeness_value if isinstance(completeness_value, dict) else {} |
@@ -3491,10 +3504,13 @@ def baseline( |
3491 | 3504 | file_cache=result.get("local_file_cache") or result.get("file_cache") or {}, |
3492 | 3505 | scanner_version=__version__, |
3493 | 3506 | ) |
3494 | | - dump_baseline(data, output) |
3495 | | - console.print( |
3496 | | - f"[green]Wrote baseline with {len(findings)} suppressed finding(s) to:[/green] {output}" |
3497 | | - ) |
| 3507 | + if to_stdout: |
| 3508 | + sys.stdout.write(serialize_baseline(data, json_output=True) + "\n") |
| 3509 | + else: |
| 3510 | + dump_baseline(data, output) |
| 3511 | + console.print( |
| 3512 | + f"[green]Wrote baseline with {len(findings)} suppressed finding(s) to:[/green] {output}" |
| 3513 | + ) |
3498 | 3514 | except typer.Exit: |
3499 | 3515 | raise |
3500 | 3516 | except (FileNotFoundError, ValueError) as e: |
|
0 commit comments