genblaze-cli 0.3.4__tar.gz → 0.3.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: genblaze-cli
3
- Version: 0.3.4
3
+ Version: 0.3.5
4
4
  Summary: CLI for genblaze manifest operations
5
5
  Project-URL: Homepage, https://github.com/backblaze-labs/genblaze
6
6
  Project-URL: Documentation, https://github.com/backblaze-labs/genblaze#readme
@@ -12,21 +12,20 @@ License-Expression: MIT
12
12
  Keywords: ai,c2pa-ready,cli,command-line,genai,genblaze,manifest,media,pipeline,provenance
13
13
  Classifier: Development Status :: 3 - Alpha
14
14
  Classifier: Environment :: Console
15
- Classifier: License :: OSI Approved :: MIT License
16
15
  Classifier: Programming Language :: Python :: 3.11
17
16
  Classifier: Programming Language :: Python :: 3.12
18
17
  Classifier: Programming Language :: Python :: 3.13
19
18
  Classifier: Topic :: Multimedia
20
19
  Classifier: Topic :: Software Development :: Libraries
21
20
  Requires-Python: >=3.11
22
- Requires-Dist: click>=8.0
23
- Requires-Dist: genblaze-core<0.4,>=0.3.6
21
+ Requires-Dist: click<9,>=8.0
22
+ Requires-Dist: genblaze-core<0.4,>=0.3.7
24
23
  Provides-Extra: dev
25
24
  Requires-Dist: pillow>=10.0; extra == 'dev'
26
25
  Requires-Dist: pytest>=7.0; extra == 'dev'
27
26
  Description-Content-Type: text/markdown
28
27
 
29
- <!-- last_verified: 2026-04-22 -->
28
+ <!-- last_verified: 2026-07-21 -->
30
29
  # genblaze-cli
31
30
 
32
31
  **Command-line toolkit for inspecting, verifying, and indexing genblaze AI-generated-media provenance manifests.**
@@ -58,6 +57,7 @@ genblaze extract video.mp4 # Extract embedded manifest → stdout
58
57
  genblaze extract video.mp4 -o m.json # …or to a file
59
58
 
60
59
  genblaze verify video.mp4 # Verify manifest hash + output sha256 declarations
60
+ genblaze verify video.mp4 --fetch # …and re-hash the actual asset bytes
61
61
  genblaze verify manifest.json # Or verify a standalone manifest file
62
62
 
63
63
  genblaze replay manifest.json # Show what a replay would do (dry run)
@@ -65,7 +65,7 @@ genblaze replay manifest.json # Show what a replay would do (dry run
65
65
  genblaze index manifest.json -o data/ # Index into partitioned Parquet tables
66
66
  ```
67
67
 
68
- Exit codes are non-zero on verification failure — safe to drop into CI pipelines, release checks, or content-moderation workflows. The command does not fetch remote asset URLs; consumers that dereference `asset.url` must hash those bytes separately and compare them with the manifest's `asset.sha256`.
68
+ Exit codes are non-zero on verification failure; safe to drop into CI pipelines, release checks, or content-moderation workflows. By default verify checks the manifest only; `--fetch` also downloads each output asset (through the same SSRF-hardened transfer path the pipeline uses) and compares its bytes against the declared sha256. Local pipelines that wrote assets outside the system temp directories (e.g. `output_dir=./out`) can admit them with `--allowed-root <dir>`.
69
69
 
70
70
  ## Typical flow
71
71
 
@@ -1,4 +1,4 @@
1
- <!-- last_verified: 2026-04-22 -->
1
+ <!-- last_verified: 2026-07-21 -->
2
2
  # genblaze-cli
3
3
 
4
4
  **Command-line toolkit for inspecting, verifying, and indexing genblaze AI-generated-media provenance manifests.**
@@ -30,6 +30,7 @@ genblaze extract video.mp4 # Extract embedded manifest → stdout
30
30
  genblaze extract video.mp4 -o m.json # …or to a file
31
31
 
32
32
  genblaze verify video.mp4 # Verify manifest hash + output sha256 declarations
33
+ genblaze verify video.mp4 --fetch # …and re-hash the actual asset bytes
33
34
  genblaze verify manifest.json # Or verify a standalone manifest file
34
35
 
35
36
  genblaze replay manifest.json # Show what a replay would do (dry run)
@@ -37,7 +38,7 @@ genblaze replay manifest.json # Show what a replay would do (dry run
37
38
  genblaze index manifest.json -o data/ # Index into partitioned Parquet tables
38
39
  ```
