Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/skillspector/inspection_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class LedgerReason(StrEnum):
OPAQUE_CONTENT = "opaque_content"
REFERENCED_UNINSPECTED = "referenced_uninspected"
REFERENCE_EXTRACTION_LIMIT = "reference_extraction_limit"
REFERENCE_MISSING = "reference_missing"
REFERENCE_UNRESOLVED = "reference_unresolved"
MANIFEST_PARSE_ERROR = "manifest_parse_error"
MANIFEST_PARSE_LIMIT = "manifest_parse_limit"
Expand Down Expand Up @@ -164,8 +165,13 @@ class LedgerReason(StrEnum):
LedgerReason.REFERENCE_EXTRACTION_LIMIT: (
"Reference extraction reached an explicit resource bound before completion."
),
LedgerReason.REFERENCE_MISSING: (
"A local path-like reference does not match any bundled artifact,"
" such as a file the skill writes at runtime."
),
LedgerReason.REFERENCE_UNRESOLVED: (
"A local path-like reference could not be resolved unambiguously."
"A local path-like reference matched more than one bundled artifact"
" and could not be resolved to a single target."
),
LedgerReason.MANIFEST_PARSE_ERROR: (
"Manifest frontmatter is malformed or uses an unsupported value shape."
Expand Down
22 changes: 21 additions & 1 deletion src/skillspector/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from skillspector.cleanup import cleanup_result
from skillspector.constants import RISK_THRESHOLD
from skillspector.graph import graph
from skillspector.inspection_ledger import LedgerReason
from skillspector.llm_utils import is_llm_available
from skillspector.logging_config import get_logger
from skillspector.suppression import effective_findings
Expand Down Expand Up @@ -143,11 +144,30 @@ async def run_scan(
execution_successful = bool(result.get("execution_successful", True))
analysis_completeness = result.get("analysis_completeness") or {}
entirely_uninspected = int(analysis_completeness.get("entirely_uninspected_files", 0))
partially_inspected = int(analysis_completeness.get("partially_inspected_files", 0))
# A missing reference names a path the bundle does not carry, such as a
# file the skill writes at runtime. Every discovered file is still
# inspected, so on its own it hides no bytes and must not fail this
# gate. An *ambiguous* reference (REFERENCE_UNRESOLVED) is different:
# it matches more than one bundled artifact, so the scanner has not
# established which bytes the instruction actually reaches, and it
# must keep blocking safe_to_install like any other exceptional
# reason.
ledger_exceptions = analysis_completeness.get("ledger_exceptions") or []
reference_caveat_only = (
bool(ledger_exceptions)
and not analysis_completeness.get("limitations")
and all(
exception.get("reason_code") == LedgerReason.REFERENCE_MISSING
for exception in ledger_exceptions
)
)
safe_to_install = (
risk_score <= RISK_THRESHOLD
and execution_successful
and entirely_uninspected == 0
and bool(analysis_completeness.get("is_complete", True))
and partially_inspected == 0
and (bool(analysis_completeness.get("is_complete", True)) or reference_caveat_only)
)
return {
"target": target,
Expand Down
6 changes: 5 additions & 1 deletion src/skillspector/nodes/build_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,11 @@ def _record_processing_runtime(*, phase: str, path: str, now: float) -> None:
path=primary_path,
start_line=int(reference["line"]),
end_line=int(reference["line"]),
reason=LedgerReason.REFERENCE_UNRESOLVED,
reason=(
LedgerReason.REFERENCE_MISSING
if reference["status"] == "missing"
else LedgerReason.REFERENCE_UNRESOLVED
),
)
for reference in references
if reference["status"] in {"missing", "ambiguous"}
Expand Down
78 changes: 77 additions & 1 deletion tests/nodes/test_security_remediation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,12 +1309,88 @@ def test_missing_primary_reference_blocks_complete_verdict(tmp_path: Path) -> No
assert not any(finding.rule_id == "AE1" for finding in result["filtered_findings"])
assert result["analysis_completeness"]["is_complete"] is False
assert any(
row["reason_code"] == "reference_unresolved"
row["reason_code"] == "reference_missing"
for row in result["analysis_completeness"]["ledger_exceptions"]
)
assert result["risk_recommendation"] != "SAFE"


@pytest.mark.asyncio
async def test_unresolved_reference_caveat_does_not_block_mcp_install(tmp_path: Path) -> None:
"""A reference caveat hides no bytes, so it must not fail safe_to_install.

Same fixture as test_missing_primary_reference_blocks_complete_verdict:
is_complete stays False and the recommendation stays non-SAFE, but every
discovered file was fully inspected and nothing was hidden from analysis.
"""
(tmp_path / "SKILL.md").write_text(
"# Skill\n\nContinue with [the local guide](missing-guide.md).\n",
encoding="utf-8",
)

verdict = await run_scan(str(tmp_path), use_llm=False, output_format="json")

assert verdict["analysis_completeness"]["is_complete"] is False
assert verdict["recommendation"] != "SAFE"
assert verdict["safe_to_install"] is True


@pytest.mark.asyncio
async def test_ambiguous_reference_caveat_still_blocks_mcp_install(tmp_path: Path) -> None:
"""An ambiguous reference is unlike a missing one: it must keep blocking.

The reference matches more than one bundled artifact, so the scanner has
not established which bytes the instruction actually reaches. Unlike the
missing-reference caveat above, this must not be exempted from
safe_to_install.
"""
(tmp_path / "SKILL.md").write_text(
"# Skill\n\nContinue with [the local guide](guide.md).\n",
encoding="utf-8",
)
(tmp_path / "docs").mkdir()
(tmp_path / "docs" / "guide.md").write_text("docs guide", encoding="utf-8")
(tmp_path / "extra").mkdir()
(tmp_path / "extra" / "guide.md").write_text("extra guide", encoding="utf-8")

verdict = await run_scan(str(tmp_path), use_llm=False, output_format="json")

assert verdict["analysis_completeness"]["is_complete"] is False
assert any(
row["reason_code"] == "reference_unresolved"
for row in verdict["analysis_completeness"]["ledger_exceptions"]
)
assert verdict["safe_to_install"] is False


@pytest.mark.asyncio
async def test_opaque_referenced_artifact_still_blocks_mcp_install(tmp_path: Path) -> None:
"""Unlike a reference caveat, a resolved-but-opaque target hides bytes and must block."""
(tmp_path / "SKILL.md").write_text(
"""---
name: binary-repro
description: A skill that ships one small PNG as reference material.
---

# Binary repro

Describe the diagram in assets/diagram.png to the user.
""",
encoding="utf-8",
)
png = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
)
assets = tmp_path / "assets"
assets.mkdir()
(assets / "diagram.png").write_bytes(png)

verdict = await run_scan(str(tmp_path), use_llm=False, output_format="json")

assert verdict["analysis_completeness"]["is_complete"] is False
assert verdict["safe_to_install"] is False


def test_normalized_view_findings_remain_primary() -> None:
content = "B\u039fUNDARY_MARKER"
response = static_runner.run_static_patterns_with_ledger(
Expand Down
Loading