Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(server): admit serving artifacts from the Hugging Face shared blob store (#812)

Krisztian Gajdar53ba100d7ee4
diff --git a/packages/sie_server/src/sie_server/config/serving_artifacts.py b/packages/sie_server/src/sie_server/config/serving_artifacts.py
index 71cc1c77b..19c968109 100644
--- a/packages/sie_server/src/sie_server/config/serving_artifacts.py
+++ b/packages/sie_server/src/sie_server/config/serving_artifacts.py
@@ -7,6 +7,7 @@ import errno
import hashlib
import json
import os
+import re
import shutil
import stat
import tempfile
@@ -42,6 +43,8 @@ _ROLE_PATTERN = r"^[a-z][a-z0-9_.-]{0,63}$"
_MAX_MANIFEST_BYTES = 8 * 1024 * 1024
_MAX_TOKENIZER_CONFIG_BYTES = 1024 * 1024
_HASH_CHUNK_BYTES = 1024 * 1024
+_SHARED_BLOB_STORE = "blobs"
+_SHARED_BLOB_NAME = re.compile(r"[0-9a-f]{64}")
def _validate_relative_posix_path(value: str, *, label: str) -> str:
@@ -250,6 +253,21 @@ def _sha256_file(path: Path) -> str:
return digest.hexdigest()
+def _is_cache_owned_file(resolved: Path, repo_root: Path) -> bool:
+ if not resolved.is_file():
+ return False
+ if resolved.is_relative_to(repo_root):
+ return True
+ # huggingface_hub keeps one copy of each Xet-backed blob per cache, at
+ # <cache>/blobs/<first two hex digits>/<xet hash>, and links the
+ # repository's blobs/<etag> entry to it.
+ return (
+ resolved.parent.parent == repo_root.parent / _SHARED_BLOB_STORE
+ and _SHARED_BLOB_NAME.fullmatch(resolved.name) is not None
+ and resolved.parent.name == resolved.name[:2]
+ )
+
+
def _load_manifest(
declaration: ServingArtifactDeclaration,
*,
@@ -273,7 +291,7 @@ def _load_manifest(
resolved_manifest = manifest_path.resolve(strict=True)
except OSError as exc:
raise ValueError("derived serving artifact manifest does not exist") from exc
- if not resolved_manifest.is_file() or not resolved_manifest.is_relative_to(repo_root):
+ if not _is_cache_owned_file(resolved_manifest, repo_root):
raise ValueError("derived serving artifact manifest is not a safe regular file")
if resolved_manifest.stat().st_size > _MAX_MANIFEST_BYTES:
raise ValueError("derived serving artifact manifest exceeds the size limit")
@@ -397,7 +415,7 @@ def _safe_source_file(snapshot_root: Path, repo_root: Path, artifact: ServingArt
resolved = source_path.resolve(strict=True)
except OSError as exc:
raise ValueError(f"derived serving artifact is missing: {artifact.path!r}") from exc
- if not resolved.is_file() or not resolved.is_relative_to(repo_root):
+ if not _is_cache_owned_file(resolved, repo_root):
raise ValueError(f"derived serving artifact escapes its repository cache: {artifact.path!r}")
file_stat = resolved.stat()
if file_stat.st_size != artifact.size_bytes:
diff --git a/packages/sie_server/tests/config/test_serving_artifacts.py b/packages/sie_server/tests/config/test_serving_artifacts.py
index 0ad1ea537..0275828eb 100644
--- a/packages/sie_server/tests/config/test_serving_artifacts.py
+++ b/packages/sie_server/tests/config/test_serving_artifacts.py
@@ -5,6 +5,7 @@ import hashlib
import json
import os
import threading
+from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
@@ -99,6 +100,24 @@ def _stage_artifact(
return declaration, repo_root
+def _link_through_shared_blob_store(repo_root: Path, relative: str, *, store: Path | None = None) -> Path:
+ """Lay one snapshot file out the way huggingface_hub stores a Xet-backed blob."""
+ snapshot_entry = repo_root / "snapshots" / DERIVED_REVISION / relative
+ content = snapshot_entry.read_bytes()
+ xet_hash = _sha256(b"xet:" + content)
+ store = store if store is not None else repo_root.parent / "blobs"
+ store_entry = store / xet_hash[:2] / xet_hash
+ store_entry.parent.mkdir(parents=True, exist_ok=True)
+ store_entry.write_bytes(content)
+ store_entry.chmod(0o444)
+ repo_blob = repo_root / "blobs" / _sha256(content)[:40]
+ repo_blob.parent.mkdir(exist_ok=True)
+ repo_blob.symlink_to(os.path.relpath(store_entry, repo_blob.parent))
+ snapshot_entry.unlink()
+ snapshot_entry.symlink_to(os.path.relpath(repo_blob, snapshot_entry.parent))
+ return store_entry
+
+
def _materialize(
declaration: ServingArtifactDeclaration,
repo_root: Path,
@@ -303,6 +322,70 @@ def test_rejects_source_symlink_escaping_repository_cache(tmp_path: Path) -> Non
_materialize(declaration, repo_root, tmp_path / "materialized")
+def test_accepts_files_hugging_face_keeps_in_its_cache_wide_shared_blob_store(tmp_path: Path) -> None:
+ declaration, repo_root = _stage_artifact(tmp_path / "hub")
+ for relative in ("model.bin", "spiece.model", declaration.manifest_path):
+ _link_through_shared_blob_store(repo_root, relative)
+ model = repo_root / "snapshots" / DERIVED_REVISION / "model.bin"
+ assert model.resolve().is_relative_to(tmp_path / "hub" / "blobs")
+
+ verified = _materialize(declaration, repo_root, tmp_path / "materialized")
+
+ assert verified.artifact_count == 4
+ assert (verified.root / "model.bin").read_bytes() == b"converted model bytes"
+ assert (verified.root / "model.bin").stat().st_ino != model.resolve().stat().st_ino
+
+
+ "relocate",
+ [
+ pytest.param(
+ lambda hub, path: hub.parent / "elsewhere" / "blobs" / path.parent.name / path.name, id="other-cache"
+ ),
+ pytest.param(lambda hub, path: hub / "blobs" / path.name, id="no-prefix-directory"),
+ pytest.param(lambda hub, path: hub / "blobs" / "zz" / path.name, id="wrong-prefix"),
+ pytest.param(lambda hub, path: hub / "blobs" / path.parent.name / "model.bin", id="not-a-content-hash"),
+ pytest.param(lambda hub, path: hub / "other" / path.parent.name / path.name, id="other-directory"),
+ ],
+)
+def test_rejects_source_outside_the_shared_blob_store_layout(
+ tmp_path: Path, relocate: Callable[[Path, Path], Path]
+) -> None:
+ hub = tmp_path / "hub"
+ declaration, repo_root = _stage_artifact(hub)
+ store_entry = _link_through_shared_blob_store(repo_root, "model.bin")
+ moved = relocate(hub, store_entry)
+ moved.parent.mkdir(parents=True, exist_ok=True)
+ store_entry.rename(moved)
+ repo_blob = next((repo_root / "blobs").iterdir())
+ repo_blob.unlink()
+ repo_blob.symlink_to(os.path.relpath(moved, repo_blob.parent))
+
+ with pytest.raises(ValueError, match=r"escapes its repository cache: 'model\.bin'"):
+ _materialize(declaration, repo_root, tmp_path / "materialized")
+
+
+def test_rejects_a_shared_blob_store_linked_from_outside_the_cache(tmp_path: Path) -> None:
+ hub = tmp_path / "hub"
+ declaration, repo_root = _stage_artifact(hub)
+ outside = tmp_path / "outside-store"
+ outside.mkdir()
+ (hub / "blobs").symlink_to(outside, target_is_directory=True)
+ _link_through_shared_blob_store(repo_root, "model.bin")
+
+ with pytest.raises(ValueError, match=r"escapes its repository cache: 'model\.bin'"):
+ _materialize(declaration, repo_root, tmp_path / "materialized")
+
+
+def test_rejects_manifest_outside_the_repository_cache_and_shared_blob_store(tmp_path: Path) -> None:
+ hub = tmp_path / "hub"
+ declaration, repo_root = _stage_artifact(hub)
+ _link_through_shared_blob_store(repo_root, declaration.manifest_path, store=tmp_path / "elsewhere" / "blobs")
+
+ with pytest.raises(ValueError, match="manifest is not a safe regular file"):
+ _materialize(declaration, repo_root, tmp_path / "materialized")
+
+
def test_rejects_snapshot_revision_alias(tmp_path: Path) -> None:
declaration, repo_root = _stage_artifact(tmp_path / "hub")
snapshot = repo_root / "snapshots" / DERIVED_REVISION