fix(ops): harden provenance gate path and command parsing

Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-09-13 16:12:55 +09:00
committed by Hermes Agent
co-authored by genichin
parent f3a6b2c410
commit 8d9d627b1c
3 changed files with 44 additions and 7 deletions
+23 -6
View File
@@ -97,6 +97,15 @@ def _sha256_bytes(payload: bytes) -> str:
return hashlib.sha256(payload).hexdigest()
def _resolve_under(root: Path, relpath: str) -> Path | None:
candidate = (root / relpath).resolve()
try:
candidate.relative_to(root)
return candidate
except ValueError:
return None
def _capture_command(command: list[str], checkout_dir: Path) -> dict:
try:
done = subprocess.run(
@@ -150,8 +159,9 @@ def collect_release_provenance(
ref_revision = expected_oci_revision or head
runtime_reports = []
for relpath in runtime_files or []:
abs_path = (checkout / relpath).resolve()
exists = abs_path.is_file()
abs_path = _resolve_under(checkout, relpath)
path_valid = abs_path is not None
exists = bool(path_valid and abs_path.is_file())
current_bytes = abs_path.read_bytes() if exists else b""
current_sha = _sha256_bytes(current_bytes) if exists else None
expected_bytes = _git_blob_bytes(checkout, ref_revision, relpath)
@@ -159,11 +169,14 @@ def collect_release_provenance(
runtime_reports.append(
{
"path": relpath,
"path_within_checkout": path_valid,
"exists": exists,
"current_sha256": current_sha,
"expected_revision": ref_revision,
"expected_sha256": expected_sha,
"matches_expected_revision": bool(exists and expected_bytes is not None and current_bytes == expected_bytes),
"matches_expected_revision": bool(
path_valid and exists and expected_bytes is not None and current_bytes == expected_bytes
),
}
)
command_outputs = {}
@@ -219,10 +232,14 @@ def isolate_dirty_checkout_files(
dirty_paths = _porcelain_paths(status)
copied: list[str] = []
for rel in dirty_paths:
src = (checkout / rel).resolve()
if not src.exists() or src.is_dir():
src = _resolve_under(checkout, rel)
if src is None or not src.exists() or src.is_dir():
continue
target = (dst / rel).resolve()
try:
target.relative_to(dst)
except ValueError:
continue
target = dst / rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, target)
copied.append(rel)