Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(egress): reject null exchange markers; harden resolver CLI; retarget credential repr redaction
高然1a1fbac66dd4
diff --git a/components/egress/mitmscripts/system.py b/components/egress/mitmscripts/system.pyindex eccbb6997..9fc771015 100644--- a/components/egress/mitmscripts/system.py+++ b/components/egress/mitmscripts/system.py@@ -864,12 +864,12 @@ def _parse_active_vault(payload: Any) -> ActiveVault:def _parse_active_vault_exchange(binding: dict[str, Any], index: int) -> dict[str, str] | None:"""Validate the optional exchange marker: exactly ``{"credential": name}``- with explicitly empty ``headers``/``substitutions``; anything else fails- the snapshot closed.+ with explicitly empty ``headers``/``substitutions``; any present value+ (including ``null``) other than that shape fails the snapshot closed."""- raw_exchange = binding.get("exchange")- if raw_exchange is None:+ if "exchange" not in binding:return None+ raw_exchange = binding["exchange"]if not isinstance(raw_exchange, dict) or set(raw_exchange) != {"credential"}:raise ActiveVaultLookupError(f"active vault binding {index} exchange must contain only credential"diff --git a/components/egress/tests/resolver/resolver.py b/components/egress/tests/resolver/resolver.pyindex 7de64a93e..92bcfeac6 100644--- a/components/egress/tests/resolver/resolver.py+++ b/components/egress/tests/resolver/resolver.py@@ -53,8 +53,8 @@ import randomimport socketimport socketserverimport ssl+import statimport sys-import threadingimport timeimport tracebackfrom typing import Any@@ -347,6 +347,12 @@ def build_server("""Construct (but do not start) the resolver server for one transport."""if transport == "uds":if os.path.exists(options.uds):+ # Only a stale socket may be removed; never unlink an arbitrary+ # filesystem object that happens to sit on the requested path.+ if not stat.S_ISSOCK(os.stat(options.uds).st_mode):+ raise SystemExit(+ f"--uds path exists and is not a socket: {options.uds}"+ )os.unlink(options.uds)server = _UnixHTTPServer(options.uds, _Handler)os.chmod(options.uds, 0o660)@@ -376,9 +382,9 @@ def serve(server: http.server.HTTPServer) -> None:try:server.serve_forever()except KeyboardInterrupt:- pass+ pass # Ctrl+C is the expected shutdown signal; cleanup runs below.finally:- server_close()+ server.server_close()if server.transport == "uds":with contextlib.suppress(OSError):os.unlink(server.server_address)diff --git a/components/egress/tests/test_mitmscripts_system.py b/components/egress/tests/test_mitmscripts_system.pyindex b791d4d3d..23e2153b8 100644--- a/components/egress/tests/test_mitmscripts_system.py+++ b/components/egress/tests/test_mitmscripts_system.py@@ -1413,6 +1413,7 @@ class SystemAddonActiveVaultStrictParseTest(unittest.TestCase):def test_exchange_marker_shape_rejects(self) -> None:markers = {"non-object": "spi-signer",+ "null-object": None,"empty-object": {},"extra-key": {"credential": "spi-signer", "endpoint": "unix:///run/x.sock"},"missing-credential": {"endpoint": "unix:///run/x.sock"},diff --git a/sdks/sandbox/python/scripts/generate_api.py b/sdks/sandbox/python/scripts/generate_api.pyindex 84b7b5609..c032f2728 100644--- a/sdks/sandbox/python/scripts/generate_api.py+++ b/sdks/sandbox/python/scripts/generate_api.py@@ -359,9 +359,10 @@ def redact_credential_reprs() -> None:"""Keep write-only credential values out of generated-model reprs.attrs synthesizes ``__repr__`` over any class-level definition, so a- module-level override is appended instead: ``InlineCredentialSource.value``- is write-only material and must never surface in logs, exception messages- (pydantic/attrs reprs included), or debug output.+ module-level override is appended instead: ``CredentialSource.value`` is+ write-only material and must never surface in logs, exception messages+ (pydantic/attrs reprs included), or debug output. ``endpoint`` is+ configuration rather than secret material and stays visible."""redaction = ("\n\n"@@ -369,14 +370,18 @@ def redact_credential_reprs() -> None:"# surface in logs, exception messages, or debug output. attrs would\n""# overwrite a class-level __repr__, so the override is applied on the\n""# class object after definition.\n"- "def _redacted_repr(self: InlineCredentialSource) -> str:\n"- ' return f"{type(self).__name__}(value=***)"\n'+ "def _redacted_repr(self: CredentialSource) -> str:\n"+ " value = \"***\" if self.value is not UNSET else \"UNSET\"\n"+ " return (\n"+ " f\"{type(self).__name__}(type_={self.type_!r}, value={value},\"\n"+ " f\" endpoint={self.endpoint!r})\"\n"+ " )\n""\n\n"- "InlineCredentialSource.__repr__ = _redacted_repr # type: ignore[method-assign]\n"+ "CredentialSource.__repr__ = _redacted_repr # type: ignore[method-assign]\n")for rel in (- "src/opensandbox/api/lifecycle/models/inline_credential_source.py",- "src/opensandbox/api/egress/models/inline_credential_source.py",+ "src/opensandbox/api/lifecycle/models/credential_source.py",+ "src/opensandbox/api/egress/models/credential_source.py",):path = Path(rel)if not path.exists():diff --git a/sdks/sandbox/python/tests/test_generate_api_redaction.py b/sdks/sandbox/python/tests/test_generate_api_redaction.pynew file mode 100644index 000000000..25f60385f--- /dev/null+++ b/sdks/sandbox/python/tests/test_generate_api_redaction.py@@ -0,0 +1,129 @@+# Copyright 2026 The OpenSandbox Authors+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++"""Regression tests for the generated CredentialSource repr redaction."""++from __future__ import annotations++import importlib.util+import os+import tempfile+import unittest+from pathlib import Path++_SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "generate_api.py"++FIXTURE_MODEL = """\+class CredentialSourceType(str):+ INLINE = "inline"+ EXCHANGE = "exchange"+++class _Unset:+ def __repr__(self) -> str:+ return "UNSET"+++UNSET = _Unset()+++class CredentialSource:+ def __init__(self, type_, value=UNSET, endpoint=UNSET):+ self.type_ = type_+ self.value = value+ self.endpoint = endpoint+"""+++def _load_generate_api():+ spec = importlib.util.spec_from_file_location(+ "generate_api_under_test", _SCRIPT+ )+ module = importlib.util.module_from_spec(spec)+ assert spec.loader is not None+ spec.loader.exec_module(module)+ return module+++class RedactCredentialReprsTest(unittest.TestCase):+ def setUp(self) -> None:+ self._tmp = tempfile.TemporaryDirectory()+ self.addCleanup(self._tmp.cleanup)+ self._cwd = os.getcwd()+ os.chdir(self._tmp.name)+ self.addCleanup(os.chdir, self._cwd)+ self.module = _load_generate_api()++ def _write_model(self, rel: str) -> Path:+ path = Path(rel)+ path.parent.mkdir(parents=True)+ path.write_text(FIXTURE_MODEL, encoding="utf-8")+ return path++ def test_value_is_redacted_while_type_and_endpoint_stay_visible(self) -> None:+ path = self._write_model(+ "src/opensandbox/api/lifecycle/models/credential_source.py"+ )++ self.module.redact_credential_reprs()++ namespace: dict[str, object] = {+ "__name__": "credential_source_under_test"+ }+ content = path.read_text(encoding="utf-8")+ exec(compile(content, str(path), "exec"), namespace)+ source = namespace["CredentialSource"]( # type: ignore[operator]+ namespace["CredentialSourceType"].INLINE, # type: ignore[attr-defined]+ "super-secret-value",+ "unix:///run/resolver.sock",+ )+ rendered = repr(source)+ self.assertNotIn("super-secret-value", rendered)+ self.assertIn("value=***", rendered)+ self.assertIn("unix:///run/resolver.sock", rendered)++ def test_unset_value_rendered_without_redaction_marker(self) -> None:+ path = self._write_model(+ "src/opensandbox/api/egress/models/credential_source.py"+ )++ self.module.redact_credential_reprs()++ namespace: dict[str, object] = {+ "__name__": "credential_source_under_test"+ }+ content = path.read_text(encoding="utf-8")+ exec(compile(content, str(path), "exec"), namespace)+ source = namespace["CredentialSource"]( # type: ignore[operator]+ namespace["CredentialSourceType"].EXCHANGE # type: ignore[attr-defined]+ )+ rendered = repr(source)+ self.assertIn("value=UNSET", rendered)+ self.assertNotIn("***", rendered)++ def test_idempotent_and_skips_absent_models(self) -> None:+ path = self._write_model(+ "src/opensandbox/api/lifecycle/models/credential_source.py"+ )++ self.module.redact_credential_reprs()+ once = path.read_text(encoding="utf-8")+ self.module.redact_credential_reprs()++ self.assertEqual(once, path.read_text(encoding="utf-8"))+ self.assertEqual(once.count("_redacted_repr"), 2)+++if __name__ == "__main__":+ unittest.main()