|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Archive provenance remains available without changing reported member identity.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import io |
| 9 | +import json |
| 10 | +import zipfile |
| 11 | +from pathlib import Path |
| 12 | +from typing import cast |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from skillspector.models import Finding |
| 17 | +from skillspector.nodes.analyzers.static_patterns_supply_chain import ( |
| 18 | + _analyze_concealed_executables, |
| 19 | +) |
| 20 | +from skillspector.nodes.build_context import build_context |
| 21 | +from skillspector.nodes.finalize_inspection_ledger import finalize_inspection_ledger |
| 22 | +from skillspector.nodes.report import report |
| 23 | +from skillspector.state import SkillspectorState |
| 24 | + |
| 25 | +PROVENANCE_FIELDS = ( |
| 26 | + "outer_path", |
| 27 | + "nested_path", |
| 28 | + "container_type", |
| 29 | + "container_ancestry", |
| 30 | + "container_depth", |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(autouse=True) |
| 35 | +def disable_provider_probe(monkeypatch: pytest.MonkeyPatch) -> None: |
| 36 | + monkeypatch.setattr( |
| 37 | + "skillspector.nodes.report.is_llm_available", lambda **_: (False, "Disabled in test") |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _zip_bytes(members: dict[str, bytes]) -> bytes: |
| 42 | + buffer = io.BytesIO() |
| 43 | + with zipfile.ZipFile(buffer, "w") as archive: |
| 44 | + for name, content in members.items(): |
| 45 | + archive.writestr(name, content) |
| 46 | + return buffer.getvalue() |
| 47 | + |
| 48 | + |
| 49 | +def _json_report(context: dict[str, object], findings: list[Finding] | None = None) -> dict: |
| 50 | + state = cast( |
| 51 | + SkillspectorState, |
| 52 | + {**context, "findings": findings or [], "output_format": "json", "use_llm": False}, |
| 53 | + ) |
| 54 | + return json.loads(report(state)["report_body"]) |
| 55 | + |
| 56 | + |
| 57 | +def test_wheel_and_recursive_zip_keep_complete_member_identity(tmp_path: Path) -> None: |
| 58 | + (tmp_path / "package.whl").write_bytes( |
| 59 | + _zip_bytes({"package/__init__.py": b"VALUE = 1\n", "package/data.txt": b"data\n"}) |
| 60 | + ) |
| 61 | + (tmp_path / "outer.zip").write_bytes( |
| 62 | + _zip_bytes({"inner.zip": _zip_bytes({"run.sh": b"#!/bin/sh\necho hello\n"})}) |
| 63 | + ) |
| 64 | + context = build_context({"skill_path": str(tmp_path)}) |
| 65 | + metadata = context["component_metadata"] |
| 66 | + source = { |
| 67 | + "source_url": "https://example.com/package.zip", |
| 68 | + "source_identity": "source-sha256:" + "a" * 64, |
| 69 | + "source_digest": "b" * 64, |
| 70 | + } |
| 71 | + for component in metadata: |
| 72 | + component.update(source) |
| 73 | + |
| 74 | + payload = _json_report(context) |
| 75 | + rows = {row["path"]: row for row in payload["components"]} |
| 76 | + |
| 77 | + assert len(payload["components"]) == len(metadata) == len(rows) |
| 78 | + assert rows.keys() == {component["path"] for component in metadata} |
| 79 | + for component in metadata: |
| 80 | + row = rows[component["path"]] |
| 81 | + for field in ("path", "type", "lines", "executable", "size_bytes"): |
| 82 | + assert row[field] == component[field] |
| 83 | + assert {field: row[field] for field in source} == source |
| 84 | + for path, outer, nested, depth in ( |
| 85 | + ("package.whl!/package/__init__.py", "package.whl", "package/__init__.py", 1), |
| 86 | + ("package.whl!/package/data.txt", "package.whl", "package/data.txt", 1), |
| 87 | + ("outer.zip!/inner.zip", "outer.zip", "inner.zip", 1), |
| 88 | + ("outer.zip!/inner.zip!/run.sh", "outer.zip", "inner.zip!/run.sh", 2), |
| 89 | + ): |
| 90 | + row = rows[path] |
| 91 | + assert row["outer_path"] == outer |
| 92 | + assert row["nested_path"] == nested |
| 93 | + assert row["container_type"] == "zip" |
| 94 | + assert row["container_ancestry"] == ["zip"] * depth |
| 95 | + assert row["container_depth"] == depth |
| 96 | + assert rows["package.whl!/package/__init__.py"]["executable"] is True |
| 97 | + assert rows["outer.zip!/inner.zip!/run.sh"]["executable"] is True |
| 98 | + assert rows["package.whl!/package/data.txt"]["executable"] is False |
| 99 | + assert not any(field in rows["package.whl"] for field in PROVENANCE_FIELDS) |
| 100 | + |
| 101 | + |
| 102 | +def test_concealed_member_keeps_security_finding_and_risk(tmp_path: Path) -> None: |
| 103 | + (tmp_path / ".hidden.zip").write_bytes(_zip_bytes({"run.sh": b"#!/bin/sh\necho hello\n"})) |
| 104 | + context = build_context({"skill_path": str(tmp_path)}) |
| 105 | + findings = _analyze_concealed_executables(context["component_metadata"]) |
| 106 | + finding = next(item for item in findings if item.rule_id == "SC9") |
| 107 | + original_fingerprint = finding.fingerprint() |
| 108 | + |
| 109 | + payload = _json_report(context, findings) |
| 110 | + issue = next(item for item in payload["issues"] if item["id"] == "SC9") |
| 111 | + member = next(row for row in payload["components"] if row["path"] == finding.file) |
| 112 | + |
| 113 | + assert issue["location"]["file"] == member["path"] == ".hidden.zip!/run.sh" |
| 114 | + assert member["outer_path"] == issue["evidence"]["outer_path"] == ".hidden.zip" |
| 115 | + assert member["nested_path"] == issue["evidence"]["nested_path"] == "run.sh" |
| 116 | + assert member["executable"] is True |
| 117 | + assert issue["severity"] == "HIGH" |
| 118 | + assert issue["match_fingerprint"] == original_fingerprint |
| 119 | + assert payload["risk_assessment"]["score"] > 0 |
| 120 | + assert payload["risk_assessment"]["recommendation"] != "SAFE" |
| 121 | + |
| 122 | + |
| 123 | +def test_truncated_nested_archive_preserves_incomplete_verdict(tmp_path: Path) -> None: |
| 124 | + (tmp_path / "outer.zip").write_bytes(_zip_bytes({"broken.zip": b"PK\x03\x04truncated"})) |
| 125 | + context = build_context({"skill_path": str(tmp_path), "use_llm": False}) |
| 126 | + finalized = finalize_inspection_ledger(cast(SkillspectorState, {**context, "use_llm": False})) |
| 127 | + |
| 128 | + payload = _json_report({**context, **finalized}) |
| 129 | + completeness = payload["analysis_completeness"] |
| 130 | + member = next(row for row in payload["components"] if row["path"] == "outer.zip!/broken.zip") |
| 131 | + |
| 132 | + assert member["outer_path"] == "outer.zip" |
| 133 | + assert member["nested_path"] == "broken.zip" |
| 134 | + assert completeness == finalized["analysis_completeness"] |
| 135 | + assert completeness["is_complete"] is False |
| 136 | + assert any( |
| 137 | + row["path"] == member["path"] and row["reason_code"] == "archive_truncated" |
| 138 | + for row in completeness["ledger_exceptions"] |
| 139 | + ) |
| 140 | + assert payload["execution_successful"] == completeness["execution_successful"] |
| 141 | + assert payload["risk_assessment"]["recommendation"] != "SAFE" |
| 142 | + |
| 143 | + |
| 144 | +def test_literal_archive_separator_in_physical_path_has_no_provenance(tmp_path: Path) -> None: |
| 145 | + directory = tmp_path / "notes!" |
| 146 | + directory.mkdir() |
| 147 | + (directory / "run.py").write_text("VALUE = 1\n", encoding="utf-8") |
| 148 | + context = build_context({"skill_path": str(tmp_path)}) |
| 149 | + |
| 150 | + payload = _json_report(context) |
| 151 | + row = next(item for item in payload["components"] if item["path"] == "notes!/run.py") |
| 152 | + |
| 153 | + assert row["executable"] is True |
| 154 | + assert not any(field in row for field in PROVENANCE_FIELDS) |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize( |
| 158 | + ("outer_path", "nested_path"), |
| 159 | + [("other.zip", "run.py"), ("bundle.zip", "other.py"), ("", "run.py"), (None, "run.py")], |
| 160 | +) |
| 161 | +def test_inconsistent_provenance_is_not_exported(outer_path: str | None, nested_path: str) -> None: |
| 162 | + component = { |
| 163 | + "path": "bundle.zip!/run.py", |
| 164 | + "executable": True, |
| 165 | + "outer_path": outer_path, |
| 166 | + "nested_path": nested_path, |
| 167 | + "container_type": "zip", |
| 168 | + "container_ancestry": ["zip"], |
| 169 | + "container_depth": 1, |
| 170 | + } |
| 171 | + |
| 172 | + row = _json_report({"component_metadata": [component]})["components"][0] |
| 173 | + |
| 174 | + assert row["path"] == component["path"] |
| 175 | + assert row["executable"] is True |
| 176 | + assert not any(field in row for field in PROVENANCE_FIELDS) |
0 commit comments