Files
OBLITERATUS/tests/test_concept_packs.py
Joseph MaglyandClaude Opus 5.5 ae9d774609 feat(concepts): require pack schema 2.0 for dataset declarations
Releases that read only 1.x manifests ignore the dataset declaration and
each row's role, so they extract from select, calibrate and locked test
rows. Gating dataset packs on a new major version makes those releases
reject the pack ("Unsupported schema_version") instead.

- Accept schema majors 1 and 2; 2.x keeps every 1.x field.
- manifest.dataset now requires schema_version 2.0+ (was 1.3+, never
  released). 1.x packs without a dataset are unchanged.
- Compare versions as (major, minor) tuples. The selector gate in
  pack.py compared only the minor number and would have rejected 2.0.
- Unknown-major tests now use 3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:46:10 -05:00

785 lines
33 KiB
Python

"""Pack v1 contracts and explicit untrusted-input attack surfaces, entirely offline."""
from copy import deepcopy
from concurrent.futures import ThreadPoolExecutor
from dataclasses import replace
from io import BytesIO
import json
import os
from pathlib import Path
import shutil
import stat
from threading import Barrier
import zipfile
from jsonschema import Draft202012Validator, ValidationError
import pytest
import yaml
from obliteratus.concepts import ConceptRegistry, ExtractionRecipe
from obliteratus.concepts.pack import ConceptPack, builtin_refusal_pack, import_pack, remote_url
from obliteratus.concepts.pack_io import MAX_FILE_BYTES, digest, read_file, zip_bytes
from obliteratus.concepts.registry import refusal_target
from obliteratus.concepts.validation import PackError, SCHEMA_PATH, validate_manifest
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS, DatasetSource
pytestmark = pytest.mark.cpu
FIXTURES = Path(__file__).parent / "fixtures" / "concept_packs"
@pytest.fixture
def pack_dir(tmp_path):
path = tmp_path / "pack"
shutil.copytree(FIXTURES / "valid_minimal", path)
return path
def manifest_at(path):
return yaml.safe_load((path / "manifest.yaml").read_bytes())
def update_manifest(path, manifest):
(path / "manifest.yaml").write_text(yaml.safe_dump(manifest, sort_keys=True))
@pytest.mark.parametrize("margin", [0, 0.03, 0.08, 1])
def test_pack_collateral_margin_round_trip(pack_dir, tmp_path, margin):
manifest = manifest_at(pack_dir)
manifest["recipe"]["collateral_margin"] = margin
update_manifest(pack_dir, manifest)
pack = ConceptPack.load(pack_dir)
assert pack.to_target().recipe.collateral_margin == margin
output = tmp_path / "margin.cvpack"
pack.write(output)
assert ConceptPack.load(output).to_target().recipe.to_dict()["collateral_margin"] == margin
@pytest.mark.parametrize("margin", [-0.01, 1.01, float("nan"), float("inf"), True, "0.03"])
def test_pack_and_recipe_reject_invalid_margin(pack_dir, margin):
manifest = manifest_at(pack_dir)
manifest["recipe"]["collateral_margin"] = margin
update_manifest(pack_dir, manifest)
with pytest.raises(ValueError):
ConceptPack.load(pack_dir)
with pytest.raises(ValueError, match="collateral_margin"):
ExtractionRecipe(collateral_margin=margin)
@pytest.mark.parametrize("value", [" ", "\t\n", "\u2003"])
def test_pack_validation_rejects_blank_preservation_before_loading(pack_dir, value):
manifest = manifest_at(pack_dir)
manifest["recipe"]["preserve"] = [value]
update_manifest(pack_dir, manifest)
with pytest.raises(PackError, match="preserve"):
validate_manifest(manifest)
with pytest.raises(ValidationError):
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest)
with pytest.raises(PackError, match="preserve"):
ConceptPack.load(pack_dir)
@pytest.mark.parametrize("preserve", [None, [], ["refusal"], ["local direction.safetensors"]])
def test_pack_preservation_round_trip(pack_dir, tmp_path, preserve):
manifest = manifest_at(pack_dir)
manifest["recipe"]["preserve"] = preserve
update_manifest(pack_dir, manifest)
pack = ConceptPack.load(pack_dir)
archive = tmp_path / "protected.cvpack"
pack.write(archive)
assert ConceptPack.load(archive).to_target().recipe.preserve == preserve
def update_positive(path, records):
data = ("\n".join(json.dumps(record) for record in records) + "\n").encode()
(path / "positive.jsonl").write_bytes(data)
manifest = manifest_at(path)
manifest["sets"]["positive"].update(n=len(records), sha256=digest(data))
update_manifest(path, manifest)
def malicious_zip(tmp_path, name, data=b"data", mode=stat.S_IFREG | 0o644):
files = ConceptPack.load(FIXTURES / "valid_minimal").files
output = BytesIO()
with zipfile.ZipFile(output, "w") as archive:
for path, payload in files.items():
archive.writestr(path, payload)
info = zipfile.ZipInfo(name)
info.create_system = 3
info.external_attr = mode << 16
archive.writestr(info, data)
path = tmp_path / "attack.cvpack"
path.write_bytes(output.getvalue())
return path
@pytest.mark.parametrize("fixture", ["valid_minimal", "valid_full"])
def test_valid_fixtures_schema_and_directory_zip_roundtrip(fixture, tmp_path):
schema = json.loads(SCHEMA_PATH.read_text())
Draft202012Validator.check_schema(schema)
pack = ConceptPack.load(FIXTURES / fixture)
Draft202012Validator(schema).validate(pack.manifest)
target = pack.to_target()
archive = tmp_path / "fixture.cvpack"
assert pack.write(archive) == pack.sha256
assert ConceptPack.load(archive).to_target() == target
if fixture == "valid_full":
assert target.tier == "research"
assert target.neighbours
assert target.caveats
else:
assert target.id == "user/synthetic"
assert target.recipe == ExtractionRecipe()
def test_extraction_pin_and_group_field_survive_pack_round_trip(pack_dir, tmp_path):
manifest = manifest_at(pack_dir)
manifest["recipe"].update(extraction_layer={"canonical/model": 0},
layer_select={"strategy": "cv_auc", "group_by": "item"})
update_manifest(pack_dir, manifest)
pack = ConceptPack.load(pack_dir)
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(pack.manifest)
archive = tmp_path / "pinned.cvpack"
pack.write(archive)
restored = ConceptPack.load(archive).to_target()
assert restored.recipe.extraction_layer == {"canonical/model": 0}
assert restored.recipe.group_by == "item"
@pytest.mark.parametrize("change", [
{"schema_version": "3.0"}, {"schema_version": 1.0}, {"id": "../escape"},
{"version": "bad"}, {"tier": "trusted"}, {"title": None}, {"licence": []},
{"recipe": {"method": "python"}}, {"recipe": {"n_directions": 0}},
{"recipe": {"extraction_layer": {"model": -1}}},
{"recipe": {"extraction_layer": {"model": True}}},
{"recipe": {"extraction_layer": {"model": 1.5}}},
{"recipe": {"extraction_layer": {"": 1}}},
{"recipe": {"extraction_layer": [1]}},
{"recipe": {"layer_select": {"strategy": "cv_auc", "group_by": " "}}},
{"sets": {"positive": {"path": "positive.jsonl", "sha256": "bad", "n": 32}, "controls": []}},
{"validation_thresholds": {"min_heldout_auc": float("inf")}},
])
def test_runtime_and_schema_reject_invalid_manifests(change):
manifest = manifest_at(FIXTURES / "valid_minimal")
manifest.update(change)
with pytest.raises(PackError):
validate_manifest(manifest)
# JSON Schema numbers include infinity; runtime intentionally rejects non-finite values.
if "validation_thresholds" not in change:
with pytest.raises(ValidationError):
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest)
def test_optional_fields_and_same_major_versions_are_forward_compatible(pack_dir):
manifest = manifest_at(pack_dir)
manifest.update(schema_version="1.17", future_extension={"hello": "world"})
update_manifest(pack_dir, manifest)
assert ConceptPack.load(pack_dir).to_target().recipe == ExtractionRecipe()
@pytest.mark.parametrize("fixture,match", [
("invalid_hash", "positive.jsonl.*expected " + "0" * 64),
("invalid_licence", "missing spdx"), ("invalid_pickle", "Forbidden data file"),
])
def test_invalid_fixtures_fail_closed(fixture, match):
with pytest.raises(PackError, match=match):
ConceptPack.load(FIXTURES / fixture)
def test_builtin_refusal_prompts_target_and_hash_are_golden(tmp_path):
pack = builtin_refusal_pack()
assert pack.sha256 == "2e7be2696e059da6dcaa892053080fd27f20c56af699243a7bc059300c764bdb"
assert pack.to_target() == refusal_target()
assert pack.to_target().positive == BUILTIN_HARMFUL
assert pack.to_target().controls == {"harmless": BUILTIN_HARMLESS}
assert pack.manifest["tier"] == "core"
assert all(entry["spdx"] == "AGPL-3.0-or-later" for entry in pack.manifest["licence"])
destination = tmp_path / "refusal.cvpack"
pack.write(destination)
registry = ConceptRegistry()
registry.load_pack(destination)
assert registry.get("refusal") == registry.get("core/refusal") == refusal_target()
assert [p.manifest["id"] for p in registry.list_packs()] == ["pain", "refusal"]
def test_builtin_refusal_impostor_is_rejected(tmp_path):
pack = builtin_refusal_pack()
data = pack.files["positive.jsonl"].replace(b'"id": "0"', b'"id": "changed"', 1)
# Alter a prompt, then honestly rehash it: the reserved target still cannot drift.
data = data.replace(BUILTIN_HARMFUL[0].encode(), b"Changed prompt", 1)
pack.files["positive.jsonl"] = data
pack.manifest["sets"]["positive"]["sha256"] = digest(data)
pack.files["manifest.yaml"] = yaml.safe_dump(pack.manifest, sort_keys=True).encode()
path = tmp_path / "impostor.cvpack"
ConceptPack(pack.files).write(path)
registry = ConceptRegistry()
with pytest.raises(PackError, match="must equal"):
registry.load_pack(path)
assert registry.get("refusal") == refusal_target()
def test_deterministic_pack_bytes_and_fixed_zip_metadata(pack_dir, tmp_path):
first, second = tmp_path / "a.cvpack", tmp_path / "b.cvpack"
sha = ConceptPack.load(pack_dir).write(first)
for path in pack_dir.rglob("*"):
if path.is_file():
path.touch()
path.chmod(0o600)
assert ConceptPack.load(pack_dir).write(second) == sha
assert first.read_bytes() == second.read_bytes()
with zipfile.ZipFile(first) as archive:
assert archive.namelist() == sorted(archive.namelist())
assert archive.comment == b""
for entry in archive.infolist():
assert entry.date_time == (1980, 1, 1, 0, 0, 0)
assert entry.external_attr >> 16 == stat.S_IFREG | 0o644
assert entry.compress_type == zipfile.ZIP_STORED
assert entry.extra == entry.comment == b""
def test_registry_directory_zip_user_namespace_and_research(tmp_path):
registry = ConceptRegistry()
registry.load_pack(FIXTURES / "valid_minimal")
assert registry.get("user/synthetic").tier == "user"
archive = tmp_path / "research.cvpack"
ConceptPack.load(FIXTURES / "valid_full").write(archive)
registry.load_pack(archive)
assert registry.get("synthetic").tier == "research"
assert {p.manifest["tier"] for p in registry.list_packs()} == {"core", "research", "user"}
with pytest.raises(PackError, match="already registered"):
registry.load_pack(archive)
@pytest.mark.parametrize("name", [
"../escape.jsonl", "/absolute.jsonl", "C:/absolute.jsonl", "controls/../escape.jsonl",
"controls//bad.jsonl", "controls/./bad.jsonl", "controls\\bad.jsonl",
"payload.py", "payload.sh", "payload.pkl", "reference/payload.pt",
"battery/nested.zip", "battery/nested.tar", "reference/program.exe",
])
def test_zip_slip_absolute_paths_code_and_nested_archive_extensions(tmp_path, name):
with pytest.raises(PackError):
ConceptPack.load(malicious_zip(tmp_path, name))
assert not (tmp_path / "escape.jsonl").exists()
@pytest.mark.parametrize("payload", [
b"\x80\x04pickle", b"#!/bin/sh\necho malicious", b"\x7fELFbinary", b"MZbinary",
b"PK\x03\x04zip", b"\x1f\x8bgzip", b"BZharchive", b"\xfd7zXZarchive",
b"7z\xbc\xafarchive", b"Rar!archive", b"\xcf\xfa\xed\xfebinary",
b"binary\x00data", b"\xff\xfeBinary", b"\x00" * 257 + b"ustar",
])
def test_disguised_binary_pickle_code_archive_magic_bytes(pack_dir, payload):
(pack_dir / "positive.jsonl").write_bytes(payload)
with pytest.raises(PackError, match="forbidden"):
ConceptPack.load(pack_dir)
@pytest.mark.parametrize("where", ["file", "directory", "root", "zip", "zip_fifo"])
def test_symlinks_and_special_files_fail_closed(pack_dir, tmp_path, where):
if where == "file":
(pack_dir / "positive.jsonl").unlink()
(pack_dir / "positive.jsonl").symlink_to(FIXTURES / "valid_minimal" / "positive.jsonl")
source = pack_dir
elif where == "directory":
shutil.rmtree(pack_dir / "controls")
(pack_dir / "controls").symlink_to(
FIXTURES / "valid_minimal" / "controls", target_is_directory=True,
)
source = pack_dir
elif where == "root":
source = tmp_path / "link"
source.symlink_to(pack_dir, target_is_directory=True)
else:
mode = stat.S_IFLNK if where == "zip" else stat.S_IFIFO
source = malicious_zip(tmp_path, "controls/link.jsonl", mode=mode | 0o644)
with pytest.raises(PackError, match="[Ss]ymlink|special"):
ConceptPack.load(source)
def test_duplicate_zip_entries_are_rejected(tmp_path):
# zipfile warns while producing this deliberately invalid input.
with pytest.warns(UserWarning, match="Duplicate name"):
path = malicious_zip(tmp_path, "positive.jsonl")
with pytest.raises(PackError, match="Duplicate"):
ConceptPack.load(path)
@pytest.mark.parametrize("kind", ["file", "zip", "entries", "total", "executable"])
def test_size_count_and_executable_permission_limits(pack_dir, tmp_path, monkeypatch, kind):
from obliteratus.concepts import pack_io
source = pack_dir
if kind == "file":
with (pack_dir / "positive.jsonl").open("wb") as stream:
stream.truncate(MAX_FILE_BYTES + 1)
elif kind == "zip":
source = malicious_zip(tmp_path, "battery/huge.jsonl", b"x" * (MAX_FILE_BYTES + 1))
elif kind == "entries":
monkeypatch.setattr(pack_io, "MAX_ENTRIES", 2)
elif kind == "total":
monkeypatch.setattr(pack_io, "MAX_PACK_BYTES", 100)
else:
(pack_dir / "positive.jsonl").chmod(0o755)
with pytest.raises(PackError, match="limit|Executable"):
ConceptPack.load(source)
@pytest.mark.parametrize("change", [
{"text": "x" * 2001}, {"text": ""}, {"id": []}, {"text": 1},
{"person": []}, {"split": {}}, {"category": None}, {"id": "1"},
])
def test_prompt_record_constraints(pack_dir, change):
records = [json.loads(line) for line in (pack_dir / "positive.jsonl").read_text().splitlines()]
records[0].update(change)
update_positive(pack_dir, records)
with pytest.raises(PackError, match="record|duplicate|category"):
ConceptPack.load(pack_dir)
@pytest.mark.parametrize("count", [7, 8, 31, 32])
def test_minimum_sets_sizes_and_underpowered_warning(pack_dir, count):
update_positive(pack_dir, [{"id": str(i), "text": "Synthetic present"} for i in range(count)])
if count < 8:
with pytest.raises(PackError):
ConceptPack.load(pack_dir)
else:
pack = ConceptPack.load(pack_dir)
assert bool(pack.warnings) == (count < 32)
@pytest.mark.parametrize("attack", ["duplicate_key", "anchor", "tag", "nesting", "oversize"])
def test_yaml_attacks(pack_dir, attack):
data = (pack_dir / "manifest.yaml").read_bytes()
if attack == "duplicate_key":
data += b"id: duplicate\n"
elif attack == "anchor":
data += b"extension: &a [hello]\ncopy: *a\n"
elif attack == "tag":
data += b"extension: !!python/object/apply:os.system [echo forbidden]\n"
elif attack == "nesting":
data += b"extension: " + b"[" * 40 + b"0" + b"]" * 40
else:
data += b"#" + b"x" * (256 * 1024)
(pack_dir / "manifest.yaml").write_bytes(data)
with pytest.raises(PackError):
ConceptPack.load(pack_dir)
def test_undeclared_files_duplicate_names_licences_and_bad_counts(pack_dir):
manifest = manifest_at(pack_dir)
for change in ("licence", "name", "count", "extra", "path"):
altered = deepcopy(manifest)
if change == "licence":
altered["licence"].append(altered["licence"][0])
elif change == "name":
altered["sets"]["controls"].append(altered["sets"]["controls"][0])
elif change == "count":
altered["sets"]["positive"]["n"] += 1
elif change == "path":
altered["sets"]["positive"]["path"] = "../positive.jsonl"
else:
(pack_dir / "baseline").mkdir()
(pack_dir / "baseline" / "extra.json").write_text("{}")
update_manifest(pack_dir, altered)
with pytest.raises(PackError):
ConceptPack.load(pack_dir)
@pytest.fixture
def remote_pack(pack_dir):
data = (pack_dir / "positive.jsonl").read_bytes()
manifest = manifest_at(pack_dir)
entry = manifest["sets"]["positive"]
del entry["path"]
entry["remote"] = {"repo": "https://github.com/example/data.git", "commit": "a" * 40,
"path": "data/present.jsonl"}
manifest["provenance"] = {"source_commit": "a" * 40}
update_manifest(pack_dir, manifest)
(pack_dir / "positive.jsonl").unlink()
return pack_dir, data
def test_remote_lint_fetch_cache_and_reverification(remote_pack, tmp_path, monkeypatch):
path, payload = remote_pack
cache = tmp_path / "cache"
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(cache))
pack = ConceptPack.load(path)
with pytest.raises(PackError, match="unfetched.*explicitly"):
pack.to_target()
registry = ConceptRegistry()
registry.load_pack(path)
assert registry.pack("user/synthetic").manifest["sets"]["positive"]["remote"]
calls = []
def fetcher(url, limit):
calls.append((url, limit))
return payload
pack.fetch(fetcher=fetcher)
assert calls == [("https://raw.githubusercontent.com/example/data/" + "a" * 40
+ "/data/present.jsonl", MAX_FILE_BYTES)]
assert registry.get("user/synthetic").positive[0] == "Synthetic present example 0"
pack.fetch(fetcher=lambda *args: pytest.fail("Cached references must not fetch again"))
cached = next((cache / "concepts" / "files").iterdir())
cached.write_bytes(payload + b" ")
with pytest.raises(PackError, match="SHA-256 mismatch"):
pack.to_target()
with pytest.raises(PackError, match="SHA-256 mismatch"):
pack.fetch(fetcher=fetcher)
assert len(calls) == 1
def test_remote_hash_mismatch_does_not_cache(remote_pack, tmp_path):
path, payload = remote_pack
cache = tmp_path / "cache"
with pytest.raises(PackError, match="positive.jsonl.*expected"):
ConceptPack.load(path).fetch(fetcher=lambda *args: payload + b" ", cache_dir=cache)
assert not cache.exists()
@pytest.mark.parametrize("change", [
{"repo": "http://github.com/example/data"}, {"repo": "https://token@github.com/example/data"},
{"repo": "https://github.com/example/data?token=secret"}, {"commit": "main"},
{"commit": "a" * 39}, {"path": "../present.jsonl"}, {"path": "/present.jsonl"},
{"path": "payload.py"},
])
def test_remote_reference_attacks(remote_pack, change):
path, _ = remote_pack
manifest = manifest_at(path)
manifest["sets"]["positive"]["remote"].update(change)
update_manifest(path, manifest)
with pytest.raises(PackError):
ConceptPack.load(path)
def test_remote_provenance_is_required(remote_pack):
path, _ = remote_pack
manifest = manifest_at(path)
del manifest["provenance"]
update_manifest(path, manifest)
with pytest.raises(PackError, match="source_commit"):
ConceptPack.load(path)
def test_non_github_repo_can_lint_but_default_fetch_is_not_supported(remote_pack):
path, _ = remote_pack
manifest = manifest_at(path)
manifest["sets"]["positive"]["remote"]["repo"] = "https://example.org/owner/data"
update_manifest(path, manifest)
pack = ConceptPack.load(path)
with pytest.raises(PackError, match="GitHub"):
remote_url(pack.manifest["sets"]["positive"]["remote"])
def test_explicit_import_hash_namespace_and_collision(pack_dir, tmp_path):
archive = tmp_path / "source.cvpack"
pack = ConceptPack.load(pack_dir)
sha = pack.write(archive)
installed = import_pack(archive, sha256=sha, install_dir=tmp_path / "installed")
assert installed.manifest["id"] == "user/synthetic"
assert installed.to_target() == replace(pack.to_target(), pack_hash=installed.sha256)
with pytest.raises(PackError, match="already installed"):
import_pack(archive, install_dir=tmp_path / "installed")
with pytest.raises(PackError, match="mismatch"):
import_pack(archive, sha256="0" * 64, install_dir=tmp_path / "wrong")
with pytest.raises(PackError, match="64-hex"):
import_pack(archive, sha256="bad")
with pytest.raises(PackError, match="mismatch"):
import_pack(pack_dir, sha256="0" * 64)
manifest = manifest_at(pack_dir)
manifest["id"] = "refusal"
update_manifest(pack_dir, manifest)
with pytest.raises(PackError, match="collision"):
import_pack(pack_dir, install_dir=tmp_path / "collision")
manifest["id"] = "user/refusal"
update_manifest(pack_dir, manifest)
imported = import_pack(pack_dir, install_dir=tmp_path / "collision")
assert imported.manifest["id"] == "user/refusal"
def test_url_import_requires_digest_and_verifies_before_reading(tmp_path):
calls = []
data = zip_bytes(ConceptPack.load(FIXTURES / "valid_minimal").files)
def fetcher(url, limit):
calls.append((url, limit))
return data
url = "https://example.org/synthetic.cvpack"
with pytest.raises(PackError, match="requires --sha256"):
import_pack(url, fetcher=fetcher)
assert not calls
with pytest.raises(PackError, match="mismatch"):
import_pack(url, sha256="0" * 64, fetcher=fetcher)
installed = import_pack(
url, sha256=digest(data), fetcher=fetcher, install_dir=tmp_path / "installed",
)
assert installed.manifest["id"] == "user/synthetic"
assert len(calls) == 2
def test_dataset_source_licence_is_optional_and_additive():
loader = lambda: (["a"], ["b"])
source = DatasetSource("test", "Test", "Synthetic", 1, loader)
assert source.licence is None
assert replace(source, licence="CC0-1.0").licence == "CC0-1.0"
def test_loaded_registry_recipe_isolation(pack_dir):
manifest = manifest_at(pack_dir)
manifest["recipe"]["readout"] = {"position": "final_token", "suffix": "original"}
update_manifest(pack_dir, manifest)
registry = ConceptRegistry()
registry.load_pack(pack_dir)
first = registry.get("user/synthetic")
original_hash = first.pack_hash
first.recipe.readout["position"] = "mean_pool"
first.controls["neutral"].clear()
second = registry.get("user/synthetic")
assert second.recipe.readout == {"position": "final_token", "suffix": "original"}
assert len(second.controls["neutral"]) == 32
assert second.pack_hash == original_hash
@pytest.mark.parametrize("prefix", [b"MZexecutable", b"\x7fELFexecutable"])
def test_executable_prefixed_zip_rejected(prefix):
archive = zip_bytes(ConceptPack.load(FIXTURES / "valid_minimal").files)
with pytest.raises(PackError, match="executable prefix"):
ConceptPack.load(prefix + archive)
@pytest.mark.parametrize("field,value", [
("id", "user/attack\n"), ("version", "1.0.0\n"),
("schema_version", "1.0\n"),
])
def test_newline_identity_rejected_by_schema_and_runtime(field, value):
manifest = manifest_at(FIXTURES / "valid_minimal")
manifest[field] = value
with pytest.raises(PackError):
validate_manifest(manifest)
with pytest.raises(ValidationError):
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest)
@pytest.mark.parametrize("repo", ["https://github.com/../repo", "https://github.com/owner/.."])
def test_repository_dot_segments_never_become_fetch_urls(repo):
with pytest.raises(PackError, match="relative path|path traversal"):
remote_url({"repo": repo, "commit": "a" * 40, "path": "data.jsonl"})
def test_remote_aggregate_and_descriptor_limits(remote_pack, tmp_path, monkeypatch):
from obliteratus.concepts import pack as pack_module
source, payload = remote_pack
pack = ConceptPack.load(source)
monkeypatch.setattr(pack_module, "MAX_PACK_BYTES", sum(map(len, pack.files.values())) + 1)
with pytest.raises(PackError, match="including remote files.*size limit"):
pack.fetch(fetcher=lambda *args: payload, cache_dir=tmp_path / "small")
assert not (tmp_path / "small").exists()
monkeypatch.setattr(pack_module, "MAX_PACK_BYTES", 32 * 1024 * 1024)
monkeypatch.setattr(pack_module, "MAX_ENTRIES", 2)
with pytest.raises(PackError, match="descriptor entry limit"):
ConceptPack.load(source)
def test_core_thresholds_card_and_redistribution_licence():
pack = builtin_refusal_pack()
for changes in ({"validation_thresholds": {"min_heldout_auc": 0.5}},
{"validation_thresholds": {"max_neighbour_abs_cos": 0.95}},
{"validation_thresholds": {"min_random_percentile": 50}}):
files = dict(pack.files)
manifest = deepcopy(pack.manifest)
manifest.update(changes)
files["manifest.yaml"] = yaml.safe_dump(manifest, sort_keys=True).encode()
with pytest.raises(PackError, match="only tighten"):
ConceptPack(files)
manifest = deepcopy(pack.manifest)
manifest["licence"][0]["spdx"] = "NOASSERTION"
files = dict(pack.files)
files["manifest.yaml"] = yaml.safe_dump(manifest, sort_keys=True).encode()
with pytest.raises(PackError, match="redistribution-compatible"):
ConceptPack(files)
def test_duplicate_json_keys_invalid_safetensors_and_missing_hash(pack_dir, tmp_path):
payload = b'{"id":"0","id":"1","text":"duplicate"}\n'
(pack_dir / "positive.jsonl").write_bytes(payload)
with pytest.raises(PackError, match="duplicate JSON key"):
ConceptPack.load(pack_dir)
source = malicious_zip(tmp_path, "reference/fake.safetensors", b"not safetensors")
with pytest.raises(PackError, match="Invalid safetensors"):
ConceptPack.load(source)
manifest = manifest_at(FIXTURES / "valid_minimal")
del manifest["sets"]["positive"]["sha256"]
with pytest.raises(PackError, match="sha256"):
validate_manifest(manifest)
def test_zip_nul_truncation_cannot_hide_path_traversal_or_code_names():
files = ConceptPack.load(FIXTURES / "valid_minimal").files
output = BytesIO()
with zipfile.ZipFile(output, "w") as archive:
for path, data in files.items():
entry = zipfile.ZipInfo(path)
if path == "positive.jsonl":
# Set filename after construction to produce raw NULs on disk.
entry.filename += "\x00../payload.py"
entry.orig_filename = entry.filename
archive.writestr(entry, data)
with pytest.raises(PackError, match="Unsafe original zip path"):
ConceptPack.load(output.getvalue())
def test_remote_matching_hash_still_rejects_executable_content(remote_pack, tmp_path):
source, _ = remote_pack
payload = b"\x7fELFbinary"
manifest = manifest_at(source)
manifest["sets"]["positive"]["sha256"] = digest(payload)
update_manifest(source, manifest)
with pytest.raises(PackError, match="content forbidden"):
ConceptPack.load(source).fetch(fetcher=lambda *args: payload, cache_dir=tmp_path / "cache")
assert not (tmp_path / "cache").exists()
def test_runtime_validation_needs_no_jsonschema(pack_dir, monkeypatch):
import sys
monkeypatch.setitem(sys.modules, "jsonschema", None)
assert ConceptPack.load(pack_dir).to_target().id == "user/synthetic"
@pytest.mark.parametrize("commit", ["main", "a" * 39, "a" * 41, "", "g" * 40,
"a" * 40 + "\n", None, 123])
def test_security_review_remote_url_rejects_unpinned_commits(commit):
with pytest.raises(PackError, match="commit.*40-hex"):
remote_url({"repo": "https://github.com/example/data", "commit": commit,
"path": "data/present.jsonl"})
@pytest.mark.parametrize("commit", ["a" * 40, "ABCDEF0123" * 4])
def test_security_review_remote_url_accepts_exact_commit_hashes(commit):
assert remote_url({"repo": "https://github.com/example/data", "commit": commit,
"path": "data/present.jsonl"}) == (
f"https://raw.githubusercontent.com/example/data/{commit}/data/present.jsonl"
)
def test_security_review_concurrent_import_has_one_winner(pack_dir, tmp_path, monkeypatch):
from obliteratus.concepts import pack as pack_module
other = tmp_path / "other"
shutil.copytree(pack_dir, other)
records = [json.loads(line) for line in (other / "positive.jsonl").read_text().splitlines()]
records[0]["text"] = "Other installer payload"
update_positive(other, records)
install_dir = tmp_path / "installed"
barrier = Barrier(2)
original_mkstemp = pack_module.tempfile.mkstemp
def simultaneous_write(*args, **kwargs):
# Both importers have finished validation and any existence check.
barrier.wait(timeout=10)
return original_mkstemp(*args, **kwargs)
monkeypatch.setattr(pack_module.tempfile, "mkstemp", simultaneous_write)
def install(source):
try:
return import_pack(source, install_dir=install_dir)
except PackError as exc:
return exc
with ThreadPoolExecutor(max_workers=2) as executor:
futures = [executor.submit(install, source) for source in (pack_dir, other)]
results = [future.result(timeout=15) for future in futures]
winners = [result for result in results if isinstance(result, ConceptPack)]
failures = [result for result in results if isinstance(result, PackError)]
assert len(winners) == len(failures) == 1
assert "already installed: user/synthetic" in str(failures[0])
destination = install_dir / "synthetic.cvpack"
assert destination.read_bytes() == zip_bytes(winners[0].files)
assert list(install_dir.iterdir()) == [destination]
@pytest.mark.parametrize("ident", ["evil", "user/refusal"])
@pytest.mark.parametrize("reader", ["load", "registry"])
def test_security_review_only_refusal_may_be_core(tmp_path, ident, reader):
pack = builtin_refusal_pack()
pack.manifest["id"] = ident
pack.files["manifest.yaml"] = yaml.safe_dump(pack.manifest, sort_keys=True).encode()
source = tmp_path / "core.cvpack"
source.write_bytes(zip_bytes(pack.files))
load = ConceptPack.load if reader == "load" else ConceptRegistry().load_pack
with pytest.raises(PackError, match="Only refusal.*core"):
load(source)
@pytest.mark.parametrize("kind", ["data", "manifest", "archive", "read_file"])
def test_security_review_hardlinked_files_are_rejected(pack_dir, tmp_path, kind):
if kind in {"data", "manifest"}:
file = pack_dir / ("positive.jsonl" if kind == "data" else "manifest.yaml")
source = pack_dir
else:
file = source = tmp_path / "source.cvpack"
ConceptPack.load(pack_dir).write(file)
os.link(file, tmp_path / "external")
load = read_file if kind == "read_file" else ConceptPack.load
with pytest.raises(PackError, match="Hardlink forbidden.*" + file.name):
load(source)
@pytest.mark.parametrize("mutation,reason", [
("executable", "Executable"), ("hardlink", "Hardlink"),
])
@pytest.mark.parametrize("reader", ["read_file", "directory"])
def test_security_review_open_rechecks_file_metadata(pack_dir, tmp_path, monkeypatch,
mutation, reason, reader):
from obliteratus.concepts import pack_io
file = pack_dir / "positive.jsonl"
original_open = pack_io.os.open
changed = []
def change_before_open(path, flags, *args, **kwargs):
if Path(path).name == file.name and not changed:
changed.append(True)
if mutation == "executable":
file.chmod(0o755)
else:
os.link(file, tmp_path / "external")
return original_open(path, flags, *args, **kwargs)
monkeypatch.setattr(pack_io.os, "open", change_before_open)
with pytest.raises(PackError, match=reason + " file forbidden|" + reason + " forbidden"):
if reader == "read_file":
read_file(file)
else:
ConceptPack.load(pack_dir)
assert changed == [True]
@pytest.mark.parametrize("field", ["text", "id", "category"])
@pytest.mark.parametrize("control", ["\x00", "\x01", "\x1b", "\x1f"])
def test_security_review_decoded_record_controls_are_rejected(pack_dir, field, control):
records = [json.loads(line) for line in (pack_dir / "positive.jsonl").read_text().splitlines()]
records[0][field] = "hello" + control + "world"
update_positive(pack_dir, records)
with pytest.raises(PackError, match="positive.jsonl.*control character.*" + field):
ConceptPack.load(pack_dir)
def test_security_review_decoded_whitespace_controls_are_allowed(pack_dir):
records = [json.loads(line) for line in (pack_dir / "positive.jsonl").read_text().splitlines()]
for field in ("text", "id", "category"):
records[0][field] = "hello\n\r\tworld"
update_positive(pack_dir, records)
assert ConceptPack.load(pack_dir).to_target().positive[0] == "hello\n\r\tworld"
def test_security_review_remote_decoded_controls_never_enter_cache(remote_pack, tmp_path):
source, payload = remote_pack
records = [json.loads(line) for line in payload.decode().splitlines()]
records[0]["text"] = "hello\x00world"
payload = ("\n".join(json.dumps(record) for record in records) + "\n").encode()
manifest = manifest_at(source)
manifest["sets"]["positive"]["sha256"] = digest(payload)
update_manifest(source, manifest)
cache = tmp_path / "cache"
with pytest.raises(PackError, match="positive.jsonl.*control character.*text"):
ConceptPack.load(source).fetch(fetcher=lambda *args: payload, cache_dir=cache)
assert not cache.exists()