Skip to content

Commit 44a3683

Browse files
committed
feat(validation): enforce HF certification isolation
1 parent fe40066 commit 44a3683

4 files changed

Lines changed: 131 additions & 2 deletions

File tree

docs/source/guides/runtime-providers.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ provider = DockerSwarmProvider()
164164

165165
Runs a registry image in a Hugging Face Sandbox. Pooled mode is the default and
166166
is appropriate for trusted same-user rollout fan-out. Dedicated mode allocates
167-
one VM per sandbox and is suited to untrusted or GPU workloads.
167+
one VM per sandbox and is suited to untrusted or GPU workloads. Official
168+
validation requires dedicated mode and rejects pooled execution.
168169

169170
```python
170171
from openenv.core.containers.runtime.hf_sandbox_provider import HFSandboxProvider
@@ -178,7 +179,8 @@ provider.wait_for_ready(base_url)
178179
```
179180

180181
Plain `env_vars` are visible as Job environment metadata. Use the dedicated
181-
mode's `secrets=` argument for encrypted runtime secrets.
182+
mode's `secrets=` argument for encrypted runtime secrets. Official validation
183+
does not forward coordinator secrets through either channel.
182184

183185
### KubernetesProvider
184186

src/openenv/validation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ValidationStatus,
1919
)
2020
from .planner import build_validation_plan, VALIDATION_POLICY_VERSION
21+
from .security import ensure_official_hf_sandbox
2122
from .task_manifest import (
2223
ArtifactSpec,
2324
EnvironmentConfig,
@@ -54,6 +55,7 @@
5455
"ValidationSeverity",
5556
"ValidationStatus",
5657
"build_validation_plan",
58+
"ensure_official_hf_sandbox",
5759
"execute_validation_plan",
5860
"format_shared_validation_report",
5961
"load_task_manifest",

src/openenv/validation/security.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
"""Fail-closed trust-boundary guards used by remote certification."""
4+
5+
from __future__ import annotations
6+
7+
import re
8+
from typing import Any
9+
10+
11+
_CREDENTIAL_NAME = re.compile(
12+
r"(?i)(authorization|credential|password|private.?key|secret|token|api.?key)"
13+
)
14+
15+
16+
def ensure_official_hf_sandbox(provider: Any) -> None:
17+
"""Reject shared HF pools for official certification workloads."""
18+
if getattr(provider, "isolation_mode", None) != "dedicated":
19+
raise RuntimeError(
20+
"Official certification requires a dedicated Hugging Face sandbox; "
21+
"pooled execution is a same-user trust boundary."
22+
)
23+
if getattr(provider, "secrets", None):
24+
raise RuntimeError(
25+
"Official subject sandboxes cannot receive coordinator secrets."
26+
)
27+
env_vars = getattr(provider, "env_vars", None) or {}
28+
credential_names = sorted(
29+
str(name) for name in env_vars if _CREDENTIAL_NAME.search(str(name))
30+
)
31+
if credential_names:
32+
raise RuntimeError(
33+
"Official subject sandboxes cannot receive credential-like "
34+
f"environment variables: {', '.join(credential_names)}"
35+
)
36+
lock = getattr(provider, "_lock_for_official_certification", None)
37+
if not callable(lock):
38+
raise RuntimeError(
39+
"Official certification requires a provider that can lock its "
40+
"validated execution settings."
41+
)
42+
lock()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
"""Tests for validation certification trust-boundary enforcement."""
4+
5+
from __future__ import annotations
6+
7+
from unittest.mock import MagicMock, patch
8+
9+
import pytest
10+
from openenv.core.containers.runtime import hf_sandbox_provider as provider_module
11+
from openenv.core.containers.runtime.hf_sandbox_provider import HFSandboxProvider
12+
from openenv.validation.security import ensure_official_hf_sandbox
13+
14+
15+
def test_official_certification_rejects_pooled_hf_sandbox() -> None:
16+
pooled = HFSandboxProvider(image="example")
17+
dedicated = HFSandboxProvider(image="example", mode="dedicated")
18+
19+
with pytest.raises(RuntimeError, match="dedicated"):
20+
ensure_official_hf_sandbox(pooled)
21+
22+
ensure_official_hf_sandbox(dedicated)
23+
24+
25+
def test_official_certification_rejects_forwarded_credentials() -> None:
26+
provider = HFSandboxProvider(
27+
image="example",
28+
mode="dedicated",
29+
env_vars={"HF_TOKEN": "secret"},
30+
)
31+
32+
with pytest.raises(RuntimeError, match="credential-like"):
33+
ensure_official_hf_sandbox(provider)
34+
35+
36+
def test_official_certification_locks_start_environment() -> None:
37+
provider = HFSandboxProvider(
38+
image="example",
39+
mode="dedicated",
40+
env_vars={"MODE": "certify"},
41+
)
42+
ensure_official_hf_sandbox(provider)
43+
44+
with pytest.raises(RuntimeError, match="environment overrides"):
45+
provider.start_container(env_vars={"HF_TOKEN": "secret"})
46+
47+
48+
def test_official_start_rejects_unverifiable_runtime_isolation() -> None:
49+
class SandboxWithoutHostIdentity:
50+
proxy_headers: dict[str, str] = {}
51+
52+
def run(self, *args: object, **kwargs: object) -> MagicMock:
53+
return MagicMock()
54+
55+
def proxy_url_for(self, port: int, path: str) -> str:
56+
return "https://sandbox.example/proxy"
57+
58+
def kill(self) -> None:
59+
self._killed = True
60+
61+
sandbox = SandboxWithoutHostIdentity()
62+
sandbox_cls = MagicMock()
63+
sandbox_cls.create.return_value = sandbox
64+
provider = HFSandboxProvider(image="example", mode="dedicated")
65+
ensure_official_hf_sandbox(provider)
66+
67+
with patch.object(provider_module, "_get_sandbox_cls", return_value=sandbox_cls):
68+
with pytest.raises(RuntimeError, match="verify dedicated isolation"):
69+
provider.start_container()
70+
71+
assert provider._sandbox is None
72+
73+
74+
def test_official_certification_cannot_lock_active_sandbox() -> None:
75+
provider = HFSandboxProvider(image="example", mode="dedicated")
76+
sandbox = MagicMock()
77+
sandbox.host_id = None
78+
provider._sandbox = sandbox
79+
80+
with pytest.raises(RuntimeError, match="before.*started"):
81+
ensure_official_hf_sandbox(provider)
82+
83+
assert provider._official_certification is False

0 commit comments

Comments
 (0)