Skip to content

feat(guard): GitHub remote ops 5 分類実装 — gh issue create/edit を github_issue_mutation_command として許可 (#1124) - #1126

Merged
squne121 merged 5 commits into
mainfrom
worktree-issue-1124-github-remote-ops-classification
Jun 22, 2026
Merged

squne121 merged 5 commits into
mainfrom
worktree-issue-1124-github-remote-ops-classification

Conversation

@squne121

@squne121 squne121 commented Jun 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

scripts/agent-guards/local_main_branch_guard.py に GitHub CLI コマンドの 5 分類体系を実装し、AI workflow 必須の gh issue create/edit を github_issue_mutation_command として許可し、gh issue view ... > tmp/*.md を readonly_artifact_export_command として許可する(Issue #1124)。

変更内容

scripts/agent-guards/local_main_branch_guard.py:

  • 5 分類語彙定数(GITHUB_CMD_CLASS_*)と TRUSTED_REPO_SLUG = "squne121/loop-protocol" を追加
  • is_github_issue_mutation_command(): gh issue create/edit に --repo squne121/loop-protocol と --body-file tmp/... が必須、--editor/--web はブロック
  • is_readonly_artifact_export_command(): gh issue view ... > tmp/<file> の形式のみ許可、>> / src/ / docs/ / .env 宛ては拒否
  • evaluate() に Step 5.5(readonly artifact export、メタキャラ検出前に評価)と Step 9.6b(github issue mutation)を追加

テスト:

  • tests/codex/test_local_main_branch_guard.py: AC8〜AC15 の境界テスト 40 ケース追加(99 → 139 テスト)
  • .claude/hooks/tests/test_local_main_branch_guard.py: AC8〜AC15 の境界テスト 37 ケース追加(208 → 245 テスト)

ドキュメント・ポリシー:

  • docs/dev/hook-boundaries.md: 5 分類の判断基準セクション追加
  • docs/dev/agent-skill-boundaries.md: gh CLI 5 分類語彙セクション追加
  • AGENTS.md: managed skill 経由の gh issue create/edit 許可を明記
  • .codex/rules/default.rules: gh issue create/edit の justification に managed skill 例外を明記

5 分類語彙

分類 reason_code 代表コマンド
display_readonly_command readonly_command gh issue view, gh pr view
readonly_artifact_export_command readonly_command gh issue view ... > tmp/*.md
github_issue_mutation_command github_remote_ops_command gh issue create/edit(--repo + --body-file 必須)
github_pr_metadata_command github_remote_ops_command gh pr comment/review
github_destructive_command gh_mutation_denied gh pr merge, gh pr checkout

Checks

  • pnpm typecheck PASS
  • pnpm lint PASS
  • pnpm test PASS(929 件通過)
  • pnpm build PASS
  • uv run pytest tests/codex/test_local_main_branch_guard.py -v PASS(139 件通過)
  • uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -v PASS(245 件通過)
  • uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_model_routing.py PASS(21 件通過)

Schema Change Applicability

変更対象はガードスクリプト・テストファイル・ドキュメントのみで、JSON Schema / YAML Schema の追加・変更なし。スキーマ変更に該当しない。

  • decision: not_schema_change
  • reason: スキーマ変更なし(guard スクリプト・テスト・docs のみ)

Schema Consumer Inventory

スキーマ変更なし(not_schema_change)のため、Schema Consumer Inventory は該当しない。消費側の影響調査は不要。

Safety Claim Matrix

本 PR は .claude/hooks/tests/ への変更を含むため、agent-surface に分類される。以下の具体的な証跡を添付する。

Claim Implemented? Not controlled Evidence Follow-up
既存の allow/block 判定が退行しない yes none 全体テスト 139 + 245 件全 PASS(TestGithubIssueMutationCommand / TestReadonlyArtifactExportCommand クラス追加確認) n/a
新 allow 条件(github_issue_mutation_command)は最小スコープ yes none --repo squne121/loop-protocol と --body-file tmp/... の両方を必須化、bare/interactive はブロック(AC10 テスト PASS) n/a
readonly_artifact_export_command は tmp/ 以外への書き込みをブロック yes none src/, docs/, .env, >> 宛てはすべてブロック(AC12 テスト PASS) n/a

Notes

PR #1120(fix: gh issue/pr GitHub ops を allowlist-closed から除外)のフォローアップとして、GitHub CLI コマンドの 5 分類体系を確立する(Closes #1124)。Allowed Paths 内の 7 ファイルのみを変更している。本 PR は Claude Code を使って実装・生成した。

@github-actions

github-actions Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #1126 closed).

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2: APPROVE. AC1-AC15 PASS. Allowed Paths OK (7 files, all within scope). BEHIND origin/main by 1 docs-only commit. Rebase needed before merge.

@squne121
squne121 force-pushed the worktree-issue-1124-github-remote-ops-classification branch from 9db499c to 5c9abf0 Compare June 22, 2026 00:41
@squne121
squne121 force-pushed the worktree-issue-1124-github-remote-ops-classification branch from 5c9abf0 to 921f51b Compare June 22, 2026 02:18
@squne121

Copy link
Copy Markdown
Owner Author

判定

REQUEST_CHANGES。現状のままマージ不可。

PR #1126 は draft の open PR で、変更は 7 ファイル・+609/-7、対象は Issue #1124 の allowed paths 内に収まっています。CI も ci、check-hook-integrity などは success です。

ただし、LOOP_VERDICT_V2: APPROVE コメントはありますが、敵対的ケースを潰せていません。特に tmp/ prefix 判定だけで remote mutation / local redirect を許しているため、guard の安全境界としては不十分です。

重大ブロッカー

B1. gh issue create が --title なしでも allow され、対話プロンプトに落ちる

Issue #1124 の趣旨は、gh issue create/edit を 非対話・repo 限定・body-file 付きで許可することです。

しかし実装は create に対して --repo と --body-file tmp/... しか要求していません。--title の存在・値を検証していないため、次が allow され得ます。

gh issue create --repo squne121/loop-protocol --body-file tmp/body.md

GitHub CLI 公式では、gh issue create は --title を省略すると入力を促す挙動です。--body-file はファイル、- は stdin から本文を読むオプションです。非対話化が必須なら --title/-t の値あり検証は必須です。([GitHub CLI]1)

修正: gh issue create は最低限 --title <value> または --title=<value> を必須にする。--title の次 token が別 flag、空、欠落なら block。


B2. --body-file tmp/... が path traversal を許す

現在の is_github_issue_mutation_command() は、--body-file の値について path != "-" and path.startswith("tmp/") しか見ていません。

そのため、tmp ディレクトリが存在する通常運用では、次のような入力が prefix 条件を通ります。

gh issue create --repo squne121/loop-protocol --title leak --body-file tmp/../AGENTS.md
gh issue edit 1124 --repo squne121/loop-protocol --body-file tmp/../docs/dev/hook-boundaries.md

GitHub CLI の --body-file はローカルファイルを読み取って issue 本文として送信します。つまりこれは「tmp 配下の本文ファイルを使う」ではなく、repo 内ファイルの remote 送信許可になっています。([GitHub CLI]1)

修正: prefix 判定ではなく canonical path 判定にする。

def is_safe_tmp_file(path: str, project_root: Path) -> bool:
    if path.startswith("/") or "\\" in path or "\x00" in path:
        return False
    p = Path(path)
    if not p.parts or p.parts[0] != "tmp":
        return False
    if ".." in p.parts:
        return False
    resolved = (project_root / p).resolve(strict=False)
    tmp_root = (project_root / "tmp").resolve(strict=False)
    return resolved.is_relative_to(tmp_root) and resolved.suffix in {".md", ".json", ".txt"}

本文ファイルは .md のみに絞る方がさらに安全です。


B3. gh issue view ... > tmp/... も path traversal で repo tracked file を書ける

readonly_artifact_export_command は > を compound/metachar 判定より前に許可します。これは設計上必要ですが、実装は RHS が tmp/ で始まるかだけを見ています。

そのため次が通る可能性があります。

gh issue view 1124 --repo squne121/loop-protocol --json body --jq .body > tmp/../docs/dev/hook-boundaries.md

gh issue view 自体は read operation ですが、shell redirect はローカル書き込みです。公式にも --json / --jq / --repo / --web などの option があり、guard 側で出力先と option matrix を絞る必要があります。([GitHub CLI]2)

修正: redirect 先も Path.resolve() で project_root/tmp 配下に限定。>>、absolute path、..、symlink escape、.git、.env、tracked docs への到達を block。加えて --web/-w は view export では block。


B4. 既存 is_github_remote_ops_command() がまだ広すぎる

PR #1126 は github_pr_metadata_command を分類語彙に入れていますが、実体は既存の is_github_remote_ops_command() です。この関数はまだかなり緩いです。gh issue comment / gh pr comment は --body / -b / --body-file の token があるだけで allow、値の存在や --body-file の安全性を検証していません。gh pr edit <N> は flag matrix を見ずに allow です。

GitHub CLI 公式では、comment 系には --edit-last、--delete-last、--create-if-none、--editor、--web、--yes、--body-file などがあり、PR edit には base branch、body file、labels、projects、reviewers、assignees など多くの metadata mutation があります。([GitHub CLI]3)

修正: #1126 内で直すか、少なくとも PR 本文と docs から「5分類が完成した」ような主張を落とし、follow-up Issue を必須化してください。理想は同時修正です。

最低限 block すべきもの:

gh issue comment 123 --body
gh issue comment 123 -e --body x
gh issue comment 123 --edit-last --body x
gh issue comment 123 --create-if-none --body x
gh issue comment 123 --body-file /etc/passwd
gh pr edit 123 --base main
gh pr edit 123 --body-file tmp/../AGENTS.md
gh pr edit 123 --add-reviewer someone

高優先の設計修正

--repo は --repo / --repo= だけでなく、公式の short alias -R もあります。#1124 のコメント側では --repo / -R 境界を要求しているため、許可するなら -R squne121/loop-protocol も扱う。許可しないなら docs に「--repo のみ」と明記してください。

gh issue create/edit は追加 flag が広すぎます。現在は --repo と --body-file さえあれば、--remove-label、--remove-parent、--project なども通り得ます。公式の gh issue edit は label/project/assignee/parent など多数の変更 option を持つため、managed skill 用なら allow flag matrix を固定するべきです。([GitHub CLI]4)

gh pr merge / gh pr checkout は引き続き block で正しいです。gh pr merge --delete-branch は branch 削除を含み、gh pr checkout は git checkout を行うため、local root branch guard の対象そのものです。([GitHub CLI]5)

追加すべきテスト

このPRのテストは happy path と単純 block は増えていますが、攻撃的ケースが足りません。

# create: non-interactive title required
"gh issue create --repo squne121/loop-protocol --body-file tmp/body.md"  # block

# body-file path traversal
"gh issue create --repo squne121/loop-protocol --title x --body-file tmp/../AGENTS.md"  # block
"gh issue edit 1124 --repo squne121/loop-protocol --body-file tmp/../docs/dev/hook-boundaries.md"  # block

# export path traversal
"gh issue view 1124 --repo squne121/loop-protocol --json body --jq .body > tmp/../docs/foo.md"  # block
"gh issue view 1124 --repo other/repo --json body > tmp/foo.json"  # block or explicitly documented
"gh issue view 1124 --web > tmp/foo.md"  # block

# remote ops inherited looseness
"gh issue comment 123 --body"  # block
"gh issue comment 123 -e --body x"  # block
"gh issue comment 123 --edit-last --body x"  # block
"gh pr edit 123 --base main"  # block

最終コメント案

PRに貼るならこれで十分です。

REQUEST_CHANGES.

方向性は正しいです。`gh issue create/edit` を managed mutation として first-class 化し、`gh issue view > tmp/*` を display-readonly から分ける判断も妥当です。

ただし現実装は安全境界として未完成です。

Blockers:
1. `gh issue create` が `--title` なしでも allow され、GitHub CLI の対話プロンプトに落ち得る。
2. `--body-file tmp/...` が prefix 判定だけなので `tmp/../AGENTS.md` 等を remote に送信できる。
3. `gh issue view ... > tmp/...` も prefix 判定だけなので `tmp/../docs/foo.md` へ書ける。
4. 既存 `is_github_remote_ops_command()` が `--body` 値なし、unsafe `--body-file`、`gh pr edit <N> --base ...` などを許し続けており、5分類 docs の安全主張と一致していない。

Fix required:
- create は `--title` value 必須。
- body-file / redirect destination は prefix ではなく canonical path で `project_root/tmp` 配下に限定。
- `--repo` / `-R` は trusted repo のみ許可。
- `--web/-w`, `--editor/-e`, `--body-file -`, `..`, absolute path, symlink escape を block。
- comment / pr edit 系の inherited loose classifier を直すか、follow-up Issue を作り、PR本文/docsから「分類体系完成」の主張を落とす。

結論

マージ不可。
CI green と AC PASS コメントは、今回の本質である「AI workflow 用 mutation をどこまで安全に許すか」の敵対的ケースを保証していません。tmp/ prefix 判定を canonical path policy に置き換え、gh issue create の --title 必須化と existing remote ops classifier の締め直しまで入れば、approve に近づきます。

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2: REQUEST_CHANGES — iteration 1 review posting (full verdict below)

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2 iteration 1 - review_subagent - verdict: REQUEST_CHANGES - merge_ready: false - merge_state_status: CLEAN - reviewed_head_sha: 8e08d96 - allowed_paths_gate.status: ok - allowed_paths_gate.producer_role: review_subagent - allowed_paths_gate.worker_report_used_as_canonical: false

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2 RB1: is_readonly_artifact_export_command lacks browser-flag check. gh issue view 123 with browser-open flag and redirect to tmp passes Step 5.5 incorrectly. is_readonly_command blocks this via _gh_has_web_flag but the export path skips this. Human review B3 required browser-flag block in export. Fix: add _gh_has_web_flag(lhs_stripped) guard before return True in is_readonly_artifact_export_command.

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2 RB2: Missing adversarial test in TestReadonlyArtifactExportCommand. AC12 parameter list needs: gh issue view 1124 with path-traversal redirect (tmp/../docs/foo.md). Implementation _is_safe_tmp_redirect_dest correctly blocks via dotdot in p.parts, but human review explicitly required this test case. Also missing: view with browser-open flag and redirect to tmp.

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2 confirmed resolved: B1 (title required for gh issue create - _find_flag_value check + test_b1 confirmed), B2 (tmp dotdot body-file path traversal blocked + test_b2_body_file_path_traversal_blocked), B4 (interactive and destructive flags blocked, base blocked for pr edit, bare body blocked, tests in TestB1B4ReviewBlockerFixes confirmed). Allowed Paths: all 7 files in contract. CI: all SUCCESS.

RB1: is_readonly_artifact_export_command now blocks --web/-w flag
  scripts/agent-guards/local_main_branch_guard.py:
  - add _gh_has_web_flag(lhs_stripped) check before return True

RB2: AC12 parametrize with 2 new blocked cases
  tests/codex/test_local_main_branch_guard.py:
  - "gh issue view 1124 > tmp/../docs/foo.md"  (path traversal)
  - "gh issue view 1124 --web > tmp/foo.md"    (--web flag)
  .claude/hooks/tests/test_local_main_branch_guard.py:
  - same 2 cases in TestReadonlyArtifactExportCommandClaude.AC12

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@squne121
squne121 force-pushed the worktree-issue-1124-github-remote-ops-classification branch from 5d61df4 to 43dffdb Compare June 22, 2026 03:40
@squne121

Copy link
Copy Markdown
Owner Author

test

@squne121

Copy link
Copy Markdown
Owner Author

RB1 PASS, RB2 PASS, APPROVE pending CI. LOOP_VERDICT_V2 to follow.

@squne121

Copy link
Copy Markdown
Owner Author

iteration 2 レビュー — bot review subagent

RB1・RB2 解消確認済み。B1〜B4 退行なし。Allowed Paths gate OK。

CI: commit 8e08d96(HEAD-1)全 12 ジョブ SUCCESS。commit 43dffdb(HEAD)は 8 行追加のみで CI 未実行(UNSTABLE)。

LOOP_VERDICT_V2:
verdict: APPROVE
merge_ready: false
merge_state_status: UNSTABLE
reviewed_head_sha: 43dffdb
required_auto_actions:
- kind: wait_for_ci
description: HEAD commit 43dffdb の CI 完走を待つ
allowed_paths_gate:
status: ok
producer_role: review_subagent
worker_report_used_as_canonical: false
changed_files_count: 7
all_within_allowed_paths: true
blockers: []
notes: RB1 is_readonly_artifact_export_command に _gh_has_web_flag チェック追加確認済み。RB2 AC12 パラメータに path traversal + web flag ケース追加確認済み。CI 未実行による UNSTABLE は完走後解消見込み。

@squne121
squne121 force-pushed the worktree-issue-1124-github-remote-ops-classification branch from 6fc695d to 43dffdb Compare June 22, 2026 04:27
@squne121
squne121 marked this pull request as ready for review June 22, 2026 04:42
@squne121
squne121 merged commit 576c2ca into main Jun 22, 2026
18 checks passed
@squne121
squne121 deleted the worktree-issue-1124-github-remote-ops-classification branch June 22, 2026 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

実装: GitHub remote ops を read-only から分離し AI workflow 必須コマンドとして分類する

1 participant