Repository navigation
feat(scanner): bind marketplace catalog identity onto plugin receipts #1141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/claude-plugin-scan-cli-1099
Are you sure you want to change the base?
Changes from 4 commits
80f56b0
1c4aa3e
e9852bd
434fd10
76387a9
f690b97
d727e57
6879379
0484372
76bf0fc
b5373d5
a1bff47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,6 +562,8 @@ def build_claude_plugin_scan_receipt( | |
| scanner_version: str = _SCANNER_VERSION, | ||
| scan_started_at: str = "", | ||
| scan_completed_at: str = "", | ||
| catalog_payload: object | None = None, | ||
| catalog_bytes: bytes | None = None, | ||
| ) -> PluginScanReceipt: | ||
| """Return a deterministic admission receipt for one plugin artifact. | ||
|
|
||
|
|
@@ -570,18 +572,24 @@ def build_claude_plugin_scan_receipt( | |
| scanner_version: Scanner release identity recorded on the receipt. | ||
| scan_started_at: Optional caller-supplied start timestamp. | ||
| scan_completed_at: Optional caller-supplied completion timestamp. | ||
| catalog_payload: Optional parsed marketplace catalog document. | ||
| catalog_bytes: Optional exact catalog file bytes. | ||
|
|
||
| Returns: | ||
| Receipt whose identity excludes wall-clock fields. ``scan_result`` is | ||
| ``pass`` only when ``.claude-plugin/`` exists and no policy findings | ||
| remain. Secret literals never appear on the receipt. | ||
| """ | ||
| hits = _collect_plugin_hits(root) | ||
| hits = list(_collect_plugin_hits(root)) | ||
| catalog = _catalog_identity(catalog_payload) | ||
| hits.extend(_catalog_bind_hits(root, catalog)) | ||
| finding_summary = tuple(sorted({hit.rule_id for hit in hits})) | ||
| identity = _plugin_identity(root) | ||
| artifact_sha256, file_count, scanned_byte_count = _artifact_digest(root) | ||
| marketplace_path = root / ".claude-plugin" / "marketplace.json" | ||
| marketplace_bytes = _regular_file_bytes(marketplace_path) | ||
| marketplace_bytes = ( | ||
| catalog_bytes if catalog_bytes is not None else _regular_file_bytes(marketplace_path) | ||
| ) | ||
|
Comment on lines
+596
to
+598
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🛡️ Analyzed with Security Review | 🟠 Major | 🏗️ Heavy lift Other (CWE-494): Download of Code Without Integrity Check Reachability: External · Exploitability: Moderate
🤖 Prompt for AI Agents |
||
| marketplace_blob_sha = _sha256(marketplace_bytes) if marketplace_bytes else "" | ||
| marketplace_entry_sha256 = _sha256( | ||
| json.dumps(identity, sort_keys=True, separators=(",", ":")).encode() | ||
|
|
@@ -607,8 +615,8 @@ def build_claude_plugin_scan_receipt( | |
| "scanner_name": _SCANNER_NAME, | ||
| "scanner_version": scanner_version, | ||
| "scanner_policy_sha256": policy_sha256, | ||
| "catalog_repository": "", | ||
| "catalog_commit_sha": "", | ||
| "catalog_repository": catalog["catalog_repository"], | ||
| "catalog_commit_sha": catalog["catalog_commit_sha"], | ||
| "marketplace_blob_sha": marketplace_blob_sha, | ||
| "marketplace_entry_sha256": marketplace_entry_sha256, | ||
| "plugin_name": identity["plugin_name"], | ||
|
|
@@ -683,6 +691,8 @@ def verify_plugin_scan_receipt( | |
| root: Path, | ||
| *, | ||
| expected_policy_sha256: str | None = None, | ||
| catalog_payload: object | None = None, | ||
| catalog_bytes: bytes | None = None, | ||
| ) -> PluginReceiptVerification: | ||
| """Fail closed unless the receipt still binds the current artifact and policy. | ||
|
|
||
|
|
@@ -691,14 +701,20 @@ def verify_plugin_scan_receipt( | |
| root: Materialized tree being admitted. | ||
| expected_policy_sha256: Caller-pinned policy digest. When omitted, | ||
| the current scanner policy bytes are required. | ||
| catalog_payload: Catalog document used when the receipt was issued. | ||
| catalog_bytes: Exact catalog bytes used when the receipt was issued. | ||
|
|
||
| Returns: | ||
| Structured mismatch field names. Empty mismatches mean the receipt | ||
| still describes this tree and policy. ``admitted`` is always false: | ||
| ``scan_result=pass`` is not Noema admission. Reasons never include | ||
| secret literals or raw bidi characters. | ||
| """ | ||
| live = build_claude_plugin_scan_receipt(root) | ||
| live = build_claude_plugin_scan_receipt( | ||
| root, | ||
| catalog_payload=catalog_payload, | ||
| catalog_bytes=catalog_bytes, | ||
| ) | ||
| current_policy_sha256 = _sha256(Path(__file__).read_bytes()) | ||
| expected = ( | ||
| current_policy_sha256 | ||
|
|
@@ -1181,6 +1197,65 @@ def _empty_identity() -> dict[str, str]: | |
| } | ||
|
|
||
|
|
||
| def _empty_catalog_identity() -> dict[str, str]: | ||
| """Return blank catalog and plugin identity fields.""" | ||
| return { | ||
| "catalog_repository": "", | ||
| "catalog_commit_sha": "", | ||
| **_empty_identity(), | ||
| } | ||
|
|
||
|
|
||
| def _catalog_identity(payload: object | None) -> dict[str, str]: | ||
| """Return catalog repository/SHA plus first plugin identity from a catalog.""" | ||
| identity = _empty_catalog_identity() | ||
| if not isinstance(payload, dict): | ||
| return identity | ||
| repo = payload.get("repository") or payload.get("catalog_repository") | ||
| sha = ( | ||
| payload.get("commit") | ||
| or payload.get("catalog_commit_sha") | ||
| or payload.get("sha") | ||
| ) | ||
| identity.update(_identity_from_payload(payload)) | ||
| if isinstance(repo, str): | ||
| identity["catalog_repository"] = repo | ||
| if isinstance(sha, str): | ||
| identity["catalog_commit_sha"] = sha | ||
| return identity | ||
|
|
||
|
|
||
| def _catalog_bind_hits(root: Path, catalog: dict[str, str]) -> tuple[PluginHit, ...]: | ||
| """Return findings when an external catalog disagrees with the artifact.""" | ||
| hits: list[PluginHit] = [] | ||
| catalog_sha = catalog.get("catalog_commit_sha") or "" | ||
| if catalog_sha and not _FULL_SHA.fullmatch(catalog_sha): | ||
| hits.append( | ||
| PluginHit( | ||
| rule_id="claude-plugin-floating-git-ref", | ||
| line=1, | ||
| snippet=catalog_sha[:120], | ||
| message=CLAUDE_PLUGIN_FLOATING_REF_MESSAGE, | ||
| file="marketplace.json", | ||
| ) | ||
| ) | ||
| plugin = _plugin_identity(root) | ||
| for field in ("plugin_name", "source_repository", "source_commit_sha"): | ||
| left, right = catalog.get(field) or "", plugin.get(field) or "" | ||
| if left and right and left != right: | ||
| hits.append( | ||
| PluginHit( | ||
| rule_id="claude-plugin-source-mismatch", | ||
| line=1, | ||
| snippet=field, | ||
| message=CLAUDE_PLUGIN_SOURCE_MISMATCH_MESSAGE, | ||
| file="marketplace.json", | ||
| ) | ||
| ) | ||
| break | ||
| return tuple(hits) | ||
|
|
||
|
|
||
| def _identity_from_payload(payload: object) -> dict[str, str]: | ||
| """Return bounded identity from one parsed marketplace or plugin document.""" | ||
| identity = _empty_identity() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| _ERROR_MARKETPLACE = "marketplace entry is missing or not a file" | ||
| _ERROR_MARKETPLACE_SIZE = "marketplace entry exceeds the bounded size" | ||
| _ERROR_MARKETPLACE_JSON = "marketplace entry is not valid JSON" | ||
| _ERROR_MARKETPLACE_IDENTITY = "marketplace catalog does not contain one matching plugin entry" | ||
| _ERROR_RECEIPT_WRITE = "cannot write receipt" | ||
| _ERROR_RECEIPT_STALE = "receipt does not match the scanned artifact" | ||
|
|
||
|
|
@@ -75,12 +76,30 @@ def scan_plugin_artifact( | |
| if not str(plugin_root) or plugin_root.is_symlink() or not plugin_root.is_dir(): | ||
| print(_ERROR_PLUGIN_ROOT, file=err) | ||
| return 1 | ||
| catalog_payload: object | None = None | ||
| catalog_bytes: bytes | None = None | ||
| if marketplace_entry is not None: | ||
| status = _validate_marketplace_entry(marketplace_entry, err) | ||
| status, catalog_payload, catalog_bytes = _load_marketplace_catalog( | ||
| marketplace_entry, err | ||
| ) | ||
| if status != 0: | ||
| return status | ||
| receipt = build_claude_plugin_scan_receipt(plugin_root) | ||
| verification = verify_plugin_scan_receipt(receipt, plugin_root) | ||
| status, catalog_payload = _select_marketplace_entry( | ||
| catalog_payload, plugin_root, err | ||
| ) | ||
| if status != 0: | ||
| return status | ||
| receipt = build_claude_plugin_scan_receipt( | ||
| plugin_root, | ||
| catalog_payload=catalog_payload, | ||
| catalog_bytes=catalog_bytes, | ||
| ) | ||
| verification = verify_plugin_scan_receipt( | ||
| receipt, | ||
| plugin_root, | ||
| catalog_payload=catalog_payload, | ||
| catalog_bytes=catalog_bytes, | ||
| ) | ||
| if not verification.matches: | ||
| print(_ERROR_RECEIPT_STALE, file=err) | ||
| return 1 | ||
|
|
@@ -93,28 +112,94 @@ def scan_plugin_artifact( | |
| return 0 if receipt.scan_result == "pass" else 1 | ||
|
|
||
|
|
||
| def _validate_marketplace_entry(path: Path, err: TextIO) -> int: | ||
| """Fail closed unless ``path`` is a bounded regular JSON catalog file.""" | ||
| def _load_marketplace_catalog( | ||
| path: Path, err: TextIO | ||
| ) -> tuple[int, object | None, bytes | None]: | ||
| """Return parsed catalog bytes or a fail-closed status.""" | ||
| if path.is_symlink() or not path.is_file(): | ||
| print(_ERROR_MARKETPLACE, file=err) | ||
| return 1 | ||
| return 1, None, None | ||
| try: | ||
| data = path.read_bytes() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🛡️ Analyzed with Security Review | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/sh
set -eu
printf '%s\n' '--- appguardrail_core/claude_plugin_scan_cli.py ---'
sed -n '1,225p' appguardrail_core/claude_plugin_scan_cli.py
printf '%s\n' '--- relevant tests ---'
sed -n '260,335p' tests/test_claude_plugin_scan_cli.pyRepository: ContextualWisdomLab/appguardrail Length of output: 10492 🤖 get_repo_knowledge executed:
Length of output: 4000 🏁 Script executed: #!/bin/sh
set -eu
sed -n '1,225p' appguardrail_core/claude_plugin_scan_cli.pyRepository: ContextualWisdomLab/appguardrail Length of output: 7868 🏁 Script executed: #!/bin/sh
set -eu
nl -ba appguardrail_core/claude_plugin_scan_cli.py | sed -n '110,145p'Repository: ContextualWisdomLab/appguardrail Length of output: 206 Denial of Service (CWE-400): Uncontrolled Resource Consumption Reachability: External · Exploitability: Moderate 카탈로그를 읽기 전에 바이트 수를 제한하십시오.
🤖 Prompt for AI Agents |
||
| except OSError: | ||
| print(_ERROR_MARKETPLACE, file=err) | ||
| return 1 | ||
| return 1, None, None | ||
| if len(data) > MAX_MARKETPLACE_BYTES: | ||
| print(_ERROR_MARKETPLACE_SIZE, file=err) | ||
| return 1 | ||
| return 1, None, None | ||
| try: | ||
| payload = json.loads(data.decode("utf-8")) | ||
| except (UnicodeDecodeError, json.JSONDecodeError): | ||
| print(_ERROR_MARKETPLACE_JSON, file=err) | ||
| return 1 | ||
| if not isinstance(payload, (dict, list)): | ||
| return 1, None, None | ||
| if not isinstance(payload, dict): | ||
| print(_ERROR_MARKETPLACE_JSON, file=err) | ||
| return 1 | ||
| return 0 | ||
| return 1, None, None | ||
| return 0, payload, data | ||
|
|
||
|
|
||
| def _materialized_plugin_name(root: Path) -> str: | ||
| """Return the local plugin name used to select one catalog entry.""" | ||
| path = root / ".claude-plugin" / "plugin.json" | ||
| try: | ||
| if path.is_symlink() or not path.is_file(): | ||
| return "" | ||
| payload = json.loads(path.read_text(encoding="utf-8")) | ||
| except (OSError, UnicodeDecodeError, json.JSONDecodeError): | ||
| return "" | ||
| if not isinstance(payload, dict): | ||
| return "" | ||
| name = payload.get("name") | ||
| return name if isinstance(name, str) else "" | ||
|
|
||
|
|
||
| def _normalize_marketplace_entry(entry: dict[str, object]) -> dict[str, object]: | ||
| """Normalize current URL/SHA catalog sources for the shared identity parser.""" | ||
| normalized = dict(entry) | ||
| source = entry.get("source") | ||
| if isinstance(source, str): | ||
| normalized["source"] = {"path": source} | ||
| return normalized | ||
| if not isinstance(source, dict): | ||
| return normalized | ||
| source_identity = dict(source) | ||
| url = source.get("url") | ||
| if "repo" not in source_identity and isinstance(url, str): | ||
| source_identity["repo"] = url | ||
| sha = source.get("sha") | ||
| if isinstance(sha, str): | ||
| source_identity["ref"] = sha | ||
| normalized["source"] = source_identity | ||
| return normalized | ||
|
|
||
|
|
||
| def _select_marketplace_entry( | ||
| payload: object | None, | ||
| plugin_root: Path, | ||
| err: TextIO, | ||
| ) -> tuple[int, object | None]: | ||
| """Select exactly one canonical catalog entry for the materialized plugin.""" | ||
| if not isinstance(payload, dict): | ||
| print(_ERROR_MARKETPLACE_JSON, file=err) | ||
| return 1, None | ||
| plugins = payload.get("plugins") | ||
| if plugins is None: | ||
| return 0, payload | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
|
||
| if not isinstance(plugins, list): | ||
| print(_ERROR_MARKETPLACE_JSON, file=err) | ||
| return 1, None | ||
| plugin_name = _materialized_plugin_name(plugin_root) | ||
| matches = [ | ||
| item | ||
| for item in plugins | ||
| if isinstance(item, dict) and item.get("name") == plugin_name | ||
| ] | ||
| if not plugin_name or len(matches) != 1: | ||
| print(_ERROR_MARKETPLACE_IDENTITY, file=err) | ||
| return 1, None | ||
| selected = dict(payload) | ||
| selected["plugins"] = [_normalize_marketplace_entry(matches[0])] | ||
| return 0, selected | ||
|
|
||
|
|
||
| def _write_receipt(path: Path, payload: str, err: TextIO) -> int: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🛡️ Analyzed with Security Review | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: ContextualWisdomLab/appguardrail
Length of output: 24896
🤖 get_repo_knowledge executed:
get_repo_knowledge ContextualWisdomLab/appguardrail /tmp/coderabbit-repo-knowledge/contextualwisdomlab-appguardrail-0e463811/conventionsLength of output: 4000
🏁 Script executed:
Repository: ContextualWisdomLab/appguardrail
Length of output: 18227
🏁 Script executed:
Repository: ContextualWisdomLab/appguardrail
Length of output: 6905
Other (CWE-494): Download of Code Without Integrity Check
Reachability: External · Exploitability: Moderate
직접 영수증 API에서도 카탈로그 선택기를 사용해야 합니다.
build_claude_plugin_scan_receipt()는catalog_payload를_catalog_identity()에 직접 전달하고,_identity_from_payload()는 첫 번째plugins항목만 읽습니다. CLI에서만_select_marketplace_entry()가 실행되므로, 직접 API는 materialized plugin과 일치하는 항목을 선택하지 않습니다.source.url만 있는 항목과 잘못된 입력은 빈 identity로 처리되어scan_result="pass"가 될 수 있습니다.CLI와 직접 API가 동일한 선택기를 사용하도록 변경하십시오. 선택기는 materialized plugin과 일치하는 항목을 정확히 하나 선택해야 합니다. 일치 항목이 없거나 중복되거나 항목 또는 source identity가 잘못되면 fail closed 해야 합니다.
source.url을 정규화하고source.sha를source.ref보다 우선하십시오.catalog_payload의 identity와catalog_bytes의marketplace_blob_sha도 동일한 원본을 나타내는지 검증하십시오.🤖 Prompt for AI Agents
Source: Coding guidelines