39
40
 
40
- Exit codes are non-zero on verification failure — safe to drop into CI pipelines, release checks, or content-moderation workflows. The command does not fetch remote asset URLs; consumers that dereference `asset.url` must hash those bytes separately and compare them with the manifest's `asset.sha256`.
41
+ Exit codes are non-zero on verification failure; safe to drop into CI pipelines, release checks, or content-moderation workflows. By default verify checks the manifest only; `--fetch` also downloads each output asset (through the same SSRF-hardened transfer path the pipeline uses) and compares its bytes against the declared sha256. Local pipelines that wrote assets outside the system temp directories (e.g. `output_dir=./out`) can admit them with `--allowed-root <dir>`.
41
42
 
42
43
  ## Typical flow
43
44
 
@@ -0,0 +1,211 @@
1
+ """Verify command — check manifest hash and output sha256 coverage."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import re
7
+ from pathlib import Path
8
+ from typing import TYPE_CHECKING
9
+ from urllib.parse import urlsplit, urlunsplit
10
+ from urllib.request import url2pathname
11
+
12
+ import click
13
+
14
+ from genblaze_cli.manifest_io import extract_manifest
15
+
16
+ if TYPE_CHECKING:
17
+ from genblaze_core.models.manifest import Manifest
18
+
19
+ # 256 KB chunks; matches the transfer layer's streaming granularity.
20
+ _FETCH_CHUNK = 256 * 1024
21
+
22
+ # A presigned URL's query string is a bearer credential for that object (#75);
23
+ # it must never be echoed in CLI output or error messages.
24
+ _URL_QUERY_RE = re.compile(r"https?://\S+?\?\S+")
25
+
26
+
27
+ def _redact_url(url: str) -> str:
28
+ parsed = urlsplit(url)
29
+ if parsed.query:
30
+ return urlunsplit(parsed._replace(query="REDACTED"))
31
+ return url
32
+
33
+
34
+ def _redact_text(text: str) -> str:
35
+ return _URL_QUERY_RE.sub(lambda m: _redact_url(m.group(0)), text)
36
+
37
+
38
+ def _hash_url_bytes(url: str, extra_roots: tuple[Path, ...] = ()) -> tuple[str, int]:
39
+ """Stream the bytes at ``url`` and return ``(sha256_hex, size)``.
40
+
41
+ ``https://`` URLs go through the transfer layer's SSRF-pinned stream;
42
+ every redirect hop is re-validated against the SSRF blocklist and DNS
43
+ pinned, because a manifest is untrusted input and ``asset.url`` can point
44
+ anywhere. ``file://`` paths must resolve under the same allowed roots the
45
+ storage and ffmpeg paths accept, plus any ``extra_roots`` the caller
46
+ explicitly opted into (mirroring ``_read_local_file``'s ``extra_roots``
47
+ semantics; pipelines run with ``output_dir=`` write assets outside the
48
+ temp-dir allowlist). Bytes are hashed as they arrive; a whole asset is
49
+ never held in memory.
50
+ """
51
+ scheme = urlsplit(url).scheme
52
+ if scheme == "https":
53
+ from genblaze_core.exceptions import StorageError
54
+ from genblaze_core.storage.transfer import (
55
+ _DEFAULT_DOWNLOAD_TIMEOUT,
56
+ _DEFAULT_MAX_DOWNLOAD_BYTES,
57
+ _http_get_stream,
58
+ )
59
+
60
+ digest = hashlib.sha256()
61
+ size = 0
62
+ resp = _http_get_stream(url, timeout=_DEFAULT_DOWNLOAD_TIMEOUT)
63
+ try:
64
+ while chunk := resp.read(_FETCH_CHUNK):
65
+ digest.update(chunk)
66
+ size += len(chunk)
67
+ if size > _DEFAULT_MAX_DOWNLOAD_BYTES:
68
+ # Same exception type and message shape as the transfer
69
+ # layer's own cap, so callers see one error taxonomy.
70
+ raise StorageError(
71
+ f"Download exceeds {_DEFAULT_MAX_DOWNLOAD_BYTES} byte limit"
72
+ )
73
+ finally:
74
+ resp.release_conn()
75
+ return digest.hexdigest(), size
76
+ if scheme == "file":
77
+ from genblaze_core._utils import ALLOWED_FILE_ROOTS
78
+
79
+ allowed = (*ALLOWED_FILE_ROOTS, *(r.resolve() for r in extra_roots))
80
+ resolved = Path(url2pathname(urlsplit(url).path)).resolve()
81
+ if not any(resolved.is_relative_to(root) for root in allowed):
82
+ raise ValueError(f"file:// path outside allowed roots: {resolved}")
83
+ digest = hashlib.sha256()
84
+ size = 0
85
+ with resolved.open("rb") as fh:
86
+ while chunk := fh.read(_FETCH_CHUNK):
87
+ digest.update(chunk)
88
+ size += len(chunk)
89
+ return digest.hexdigest(), size
90
+ raise ValueError(f"unsupported URL scheme {scheme!r} (https:// or file:// only)")
91
+
92
+
93
+ def _fetch_and_compare(
94
+ manifest: Manifest, extra_roots: tuple[Path, ...] = ()
95
+ ) -> tuple[int, list[str]]:
96
+ """Fetch every output asset and compare bytes against the manifest.
97
+
98
+ Returns ``(asset_count, failures)``. An asset that cannot be fetched is a
99
+ failure, not a skip. ``--fetch`` promises byte verification, so an
100
+ unverifiable asset must not contribute to an OK verdict. One asset
101
+ failing does not stop the rest: a corrupt asset and an unreachable one
102
+ should both surface in a single pass.
103
+ """
104
+ count = 0
105
+ failures: list[str] = []
106
+ for step in manifest.run.steps:
107
+ for asset in step.assets:
108
+ count += 1
109
+ label = f"asset {asset.asset_id[:8]} ({_redact_url(asset.url)})"
110
+ try:
111
+ digest, size = _hash_url_bytes(asset.url, extra_roots)
112
+ except Exception as exc:
113
+ failures.append(f"{label}: fetch failed: {_redact_text(str(exc))}")
114
+ continue
115
+ if digest != asset.sha256:
116
+ failures.append(
117
+ f"{label}: sha256 mismatch: manifest declares {asset.sha256}, "
118
+ f"fetched bytes hash to {digest}"
119
+ )
120
+ elif asset.size_bytes is not None and size != asset.size_bytes:
121
+ failures.append(
122
+ f"{label}: size mismatch: manifest declares "
123
+ f"{asset.size_bytes} bytes, fetched {size}"
124
+ )
125
+ return count, failures
126
+
127
+
128
+ @click.command()
129
+ @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
130
+ @click.option(
131
+ "--hash-only",
132
+ is_flag=True,
133
+ help="Only verify canonical_hash; skip output sha256 and asset-metadata checks.",
134
+ )
135
+ @click.option(
136
+ "--fetch",
137
+ is_flag=True,
138
+ help="Also download each output asset and compare its bytes to the declared sha256.",
139
+ )
140
+ @click.option(
141
+ "--allowed-root",
142
+ "allowed_roots",
143
+ multiple=True,
144
+ type=click.Path(exists=True, file_okay=False, path_type=Path),
145
+ help=(
146
+ "With --fetch: additionally trust file:// assets under this directory "
147
+ "(repeatable). By default only the system temp directories are allowed; "
148
+ "pipelines run with output_dir= write assets elsewhere."
149
+ ),
150
+ )
151
+ def verify(file: Path, hash_only: bool, fetch: bool, allowed_roots: tuple[Path, ...]) -> None:
152
+ """Verify an embedded, sidecar, or standalone genblaze manifest."""
153
+ if fetch and hash_only:
154
+ raise click.UsageError("--fetch and --hash-only are mutually exclusive.")
155
+ if allowed_roots and not fetch:
156
+ raise click.UsageError("--allowed-root requires --fetch.")
157
+ try:
158
+ manifest = extract_manifest(file)
159
+ report = manifest.verification_report()
160
+ if not report.hash_ok:
161
+ click.echo("FAIL: manifest hash mismatch.", err=True)
162
+ raise click.exceptions.Exit(1)
163
+ if hash_only:
164
+ click.echo("OK: manifest hash verified. Asset bytes were not fetched or compared.")
165
+ return
166
+ if report.unverified_sha256_ids:
167
+ click.echo(
168
+ f"FAIL: {len(report.unverified_sha256_ids)} output asset(s) "
169
+ "missing or malformed sha256.",
170
+ err=True,
171
+ )
172
+ raise click.exceptions.Exit(1)
173
+ # Out-of-spec numeric/media_type metadata (e.g. width=0) is tolerated on
174
+ # load by parse_manifest() but fails verify() (#149); surface it here so
175
+ # the CLI verdict matches Manifest.verify()/report.ok instead of a stale
176
+ # "OK" that only checked sha256.
177
+ if report.invalid_metadata_ids:
178
+ click.echo(
179
+ f"FAIL: {len(report.invalid_metadata_ids)} output asset(s) "
180
+ "carry out-of-spec numeric/media_type metadata "
181
+ "(e.g. width=0, or a malformed media_type).",
182
+ err=True,
183
+ )
184
+ raise click.exceptions.Exit(1)
185
+ if fetch:
186
+ count, failures = _fetch_and_compare(manifest, allowed_roots)
187
+ if failures:
188
+ for line in failures:
189
+ click.echo(f"FAIL: {line}", err=True)
190
+ raise click.exceptions.Exit(1)
191
+ if count == 0:
192
+ # Distinguish "nothing to check" from "checked and matched":
193
+ # a vacuous OK must not read like a byte-verification pass.
194
+ click.echo("OK: manifest hash verified; no output assets to fetch.")
195
+ return
196
+ click.echo(
197
+ f"OK: manifest hash verified; {count} output asset(s) "
198
+ "fetched and matched their declared sha256."
199
+ )
200
+ return
201
+ click.echo(
202
+ "OK: manifest hash verified; all output assets declare sha256 and "
203
+ "carry in-spec metadata. Asset bytes were not fetched or compared; "
204
+ "add --fetch to verify the media itself."
205
+ )
206
+ except click.exceptions.Exit:
207
+ raise
208
+ except Exception as exc:
209
+ # Prefix with exception type so "PermissionError: ..." and
210
+ # "EmbeddingError: ..." are distinguishable at a glance.
211
+ raise click.ClickException(f"{type(exc).__name__}: {exc}") from exc
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "genblaze-cli"
7
- version = "0.3.4"
7
+ version = "0.3.5"
8
8
  description = "CLI for genblaze manifest operations"
9
9
  authors = [{name = "Jeronimo De Leon", email = "jdeleon@backblaze.com"}]
10
10
  readme = "README.md"
@@ -12,7 +12,6 @@ requires-python = ">=3.11"
12
12
  license = "MIT"
13
13
  classifiers = [
14
14
  "Development Status :: 3 - Alpha",
15
- "License :: OSI Approved :: MIT License",
16
15
  "Environment :: Console",
17
16
  "Programming Language :: Python :: 3.11",
18
17
  "Programming Language :: Python :: 3.12",
@@ -23,8 +22,10 @@ classifiers = [
23
22
  keywords = ["genblaze", "ai", "media", "manifest", "provenance", "c2pa-ready", "genai", "pipeline", "cli", "command-line"]
24
23
 
25
24
  dependencies = [
26
- "genblaze-core>=0.3.6,<0.4",
27
- "click>=8.0",
25
+ "genblaze-core>=0.3.7,<0.4",
26
+ # <9: bounded-major convention (#61) — caps below the next major so a
27
+ # future click release can't silently break the CLI at resolve time.
28
+ "click>=8.0,<9",
28
29
  ]
29
30
 
30
31
  [project.urls]
@@ -0,0 +1,312 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ import pytest
6
+ from click.testing import CliRunner
7
+ from genblaze_cli.main import cli
8
+ from genblaze_core.builders import RunBuilder, StepBuilder
9
+ from genblaze_core.models.asset import Asset
10
+ from genblaze_core.models.enums import StepStatus
11
+ from genblaze_core.models.manifest import Manifest
12
+
13
+
14
+ def _create_fetch_manifest(tmp_path: Path, data: bytes = b"genblaze bytes") -> tuple[Path, Path]:
15
+ """Manifest JSON + on-disk file:// asset whose sha256/size are byte-bound."""
16
+ asset_path = tmp_path / "asset.bin"
17
+ asset_path.write_bytes(data)
18
+ step = StepBuilder("test", "test-model").prompt("hello").build()
19
+ step.status = StepStatus.SUCCEEDED
20
+ asset = Asset(url=asset_path.as_uri(), media_type="image/png")
21
+ asset.set_hash(data)
22
+ step.assets = [asset]
23
+ run = RunBuilder("fetch-test").add_step(step).build()
24
+ manifest = Manifest.from_run(run)
25
+ manifest_path = tmp_path / "manifest.json"
26
+ manifest_path.write_text(manifest.to_canonical_json(), encoding="utf-8")
27
+ return manifest_path, asset_path
28
+
29
+
30
+ def test_verify_fetch_matching_bytes(tmp_path: Path) -> None:
31
+ manifest_path, _ = _create_fetch_manifest(tmp_path)
32
+
33
+ runner = CliRunner()
34
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
35
+
36
+ assert result.exit_code == 0, result.output
37
+ assert "1 output asset(s) fetched and matched their declared sha256" in result.output
38
+
39
+
40
+ def test_verify_fetch_detects_tampered_bytes(tmp_path: Path) -> None:
41
+ manifest_path, asset_path = _create_fetch_manifest(tmp_path)
42
+ asset_path.write_bytes(b"tampered after the manifest was written")
43
+
44
+ runner = CliRunner()
45
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
46
+ combined = result.output + getattr(result, "stderr", "")
47
+
48
+ assert result.exit_code != 0
49
+ assert "sha256 mismatch" in combined
50
+
51
+
52
+ def test_verify_fetch_detects_size_mismatch(tmp_path: Path) -> None:
53
+ """Correct digest but wrong declared size_bytes must fail --fetch."""
54
+ data = b"genblaze bytes"
55
+ asset_path = tmp_path / "asset.bin"
56
+ asset_path.write_bytes(data)
57
+ step = StepBuilder("test", "test-model").prompt("hello").build()
58
+ step.status = StepStatus.SUCCEEDED
59
+ asset = Asset(url=asset_path.as_uri(), media_type="image/png")
60
+ asset.set_hash(data)
61
+ asset.size_bytes = len(data) + 1
62
+ step.assets = [asset]
63
+ run = RunBuilder("fetch-size").add_step(step).build()
64
+ manifest_path = tmp_path / "manifest.json"
65
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
66
+
67
+ runner = CliRunner()
68
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
69
+ combined = result.output + getattr(result, "stderr", "")
70
+
71
+ assert result.exit_code != 0
72
+ assert "size mismatch" in combined
73
+
74
+
75
+ def test_verify_fetch_reports_every_failing_asset(tmp_path: Path) -> None:
76
+ """One bad asset must not stop verification of the rest."""
77
+ tampered = tmp_path / "tampered.bin"
78
+ tampered.write_bytes(b"original")
79
+ missing = tmp_path / "missing.bin"
80
+ missing.write_bytes(b"will be deleted")
81
+ step = StepBuilder("test", "test-model").prompt("hello").build()
82
+ step.status = StepStatus.SUCCEEDED
83
+ assets = []
84
+ for path in (tampered, missing):
85
+ asset = Asset(url=path.as_uri(), media_type="image/png")
86
+ asset.set_hash(path.read_bytes())
87
+ assets.append(asset)
88
+ step.assets = assets
89
+ run = RunBuilder("fetch-multi").add_step(step).build()
90
+ manifest_path = tmp_path / "manifest.json"
91
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
92
+ tampered.write_bytes(b"changed")
93
+ missing.unlink()
94
+
95
+ runner = CliRunner()
96
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
97
+ combined = result.output + getattr(result, "stderr", "")
98
+
99
+ assert result.exit_code != 0
100
+ assert "sha256 mismatch" in combined
101
+ assert "fetch failed" in combined
102
+
103
+
104
+ def test_verify_fetch_rejects_hash_only_combo(tmp_path: Path) -> None:
105
+ manifest_path, _ = _create_fetch_manifest(tmp_path)
106
+
107
+ runner = CliRunner()
108
+ result = runner.invoke(cli, ["verify", "--fetch", "--hash-only", str(manifest_path)])
109
+ combined = result.output + getattr(result, "stderr", "")
110
+
111
+ assert result.exit_code != 0
112
+ assert "mutually exclusive" in combined
113
+
114
+
115
+ def test_verify_fetch_https_streams_through_pinned_transfer(
116
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
117
+ ) -> None:
118
+ """https assets must go through transfer's SSRF-pinned stream, chunked, and
119
+ release the connection afterwards."""
120
+ data = b"remote asset bytes" * 1024
121
+ step = StepBuilder("test", "test-model").prompt("hello").build()
122
+ step.status = StepStatus.SUCCEEDED
123
+ asset = Asset(url="https://cdn.example.com/output.png", media_type="image/png")
124
+ asset.set_hash(data)
125
+ step.assets = [asset]
126
+ run = RunBuilder("fetch-https").add_step(step).build()
127
+ manifest_path = tmp_path / "manifest.json"
128
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
129
+
130
+ released = {"called": False}
131
+
132
+ class _FakeResp:
133
+ def __init__(self) -> None:
134
+ self._buf = data
135
+
136
+ def read(self, size: int) -> bytes:
137
+ chunk, self._buf = self._buf[:size], self._buf[size:]
138
+ return chunk
139
+
140
+ def release_conn(self) -> None:
141
+ released["called"] = True
142
+
143
+ monkeypatch.setattr(
144
+ "genblaze_core.storage.transfer._http_get_stream",
145
+ lambda url, *, timeout: _FakeResp(),
146
+ )
147
+
148
+ runner = CliRunner()
149
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
150
+
151
+ assert result.exit_code == 0, result.output
152
+ assert "fetched and matched" in result.output
153
+ assert released["called"]
154
+
155
+
156
+ def test_verify_fetch_redacts_presigned_query(
157
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
158
+ ) -> None:
159
+ """A presigned query string is a bearer credential; it must not survive
160
+ into CLI output even when the transfer layer's error message echoes it."""
161
+ from genblaze_core.exceptions import StorageError
162
+
163
+ url = "https://cdn.example.com/output.png?X-Amz-Signature=SECRETSIG"
164
+ step = StepBuilder("test", "test-model").prompt("hello").build()
165
+ step.status = StepStatus.SUCCEEDED
166
+ asset = Asset(url=url, media_type="image/png")
167
+ asset.set_hash(b"whatever")
168
+ step.assets = [asset]
169
+ run = RunBuilder("fetch-redact").add_step(step).build()
170
+ manifest_path = tmp_path / "manifest.json"
171
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
172
+
173
+ def _raise(url: str, *, timeout: float) -> None:
174
+ raise StorageError(f"HTTP 403 downloading {url}")
175
+
176
+ monkeypatch.setattr("genblaze_core.storage.transfer._http_get_stream", _raise)
177
+
178
+ runner = CliRunner()
179
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
180
+ combined = result.output + getattr(result, "stderr", "")
181
+
182
+ assert result.exit_code != 0
183
+ assert "SECRETSIG" not in combined
184
+ assert "REDACTED" in combined
185
+
186
+
187
+ def test_verify_fetch_rejects_file_url_outside_allowed_roots(tmp_path: Path) -> None:
188
+ step = StepBuilder("test", "test-model").prompt("hello").build()
189
+ step.status = StepStatus.SUCCEEDED
190
+ asset = Asset(url="file:///etc/hosts", media_type="image/png")
191
+ asset.sha256 = "f" * 64
192
+ step.assets = [asset]
193
+ run = RunBuilder("fetch-roots").add_step(step).build()
194
+ manifest_path = tmp_path / "manifest.json"
195
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
196
+
197
+ runner = CliRunner()
198
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
199
+ combined = result.output + getattr(result, "stderr", "")
200
+
201
+ assert result.exit_code != 0
202
+ assert "outside allowed roots" in combined
203
+
204
+
205
+ def test_verify_default_message_points_to_fetch(tmp_path: Path) -> None:
206
+ """The default OK verdict must route users to --fetch instead of
207
+ dead-ending at 'bytes were not compared'."""
208
+ manifest_path, _ = _create_fetch_manifest(tmp_path)
209
+
210
+ runner = CliRunner()
211
+ result = runner.invoke(cli, ["verify", str(manifest_path)])
212
+
213
+ assert result.exit_code == 0
214
+ assert "add --fetch" in result.output
215
+
216
+
217
+ def test_verify_fetch_blocks_ssrf_urls_via_real_transfer_guard(tmp_path: Path) -> None:
218
+ """No mocking: --fetch must route through core's real SSRF guard.
219
+
220
+ localhost and the cloud-metadata IP are rejected by resolve_ssrf before any
221
+ connection is opened, so this exercises the genuine security path offline.
222
+ If anyone ever swaps the hardened fetch for a naive HTTP client, this test
223
+ starts making real network calls and fails.
224
+ """
225
+ step = StepBuilder("test", "test-model").prompt("hello").build()
226
+ step.status = StepStatus.SUCCEEDED
227
+ assets = []
228
+ for url in ("https://localhost/asset.png", "https://169.254.169.254/latest/meta-data"):
229
+ asset = Asset(url=url, media_type="image/png")
230
+ asset.sha256 = "f" * 64
231
+ assets.append(asset)
232
+ step.assets = assets
233
+ run = RunBuilder("fetch-ssrf").add_step(step).build()
234
+ manifest_path = tmp_path / "manifest.json"
235
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
236
+
237
+ runner = CliRunner()
238
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
239
+ # result.output is the mixed stream on every click version (stderr raises
240
+ # on click<8.2; on >=8.2 output+stderr would double-count these lines).
241
+ combined = result.output
242
+
243
+ assert result.exit_code != 0
244
+ # Both assets must surface as fetch failures in a single pass.
245
+ assert combined.count("fetch failed") == 2
246
+ # And the failures must come from the SSRF guard, not a connection attempt.
247
+ assert "not allowed" in combined
248
+
249
+
250
+ def test_verify_fetch_allowed_root_admits_file_outside_default_roots(
251
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
252
+ ) -> None:
253
+ """--allowed-root extends the file:// allowlist, mirroring the transfer
254
+ layer's extra_roots semantics for pipelines run with output_dir=."""
255
+ manifest_path, _ = _create_fetch_manifest(tmp_path)
256
+ # Simulate a root outside the temp-dir allowlist: with no default roots,
257
+ # tmp_path is only reachable via --allowed-root.
258
+ monkeypatch.setattr("genblaze_core._utils.ALLOWED_FILE_ROOTS", ())
259
+
260
+ runner = CliRunner()
261
+ result = runner.invoke(
262
+ cli, ["verify", "--fetch", "--allowed-root", str(tmp_path), str(manifest_path)]
263
+ )
264
+
265
+ assert result.exit_code == 0, result.output
266
+ assert "fetched and matched" in result.output
267
+
268
+
269
+ def test_verify_fetch_allowed_root_requires_fetch(tmp_path: Path) -> None:
270
+ manifest_path, _ = _create_fetch_manifest(tmp_path)
271
+
272
+ runner = CliRunner()
273
+ result = runner.invoke(cli, ["verify", "--allowed-root", str(tmp_path), str(manifest_path)])
274
+ combined = result.output + getattr(result, "stderr", "")
275
+
276
+ assert result.exit_code != 0
277
+ assert "requires --fetch" in combined
278
+
279
+
280
+ def test_verify_fetch_no_output_assets_reports_nothing_to_fetch(tmp_path: Path) -> None:
281
+ """Zero output assets must not print a vacuous 'fetched and matched' OK."""
282
+ step = StepBuilder("test", "test-model").prompt("hello").build()
283
+ step.status = StepStatus.SUCCEEDED
284
+ step.assets = []
285
+ run = RunBuilder("fetch-empty").add_step(step).build()
286
+ manifest_path = tmp_path / "manifest.json"
287
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
288
+
289
+ runner = CliRunner()
290
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
291
+
292
+ assert result.exit_code == 0, result.output
293
+ assert "no output assets to fetch" in result.output
294
+
295
+
296
+ def test_verify_fetch_rejects_http_scheme(tmp_path: Path) -> None:
297
+ """Plain http:// is rejected; https-only, same policy as the transfer layer."""
298
+ step = StepBuilder("test", "test-model").prompt("hello").build()
299
+ step.status = StepStatus.SUCCEEDED
300
+ asset = Asset(url="http://cdn.example.com/output.png", media_type="image/png")
301
+ asset.sha256 = "f" * 64
302
+ step.assets = [asset]
303
+ run = RunBuilder("fetch-http").add_step(step).build()
304
+ manifest_path = tmp_path / "manifest.json"
305
+ manifest_path.write_text(Manifest.from_run(run).to_canonical_json(), encoding="utf-8")
306
+
307
+ runner = CliRunner()
308
+ result = runner.invoke(cli, ["verify", "--fetch", str(manifest_path)])
309
+ combined = result.output + getattr(result, "stderr", "")
310
+
311
+ assert result.exit_code != 0
312
+ assert "unsupported URL scheme" in combined
@@ -1,56 +0,0 @@
1
- """Verify command — check manifest hash and output sha256 coverage."""
2
-
3
- from pathlib import Path
4
-
5
- import click
6
-
7
- from genblaze_cli.manifest_io import extract_manifest
8
-
9
-
10
- @click.command()
11
- @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
12
- @click.option(
13
- "--hash-only",
14
- is_flag=True,
15
- help="Only verify canonical_hash; skip output sha256 and asset-metadata checks.",
16
- )
17
- def verify(file: Path, hash_only: bool) -> None:
18
- """Verify an embedded, sidecar, or standalone genblaze manifest."""
19
- try:
20
- manifest = extract_manifest(file)
21
- report = manifest.verification_report()
22
- if not report.hash_ok:
23
- click.echo("FAIL: manifest hash mismatch.", err=True)
24
- raise click.exceptions.Exit(1)
25
- if hash_only:
26
- click.echo("OK: manifest hash verified. Asset bytes were not fetched or compared.")
27
- return
28
- if report.unverified_sha256_ids:
29
- click.echo(
30
- f"FAIL: {len(report.unverified_sha256_ids)} output asset(s) "
31
- "missing or malformed sha256.",
32
- err=True,
33
- )
34
- raise click.exceptions.Exit(1)
35
- # Out-of-spec numeric/media_type metadata (e.g. width=0) is tolerated on
36
- # load by parse_manifest() but fails verify() (#149); surface it here so
37
- # the CLI verdict matches Manifest.verify()/report.ok instead of a stale
38
- # "OK" that only checked sha256.
39
- if report.invalid_metadata_ids:
40
- click.echo(
41
- f"FAIL: {len(report.invalid_metadata_ids)} output asset(s) "
42
- "carry out-of-spec numeric/media_type metadata "
43
- "(e.g. width=0, or a malformed media_type).",
44
- err=True,
45
- )
46
- raise click.exceptions.Exit(1)
47
- click.echo(
48
- "OK: manifest hash verified; all output assets declare sha256 and "
49
- "carry in-spec metadata. Asset bytes were not fetched or compared."
50
- )
51
- except click.exceptions.Exit:
52
- raise
53
- except Exception as exc:
54
- # Prefix with exception type so "PermissionError: ..." and
55
- # "EmbeddingError: ..." are distinguishable at a glance.
56
- raise click.ClickException(f"{type(exc).__name__}: {exc}") from exc
File without changes