#!/usr/bin/env python3 """test_secret_boundary_contract.py — Structural and sentinel tests for secret boundary (Issue #412). Tests verify: 1. sentinel fixture does not appear in manifest/log/artifact in any encoded form (AC10) 2. settings.json contains PreToolUse secret_boundary_guard hook (AC13) 3. guard exits with code 2 on malformed stdin (AC13) 4. guard stderr does not echo command/path/env values (AC4, AC13) 5. session-manifest.yml has no pull_request_target (AC7, AC13) 6. session-manifest.yml permissions are contents: read or {} (AC6, AC13) 7. session-manifest.yml has no secrets. references (AC5, AC13) 8. guard blocks credential file access via Bash (B3) 9. manifest schema contract includes secret_policy field (B2) """ import json import re import subprocess from pathlib import Path import pytest # Resolve paths relative to this test file so that worktree isolation is maintained. # git rev-parse --show-toplevel returns the main repo root (not the worktree), # so we use __file__ to anchor paths to the worktree. # # Test file is at: /.claude/hooks/tests/test_secret_boundary_contract.py # Worktree root is: / _THIS_FILE = Path(__file__).resolve() REPO_ROOT = _THIS_FILE.parent.parent.parent.parent # worktree root SETTINGS_JSON_PATH = REPO_ROOT / ".claude" / "settings.json" GUARD_PATH = REPO_ROOT / ".claude" / "hooks" / "secret_boundary_guard.sh" # ============================================================================= # Sentinel fixture # ============================================================================= SENTINEL_PLAINTEXT = "SENTINEL_SECRET_412_TEST_abc123XYZ" def _encode_variants(value: str) -> list[str]: """Return multiple encoded representations of the sentinel value.""" import base64 import hashlib import urllib.parse b64 = base64.b64encode(value.encode()).decode() b64url = base64.urlsafe_b64encode(value.encode()).decode() variants = [ value, # raw b64, # base64 b64url, # base64url value.encode().hex(), # hex urllib.parse.quote(value), # urlencoded hashlib.sha256(value.encode()).hexdigest(), # sha256 hash hashlib.sha1(value.encode()).hexdigest(), # sha1 hash value[:8], # prefix partial (first 8 chars) value[-8:], # suffix partial (last 8 chars) ] return variants def _scan_file_for_sentinel(file_path: Path, sentinel: str) -> list[str]: """Return list of matching lines found in file.""" if not file_path.exists(): return [] variants = _encode_variants(sentinel) hits = [] content = file_path.read_text(errors="replace") for variant in variants: for i, line in enumerate(content.splitlines(), 1): if variant in line: hits.append(f"{file_path}:{i}: found variant '{variant[:20]}...'") return hits # ============================================================================= # AC10: sentinel not injected into manifest/artifact in any encoded form # ============================================================================= def test_sentinel_not_in_settings_json(): """GIVEN a sentinel secret value, WHEN scanning settings.json, THEN the sentinel must not appear in any encoded form.""" hits = _scan_file_for_sentinel(SETTINGS_JSON_PATH, SENTINEL_PLAINTEXT) assert hits == [], f"Sentinel found in settings.json: {hits}" # ============================================================================= # AC13: settings.json contains PreToolUse secret_boundary_guard hook # ============================================================================= def test_settings_has_pretooluse_secret_boundary_guard(): """GIVEN settings.json exists, WHEN checking hooks, THEN PreToolUse section must contain secret_boundary_guard hook.""" assert SETTINGS_JSON_PATH.exists(), f"settings.json not found: {SETTINGS_JSON_PATH}" with open(SETTINGS_JSON_PATH) as f: settings = json.load(f) hooks = settings.get("hooks", {}) pre_tool_use = hooks.get("PreToolUse", []) assert pre_tool_use, "PreToolUse hooks section is missing or empty" # Check that at least one hook references secret_boundary_guard guard_found = False for entry in pre_tool_use: for hook in entry.get("hooks", []): command = hook.get("command", "") args = hook.get("args", []) if "secret_boundary_guard" in command or any( "secret_boundary_guard" in str(a) for a in args ): guard_found = True break assert guard_found, ( "secret_boundary_guard not found in PreToolUse hooks in settings.json" ) # ============================================================================= # AC13: guard exits with code 2 on malformed stdin # ============================================================================= def test_guard_exits_2_on_malformed_stdin(): """GIVEN secret_boundary_guard.sh exists, WHEN given malformed stdin (not JSON), THEN exit code must be 2.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" result = subprocess.run( [str(GUARD_PATH)], input="not valid json", text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for malformed stdin, got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_exits_2_on_empty_stdin(): """GIVEN secret_boundary_guard.sh exists, WHEN given empty stdin, THEN exit code must be 2 (tool_name missing = fail closed).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" result = subprocess.run( [str(GUARD_PATH)], input="{}", text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for empty JSON (tool_name missing), got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_allows_safe_bash_command(): """GIVEN a safe Bash tool input, WHEN guard processes it, THEN exit code must be 0.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "echo hello"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 0, ( f"Expected exit code 0 for safe command, got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_printenv(): """GIVEN a Bash tool input with printenv, WHEN guard processes it, THEN exit code must be 2.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "printenv"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for printenv, got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_env_file_read(): """GIVEN a Read tool input targeting .env file, WHEN guard processes it, THEN exit code must be 2.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Read", "tool_input": {"file_path": "/repo/.env"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for .env read, got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_gh_secret(): """GIVEN a Bash tool input with 'gh secret', WHEN guard processes it, THEN exit code must be 2.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "gh secret list"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for 'gh secret list', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) # ============================================================================= # AC4 / AC13: guard stderr does not echo command/path/env values # ============================================================================= def test_guard_blocks_sed_env_read(): """GIVEN a Bash tool input using sed to read .env, WHEN guard processes it, THEN exit code must be 2 (B5: sed bypass detection).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "sed -n p .env"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for 'sed -n p .env', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_awk_env_read(): """GIVEN a Bash tool input using awk to read .env, WHEN guard processes it, THEN exit code must be 2 (B5: awk bypass detection).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "awk 1 .env"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for 'awk 1 .env', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_env_standalone(): """GIVEN a Bash tool input with bare 'env' command, WHEN guard processes it, THEN exit code must be 2 (B1/B2: env dumps all environment variables).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "env"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for bare 'env', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_blocks_set_standalone(): """GIVEN a Bash tool input with bare 'set' command, WHEN guard processes it, THEN exit code must be 2 (B1/B2: set dumps all shell variables).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": "set"}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for bare 'set', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) def test_guard_stderr_no_command_echo_on_block(): """GIVEN guard blocks a command, WHEN checking stderr, THEN stderr must not contain the actual command string.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" secret_command = "printenv MY_SECRET_TOKEN_xyz987" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": secret_command}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, "Guard should have blocked this command" # The actual command value must NOT appear in stderr assert "MY_SECRET_TOKEN_xyz987" not in result.stderr, ( f"Guard stderr leaked command value: {result.stderr[:200]}" ) assert "printenv" not in result.stderr.lower() or "printenv" not in secret_command.split()[0], ( # Allow "blocked: high-risk Bash command pattern detected" — generic message is OK # But should not echo back the actual command string "Guard stderr must use generic message, not echo command details" ) def test_guard_stderr_no_path_echo_on_block(): """GIVEN guard blocks a path access, WHEN checking stderr, THEN stderr must not contain the actual path.""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" secret_path = "/home/user/secrets/my_api_key.txt" payload = json.dumps({"tool_name": "Read", "tool_input": {"file_path": secret_path}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, "Guard should have blocked this path" # The actual path must NOT appear in stderr assert "my_api_key" not in result.stderr, ( f"Guard stderr leaked path value: {result.stderr[:200]}" ) # ============================================================================= # B3: guard blocks credential file access via Bash (cat/sed/awk/etc.) # ============================================================================= @pytest.mark.parametrize("credential_path", [ "~/.netrc", "~/.npmrc", "~/.pypirc", "~/.aws/credentials", "~/.aws/config", "~/.config/gcloud/application_default_credentials.json", "~/.kube/config", "~/.kube/credentials", ]) def test_guard_blocks_cat_credential_files(credential_path): """GIVEN a Bash tool input using cat to read a credential file, WHEN guard processes it, THEN exit code must be 2 (B3: credential file bypass via Bash).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" command = f"cat {credential_path}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": command}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for 'cat {credential_path}', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) @pytest.mark.parametrize("bypass_cmd", [ "sed -n '1p' ~/.netrc", "awk '{print}' ~/.aws/credentials", "grep '' ~/.npmrc", "rg . ~/.pypirc", ]) def test_guard_blocks_bypass_commands_on_credential_files(bypass_cmd): """GIVEN a Bash tool input using bypass commands (sed/awk/grep/rg) on credential files, WHEN guard processes it, THEN exit code must be 2 (B3: bypass detection for credential files).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Bash", "tool_input": {"command": bypass_cmd}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for '{bypass_cmd}', got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) @pytest.mark.parametrize("sensitive_path", [ "/home/user/.netrc", "/home/user/.npmrc", "/home/user/.pypirc", "/home/user/.aws/credentials", "/home/user/.aws/config", "/home/user/.kube/config", "/home/user/.kube/credentials", "/home/user/.config/gcloud/application_default_credentials.json", ]) def test_guard_blocks_read_tool_on_credential_files(sensitive_path): """GIVEN a Read tool input targeting a credential file, WHEN guard processes it, THEN exit code must be 2 (B3: credential path detection for Read tool).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" payload = json.dumps({"tool_name": "Read", "tool_input": {"file_path": sensitive_path}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == 2, ( f"Expected exit code 2 for Read({sensitive_path}), got {result.returncode}\n" f"stderr: {result.stderr[:200]}" ) # ============================================================================= # B2 (alternative): manifest schema contract includes secret_policy field # ============================================================================= # ============================================================================= # B4: settings.json deny rules use ./ prefix form # ============================================================================= def test_settings_deny_has_dot_slash_prefix_forms(): """GIVEN settings.json exists, WHEN checking deny rules, THEN at least one deny rule must use ./ prefix form (e.g. Read(./.env)). This ensures B4 fix: path matching covers both relative forms. """ assert SETTINGS_JSON_PATH.exists(), f"settings.json not found: {SETTINGS_JSON_PATH}" with open(SETTINGS_JSON_PATH) as f: settings = json.load(f) deny_rules = settings.get("permissions", {}).get("deny", []) assert deny_rules, "No deny rules found in settings.json" dot_slash_rules = [r for r in deny_rules if re.search(r"Read\(\./", r)] assert len(dot_slash_rules) > 0, ( f"No deny rules with './' prefix form found.\n" f"Expected at least 'Read(./.env)' or similar.\n" f"Current deny rules: {deny_rules}" ) # ============================================================================= # AC7 / AC13: workflow has no pull_request_target # ============================================================================= # ============================================================================= # AC6 / AC13: workflow permissions are contents: read or {} # ============================================================================= # ============================================================================= # AC5 / AC13: workflow has no secrets. references # ============================================================================= # ============================================================================= # MultiEdit regression: 既存 tool の非回帰確認 (AC4) # ============================================================================= def test_multiedit_in_sensitive_tools(): """GIVEN secret_boundary_guard.sh exists, WHEN checking SENSITIVE_TOOLS definition, THEN MultiEdit must be included (AC1 structural check via script content).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" content = GUARD_PATH.read_text() assert "MultiEdit" in content, ( "MultiEdit not found in secret_boundary_guard.sh — AC1 violated" ) # SENSITIVE_TOOLS 行に MultiEdit が含まれることを確認 sensitive_tools_line = [ line for line in content.splitlines() if "SENSITIVE_TOOLS" in line and "MultiEdit" in line ] assert sensitive_tools_line, ( "SENSITIVE_TOOLS variable definition does not include MultiEdit" ) def test_settings_multiedit_in_secret_guard_matcher(): """GIVEN settings.json exists, WHEN checking secret_boundary_guard matcher, THEN MultiEdit must be included in the matcher (AC2).""" assert SETTINGS_JSON_PATH.exists(), f"settings.json not found: {SETTINGS_JSON_PATH}" with open(SETTINGS_JSON_PATH) as f: settings = json.load(f) pre_tool_use = settings.get("hooks", {}).get("PreToolUse", []) assert pre_tool_use, "PreToolUse hooks section is missing or empty" for entry in pre_tool_use: for hook in entry.get("hooks", []): command = hook.get("command", "") if "secret_boundary_guard" in command: matcher = entry.get("matcher", "") assert "MultiEdit" in matcher.split("|"), ( f"secret_boundary_guard matcher does not include MultiEdit: {matcher!r}" ) return pytest.fail("secret_boundary_guard not found in PreToolUse hooks") @pytest.mark.parametrize("tool_name,path,expected_exit", [ # 既存 tool の block 挙動が回帰しないことを確認 ("Read", "/home/user/.env", 2), ("Write", "/home/user/.env", 2), ("Edit", "/home/user/.env", 2), ("Grep", "/home/user/.env", 2), ("Glob", "/home/user/.env", 2), # MultiEdit も同様に block されること ("MultiEdit", "/home/user/.env", 2), # MultiEdit で safe path は allow されること ("MultiEdit", "/home/user/projects/src/main.py", 0), ]) def test_existing_tools_no_regression(tool_name, path, expected_exit): """GIVEN guard processes various tool inputs, WHEN checking exit codes, THEN existing tool block/allow behavior must not regress (AC4).""" assert GUARD_PATH.exists(), f"Guard script not found: {GUARD_PATH}" if tool_name in ("Glob", "Grep"): payload = json.dumps({"tool_name": tool_name, "tool_input": {"pattern": path}}) else: payload = json.dumps({"tool_name": tool_name, "tool_input": {"file_path": path}}) result = subprocess.run( [str(GUARD_PATH)], input=payload, text=True, capture_output=True, ) assert result.returncode == expected_exit, ( f"Expected exit code {expected_exit} for {tool_name}({path}), " f"got {result.returncode}\n" f"stderr: {result.stderr[:200]}" )