From db0af2abf3fefef6b96206c2afbe85adda6e97d2 Mon Sep 17 00:00:00 2001 From: okash1n <48118431+okash1n@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:27:42 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B5=B7=E5=8B=95=E6=99=82=E3=81=AB=20W?= =?UTF-8?q?rite(path)=20permission=20=E3=83=AB=E3=83=BC=E3=83=AB=E3=81=AE?= =?UTF-8?q?=E8=AD=A6=E5=91=8A=E3=81=8C=207=20=E8=A1=8C=E5=87=BA=E3=82=8B?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3=EF=BC=88v0.79.2?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit config/permissions.json の permissions.deny に Write(path) 形式のルールが 7 件含まれていた。Claude Code はファイルの権限チェックに Edit(path) ルール だけを使い(Edit ルールはファイルを編集するすべての組み込みツールに適用 される)、2.1.210 以降は Write(path) ルールごとに起動時の警告を表示する。 - config/permissions.json: Write(path) の deny ルール 7 件を削除した。同じ パスの Edit(path) ルールは以前から併記されているため保護範囲は変わらない - lib/update.sh: 更新の設定フェーズで settings.json の permissions.deny から この 7 文字列(完全一致)だけを取り除く _strip_ineffective_permission_rules を追加した。permissions.deny を手で編集した環境では、非対話の 3-way merge が「キットが削除した項目」を残すため。利用者が追加したルールと並び順は 変えず、該当がなければ書き換えない - MDM: 配備される settings.json から 7 件がなくなる。policy.json の SHA-256 は 変わらない(Minimal / Standard / Full × 日本語で確認) - テスト: permission ルールの静的検査 1 件、掃除関数の単体テスト 6 件、 実ビルドを通す設定フェーズのテスト 5 件を追加した(修正前は 10 件が失敗) --- CHANGELOG.md | 10 +++ config/permissions.json | 9 +- lib/update.sh | 64 ++++++++++++++ tests/unit/test-hook-fixtures.sh | 29 +++++++ tests/unit/test-retired-hooks.sh | 135 +++++++++++++++++++++++++++++ tests/unit/test-update-refactor.sh | 126 +++++++++++++++++++++++++++ 6 files changed, 365 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e446bb..0e6abfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/). +## [0.79.2] - 2026-10-08 + +### Fixed +- **キット導入後、Claude Code の起動時に `Permission deny rule (...settings.json): Write(./.env) is not matched by file permission checks — only Edit(path) rules are. ...` という警告が 7 行表示される問題を修正**: `config/permissions.json` の `permissions.deny` に `Write(path)` 形式のルールが 7 件(`Write(./.env)` / `Write(./.env.*)` / `Write(./**/.env)` / `Write(./**/.env.*)` / `Write(/secrets/**)` / `Write(~/.zshrc)` / `Write(~/.bashrc)`)含まれていた。Claude Code はファイルの権限チェックに `Edit(path)` ルールだけを使い(`Edit` ルールはファイルを編集するすべての組み込みツールに適用される)、`Write(path)` ルールは一致させない。2.1.210 以降はこの形式のルールごとに起動時の警告を表示する + - **修正**: 7 件の `Write(path)` ルールを `config/permissions.json` から削除した。同じパスの `Edit(path)` ルールは以前から併記されているため、拒否されるファイル操作は変わらない(削除したルールはもともと一致していなかった)。`allow` と他の deny ルールは変更していない + - **既存インストールへの影響**: 次回の `setup.sh --update`(`/update-kit`、自動アップデート、`install.sh` の再実行)で、`settings.json` の `permissions.deny` から上の 7 件が取り除かれる。`permissions.deny` を手で編集していた環境では、非対話更新の 3-way merge が「キットが削除した項目」を利用者側の値として残すため、更新時にこの 7 つの文字列に完全一致するルールだけを取り除く `_strip_ineffective_permission_rules` を `lib/update.sh` に追加した。対話・非対話のどちらの更新でも、3-way merge の実行有無にかかわらず毎回実行する。利用者が追加したルール(7 件以外の `Write(path)` ルールを含む)、ルールの並び順、`allow` / `ask` などほかのキーは変更せず、該当するルールがない `settings.json` は書き換えない + - **対象外のケース**: manifest を持たず既存の `settings.json` だけがある状態への新規インストールは更新処理を通らない(bootstrap merge だけを行う)ため、その `settings.json` に 7 件のルールが残っていれば警告も残る。該当する場合は `permissions.deny` から上の 7 件を手で削除する + - **MDM**: `mdm/render-expected.py` は `config/permissions.json` から `settings.json` を描画するため、配備される `settings.json` から 7 件のルールがなくなる(MDM の更新は `settings.json` 全体を置き換える)。`KIT_MDM_EXPECTED_POLICY_SHA256` は変わらない(Minimal / Standard / Full × 日本語の 3 通りで、変更前後の `policy.json` の SHA-256 が一致することを確認)。renderer と installer は変更していないため、install bundle の再配布は不要 + - **回帰テスト**: アサーションを 12 件追加した。`tests/unit/test-hook-fixtures.sh` に、`config/settings-base.json` / `config/permissions.json` / `features/*/hooks*.json` の `permissions` に `Write(path)` / `NotebookEdit(path)` / `Glob(path)` 形式のルールがないことの静的検査を 1 件、`tests/unit/test-retired-hooks.sh` に掃除関数の単体テストを 6 件(7 件の削除と並び順の保持、キットが出荷していない `Write` ルールと deny 以外のリストを変えないこと、該当がなければ書き換えないこと、`permissions.deny` がない・形が想定外の設定で壊れないこと、不正な JSON で失敗して書き換えないこと、掃除対象が v0.79.1 の 7 件に固定されていること)、`tests/unit/test-update-refactor.sh` に更新の設定フェーズ(`_update_phase_settings`)のテストを 5 件(呼び出しの検査 1 件と、実際のビルドを使って通す 4 件:`permissions.deny` を編集した環境の非対話更新、bootstrap merge、未編集の環境、7 件を持たない環境は書き換えないこと)。修正前のコードでは 12 件中 10 件が失敗する(静的検査は 7 件を検出する。成功する 2 件は、不正な JSON で失敗することと、7 件を持たない環境を書き換えないことの確認) + ## [0.79.1] - 2026-10-07 ### Fixed diff --git a/config/permissions.json b/config/permissions.json index e86457b..72bec24 100644 --- a/config/permissions.json +++ b/config/permissions.json @@ -48,17 +48,10 @@ "Edit(./.env.*)", "Edit(./**/.env)", "Edit(./**/.env.*)", - "Write(./.env)", - "Write(./.env.*)", - "Write(./**/.env)", - "Write(./**/.env.*)", "Read(/secrets/**)", "Edit(/secrets/**)", - "Write(/secrets/**)", "Edit(~/.zshrc)", - "Write(~/.zshrc)", - "Edit(~/.bashrc)", - "Write(~/.bashrc)" + "Edit(~/.bashrc)" ], "allow": [ "Read", diff --git a/lib/update.sh b/lib/update.sh index caa8224..83b0db4 100644 --- a/lib/update.sh +++ b/lib/update.sh @@ -2014,6 +2014,65 @@ _strip_retired_hook_entries() { fi } +# Write(path) deny rules config/permissions.json shipped until v0.79.1. Claude +# Code 2.1.210+ ignores Write(path) rules (file permission checks only match +# Edit(path), which covers every file-editing tool) and prints a startup +# warning for each one. Their Edit(path) pairs stay in the kit, so dropping +# them does not change what is protected. Historical list: never add to it. +_INEFFECTIVE_KIT_PERMISSION_RULES=( + "Write(./.env)" + "Write(./.env.*)" + "Write(./**/.env)" + "Write(./**/.env.*)" + "Write(/secrets/**)" + "Write(~/.zshrc)" + "Write(~/.bashrc)" +) + +# _strip_ineffective_permission_rules - Remove the kit's former Write(path) +# deny rules from settings.json. +# +# Usage: _strip_ineffective_permission_rules +# +# A user-touched permissions.deny keeps kit-removed values through the +# non-interactive 3-way merge (and the bootstrap merge keeps every existing +# value), so the rules would survive the v0.79.2 update and keep the warnings. +# Only exact matches of the strings above are removed from permissions.deny; +# other rules (including Write rules the user added), their order and every +# other key stay as they are, and a file without a match is not rewritten. +_strip_ineffective_permission_rules() { + local settings_file="$1" + [[ -f "$settings_file" ]] || return 1 + local rules_json + rules_json="$(printf '%s\n' "${_INEFFECTIVE_KIT_PERMISSION_RULES[@]}" \ + | jq -R . | jq -cs .)" || return 1 + local filter=' + def ineffective: type == "string" and (. as $r | $rules | index($r) != null);' + local probe_rc=0 + jq -e --argjson rules "$rules_json" "$filter"' + [.permissions? | objects | .deny? | arrays | .[] | select(ineffective)] + | length > 0 + ' "$settings_file" >/dev/null 2>&1 || probe_rc=$? + case "$probe_rc" in + 0) ;; + 1) return 0 ;; + *) return 1 ;; + esac + local tmp + tmp="$(mktemp)" || return 1 + _SETUP_TMP_FILES+=("$tmp") + if jq --argjson rules "$rules_json" "$filter"' + .permissions.deny |= map(select(ineffective | not)) + ' "$settings_file" > "$tmp" 2>/dev/null; then + mv "$tmp" "$settings_file" || return 1 + ok "Removed Write(path) permission rules Claude Code ignores from settings.json" + else + rm -f "$tmp" 2>/dev/null || true + warn "Could not remove ineffective Write(path) permission rules from settings.json" + return 1 + fi +} + # --------------------------------------------------------------------------- # _strip_superseded_kit_hook_generations - Drop stale generations of kit hooks # @@ -2506,6 +2565,11 @@ _update_phase_settings() { # current and new kit all differ, so without this sweep an already-duplicated # array persists until the kit next edits that very array. _strip_superseded_kit_hook_generations "$current_settings" "$new_settings" || return 1 + + # A user-touched permissions.deny keeps the Write(path) rules the kit shipped + # until v0.79.1 (the non-interactive merge keeps kit-removed values), and + # Claude Code 2.1.210+ warns about each of them at startup. Drop them. + _strip_ineffective_permission_rules "$current_settings" || return 1 } # _claude_md_user_section_has_content - Returns 0 when the user section of a diff --git a/tests/unit/test-hook-fixtures.sh b/tests/unit/test-hook-fixtures.sh index 286a538..67ea81c 100644 --- a/tests/unit/test-hook-fixtures.sh +++ b/tests/unit/test-hook-fixtures.sh @@ -209,3 +209,32 @@ if [[ "$_auf_rc" -eq 0 ]] \ else fail "hook-fixtures: auto-update should consume SessionStart fixture without network access" fi + +# Claude Code 2.1.210+ ignores Write(path) / NotebookEdit(path) / Glob(path) +# permission rules and prints a startup warning for each one: file permission +# checks only match Edit(path) (which covers every file-editing tool) and +# Read(path). Every permission source the settings builder merges must avoid +# them. +_perm_bad_rules="" +for _perm_src in "$PROJECT_DIR/config/settings-base.json" \ + "$PROJECT_DIR/config/permissions.json" \ + "$PROJECT_DIR"/features/*/hooks*.json; do + [[ -f "$_perm_src" ]] || continue + _perm_found="" + if ! _perm_found="$(jq -r ' + (.permissions // {}) | to_entries[] + | select(.value | type == "array") | .value[] + | select(type == "string" and test("^(Write|NotebookEdit|Glob)\\(")) + ' "$_perm_src" 2>/dev/null)"; then + _perm_found="(unreadable)" + fi + while IFS= read -r _perm_rule; do + [[ -n "$_perm_rule" ]] || continue + _perm_bad_rules+="${_perm_src#"$PROJECT_DIR"/}: ${_perm_rule}"$'\n' + done <<< "$_perm_found" +done +if [[ -z "$_perm_bad_rules" ]]; then + pass "hook-fixtures: permission rules avoid the Write/NotebookEdit/Glob(path) forms Claude Code ignores" +else + fail "hook-fixtures: permission rules must use Edit(path)/Read(path); found $(printf '%s' "$_perm_bad_rules" | grep -c .): $(printf '%s' "$_perm_bad_rules" | tr '\n' ' ')" +fi diff --git a/tests/unit/test-retired-hooks.sh b/tests/unit/test-retired-hooks.sh index 453eec6..4bbfdf1 100644 --- a/tests/unit/test-retired-hooks.sh +++ b/tests/unit/test-retired-hooks.sh @@ -561,4 +561,139 @@ printf '%s\n' '{"hooks":{"PreToolUse":[{"matcher":"*","hooks":[{"type":"command" fi } +# ── _strip_ineffective_permission_rules ──────────────────────────────────── +# +# Up to v0.79.1 the kit shipped seven Write(path) deny rules next to their +# Edit(path) pairs. Claude Code 2.1.210+ ignores Write(path) rules and prints a +# startup warning for each one. A user-touched permissions.deny keeps them +# through the non-interactive 3-way merge ("removed by the kit"), so this sweep +# removes exactly those seven strings on every update. +_rh_perm() { # + HOME=/home/u "${BASH:-bash}" -c ' + set -uo pipefail + PROJECT_DIR="'"$PROJECT_DIR"'" + ok(){ :; }; warn(){ :; }; info(){ :; }; is_true(){ [[ "$1" == "true" ]]; } + _SETUP_TMP_FILES=() + source "$PROJECT_DIR/lib/features.sh" + source "$PROJECT_DIR/lib/snapshot.sh" + source "$PROJECT_DIR/lib/update.sh" 2>/dev/null || true + _strip_ineffective_permission_rules "'"$1"'" + ' +} + +_rh_kit_write_rules='["Write(./.env)","Write(./.env.*)","Write(./**/.env)","Write(./**/.env.*)","Write(/secrets/**)","Write(~/.zshrc)","Write(~/.bashrc)"]' + +{ + test_name="permission-sweep: the seven kit Write(path) deny rules are removed, other rules keep their order" + _rh_pm="$_rh_tmp/perm-strip.json" + printf '%s\n' '{"permissions":{ + "allow":["Read","Write","Edit"], + "deny":["Bash(sudo *)","Edit(./.env)","Write(./.env)","Write(./.env.*)","Bash(my-own *)", + "Write(./**/.env)","Write(./**/.env.*)","Read(/secrets/**)","Edit(/secrets/**)", + "Write(/secrets/**)","Edit(~/.zshrc)","Write(~/.zshrc)","Edit(~/.bashrc)", + "Write(~/.bashrc)","Read(~/my-notes/**)"], + "defaultMode":"default"}, + "env":{"A":"1"}}' > "$_rh_pm" + _rh_rc=0; _rh_perm "$_rh_pm" >/dev/null 2>&1 || _rh_rc=$? + if [[ "$_rh_rc" -eq 0 ]] \ + && jq -e ' + .permissions.deny == ["Bash(sudo *)","Edit(./.env)","Bash(my-own *)","Read(/secrets/**)", + "Edit(/secrets/**)","Edit(~/.zshrc)","Edit(~/.bashrc)","Read(~/my-notes/**)"] + and .permissions.allow == ["Read","Write","Edit"] + and .permissions.defaultMode == "default" + and .env == {"A":"1"}' "$_rh_pm" >/dev/null 2>&1; then + pass "$test_name" + else + fail "$test_name" + fi +} + +{ + test_name="permission-sweep: Write rules the kit never shipped and non-deny lists are untouched" + _rh_pm="$_rh_tmp/perm-user-write.json" + printf '{"permissions":{"allow":["Write(./.env)"],"ask":["Write(~/.zshrc)"],"deny":["Write(./.env.local)","Write(~/.ssh/config)"," Write(./.env)"]}}\n' > "$_rh_pm" + _rh_before="$(cat "$_rh_pm")" + _rh_inode_before="$(_rh_inode "$_rh_pm")" + _rh_rc=0; _rh_perm "$_rh_pm" >/dev/null 2>&1 || _rh_rc=$? + if [[ "$_rh_rc" -eq 0 && "$(cat "$_rh_pm")" == "$_rh_before" \ + && "$(_rh_inode "$_rh_pm")" == "$_rh_inode_before" ]]; then + pass "$test_name" + else + fail "$test_name" + fi +} + +{ + test_name="permission-sweep: settings without a kit Write rule are not rewritten" + _rh_pm="$_rh_tmp/perm-clean.json" + # Compact, key order the kit never writes: any jq round trip changes bytes. + printf '{"permissions":{"deny":["Edit(./.env)","Read(./.env)"],"allow":["Edit"]},"env":{"A":"1"}}\n' > "$_rh_pm" + _rh_before="$(cat "$_rh_pm")" + _rh_inode_before="$(_rh_inode "$_rh_pm")" + _rh_rc=0; _rh_perm "$_rh_pm" >/dev/null 2>&1 || _rh_rc=$? + if [[ "$_rh_rc" -eq 0 && "$(cat "$_rh_pm")" == "$_rh_before" \ + && "$(_rh_inode "$_rh_pm")" == "$_rh_inode_before" ]]; then + pass "$test_name" + else + fail "$test_name" + fi +} + +{ + test_name="permission-sweep: settings without permissions.deny (or with odd shapes) are left alone" + _rh_ok=true + _rh_i=0 + for _rh_doc in '{"env":{"A":"1"}}' '{"permissions":{"allow":["Edit"]}}' \ + '{"permissions":{"deny":null}}' '{"permissions":{"deny":"Write(./.env)"}}' \ + '{"permissions":"Write(./.env)"}' '{"permissions":{"deny":[{"rule":"Write(./.env)"},7]}}' \ + '[]' '{}'; do + _rh_i=$((_rh_i + 1)) + _rh_pm="$_rh_tmp/perm-shape-$_rh_i.json" + printf '%s\n' "$_rh_doc" > "$_rh_pm" + _rh_rc=0; _rh_perm "$_rh_pm" >/dev/null 2>&1 || _rh_rc=$? + if [[ "$_rh_rc" -ne 0 || "$(cat "$_rh_pm")" != "$_rh_doc" ]]; then + _rh_ok=false + fi + done + if [[ "$_rh_ok" == "true" ]]; then + pass "$test_name" + else + fail "$test_name" + fi +} + +{ + test_name="permission-sweep: invalid settings fail without being rewritten" + _rh_pm="$_rh_tmp/perm-broken.json" + printf '{"permissions": ' > "$_rh_pm" + _rh_rc=0; _rh_perm "$_rh_pm" >/dev/null 2>&1 || _rh_rc=$? + _rh_rc_missing=0; _rh_perm "$_rh_tmp/no-such-settings.json" >/dev/null 2>&1 || _rh_rc_missing=$? + if [[ "$_rh_rc" -ne 0 && "$_rh_rc_missing" -ne 0 \ + && "$(cat "$_rh_pm")" == '{"permissions": ' ]]; then + pass "$test_name" + else + fail "$test_name" + fi +} + +{ + test_name="permission-sweep: the swept list is exactly the Write rules v0.79.1 shipped" + # Pin the sweep to the historical kit rules; a new kit rule must never be + # added to this list (the static check in test-hook-fixtures.sh keeps + # config/permissions.json free of the form). + _rh_swept="$(HOME=/home/u "${BASH:-bash}" -c ' + PROJECT_DIR="'"$PROJECT_DIR"'" + ok(){ :; }; warn(){ :; }; info(){ :; }; is_true(){ [[ "$1" == "true" ]]; } + source "$PROJECT_DIR/lib/features.sh" + source "$PROJECT_DIR/lib/snapshot.sh" + source "$PROJECT_DIR/lib/update.sh" 2>/dev/null || true + printf "%s\n" "${_INEFFECTIVE_KIT_PERMISSION_RULES[@]}" + ' 2>/dev/null | jq -R . | jq -cs .)" + if [[ "$_rh_swept" == "$(jq -c . <<< "$_rh_kit_write_rules")" ]]; then + pass "$test_name" + else + fail "$test_name (got: $_rh_swept)" + fi +} + rm -rf "$_rh_tmp" diff --git a/tests/unit/test-update-refactor.sh b/tests/unit/test-update-refactor.sh index bd93928..87645c2 100644 --- a/tests/unit/test-update-refactor.sh +++ b/tests/unit/test-update-refactor.sh @@ -690,3 +690,129 @@ _lgu_same_as() { # fail "$test_name" fi } + +# Up to v0.79.1 config/permissions.json shipped seven Write(path) deny rules, +# which Claude Code 2.1.210+ ignores with a startup warning per rule. An +# install whose permissions.deny the user edited keeps them through the +# non-interactive 3-way merge ("removed by the kit"), so the update must sweep +# them out of settings.json regardless of which merge path ran. The v0.79.1 +# shape is derived from a current build by putting the seven rules back. +{ + test_name="update: the permission sweep runs in the settings phase" + if declare -f _update_phase_settings \ + | grep -qE '^[[:space:]]*_strip_ineffective_permission_rules "\$current_settings" \|\| return 1'; then + pass "$test_name" + else + fail "$test_name" + fi +} + +_pwu_tmp="$(mktemp -d)" +_SETUP_TMP_FILES+=("$_pwu_tmp") +_pwu_write='["Write(./.env)","Write(./.env.*)","Write(./**/.env)","Write(./**/.env.*)","Write(/secrets/**)","Write(~/.zshrc)","Write(~/.bashrc)"]' +_pwu_user_rule='Read(~/my-notes/**)' +_pwu_prepare_rc=0 +( + set +e +u + export HOME="$_pwu_tmp/home" + mkdir -p "$HOME" + # shellcheck source=lib/progress.sh + source "$PROJECT_DIR/lib/progress.sh" + # shellcheck source=i18n/en/strings.sh + source "$PROJECT_DIR/i18n/en/strings.sh" + # shellcheck source=profiles/standard.conf + source "$PROJECT_DIR/profiles/standard.conf" + # shellcheck disable=SC2034 # consumed by the sourced builder and updater + LANGUAGE=en COMMIT_ATTRIBUTION=false KIT_MDM_MANAGED=false DRY_RUN=false + # shellcheck disable=SC2034 # consumed by the sourced merge library + _MERGE_INTERACTIVE=false + + build_settings_file "$_pwu_tmp/kit.json" >/dev/null 2>&1 || exit 1 + jq --argjson w "$_pwu_write" '.permissions.deny |= (. + ($w - .))' \ + "$_pwu_tmp/kit.json" > "$_pwu_tmp/v0791.json" || exit 1 + jq --arg r "$_pwu_user_rule" '.permissions.deny += [$r]' \ + "$_pwu_tmp/v0791.json" > "$_pwu_tmp/v0791-user.json" || exit 1 + jq --arg r "$_pwu_user_rule" '.permissions.deny += [$r]' \ + "$_pwu_tmp/kit.json" > "$_pwu_tmp/kit-user.json" || exit 1 + + _pwu_case() { #