-
Notifications
You must be signed in to change notification settings - Fork 16
fix(master): load per-challenge embed.env into isolated child env #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+258
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| """Embedded challenge env-file contract for the master supervisor. | ||
|
|
||
| ``docker/master-entrypoint.sh`` launches each embedded challenge under | ||
| ``env -i`` so Prism never inherits ``CHALLENGE_*`` and agent-challenge never | ||
| inherits ``PRISM_*``. That isolation is deliberate, but it also dropped every | ||
| operator-supplied setting -- notably the Phala attestation switches | ||
| (``CHALLENGE_PHALA_ATTESTATION_ENABLED``, ``CHALLENGE_ATTESTED_REVIEW_ENABLED``) | ||
| and ``PHALA_CLOUD_API_KEY`` -- because the allowlist was hardcoded with no | ||
| extension point. | ||
|
|
||
| These tests lock the supported extension point: a per-challenge env file whose | ||
| keys are merged into the isolated child environment, without breaking | ||
| cross-challenge isolation. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[2] | ||
| ENTRYPOINT = REPO_ROOT / "docker/master-entrypoint.sh" | ||
|
|
||
| FAKE_UVICORN = """#!/usr/bin/env bash | ||
| # Dump the isolated child environment so the test can assert on it. | ||
| # The dump path is baked in: `env -i` strips DUMP_DIR from the child env. | ||
| target="unknown" | ||
| for arg in "$@"; do | ||
| case "${arg}" in | ||
| prism_challenge.app:app) target="prism" ;; | ||
| agent_challenge.app:app) target="ac" ;; | ||
| esac | ||
| done | ||
| env > "__DUMP_DIR__/${target}.env" | ||
| """ | ||
|
|
||
| FAKE_PYTHON = """#!/usr/bin/env bash | ||
| exit 0 | ||
| """ | ||
|
|
||
|
|
||
| def _write_exec(path: Path, body: str) -> None: | ||
| path.write_text(body, encoding="utf-8") | ||
| path.chmod(0o755) | ||
|
|
||
|
|
||
| def _run_entrypoint(tmp_path: Path, ac_env_file_body: str | None) -> dict[str, str]: | ||
| """Run the entrypoint with stubbed uvicorn/python; return child env dumps.""" | ||
|
|
||
| bin_dir = tmp_path / "bin" | ||
| dump_dir = tmp_path / "dump" | ||
| ac_dir = tmp_path / "ac" | ||
| prism_dir = tmp_path / "prism" | ||
| for directory in (bin_dir, dump_dir, ac_dir, prism_dir): | ||
| directory.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| _write_exec( | ||
| bin_dir / "uvicorn", FAKE_UVICORN.replace("__DUMP_DIR__", str(dump_dir)) | ||
| ) | ||
| _write_exec(bin_dir / "python", FAKE_PYTHON) | ||
|
|
||
| token_file = tmp_path / "shared_token" | ||
| token_file.write_text("test-token", encoding="utf-8") | ||
|
|
||
| if ac_env_file_body is not None: | ||
| (ac_dir / "embed.env").write_text(ac_env_file_body, encoding="utf-8") | ||
|
|
||
| env = { | ||
| "PATH": f"{bin_dir}:{os.environ.get('PATH', '/usr/bin:/bin')}", | ||
| "HOME": str(tmp_path), | ||
| "DUMP_DIR": str(dump_dir), | ||
| "BASE_MASTER_AC_DATA_DIR": str(ac_dir), | ||
| "BASE_MASTER_PRISM_DATA_DIR": str(prism_dir), | ||
| "PRISM_SHARED_TOKEN_FILE": str(token_file), | ||
| "CHALLENGE_SHARED_TOKEN_FILE": str(token_file), | ||
| } | ||
|
|
||
| subprocess.run( | ||
| ["bash", str(ENTRYPOINT), "/bin/true"], | ||
| env=env, | ||
| check=True, | ||
| capture_output=True, | ||
| timeout=60, | ||
| ) | ||
|
|
||
| dumps: dict[str, str] = {} | ||
| for name in ("ac", "prism"): | ||
| dump = dump_dir / f"{name}.env" | ||
| dumps[name] = dump.read_text(encoding="utf-8") if dump.is_file() else "" | ||
| return dumps | ||
|
|
||
|
|
||
| def test_ac_env_file_supplies_phala_settings_to_isolated_child(tmp_path: Path) -> None: | ||
| """Operator embed.env must reach the agent-challenge child under env -i.""" | ||
|
|
||
| dumps = _run_entrypoint( | ||
| tmp_path, | ||
| "\n".join( | ||
| [ | ||
| "# durable AC embed overrides", | ||
| "CHALLENGE_PHALA_ATTESTATION_ENABLED=true", | ||
| "CHALLENGE_ATTESTED_REVIEW_ENABLED=true", | ||
| "PHALA_CLOUD_API_KEY=phala-secret", | ||
| "BASE_CHALLENGE_SLUG=agent-challenge", | ||
| "", | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| ac_env = dumps["ac"] | ||
| assert "CHALLENGE_PHALA_ATTESTATION_ENABLED=true" in ac_env | ||
| assert "CHALLENGE_ATTESTED_REVIEW_ENABLED=true" in ac_env | ||
| assert "PHALA_CLOUD_API_KEY=phala-secret" in ac_env | ||
| assert "BASE_CHALLENGE_SLUG=agent-challenge" in ac_env | ||
|
|
||
|
|
||
| def test_ac_env_file_does_not_leak_into_prism_child(tmp_path: Path) -> None: | ||
| """Cross-challenge isolation must survive the env-file merge.""" | ||
|
|
||
| dumps = _run_entrypoint( | ||
| tmp_path, | ||
| "CHALLENGE_PHALA_ATTESTATION_ENABLED=true\nPHALA_CLOUD_API_KEY=phala-secret\n", | ||
| ) | ||
|
|
||
| prism_env = dumps["prism"] | ||
| assert "CHALLENGE_PHALA_ATTESTATION_ENABLED" not in prism_env | ||
| assert "PHALA_CLOUD_API_KEY" not in prism_env | ||
|
|
||
|
|
||
| def test_ac_env_file_overrides_builtin_default(tmp_path: Path) -> None: | ||
| """File-provided values win over the hardcoded defaults.""" | ||
|
|
||
| dumps = _run_entrypoint(tmp_path, "CHALLENGE_DOCKER_ENABLED=true\n") | ||
|
|
||
| assert "CHALLENGE_DOCKER_ENABLED=true" in dumps["ac"] | ||
| assert "CHALLENGE_DOCKER_ENABLED=false" not in dumps["ac"] | ||
|
|
||
|
|
||
| def test_missing_env_file_is_not_fatal(tmp_path: Path) -> None: | ||
| """Absent embed.env keeps the previous behaviour (defaults only).""" | ||
|
|
||
| dumps = _run_entrypoint(tmp_path, None) | ||
|
|
||
| assert "CHALLENGE_DOCKER_ENABLED=false" in dumps["ac"] | ||
| assert "PHALA_CLOUD_API_KEY" not in dumps["ac"] | ||
|
|
||
|
|
||
| def test_env_file_ignores_comments_blanks_and_malformed_keys(tmp_path: Path) -> None: | ||
| """Only well-formed KEY=VALUE lines with allowed prefixes are exported.""" | ||
|
|
||
| dumps = _run_entrypoint( | ||
| tmp_path, | ||
| "\n".join( | ||
| [ | ||
| "# comment", | ||
| "", | ||
| " ", | ||
| "export CHALLENGE_EVAL_MAX_ATTEMPTS=3", | ||
| "not-a-valid-key=nope", | ||
| "RANDOM_UNRELATED=leak", | ||
| "CHALLENGE_EVAL_APP_IDENTITY=app-id", | ||
| "", | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| ac_env = dumps["ac"] | ||
| assert "CHALLENGE_EVAL_MAX_ATTEMPTS=3" in ac_env | ||
| assert "CHALLENGE_EVAL_APP_IDENTITY=app-id" in ac_env | ||
| assert "RANDOM_UNRELATED" not in ac_env | ||
| assert "not-a-valid-key" not in ac_env | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Do not persist raw API-key values in test artifacts.
The fake
uvicorndumps the full child environment, including the rawPHALA_CLOUD_API_KEY; the related fixtures and assertions also retain that value. Redact or hash sensitive variables in the dump, and use a SHA/digest fixture when validating propagation.tests/unit/test_master_embed_env_file.py#L25-L36: redact/hash sensitive environment values before writing the dump.tests/unit/test_master_embed_env_file.py#L94-L115: replace the raw API-key fixture/assertion with a digest-based value.tests/unit/test_master_embed_env_file.py#L118-L128: remove the raw API-key fixture from the isolation test.As per coding guidelines: “Never log, document, or include evidence containing private keys, wallet mnemonics, API tokens, or full secret values; only names, digests, and SHAs are permitted.”
📍 Affects 1 file
tests/unit/test_master_embed_env_file.py#L25-L36(this comment)tests/unit/test_master_embed_env_file.py#L94-L115tests/unit/test_master_embed_env_file.py#L118-L128🤖 Prompt for AI Agents
Source: Coding guidelines