Repository navigation
Expand file tree
/
Copy pathtest_primary_input_completeness.py
More file actions
425 lines (355 loc) · 16.7 KB
/
Copy pathtest_primary_input_completeness.py
File metadata and controls
425 lines (355 loc) · 16.7 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
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Unsupported requested content must never become a passive-asset exclusion."""
import bz2
import gzip
import io
import json
import tarfile
import zipfile
from pathlib import Path, PurePosixPath
import pytest
import skillspector.nodes.build_context as build_context_module
from skillspector.input_handler import InputHandler
from skillspector.mcp_server import run_scan
_SKILL = b"---\nname: primary-input-check\ndescription: Summarize supplied text.\n---\n# Notes\nSummarize the supplied text.\n"
_PNG = b"\x89PNG\r\n\x1a\n" + bytes(range(32))
def _archive_bytes(members: dict[str, bytes], *, compressed: bool = False) -> bytes:
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode="w") as archive:
for path, content in members.items():
member = tarfile.TarInfo(path)
member.size = len(content)
archive.addfile(member, io.BytesIO(content))
data = stream.getvalue() if members else bytes(2 * tarfile.BLOCKSIZE)
return gzip.compress(data) if compressed else data
@pytest.mark.parametrize(
("name", "data", "directory"),
[
("payload.dat", bytes([0x80, 0x81, 0x82, 0x83, 0, 0xFF]) * 20, False),
("SKILL.md", _SKILL.decode().encode("utf-16"), False),
("SKILL.md", _SKILL.decode().encode("utf-16"), True),
("skill.md", _SKILL.decode().encode("utf-16-be"), True),
("notes.md", bz2.compress(_SKILL), False),
("SKILL.md", _SKILL + b"Legacy caf\xe9 text.\n", True),
("notes.md", _SKILL + b"\xff", False),
],
ids=[
"opaque-file",
"utf16-file",
"utf16-primary",
"utf16-no-bom",
"renamed-bzip2",
"lossy-primary",
"lossy-explicit",
],
)
async def test_unsupported_primary_is_incomplete_and_not_install_safe(
tmp_path: Path, name: str, data: bytes, directory: bool
) -> None:
target = tmp_path / name
target.write_bytes(data)
result = await run_scan(str(tmp_path if directory else target), use_llm=False)
assert result["safe_to_install"] is False
completeness = result["analysis_completeness"]
assert completeness["is_complete"] is False
assert completeness["total_components"] >= 1
assert completeness["coverage_percent"] < 100.0
assert result["recommendation"] != "SAFE"
exceptions = completeness["ledger_exceptions"]
primary = next(
item
for item in exceptions
if item["path"] == name and item["reason_code"] == "unsupported_primary_content"
)
assert primary["message"] == (
"The requested file or primary instructions could not be interpreted. "
"Provide UTF-8 text, a supported ZIP, TAR, or GZIP container, "
"or an extracted directory instead."
)
assert not any(item["path"] == name for item in completeness["scope_exclusions"])
@pytest.mark.parametrize("referenced", [False, True])
async def test_passive_asset_keeps_existing_reference_policy(
tmp_path: Path, referenced: bool
) -> None:
instructions = _SKILL + (b"\nRead [image](image.png).\n" if referenced else b"")
(tmp_path / "SKILL.md").write_bytes(instructions)
(tmp_path / "image.png").write_bytes(_PNG)
result = await run_scan(str(tmp_path), use_llm=False)
assert result["analysis_completeness"]["is_complete"] is not referenced
assert result["safe_to_install"] is not referenced
assert not any(
item["reason_code"] == "unsupported_primary_content"
for item in result["analysis_completeness"]["ledger_exceptions"]
)
if not referenced:
assert any(
item["path"] == "image.png" and item["reason_code"] == "binary_content"
for item in result["analysis_completeness"]["scope_exclusions"]
)
else:
assert any(
item["path"] == "image.png"
and item["reason_code"] == "opaque_content"
and item["message"] == "Artifact contents could not be fully interpreted."
for item in result["analysis_completeness"]["ledger_exceptions"]
)
@pytest.mark.parametrize("nested_archive", [False, True])
async def test_primary_failure_preserves_excluded_executable_evidence(
tmp_path: Path, nested_archive: bool
) -> None:
(tmp_path / "SKILL.md").write_bytes(_SKILL.decode().encode("utf-16"))
excluded = tmp_path / "node_modules" / "example"
excluded.mkdir(parents=True)
if nested_archive:
with zipfile.ZipFile(excluded / "bundle.zip", "w") as archive:
archive.writestr("worker.py", "print('inert example')\n")
executable_path = "node_modules/example/bundle.zip!/worker.py"
else:
(excluded / "worker.py").write_text("print('inert example')\n", encoding="utf-8")
executable_path = "node_modules/example/worker.py"
result = await run_scan(str(tmp_path), use_llm=False)
completeness = result["analysis_completeness"]
assert completeness["status"] == "failed"
assert result["execution_successful"] is False
assert result["safe_to_install"] is False
exceptions = completeness["ledger_exceptions"]
assert any(
item["path"] == "SKILL.md" and item["reason_code"] == "unsupported_primary_content"
for item in exceptions
)
assert any(
item["path"] == executable_path and item["reason_code"] == "excluded_executable_content"
for item in exceptions
)
@pytest.mark.parametrize("layout", ["flat", "nested", "empty", "renamed"])
@pytest.mark.parametrize("archive_kind", ["zip", "tar", "gzip"])
async def test_supported_archives_remain_complete(
tmp_path: Path, layout: str, archive_kind: str
) -> None:
prefix = "skill/" if layout == "nested" else ""
members = {} if layout == "empty" else {prefix + "SKILL.md": _SKILL, prefix + "image.png": _PNG}
suffix = {"zip": ".zip", "tar": ".tar", "gzip": ".tar.gz"}[archive_kind]
name = "bundle" + suffix
if layout == "renamed":
name = "bundle.dat" if archive_kind == "zip" else "notes.md"
target = tmp_path / name
if archive_kind == "zip":
with zipfile.ZipFile(target, "w") as archive:
for path, content in members.items():
archive.writestr(path, content)
else:
target.write_bytes(_archive_bytes(members, compressed=archive_kind == "gzip"))
result = await run_scan(str(target), use_llm=False)
assert result["analysis_completeness"]["is_complete"] is True
assert result["safe_to_install"] is True
@pytest.mark.parametrize("compressed", [False, True])
@pytest.mark.parametrize("member_type", [tarfile.FIFOTYPE, tarfile.GNUTYPE_SPARSE])
async def test_unsupported_tar_members_keep_incomplete_reference_evidence(
tmp_path: Path, compressed: bool, member_type: bytes
) -> None:
name = "bundle.tar.gz" if compressed else "bundle.tar"
info = tarfile.TarInfo("unsupported")
info.type = member_type
data = info.tobuf(format=tarfile.GNU_FORMAT) + bytes(1024)
(tmp_path / name).write_bytes(gzip.compress(data) if compressed else data)
(tmp_path / "SKILL.md").write_bytes(_SKILL + f"\nRead [archive]({name}).\n".encode())
result = await run_scan(str(tmp_path), use_llm=False)
assert result["safe_to_install"] is False
assert result["analysis_completeness"]["is_complete"] is False
assert result["recommendation"] != "SAFE"
assert any(
item["path"].startswith(name)
and item["reason_code"] == "archive_unsupported_member"
and item["message"]
== "Archive member uses a type or feature that is not supported for inspection."
and item["outcome"] == "skipped"
for item in result["analysis_completeness"]["ledger_exceptions"]
)
assert any(issue["id"] == "AE1" and issue["severity"] == "HIGH" for issue in result["findings"])
@pytest.mark.parametrize("extension", ["zip", "dat"])
@pytest.mark.parametrize("member", ["SKILL.md", "pkg/skill.md", "one/SKILL.md"])
async def test_zip_instruction_identity_survives_container_boundaries(
tmp_path: Path, extension: str, member: str
) -> None:
target = tmp_path / f"bundle.{extension}"
with zipfile.ZipFile(target, "w") as archive:
archive.writestr(member, _SKILL.decode().encode("utf-16"))
archive.writestr(str(PurePosixPath(member).parent / "image.png"), _PNG)
if member.startswith("one/"):
archive.writestr("two/README.md", "Summarize text.")
result = await run_scan(str(target), use_llm=False)
assert result["safe_to_install"] is False
assert result["execution_successful"] is False
completeness = result["analysis_completeness"]
assert completeness["status"] == "failed"
assert any(
row["reason_code"] == "unsupported_primary_content"
and row["path"].endswith(member.rsplit("/", 1)[-1])
for row in completeness["ledger_exceptions"]
)
assert not any(
row["path"].endswith(("SKILL.md", "skill.md")) for row in completeness["scope_exclusions"]
)
assert any(row["path"].endswith("image.png") for row in completeness["scope_exclusions"])
async def test_nested_directory_instructions_are_required(tmp_path: Path) -> None:
target = tmp_path / "pkg" / "SKILL.md"
target.parent.mkdir()
target.write_bytes(_PNG)
result = await run_scan(str(tmp_path), use_llm=False)
assert result["safe_to_install"] is False
assert any(
row["path"] == "pkg/SKILL.md" and row["reason_code"] == "unsupported_primary_content"
for row in result["analysis_completeness"]["ledger_exceptions"]
)
@pytest.mark.parametrize("prefix", [b"", _SKILL])
@pytest.mark.parametrize("limit_name", ["MAX_ANALYZABLE_FILE_BYTES", "MAX_TOTAL_CACHED_BYTES"])
async def test_utf8_codepoint_split_by_cache_limit_stays_partial(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, prefix: bytes, limit_name: str
) -> None:
# The cached prefix ends after the first byte of a valid multibyte letter.
monkeypatch.setattr(build_context_module, limit_name, len(prefix) + 1)
(tmp_path / "SKILL.md").write_bytes(prefix + "é\n".encode())
result = await run_scan(str(tmp_path), use_llm=False)
assert result["safe_to_install"] is False
assert result["execution_successful"] is True
assert result["analysis_completeness"]["status"] == "partial"
exceptions = result["analysis_completeness"]["ledger_exceptions"]
expected_reason = (
"size_limit" if limit_name == "MAX_ANALYZABLE_FILE_BYTES" else "total_bytes_limit"
)
assert any(row["reason_code"] == expected_reason for row in exceptions)
assert not any(row["reason_code"] == "unsupported_primary_content" for row in exceptions)
async def test_explicit_text_with_binary_extension_preserves_source_path(tmp_path: Path) -> None:
target = tmp_path / "instructions.png"
target.write_bytes(_SKILL)
result = await run_scan(str(target), use_llm=False)
assert result["analysis_completeness"]["is_complete"] is True
payload = json.loads(result["report"])
assert payload["analysis_completeness"]["fully_inspected_files"] == 1
@pytest.mark.parametrize(
"text",
[
b"BZh is the bzip2 file prefix. Summarize the supplied document.",
b"BZh9 is a bzip2 header. Summarize the supplied document.",
b"# Notes\n" + b" " * 249 + b"ustar denotes the TAR format.\n",
],
)
async def test_archive_magic_words_remain_analyzable_text(tmp_path: Path, text: bytes) -> None:
target = tmp_path / "notes.md"
target.write_bytes(text)
result = await run_scan(str(target), use_llm=False)
assert result["safe_to_install"] is True
assert result["analysis_completeness"]["is_complete"] is True
assert result["analysis_completeness"]["fully_inspected_files"] == 1
assert not result["analysis_completeness"]["ledger_exceptions"]
@pytest.mark.parametrize("archive", [False, True])
async def test_downloaded_file_keeps_primary_identity_but_zip_members_are_passive(
monkeypatch: pytest.MonkeyPatch, archive: bool
) -> None:
stream = io.BytesIO()
with zipfile.ZipFile(stream, "w") as bundle:
bundle.writestr("SKILL.md", _SKILL)
bundle.writestr("image.png", _PNG)
content = stream.getvalue() if archive else _PNG
headers = {"content-type": "application/zip" if archive else "application/octet-stream"}
target = "https://raw.githubusercontent.com/example/skill/main/notes.md"
monkeypatch.setattr(
InputHandler,
"_download_with_redirect_validation",
lambda _self, _url: (headers, target, content),
)
result = await run_scan(target, use_llm=False)
assert result["analysis_completeness"]["is_complete"] is archive
assert result["safe_to_install"] is archive
if not archive:
assert any(
item["path"] == "notes.md" and item["reason_code"] == "unsupported_primary_content"
for item in result["analysis_completeness"]["ledger_exceptions"]
)
@pytest.mark.parametrize("limit", [1, 12])
@pytest.mark.parametrize("cap", ["bundle", "workflow"])
@pytest.mark.parametrize(
"content",
[_SKILL.decode().encode("utf-16"), b"---\nname: [unterminated\n---\nHello\xff"],
ids=["utf16", "invalid-utf8-and-manifest"],
)
async def test_primary_failure_survives_ledger_overflow(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, limit: int, cap: str, content: bytes
) -> None:
from typer.testing import CliRunner
import skillspector.state as state_module
from skillspector.cli import app
if cap == "bundle":
monkeypatch.setattr(build_context_module, "MAX_BUNDLE_LEDGER_EVENTS", limit)
else:
monkeypatch.setattr(state_module, "MAX_INSPECTION_LEDGER_EVENTS", limit)
(tmp_path / "SKILL.md").write_bytes(content)
excluded = tmp_path / "node_modules" / "example"
excluded.mkdir(parents=True)
for index in range(10):
(excluded / f"{index}.py").write_text("pass\n", encoding="utf-8")
result = await run_scan(str(tmp_path), use_llm=False)
assert result["execution_successful"] is False
assert result["safe_to_install"] is False
completeness = result["analysis_completeness"]
assert completeness["status"] == "failed"
assert any(
row["path"] == "SKILL.md"
and row["reason_code"] == "unsupported_primary_content"
and row["fatal"]
for row in completeness["ledger_exceptions"]
)
cli = CliRunner().invoke(app, ["scan", str(tmp_path), "--no-llm", "--format", "json"])
assert cli.exit_code == 2, cli.output
@pytest.mark.parametrize("extension", ["zip", "dat"])
async def test_supported_zip_instruction_member_remains_complete(
tmp_path: Path, extension: str
) -> None:
inner = io.BytesIO()
with zipfile.ZipFile(inner, "w") as archive:
archive.writestr("notes.md", _SKILL)
target = tmp_path / f"bundle.{extension}"
with zipfile.ZipFile(target, "w") as archive:
archive.writestr("pkg/SKILL.md", inner.getvalue())
result = await run_scan(str(target), use_llm=False)
assert result["execution_successful"] is True
assert result["analysis_completeness"]["is_complete"] is True
assert result["safe_to_install"] is True
@pytest.mark.parametrize("extension", ["docx", "xlsx", "pptx"])
async def test_archive_extension_does_not_hide_unsupported_primary_bytes(
tmp_path: Path, extension: str
) -> None:
target = tmp_path / f"notes.{extension}"
target.write_bytes(_SKILL.decode().encode("utf-16"))
result = await run_scan(str(target), use_llm=False)
assert result["execution_successful"] is False
assert result["analysis_completeness"]["status"] == "failed"
assert any(
row["reason_code"] == "unsupported_primary_content" and row["fatal"]
for row in result["analysis_completeness"]["ledger_exceptions"]
)
@pytest.mark.parametrize("failure", ["malformed", "depth_limit"])
async def test_recognized_nested_zip_keeps_archive_failure_accounting(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, failure: str
) -> None:
import skillspector.nested_artifacts as nested_module
inner = io.BytesIO()
with zipfile.ZipFile(inner, "w") as archive:
archive.writestr("notes.md", _SKILL)
content = inner.getvalue()
if failure == "malformed":
content = b"PK\x03\x04truncated"
expected_reason = "archive_truncated"
else:
monkeypatch.setattr(nested_module, "ARCHIVE_MAX_DEPTH", 1)
expected_reason = "archive_depth_limit"
target = tmp_path / "bundle.dat"
with zipfile.ZipFile(target, "w") as archive:
archive.writestr("pkg/SKILL.md", content)
result = await run_scan(str(target), use_llm=False)
assert result["safe_to_install"] is False
assert result["analysis_completeness"]["is_complete"] is False
reasons = {row["reason_code"] for row in result["analysis_completeness"]["ledger_exceptions"]}
assert expected_reason in reasons
assert "unsupported_primary_content" not in reasons