|
| 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