-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcli.py
More file actions
652 lines (578 loc) · 22.6 KB
/
Copy pathcli.py
File metadata and controls
652 lines (578 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""CLI for Skillspector — thin wrapper over the LangGraph workflow.
Maps CLI args to initial state, invokes the graph, then maps result to output and exit code.
No business logic; workflow lives in the graph.
"""
from __future__ import annotations
import json
import os
import sys
from enum import StrEnum
from pathlib import Path
from typing import Annotated, cast
import typer
from langchain_core.runnables import RunnableConfig
from rich.console import Console
from skillspector import __version__
from skillspector.cleanup import cleanup_result
from skillspector.constants import RISK_THRESHOLD
from skillspector.graph import graph
from skillspector.logging_config import get_logger, set_level
from skillspector.mcp_registry import scan_registry
from skillspector.multi_skill import MultiSkillDetectionResult, detect_skills
from skillspector.suppression import build_baseline_dict, dump_baseline, load_baseline
logger = get_logger(__name__)
def _ensure_utf8_streams() -> None:
"""Reconfigure stdout/stderr to UTF-8 so Unicode report output does not crash.
On Windows the default console encoding (e.g. cp1252) cannot encode the
box-drawing characters and icons used in the terminal report, which raises
UnicodeEncodeError. Reconfiguring with errors="replace" makes output robust
across platforms without crashing.
"""
for stream in (sys.stdout, sys.stderr):
reconfigure = getattr(stream, "reconfigure", None)
if reconfigure is not None:
try:
reconfigure(encoding="utf-8", errors="replace")
except (ValueError, OSError):
logger.debug("Could not reconfigure %s to UTF-8", stream)
_ensure_utf8_streams()
app = typer.Typer(
name="skillspector",
help="Security scanner for AI agent skills (LangGraph). Detect vulnerabilities before installation.",
add_completion=False,
no_args_is_help=True,
)
console = Console()
# Fatal errors go to stderr. Anything driving the CLI from a script separates the two streams,
# and with the message on stdout the only diagnosis available was thrown away: a failed scan
# left an empty error log and the caller had nothing to act on.
err_console = Console(stderr=True)
class FormatChoice(StrEnum):
"""Output format choices for the CLI."""
terminal = "terminal"
json = "json"
markdown = "markdown"
sarif = "sarif"
class TransportChoice(StrEnum):
"""Transport choices for the MCP server."""
stdio = "stdio"
http = "http"
def version_callback(value: bool) -> None:
"""Print version and exit."""
if value:
console.print(f"SkillSpector v{__version__}")
raise typer.Exit()
@app.callback()
def main(
version: Annotated[
bool | None,
typer.Option(
"--version",
"-v",
help="Show version and exit.",
callback=version_callback,
is_eager=True,
),
] = None,
) -> None:
"""
SkillSpector - Security scanner for AI agent skills (LangGraph).
Analyze skill bundles to detect vulnerabilities and security risks.
Supports: Git URL, file URL, .zip file, .md file, or directory.
"""
pass
def _scan_state(
input_path: str,
format: FormatChoice,
no_llm: bool,
yara_rules_dir: str | None = None,
baseline: Path | None = None,
show_suppressed: bool = False,
) -> dict[str, object]:
"""Build initial graph state from scan CLI args."""
state: dict[str, object] = {
"input_path": input_path,
"output_format": format.value,
"use_llm": not no_llm,
}
if yara_rules_dir is not None:
state["yara_rules_dir"] = yara_rules_dir
if baseline is not None:
# Loading may raise FileNotFoundError/ValueError, mapped to exit code 2 by scan().
state["baseline"] = load_baseline(baseline)
state["show_suppressed"] = show_suppressed
return state
def _result_body(result: dict) -> str:
report_body = result.get("report_body") or ""
if not report_body and result.get("sarif_report") is not None:
report_body = json.dumps(result["sarif_report"], indent=2)
return report_body
def _write_result(
result: dict[str, object],
output: Path | None,
format: FormatChoice,
) -> None:
"""Write report_body to file or stdout. Uses sarif_report if report_body missing."""
report_body = _result_body(result)
if output:
Path(output).write_text(report_body, encoding="utf-8")
if format == FormatChoice.terminal:
console.print(f"\n[green]Report saved to:[/green] {output}")
else:
console.print(f"Report saved to: {output}")
else:
if format == FormatChoice.terminal:
console.print(report_body)
else:
print(report_body)
def _recursive_json_payload(result: dict[str, object]) -> dict[str, object] | None:
"""Return parsed report_body when it is valid JSON object text."""
raw_report_body = result.get("report_body")
if not isinstance(raw_report_body, str):
return None
try:
parsed = json.loads(raw_report_body)
except json.JSONDecodeError:
return None
return parsed if isinstance(parsed, dict) else None
@app.command()
def scan(
input_path: Annotated[
str,
typer.Argument(
help="Path or URL to scan. Supports: Git URL, file URL, zip file, .md file, or directory.",
),
],
format: Annotated[
FormatChoice,
typer.Option(
"--format",
"-f",
help="Output format.",
case_sensitive=False,
),
] = FormatChoice.terminal,
output: Annotated[
Path | None,
typer.Option(
"--output",
"-o",
help="Output file path. If not specified, prints to stdout.",
),
] = None,
no_llm: Annotated[
bool,
typer.Option(
"--no-llm",
help="Skip LLM analysis (faster, less accurate). Uses static analysis only.",
),
] = False,
yara_rules_dir: Annotated[
Path | None,
typer.Option(
"--yara-rules-dir",
help="Directory containing additional YARA rule files (.yar/.yara) to load alongside built-in rules.",
),
] = None,
recursive: Annotated[
bool,
typer.Option(
"--recursive",
"-r",
help="Scan immediate subdirectories that each contain a SKILL.md as independent skills.",
),
] = False,
baseline: Annotated[
Path | None,
typer.Option(
"--baseline",
"-b",
help="Baseline file (YAML/JSON) of suppressed findings. Matching findings "
"are dropped before scoring. Generate one with 'skillspector baseline'.",
),
] = None,
show_suppressed: Annotated[
bool,
typer.Option(
"--show-suppressed",
help="List findings suppressed by the baseline in the report (they still "
"do not count toward the risk score).",
),
] = False,
verbose: Annotated[
bool,
typer.Option(
"--verbose",
"-V",
help="Show detailed progress.",
),
] = False,
mcp_registry: Annotated[
bool,
typer.Option(
"--mcp-registry",
help="Scan an MCP Registry payload or URL instead of a skill.",
),
] = False,
) -> None:
"""
Scan a skill for security vulnerabilities.
Examples:
skillspector scan ./my-skill/
skillspector scan ./my-skill/ --format json --output report.json
skillspector scan https://github.com/user/my-skill --no-llm
skillspector scan ./skill-collection/ --recursive
Environment variables:
SKILLSPECTOR_PROVIDER Active LLM provider: openai | anthropic |
anthropic_proxy | bedrock | nv_build |
nv_inference. Defaults to the NVIDIA path
(nv_inference, falling back to nv_build in
OSS builds).
SKILLSPECTOR_MODEL Override the active provider's default
model (applies to every analyzer slot).
SKILLSPECTOR_LOG_LEVEL DEBUG | INFO | WARNING | ERROR (default WARNING).
Provider credentials (one of):
OPENAI_API_KEY [+ OPENAI_BASE_URL] for SKILLSPECTOR_PROVIDER=openai
ANTHROPIC_API_KEY for SKILLSPECTOR_PROVIDER=anthropic
AWS_PROFILE (optional) + AWS_REGION for SKILLSPECTOR_PROVIDER=bedrock
(AWS_PROFILE: standard boto3 credential
chain when unset; AWS_REGION default: us-west-2)
NVIDIA_INFERENCE_KEY for the NVIDIA providers
"""
if mcp_registry:
if recursive or baseline is not None or show_suppressed or yara_rules_dir is not None:
console.print(
"[red]Error:[/red] --mcp-registry cannot be combined with "
"--recursive, --baseline, --show-suppressed, or --yara-rules-dir"
)
raise typer.Exit(code=2)
if format != FormatChoice.json:
console.print("[red]Error:[/red] --mcp-registry currently supports only --format json")
raise typer.Exit(code=2)
try:
result = scan_registry(input_path)
report = json.dumps(result, indent=2)
if output:
output.write_text(report, encoding="utf-8")
console.print(f"Report saved to: {output}")
else:
print(report)
if result["risk_score"] > RISK_THRESHOLD:
raise typer.Exit(code=1)
except typer.Exit:
raise
except Exception as e:
console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
return
if verbose:
set_level("DEBUG")
resolved_path = Path(input_path).resolve()
if recursive and resolved_path.is_dir():
detection = detect_skills(resolved_path)
if detection.is_multi_skill:
if baseline is not None:
console.print(
"[red]Error:[/red] --baseline is not supported for recursive "
"multi-skill scans; scan each sub-skill with its own baseline"
)
raise typer.Exit(code=2)
_scan_multi_skill(detection, format, output, no_llm, yara_rules_dir, verbose)
return
if not detection.has_root_skill and len(detection.skills) == 0:
console.print(
"[yellow]Warning:[/yellow] --recursive specified but no sub-skills "
"detected. Scanning as single skill."
)
elif resolved_path.is_dir():
detection = detect_skills(resolved_path)
if detection.is_multi_skill:
console.print(
f"[yellow]Warning:[/yellow] Found {len(detection.skills)} skills in "
f"this directory. Use --recursive to scan each independently."
)
result = None
try:
yara_dir = str(yara_rules_dir.resolve()) if yara_rules_dir else None
state = _scan_state(
input_path,
format,
no_llm,
yara_rules_dir=yara_dir,
baseline=baseline,
show_suppressed=show_suppressed,
)
if verbose:
console.print("[dim]Running scan...[/dim]")
logger.debug(
"Scan started: input_path=%s, format=%s, use_llm=%s",
input_path,
format,
not no_llm,
)
trace_config = _build_trace_config(input_path, format, no_llm)
result = graph.invoke(state, config=trace_config)
_write_result(result, output, format)
if result.get("execution_successful") is False:
raise typer.Exit(code=2)
if (result.get("risk_score") or 0) > RISK_THRESHOLD:
raise typer.Exit(code=1)
except typer.Exit:
raise
except (FileNotFoundError, ValueError) as e:
err_console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
except Exception as e:
if verbose:
err_console.print_exception()
else:
err_console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
finally:
if result is not None:
cleanup_result(result)
def _build_trace_config(input_path: str, format: FormatChoice, no_llm: bool) -> RunnableConfig:
"""Build LangSmith trace config for a scan invocation."""
env = os.environ.get("ENV", "dev")
tags = ["skillspector", f"environment:{env}"]
extra_tags = os.environ.get("LANGCHAIN_TAGS_EXTRA", "")
tags.extend(t.strip() for t in extra_tags.split(",") if t.strip())
return {
"run_name": "skillspector-scan",
"tags": tags,
"metadata": {
"input_path": input_path,
"use_llm": not no_llm,
"output_format": format.value,
"version": __version__,
},
}
def _scan_multi_skill(
detection: MultiSkillDetectionResult,
format: FormatChoice,
output: Path | None,
no_llm: bool,
yara_rules_dir: Path | None,
verbose: bool,
) -> None:
"""Scan each detected sub-skill independently and produce a combined report."""
skills = detection.skills
console.print(f"[bold]Multi-skill directory detected:[/bold] {len(skills)} skills found\n")
results: list[dict[str, object]] = []
max_score = 0
execution_failed = False
for i, skill in enumerate(skills, 1):
console.print(
f" [{i}/{len(skills)}] Scanning [bold]{skill.name}[/bold] ({skill.relative_path}/)"
)
yara_dir = str(yara_rules_dir.resolve()) if yara_rules_dir else None
state = _scan_state(str(skill.path), format, no_llm, yara_rules_dir=yara_dir)
trace_config = _build_trace_config(str(skill.path), format, no_llm)
try:
result = graph.invoke(state, config=trace_config)
results.append(result)
if result.get("execution_successful") is False:
execution_failed = True
score = result.get("risk_score") or 0
if isinstance(score, int) and score > max_score:
max_score = score
severity = result.get("risk_severity") or "LOW"
console.print(f" Score: {score}/100 ({severity})\n")
except Exception as e:
err_console.print(f" [red]Error:[/red] {e}\n")
execution_failed = True
results.append({"skill_name": skill.name, "error": str(e)})
console.print("\n[bold]═══ Multi-Skill Summary ═══[/bold]\n")
console.print(
f" {'Skill':<30} {'Score':<8} {'Severity':<12} {'Findings':<10} {'Execution':<10}"
)
console.print(f" {'─' * 30} {'─' * 8} {'─' * 12} {'─' * 10} {'─' * 10}")
for skill, result in zip(skills, results, strict=True):
if "error" in result:
console.print(f" {skill.name:<30} {'ERROR':<8} {'—':<12} {'—':<10} {'error':<10}")
continue
score = result.get("risk_score", 0)
severity = result.get("risk_severity", "LOW")
filtered = result.get("filtered_findings") or result.get("findings")
finding_count = len(filtered) if isinstance(filtered, list) else 0
execution = "failed" if result.get("execution_successful") is False else "successful"
console.print(
f" {skill.name:<30} {score:<8} {severity:<12} {finding_count:<10} {execution:<10}"
)
console.print("")
if output and format == FormatChoice.json:
combined: dict[str, object] = {
"multi_skill": True,
"skill_count": len(skills),
"max_risk_score": max_score,
"execution_successful": not execution_failed,
"skills": [],
}
combined_skills = cast(list[dict[str, object]], combined["skills"])
for skill, result in zip(skills, results, strict=True):
if "error" in result:
combined_skills.append({"name": skill.name, "error": result["error"]})
else:
payload = _recursive_json_payload(result) or {}
selected_findings = result.get("filtered_findings") or result.get("findings") or []
finding_count = len(selected_findings) if isinstance(selected_findings, list) else 0
entry = {
"name": skill.name,
"path": skill.relative_path,
"risk_score": result.get("risk_score", 0),
"risk_severity": result.get("risk_severity", "LOW"),
"finding_count": finding_count,
"execution_successful": result.get("execution_successful", True),
}
entry.update(payload)
entry["name"] = skill.name
entry["path"] = skill.relative_path
entry["risk_score"] = result.get("risk_score", 0)
entry["risk_severity"] = result.get("risk_severity", "LOW")
entry["finding_count"] = finding_count
entry["execution_successful"] = result.get("execution_successful", True)
combined_skills.append(entry)
Path(output).write_text(json.dumps(combined, indent=2), encoding="utf-8")
console.print(f"[green]Combined report saved to:[/green] {output}")
elif output:
# concatenated non-JSON output: not merged SARIF
sections = []
for skill, result in zip(skills, results, strict=True):
if "error" not in result:
sections.append(f"--- {skill.relative_path} ---\n\n{_result_body(result)}")
Path(output).write_text("\n\n".join(sections), encoding="utf-8")
console.print(f"[green]Combined report saved to:[/green] {output}")
if execution_failed:
raise typer.Exit(code=2)
if max_score > RISK_THRESHOLD:
raise typer.Exit(code=1)
@app.command()
def mcp(
transport: Annotated[
TransportChoice,
typer.Option(
"--transport",
"-t",
help="Transport: FastMCP stdio for local CLI agents, http for remote/A2A callers.",
case_sensitive=False,
),
] = TransportChoice.stdio,
host: Annotated[
str,
typer.Option("--host", help="Host to bind (http transport only)."),
] = "127.0.0.1",
port: Annotated[
int,
typer.Option("--port", help="Port to bind (http transport only)."),
] = 8000,
) -> None:
"""
Run SkillSpector as an MCP server.
Exposes a single tool, ``scan_skill``, so any MCP-capable agent (Claude Code,
Codex CLI, Gemini CLI) or remote runtime can scan a skill and gate installs
on the verdict.
Requires the optional mcp extra. Reinstall the GitHub tool package with
that extra enabled, as shown in the README Quick Start section.
Examples:
skillspector mcp # FastMCP stdio for local CLI agents
skillspector mcp --transport http --port 8000
"""
try:
from skillspector.mcp_server import run as run_mcp
run_mcp(transport=transport.value, host=host, port=port)
except ModuleNotFoundError as e:
err_console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
@app.command()
def baseline(
input_path: Annotated[
str,
typer.Argument(
help="Path or URL to scan. Supports: Git URL, file URL, zip file, .md file, or directory.",
),
],
output: Annotated[
Path,
typer.Option(
"--output",
"-o",
help="Where to write the baseline file (YAML; .json extension writes JSON).",
),
] = Path(".skillspector-baseline.yaml"),
no_llm: Annotated[
bool,
typer.Option(
"--no-llm",
help="Skip LLM analysis when generating the baseline (static analysis only).",
),
] = False,
reason: Annotated[
str,
typer.Option(
"--reason",
help="Reason recorded for every suppressed finding in the baseline.",
),
] = "Accepted finding (auto-generated baseline)",
verbose: Annotated[
bool,
typer.Option("--verbose", "-V", help="Show detailed progress."),
] = False,
) -> None:
"""
Generate a baseline file that suppresses every finding in the current scan.
Run this once to accept all existing findings, then commit the file and pass
it to future scans with --baseline so only NEW findings are reported.
Examples:
skillspector baseline ./my-skill/
skillspector baseline ./my-skill/ -o team-baseline.yaml --no-llm
skillspector scan ./my-skill/ --baseline .skillspector-baseline.yaml
"""
result = None
try:
if verbose:
set_level("DEBUG")
console.print("[dim]Scanning to build baseline...[/dim]")
# output_format is irrelevant here; we consume findings, not report_body.
state = _scan_state(input_path, FormatChoice.json, no_llm)
result = graph.invoke(state)
findings = result.get("filtered_findings") or result.get("findings") or []
data = build_baseline_dict(
findings,
reason=reason,
file_cache=result.get("file_cache") or {},
scanner_version=__version__,
)
dump_baseline(data, output)
console.print(
f"[green]Wrote baseline with {len(findings)} suppressed finding(s) to:[/green] {output}"
)
except typer.Exit:
raise
except (FileNotFoundError, ValueError) as e:
err_console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
except Exception as e:
if verbose:
err_console.print_exception()
else:
err_console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(code=2) from e
finally:
if result is not None:
cleanup_result(result)
if __name__ == "__main__":
app()