|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""PreToolUse hook: confine the reviewer's Read/Glob/Grep to the reviewed checkout. |
| 3 | +
|
| 4 | +Read/Glob/Grep are otherwise unrestricted by path, so the untrusted model could read the |
| 5 | +OIDC credentials in /proc/self/environ or $GITHUB_ENV and the scoped checkout token in |
| 6 | +./pytorch/.git/config. This is deny-by-default: a target is allowed only when its |
| 7 | +os.path.realpath (symlinks and '..' resolved, because the ./pytorch tree is attacker- |
| 8 | +controlled) lands under an allowed root with an os.sep boundary, and never when the resolved |
| 9 | +path carries a .git component. exit 2 blocks with a stderr reason; exit 0 defers to the normal |
| 10 | +permission flow. Depends only on the standard library so it runs under the CI system python3, |
| 11 | +and fails closed on ANY error: claude-code-action treats every non-2 exit as non-blocking, so |
| 12 | +main() converts any unexpected exception into a blocking exit 2. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
| 21 | +_SCRATCH_BASENAME_PREFIX = "greenlight-" |
| 22 | +_ALLOWED_DESC = "./pytorch, ./.claude/skills, ./.claude/hooks, and /tmp/greenlight-* scratch" |
| 23 | + |
| 24 | + |
| 25 | +def _deny(reason: str) -> int: |
| 26 | + print(reason, file=sys.stderr) |
| 27 | + return 2 |
| 28 | + |
| 29 | + |
| 30 | +def _scratch_prefix() -> str: |
| 31 | + # /tmp is a symlink on macOS (-> /private/tmp); realpath it so this prefix matches the |
| 32 | + # realpath of the target on both the CI runner (/tmp) and dev machines (/private/tmp). |
| 33 | + return os.path.realpath("/tmp") + os.sep + _SCRATCH_BASENAME_PREFIX # noqa: S108 |
| 34 | + |
| 35 | + |
| 36 | +def _allowed_roots(workspace: str) -> list[str]: |
| 37 | + roots = [ |
| 38 | + os.path.join(workspace, "pytorch"), |
| 39 | + os.path.join(workspace, ".claude", "skills"), |
| 40 | + os.path.join(workspace, ".claude", "hooks"), |
| 41 | + ] |
| 42 | + return [os.path.realpath(root) for root in roots] |
| 43 | + |
| 44 | + |
| 45 | +def _under_root(resolved: str, root: str) -> bool: |
| 46 | + return resolved == root or resolved.startswith(root + os.sep) |
| 47 | + |
| 48 | + |
| 49 | +def _reject_dotdot(field: str, value: str) -> int: |
| 50 | + if ".." in value: |
| 51 | + return _deny(f"read blocked: '..' is not allowed in {field}.") |
| 52 | + return 0 |
| 53 | + |
| 54 | + |
| 55 | +def _reject_glob_syntax(field: str, value: object) -> int: |
| 56 | + # pattern (Glob) / glob (Grep) are glob syntax, not paths: a '..' or a leading '/' escapes the |
| 57 | + # confined search path. Grep's 'pattern' is a search regex where '..' is legitimate ("any two |
| 58 | + # chars"), so it is never routed here. |
| 59 | + if not isinstance(value, str): |
| 60 | + return 0 |
| 61 | + denied = _reject_dotdot(field, value) |
| 62 | + if denied: |
| 63 | + return denied |
| 64 | + if value.startswith("/"): |
| 65 | + return _deny(f"read blocked: an absolute {field} is not allowed; pass a relative glob under ./pytorch.") |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +def _check_target(target: str, workspace: str) -> int: |
| 70 | + resolved = os.path.realpath(target) |
| 71 | + # Lowercase the components: a case-insensitive filesystem serves ./pytorch/.GIT/config too. |
| 72 | + if ".git" in [part.lower() for part in resolved.split(os.sep)]: |
| 73 | + return _deny(f"read blocked: '.git' is off-limits ({resolved}).") |
| 74 | + if resolved.startswith(_scratch_prefix()): |
| 75 | + return 0 |
| 76 | + if any(_under_root(resolved, root) for root in _allowed_roots(workspace)): |
| 77 | + return 0 |
| 78 | + return _deny(f"read blocked: {resolved} is outside the allowed roots ({_ALLOWED_DESC}).") |
| 79 | + |
| 80 | + |
| 81 | +def _check_read(tool_input: dict[str, object], workspace: str) -> int: |
| 82 | + file_path = tool_input.get("file_path") |
| 83 | + if not isinstance(file_path, str) or not file_path: |
| 84 | + return _deny("read blocked: Read requires a file_path under ./pytorch.") |
| 85 | + denied = _reject_dotdot("file_path", file_path) |
| 86 | + if denied: |
| 87 | + return denied |
| 88 | + return _check_target(file_path, workspace) |
| 89 | + |
| 90 | + |
| 91 | +def _check_search_path(tool_input: dict[str, object], workspace: str) -> int: |
| 92 | + path = tool_input.get("path") |
| 93 | + if not isinstance(path, str) or not path: |
| 94 | + return _deny(f"read blocked: reads are confined to {_ALLOWED_DESC}; pass an explicit path under ./pytorch.") |
| 95 | + denied = _reject_dotdot("path", path) |
| 96 | + if denied: |
| 97 | + return denied |
| 98 | + return _check_target(path, workspace) |
| 99 | + |
| 100 | + |
| 101 | +def _check_glob(tool_input: dict[str, object], workspace: str) -> int: |
| 102 | + denied = _reject_glob_syntax("pattern", tool_input.get("pattern")) |
| 103 | + if denied: |
| 104 | + return denied |
| 105 | + return _check_search_path(tool_input, workspace) |
| 106 | + |
| 107 | + |
| 108 | +def _check_grep(tool_input: dict[str, object], workspace: str) -> int: |
| 109 | + denied = _reject_glob_syntax("glob", tool_input.get("glob")) |
| 110 | + if denied: |
| 111 | + return denied |
| 112 | + return _check_search_path(tool_input, workspace) |
| 113 | + |
| 114 | + |
| 115 | +def _handle(raw_event: str) -> int: |
| 116 | + try: |
| 117 | + event = json.loads(raw_event) |
| 118 | + except (json.JSONDecodeError, ValueError) as exc: |
| 119 | + return _deny(f"read blocked: unparseable hook event ({exc}).") |
| 120 | + if not isinstance(event, dict): |
| 121 | + return _deny("read blocked: hook event is not a JSON object.") |
| 122 | + |
| 123 | + workspace = os.environ.get("GITHUB_WORKSPACE", "") |
| 124 | + if not workspace: |
| 125 | + return _deny("read blocked: GITHUB_WORKSPACE is unset.") |
| 126 | + |
| 127 | + tool_input = event.get("tool_input") |
| 128 | + if not isinstance(tool_input, dict): |
| 129 | + tool_input = {} |
| 130 | + |
| 131 | + tool_name = event.get("tool_name") |
| 132 | + if tool_name == "Read": |
| 133 | + return _check_read(tool_input, workspace) |
| 134 | + if tool_name == "Glob": |
| 135 | + return _check_glob(tool_input, workspace) |
| 136 | + if tool_name == "Grep": |
| 137 | + return _check_grep(tool_input, workspace) |
| 138 | + return _deny(f"read blocked: unsupported tool {tool_name!r}.") |
| 139 | + |
| 140 | + |
| 141 | +def main() -> int: |
| 142 | + try: |
| 143 | + return _handle(sys.stdin.read()) |
| 144 | + except Exception as exc: # every non-2 exit is non-blocking upstream, so any error must deny |
| 145 | + return _deny(f"read blocked: unexpected error: {exc!r}") |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + raise SystemExit(main()) |
0 commit comments