Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 8 additions & 13 deletions src/vouch/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

from . import audit
from .models import Source
from .storage import ArtifactNotFoundError, KBStore, sha256_hex
Expand Down Expand Up @@ -42,18 +40,15 @@ def verify_source(store: KBStore, source: Source) -> VerificationResult:
external_status = "n/a"
note: str | None = None
if source.type.value == "file":
ext = Path(source.locator)
if ext.is_file():
try:
external_status = (
"match" if sha256_hex(ext.read_bytes()) == source.id else "drift"
)
except OSError as e:
external_status = "missing"
note = f"unreadable: {e}"
else:
try:
_resolved, external_body = store.read_under_root(source.locator)
except (OSError, ValueError) as e:
external_status = "missing"
note = "external file path no longer exists"
note = f"unreadable: {e}"
else:
external_status = (
"match" if sha256_hex(external_body) == source.id else "drift"
)

return VerificationResult(
source=source, stored_ok=stored_ok,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ def test_verify_detects_external_drift(store: KBStore, tmp_path: Path) -> None:
assert target.external_status == "drift"


def test_verify_refuses_off_root_file_locator(store: KBStore, tmp_path: Path) -> None:
outside = tmp_path.parent / f"{tmp_path.name}-outside.txt"
outside.write_bytes(b"secret")
src = store.put_source(
b"placeholder",
title="outside",
locator=str(outside),
source_type="file",
)

result = verify.verify_source(store, src)

assert result.stored_ok is True
assert result.external_status == "missing"
assert result.note is not None
assert "unreadable" in result.note


def test_verify_handles_missing_stored_content(store: KBStore, tmp_path: Path) -> None:
# Regression for #30: verify_source caught FileNotFoundError, but
# store.read_source_content() raises ArtifactNotFoundError (a KeyError
Expand Down
Loading