From 5f8fb391fc7d26a339269d671e3afa5aacf44315 Mon Sep 17 00:00:00 2001 From: Howon Lee Date: Tue, 10 Nov 2026 14:55:08 -0800 Subject: [PATCH 1/2] fix: bind command approval to Git database and history (VULN-566) Bind approval to both Git directory instances and a reviewed commit stored outside the repository. Reject unrelated history without trusting replace refs, grafts, commit-graph caches, or lazy fetching. Share the checks with generated Bash, Zsh and Fish helpers and invalidate older approvals/caches. Validation: full BATS suite (1,057 cases), 18 targeted Bash 3.2 tests, ShellCheck, completion generation, and CLI/missing-history smoke checks. Added regression coverage for replacement, forged ancestry and approval migration; no tests removed. --- CHANGELOG.md | 1 + README.md | 4 +- docs/configuration.md | 10 +- docs/security-vuln-566-followup.md | 10 ++ lib/commands/help.sh | 12 +- lib/commands/init.sh | 70 +++++----- lib/commands/trust.sh | 12 +- lib/hooks.sh | 53 +++----- lib/trust_history.sh | 35 +++++ tests/cmd_trust.bats | 14 ++ tests/hooks.bats | 3 +- tests/init.bats | 20 +-- tests/trust_followup.bats | 58 -------- tests/trust_history.bats | 207 +++++++++++++++++++++++++++++ tests/trust_storage.bats | 13 +- 15 files changed, 363 insertions(+), 159 deletions(-) create mode 100644 lib/trust_history.sh create mode 100644 tests/trust_history.bats diff --git a/CHANGELOG.md b/CHANGELOG.md index b90fd6b..79e2781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Fixed +- Command approval now binds to the object database directory and shared commit history, so replacing Git contents while preserving `.git` requires new approval. CLI and Bash/Zsh/Fish helpers ignore replace refs, grafts, and commit-graph caches when checking history. Upgrading requires fresh `git gtr trust` approval and reloading shell integration. - The integration test helper now canonicalizes its temporary repository path. On macOS `mktemp -d` returns a `/var` path while git records the resolved `/private/var` one, so `cmd_clean --merged uses nested registered worktree path` compared two spellings of the same directory and failed locally while passing in CI. ## [2.11.1] - 2026-09-14 diff --git a/README.md b/README.md index 6a846e1..17613e4 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ gtr cd my-feature gtr cd 1 ``` -`git gtr init ` reuses cached output only when both the installed version and shell integration schema match. Keep the invocation in your shell configuration so upgrades refresh the helpers automatically. If your existing setup sources a cache file directly or checks a hard-coded `init=` value, replace it with the setup above, then restart your shell or run the matching setup command in the current shell. Upgrades to the v3 trust format require reviewing commands again with `git gtr trust`. +`git gtr init ` reuses cached output only when both the installed version and shell integration schema match. Keep the invocation in your shell configuration so upgrades refresh the helpers automatically. If your existing setup sources a cache file directly or checks a hard-coded `init=` value, replace it with the setup above, then restart your shell or run the matching setup command in the current shell. Upgrades to the v4 trust format require reviewing commands again with `git gtr trust`. The cache lives under `${XDG_CACHE_HOME:-$HOME/.cache}/gtr/`. New cache directories and files are private to your user. Each invocation checks ownership and write permissions, including parent directories and any XDG symlink targets. If the location is unsafe or unavailable, `git gtr init` returns fresh shell integration without reading or updating that cache. @@ -425,7 +425,7 @@ Review and approve executable commands defined in the repository's `.gtrconfig` git gtr trust # Review and approve .gtrconfig commands ``` -Trust is bound to the repository path, the filesystem identity of its common Git directory, and executable command definitions. Replacing or recloning a repository at the same path, or changing hooks or editor/AI defaults, requires approval again. Older path-only approvals also require renewal. +Trust is bound to the repository path, the filesystem identities of its common Git directory and object database, executable command definitions, and shared history with an approved commit. Replacing either directory, introducing unrelated history, or changing hooks or editor/AI defaults requires approval again. Normal commits, garbage collection, and related branches retain approval. Older approvals also require renewal. See the [configuration guide](docs/configuration.md) for the full boundary. Hooks and defaults from Git configuration do not require `.gtrconfig` approval, but GTR first checks configuration ownership and write permissions. It refuses diff --git a/docs/configuration.md b/docs/configuration.md index 7f4c1ae..b3f2beb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -108,15 +108,11 @@ Create a `.gtrconfig` file in your repository root to share configuration across > [!IMPORTANT] > Hooks and editor/AI defaults from `.gtrconfig` are executable command entries. They are ignored until you review them with `git gtr trust`; changing those entries requires re-approval. -Approval is bound to the repository path and the filesystem identity of its common Git directory. Replacing that directory, for example with a fresh clone at the same path, needs new approval, as do approvals created by older versions that only used the path. Normal commits and linked worktrees retain approval. This is command-definition trust: scripts, dependencies, and tools invoked by approved commands are not hashed or pinned, so their contents can change within the trusted repository. Review delegated code and trust its contributors before approving. +Approval is bound to the repository path, the filesystem identities of its common Git directory and object database, and a commit recorded outside the repository in the approval marker. Replacing the object database requires new approval even if `.git` itself and shared history are retained. Current history must also share an ancestor with the approved commit; retaining old objects, replace refs, grafts, or a forged commit-graph cache does not establish shared history. This check uses local commit objects and never fetches missing history. Normal commits, garbage collection, related branches, and linked worktrees retain approval. Orphan histories and shallow or rewritten histories with no available common ancestor need new approval. Create an initial commit before approving commands. -Preserving the common Git directory while replacing its contents preserves its -identity and approval when the command definitions are unchanged. This check -does not attest repository contents, history, or provenance. Do not reuse an -approved Git directory to import a repository whose contributors you do not -trust; use a separate clone and review its commands and delegated code. +This is command-definition trust: scripts, dependencies, and tools invoked by approved commands are not hashed or pinned, so their contents can change within the trusted repository. Rewriting files while deliberately retaining both directory instances and shared history is indistinguishable from an update to that repository. Review delegated code and trust its contributors before approving. -Command definitions are captured once for trust verification and consumption, with NUL-delimited records preserving multiline values. The version 3 trust format invalidates older approvals; review commands again with `git gtr trust` after upgrading. +Command definitions are captured once for trust verification and consumption, with NUL-delimited records preserving multiline values. The version 4 trust format invalidates older approvals; review commands again with `git gtr trust` after upgrading. The identity includes device, inode, and birth time with nanosecond precision. If the filesystem or `stat` implementation cannot provide a known birth time, approval fails closed and `.gtrconfig` executable commands remain disabled. diff --git a/docs/security-vuln-566-followup.md b/docs/security-vuln-566-followup.md index a9b511c..76574dc 100644 --- a/docs/security-vuln-566-followup.md +++ b/docs/security-vuln-566-followup.md @@ -1,5 +1,15 @@ # VULN-566 follow-up validation +> [!IMPORTANT] +> The findings below record the pre-v4 behavior at `fc8afc0` and v3.0.0. +> Trust v4 now rejects object-database replacement and unrelated history, even +> when the common Git directory is preserved. The four former permissive +> replacement tests are superseded by the rejection and recovery regressions in +> `tests/trust_history.bats`; the three legacy-cache tests remain in +> `tests/trust_followup.bats`. Updates that preserve both directory instances and +> shared history still retain command-definition approval. This change is not +> evidence of a published release or researcher acceptance. + Retested on November 10, 2026 against main `fc8afc0` and the published `v3.0.0` tag (`2d4852d`). All execution fixtures write harmless marker files inside disposable repositories. No real shell startup files are modified. diff --git a/lib/commands/help.sh b/lib/commands/help.sh index 9f591a1..e143282 100644 --- a/lib/commands/help.sh +++ b/lib/commands/help.sh @@ -439,7 +439,7 @@ New cache directories and files are private. Ownership and write permissions are checked through the parent path; unsafe or unavailable caches are bypassed. Replace older setup snippets that source a cache directly or check a hard-coded 'init=' value, then restart your shell or run the matching setup command below. -Upgrades to the v3 trust format require reviewing commands with 'git gtr trust'. +Upgrades to the v4 trust format require reviewing commands with 'git gtr trust'. Supported shells: bash, zsh, fish @@ -491,10 +491,12 @@ used until explicitly trusted. This prevents malicious contributors from injecting arbitrary shell commands via shared .gtrconfig files. Trust is stored per repository -path, common Git directory filesystem identity, and executable command -definitions in ~/.config/gtr/trusted/. Replacing a repository at the same -path or changing those commands requires new approval. Older path-only -approvals require renewal. +path, Git and object database directory identities, executable command +definitions, and approved commit history in ~/.config/gtr/trusted/. +Replacing either directory, switching to unrelated history, or changing +those commands requires new approval. Older approvals require renewal. +Normal commits and related branches retain approval. Create an initial +commit before approving commands; missing history is not fetched. Approval includes scripts and programs invoked by these commands. Their contents are not hashed or pinned; review delegated code and trust its diff --git a/lib/commands/init.sh b/lib/commands/init.sh index 8ebceab..9ce061b 100644 --- a/lib/commands/init.sh +++ b/lib/commands/init.sh @@ -11,6 +11,16 @@ if ! command -v _gtr_config_check >/dev/null 2>&1; then fi fi +if ! command -v _hooks_history_matches >/dev/null 2>&1; then + if [ -n "${ZSH_VERSION:-}" ]; then + # shellcheck disable=SC2296,SC2298,SC1091 + . "${${(%):-%x}:h}/../trust_history.sh" + else + # shellcheck source=lib/trust_history.sh + . "${BASH_SOURCE[0]%/*}/../trust_history.sh" + fi +fi + # Cache contents are executable code. Reuse the config validator's component # walk so safe XDG symlinks work without hiding writable link parents/targets. _init_cache_path_is_safe() { @@ -145,8 +155,8 @@ cmd_init() { case "$cache_dir" in /*) ;; *) cache_dir="$(pwd -P)/$cache_dir" ;; esac local cache_file="$cache_dir/init-${func_name}.${shell}" # Bump when generated helpers change, even if GTR_VERSION is unchanged. - # Schema 16 preserves sticky permission bits in BSD trust-storage checks. - local cache_schema="${GTR_INIT_CACHE_VERSION:-16}" + # Schema 17 checks continuity with approved repository history (trust v4). + local cache_schema="${GTR_INIT_CACHE_VERSION:-17}" local cache_stamp="# gtr-cache: version=${GTR_VERSION:-unknown} init=${cache_schema} func=$func_name shell=$shell" # A matching public stamp alone does not make executable cache data trusted. @@ -337,6 +347,8 @@ _init_emit_posix_trust_helpers() { # Keep the snapshot encoding and extraction identical to the CLI. _init_emit_snapshot_helpers + typeset -f _hooks_repo_identity | sed 's/_hooks_repo_identity/__FUNC___hooks_repo_identity/g' + typeset -f _hooks_history_matches | sed 's/_hooks_history_matches/__FUNC___hooks_history_matches/g' _init_emit_trust_storage_helpers | sed -e 's/_hooks_/__FUNC___hooks_/g' -e 's/_config_safety_acl_check/__FUNC___check_storage_acl/g' @@ -381,22 +393,6 @@ __SHELL_PRELUDE__ printf '%s/%s\n' "$_gtr_repo_root" "$(basename "$_gtr_config_file")" } -__FUNC___hooks_repo_identity() { -__SHELL_PRELUDE__ - local _gtr_repo_root="$1" - local _gtr_common_dir _gtr_identity _gtr_birth_time - _gtr_common_dir="$(git -C "$_gtr_repo_root" rev-parse --git-common-dir 2>/dev/null)" || return 1 - _gtr_common_dir="$(cd "$_gtr_repo_root" && cd "$_gtr_common_dir" && pwd -P)" || return 1 - _gtr_identity="$(LC_ALL=C TZ=UTC stat -c '%d:%i:%W:%w' "$_gtr_common_dir" 2>/dev/null)" || - _gtr_identity="$(LC_ALL=C stat -f '%d:%i:%.9FB' "$_gtr_common_dir" 2>/dev/null)" || return 1 - _gtr_birth_time="${_gtr_identity#*:*:}" - _gtr_birth_time="${_gtr_birth_time%%:*}" - case "$_gtr_birth_time" in - (''|0|0.*|*[!0-9.]*) return 1 ;; - esac - printf '%s\n%s\n' "$_gtr_common_dir" "$_gtr_identity" -} - __FUNC___hooks_current_trust_key() { __SHELL_PRELUDE__ local _gtr_config_file="$1" @@ -408,18 +404,20 @@ __SHELL_PRELUDE__ _gtr_hook_hash="$(__FUNC___hooks_current_content_hash "$_gtr_config_file" "$_gtr_definitions" 2>/dev/null)" || return 1 _gtr_repo_root="$(__FUNC___hooks_repo_root "$_gtr_config_file" 2>/dev/null)" || return 1 _gtr_repo_identity="$(__FUNC___hooks_repo_identity "$_gtr_repo_root" 2>/dev/null)" || return 1 - printf 'v3\n%s\n%s\n%s\n' "$_gtr_repo_root" "$_gtr_repo_identity" "$_gtr_hook_hash" | shasum -a 256 | cut -d' ' -f1 + printf 'v4\n%s\n%s\n%s\n' "$_gtr_repo_root" "$_gtr_repo_identity" "$_gtr_hook_hash" | shasum -a 256 | cut -d' ' -f1 } __FUNC___hooks_marker_matches_config() { __SHELL_PRELUDE__ local _gtr_trust_path="$1" local _gtr_config_file="$2" - local _gtr_marker _gtr_canonical_config + local _gtr_marker _gtr_canonical_config _gtr_approved_commit _gtr_trust_path="$(__FUNC___hooks_safe_trust_path "$_gtr_trust_path")" || return 1 _gtr_marker="$(cat "$_gtr_trust_path" 2>/dev/null)" || return 1 _gtr_canonical_config="$(__FUNC___hooks_canonical_config_path "$_gtr_config_file" 2>/dev/null)" || return 1 - [ "$_gtr_marker" = "$_gtr_canonical_config" ] + _gtr_approved_commit="${_gtr_marker%%$'\n'*}" + [ "${_gtr_marker#*$'\n'}" = "$_gtr_canonical_config" ] || return 1 + __FUNC___hooks_history_matches "${_gtr_canonical_config%/*}" "$_gtr_approved_commit" } __FUNC___hooks_are_trusted() { @@ -440,7 +438,7 @@ EOF # Usage: _init_emit_fish_trust_helpers # Prints self-contained definitions with __FUNC__ placeholders; runs no hooks. _init_emit_fish_trust_helpers() { - local snapshot_helpers storage_helpers + local snapshot_helpers storage_helpers history_helper identity_helper # Quote for a Fish single-quoted string. Bash 3.2's parameter substitution # over-escapes single quotes here, so use sed for consistent generated code. snapshot_helpers=$(_init_emit_snapshot_helpers | @@ -450,6 +448,12 @@ _init_emit_fish_trust_helpers() { storage_helpers=$(_init_emit_trust_storage_helpers | sed -e 's/\\/\\\\/g' -e "s/'/\\\\'/g") printf "function __FUNC___hooks_safe_trust_path\n command bash -c '%s\n_hooks_safe_trust_path \"\$1\"' bash \"\$argv[1]\"\nend\n" "$storage_helpers" + history_helper=$(typeset -f _hooks_history_matches | + sed -e 's/\\/\\\\/g' -e "s/'/\\\\'/g") + printf "function __FUNC___hooks_history_matches\n command bash -c '%s\n_hooks_history_matches \"\$1\" \"\$2\"' bash \"\$argv[1]\" \"\$argv[2]\"\nend\n" "$history_helper" + identity_helper=$(typeset -f _hooks_repo_identity | + sed -e 's/\\/\\\\/g' -e "s/'/\\\\'/g") + printf "function __FUNC___hooks_repo_identity\n command bash -c '%s\n_hooks_repo_identity \"\$1\"' bash \"\$argv[1]\"\nend\n" "$identity_helper" cat <<'FISH' function __FUNC___gtrconfig_path set -l _gtr_git_common_dir (git rev-parse --git-common-dir 2>/dev/null) @@ -486,22 +490,6 @@ function __FUNC___hooks_canonical_config_path printf '%s/%s\n' "$_gtr_repo_root" (basename "$_gtr_config_file") end -function __FUNC___hooks_repo_identity - # Keep the identity algorithm identical to the CLI and POSIX wrappers. - command sh -c ' - common_dir=$(git -C "$1" rev-parse --git-common-dir 2>/dev/null) || exit 1 - common_dir=$(cd "$1" && cd "$common_dir" && pwd -P) || exit 1 - identity=$(LC_ALL=C TZ=UTC stat -c "%d:%i:%W:%w" "$common_dir" 2>/dev/null) || - identity=$(LC_ALL=C stat -f "%d:%i:%.9FB" "$common_dir" 2>/dev/null) || exit 1 - birth_time="${identity#*:*:}" - birth_time="${birth_time%%:*}" - case "$birth_time" in - ""|0|0.*|*[!0-9.]*) exit 1 ;; - esac - printf "%s\\n%s\\n" "$common_dir" "$identity" - ' sh "$argv[1]" -end - function __FUNC___hooks_current_trust_key set -l _gtr_config_file "$argv[1]" set -l _gtr_definitions "$argv[2]" @@ -515,7 +503,7 @@ function __FUNC___hooks_current_trust_key set -l _gtr_repo_identity (__FUNC___hooks_repo_identity "$_gtr_repo_root" 2>/dev/null | string collect) test $pipestatus[1] -eq 0; or return 1 test -n "$_gtr_repo_identity"; or return 1 - printf 'v3\n%s\n%s\n%s\n' "$_gtr_repo_root" "$_gtr_repo_identity" "$_gtr_hook_hash" | shasum -a 256 | cut -d' ' -f1 + printf 'v4\n%s\n%s\n%s\n' "$_gtr_repo_root" "$_gtr_repo_identity" "$_gtr_hook_hash" | shasum -a 256 | cut -d' ' -f1 end function __FUNC___hooks_marker_matches_config @@ -524,8 +512,10 @@ function __FUNC___hooks_marker_matches_config set _gtr_trust_path (__FUNC___hooks_safe_trust_path "$_gtr_trust_path") or return 1 set -l _gtr_marker (cat "$_gtr_trust_path" 2>/dev/null) + test (count $_gtr_marker) -eq 2; or return 1 set -l _gtr_canonical_config (__FUNC___hooks_canonical_config_path "$_gtr_config_file" 2>/dev/null) - test "$_gtr_marker" = "$_gtr_canonical_config" + test "$_gtr_marker[2]" = "$_gtr_canonical_config"; or return 1 + __FUNC___hooks_history_matches (dirname "$_gtr_canonical_config") "$_gtr_marker[1]" end function __FUNC___hooks_are_trusted diff --git a/lib/commands/trust.sh b/lib/commands/trust.sh index 0b00d5f..4a6421c 100644 --- a/lib/commands/trust.sh +++ b/lib/commands/trust.sh @@ -11,9 +11,10 @@ cmd_trust() { fi # Bind the definitions to the repository present before they are read. - local repo_root repo_identity + local repo_root repo_identity approved_commit repo_root=$(_hooks_repo_root "$config_file") || return 1 repo_identity=$(_hooks_repo_identity "$repo_root") || return 1 + approved_commit=$(git -C "$repo_root" rev-parse --verify 'HEAD^{commit}' 2>/dev/null) || true # Show all trusted command entries from .gtrconfig local hook_content @@ -23,6 +24,10 @@ cmd_trust() { log_info "No hooks or executable defaults defined in $config_file" return 0 fi + if [ -z "$approved_commit" ]; then + log_error "Commit the initial repository history before approving commands" + return 1 + fi if _hooks_snapshot_is_trusted "$config_file" "$hook_content"; then log_info "Executable commands in $config_file are already trusted" @@ -45,10 +50,11 @@ cmd_trust() { log_warn "Approval includes scripts and programs they invoke; their contents are not pinned." if prompt_yes_no "Trust these commands?"; then - if _hooks_write_trust_marker "$trust_path" "$config_file"; then + if _hooks_write_trust_marker "$trust_path" "$config_file" "$approved_commit"; then local current_trust_path current_trust_path=$(_hooks_current_trust_path "$config_file") || true - if [ "$current_trust_path" != "$trust_path" ]; then + if [ "$current_trust_path" != "$trust_path" ] || + ! _hooks_marker_matches_config "$trust_path" "$config_file"; then log_warn "Executable commands or repository identity changed during review; current commands remain untrusted" return 1 fi diff --git a/lib/hooks.sh b/lib/hooks.sh index 5cdc1b4..4523846 100644 --- a/lib/hooks.sh +++ b/lib/hooks.sh @@ -4,6 +4,8 @@ # Hook dispatch can enter a different linked worktree after CLI startup. # shellcheck source=lib/config_safety.sh . "${BASH_SOURCE[0]%/*}/config_safety.sh" +# shellcheck source=lib/trust_history.sh +. "${BASH_SOURCE[0]%/*}/trust_history.sh" # ── Hook trust model ──────────────────────────────────────────────────── # Hooks and executable defaults from .gtrconfig files (committed to @@ -12,8 +14,9 @@ # files. # # Trust state is stored in ~/.config/gtr/trusted/ -# where binds the canonical repo root, Git directory identity and trusted -# content hash. Replacing a repository at the same path requires new approval. +# where binds the canonical repo root, Git/object directory identities and trusted +# content hash. The marker also records an approved commit: unrelated history +# requires new approval even if the common Git directory itself is preserved. # Trust approves command definitions, including any code they delegate to; it # does not pin script contents, dependencies or repository snapshot state. @@ -202,27 +205,6 @@ _hooks_repo_root() { ) } -# Identify this repository instance, rather than just its reusable path. -# Use the common Git directory so normal commits and linked worktrees do not -# invalidate approval. Require a known birth time and preserve nanoseconds to -# distinguish rapid inode reuse. Filesystems without birth times fail closed. -# GNU %W is whole seconds even with precision; %w retains birth nanoseconds. -# Do not use a repository-controlled config value or file as an identity token. -_hooks_repo_identity() { - local repo_root="$1" - local common_dir identity birth_time - common_dir=$(git -C "$repo_root" rev-parse --git-common-dir 2>/dev/null) || return 1 - common_dir=$(cd "$repo_root" && cd "$common_dir" && pwd -P) || return 1 - identity=$(LC_ALL=C TZ=UTC stat -c '%d:%i:%W:%w' "$common_dir" 2>/dev/null) || - identity=$(LC_ALL=C stat -f '%d:%i:%.9FB' "$common_dir" 2>/dev/null) || return 1 - birth_time="${identity#*:*:}" - birth_time="${birth_time%%:*}" - case "$birth_time" in - ''|0|0.*|*[!0-9.]*) return 1 ;; - esac - printf '%s\n%s\n' "$common_dir" "$identity" -} - # Resolve the canonical path for a .gtrconfig file # Usage: _hooks_canonical_config_path _hooks_canonical_config_path() { @@ -244,7 +226,7 @@ _hooks_reviewed_trust_key() { if [ -z "$repo_identity" ]; then repo_identity=$(_hooks_repo_identity "$repo_root") || return 1 fi - printf 'v3\n%s\n%s\n%s\n' "$repo_root" "$repo_identity" "$hash" | shasum -a 256 | cut -d' ' -f1 + printf 'v4\n%s\n%s\n%s\n' "$repo_root" "$repo_identity" "$hash" | shasum -a 256 | cut -d' ' -f1 } # Compute the repo-scoped trust key for the current trusted command content @@ -290,17 +272,19 @@ _hooks_trust_path_for_content() { _hooks_reviewed_trust_path "$1" "$2" } -# Check whether a trust marker matches the canonical config path. +# Check the canonical config path and continuity with the approved history. # Usage: _hooks_marker_matches_config _hooks_marker_matches_config() { local trust_path="$1" local config_file="$2" - local marker_content canonical_config_file + local marker_content canonical_config_file approved_commit trust_path=$(_hooks_safe_trust_path "$trust_path") || return 1 marker_content="$(cat "$trust_path" 2>/dev/null)" || return 1 canonical_config_file=$(_hooks_canonical_config_path "$config_file") || return 1 - [ "$marker_content" = "$canonical_config_file" ] + approved_commit="${marker_content%%$'\n'*}" + [ "${marker_content#*$'\n'}" = "$canonical_config_file" ] || return 1 + _hooks_history_matches "${canonical_config_file%/*}" "$approved_commit" } # Check if .gtrconfig command definitions are trusted for the current repository @@ -316,10 +300,11 @@ _hooks_are_trusted() { } # Write a trust marker that matches the reviewed command definitions -# Usage: _hooks_write_trust_marker [config_file] +# Usage: _hooks_write_trust_marker [approved_commit] _hooks_write_trust_marker() { local trust_path="$1" local config_file="${2:-}" + local approved_commit="${3:-}" local temp_path="" local canonical_config_file="" @@ -330,9 +315,12 @@ _hooks_write_trust_marker() { return 1 } canonical_config_file=$(_hooks_canonical_config_path "$config_file") || return 1 + if [ -z "$approved_commit" ]; then + approved_commit=$(git -C "${canonical_config_file%/*}" rev-parse --verify 'HEAD^{commit}' 2>/dev/null) || return 1 + fi temp_path="$(umask 077; mktemp "${trust_path%/*}/.trust.XXXXXX")" || return 1 - if ! printf '%s\n' "$canonical_config_file" > "$temp_path"; then + if ! printf '%s\n%s\n' "$approved_commit" "$canonical_config_file" > "$temp_path"; then rm -f "$temp_path" return 1 fi @@ -347,11 +335,14 @@ _hooks_write_trust_marker() { # Usage: _hooks_mark_trusted _hooks_mark_trusted() { local config_file="$1" - local hook_content trust_path + local hook_content trust_path repo_root approved_commit + repo_root=$(_hooks_repo_root "$config_file") || return 1 + approved_commit=$(git -C "$repo_root" rev-parse --verify 'HEAD^{commit}' 2>/dev/null) || true hook_content=$(_hooks_read_definitions "$config_file") || return 0 + [ -n "$approved_commit" ] || return 1 trust_path=$(_hooks_reviewed_trust_path "$config_file" "$hook_content") || return 1 - _hooks_write_trust_marker "$trust_path" "$config_file" + _hooks_write_trust_marker "$trust_path" "$config_file" "$approved_commit" } # Get hooks, filtering out untrusted .gtrconfig hooks with a warning diff --git a/lib/trust_history.sh b/lib/trust_history.sh new file mode 100644 index 0000000..3ee7668 --- /dev/null +++ b/lib/trust_history.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Shared by CLI trust checks and the self-contained generated shell helpers. + +# Bind approval to both the Git directory and its object database. Replacing +# children while retaining only .git must not preserve the repository identity. +# Normal commits, repacks and linked worktrees retain these directory instances. +_hooks_repo_identity() { + local repo_root="$1" common_dir directory identity birth_time + common_dir=$(git -C "$repo_root" rev-parse --git-common-dir 2>/dev/null) || return 1 + common_dir=$(cd "$repo_root" && cd "$common_dir" && pwd -P) || return 1 + for directory in "$common_dir" "$common_dir/objects"; do + directory=$(cd "$directory" && pwd -P) || return 1 + # GNU %W loses fractional seconds; %w and BSD %.9FB retain birth precision. + identity=$(LC_ALL=C TZ=UTC stat -c '%d:%i:%W:%w' "$directory" 2>/dev/null) || + identity=$(LC_ALL=C stat -f '%d:%i:%.9FB' "$directory" 2>/dev/null) || return 1 + birth_time="${identity#*:*:}" + birth_time="${birth_time%%:*}" + case "$birth_time" in ''|0|0.*|*[!0-9.]*) return 1 ;; esac + printf '%s\n%s\n' "$directory" "$identity" + done +} + +# Require actual shared commit history, not just objects retained in a replaced +# database. Repository-owned replace refs, grafts and commit-graph caches cannot +# establish continuity. Approval checks must never fetch from a promisor remote. +# This attests repository continuity, not the bytes of delegated scripts. +_hooks_history_matches() { + local repo_root="$1" approved_commit="$2" + case "$approved_commit" in ''|*[!0-9a-f]*) return 1 ;; esac + case "${#approved_commit}" in 40|64) ;; *) return 1 ;; esac + # The transport allowlist also blocks lazy fetches on older Git versions that + # do not recognize GIT_NO_LAZY_FETCH. Read parent links from actual objects. + GIT_ALLOW_PROTOCOL='' GIT_NO_LAZY_FETCH=1 GIT_NO_REPLACE_OBJECTS=1 GIT_GRAFT_FILE=/dev/null \ + git -C "$repo_root" -c core.commitGraph=false merge-base "$approved_commit" HEAD >/dev/null 2>&1 +} diff --git a/tests/cmd_trust.bats b/tests/cmd_trust.bats index 1a85fea..879b509 100644 --- a/tests/cmd_trust.bats +++ b/tests/cmd_trust.bats @@ -133,6 +133,20 @@ EOF ! _hooks_are_trusted "$TEST_REPO/.gtrconfig" } +@test "cmd_trust rejects unrelated history introduced during confirmation" { + git config -f "$TEST_REPO/.gtrconfig" hooks.postCd 'echo harmless' + prompt_yes_no() { + git checkout --quiet --orphan replacement + git -c user.name=Fixture -c user.email=fixture@example.test \ + commit --allow-empty --quiet -m unrelated + return 0 + } + run cmd_trust + [ "$status" -eq 1 ] + [[ "$output" == *"repository identity changed during review"* ]] + ! _hooks_are_trusted "$TEST_REPO/.gtrconfig" +} + @test "cmd_trust snapshots identity before reading definitions" { git config -f "$TEST_REPO/.gtrconfig" hooks.postCd 'sh ./bootstrap.sh' printf 'echo original\n' > "$TEST_REPO/bootstrap.sh" diff --git a/tests/hooks.bats b/tests/hooks.bats index b0c2538..ae8019b 100644 --- a/tests/hooks.bats +++ b/tests/hooks.bats @@ -223,10 +223,11 @@ EOF _hooks_are_trusted "$TEST_REPO/.gtrconfig" } -@test "repository identity stays stable across commits and linked worktrees" { +@test "repository identity stays stable across commits, garbage collection and linked worktrees" { git config -f "$TEST_REPO/.gtrconfig" hooks.postCreate 'echo harmless' _hooks_mark_trusted "$TEST_REPO/.gtrconfig" git -C "$TEST_REPO" commit --allow-empty -m second --quiet + git -C "$TEST_REPO" gc --quiet create_test_worktree identity-test (cd "$TEST_WT_PATH" && _hooks_are_trusted "$TEST_REPO/.gtrconfig") } diff --git a/tests/init.bats b/tests/init.bats index 3d8eab0..d0b3068 100644 --- a/tests/init.bats +++ b/tests/init.bats @@ -225,6 +225,8 @@ run_generated_replacement_repo_hooks() ( export XDG_CONFIG_HOME="$fixture/config" mkdir -p "$REPO_FIXTURE" git -C "$REPO_FIXTURE" init --quiet + git -C "$REPO_FIXTURE" -c user.name=Fixture -c user.email=fixture@example.test \ + -c commit.gpgsign=false commit --allow-empty --quiet -m initial git config -f "$REPO_FIXTURE/.gtrconfig" hooks.postCd 'sh "$REPO_ROOT/bootstrap.sh"' printf 'echo original > "$REPO_ROOT/hook-result"\n' > "$REPO_FIXTURE/bootstrap.sh" source "$PROJECT_ROOT/lib/hooks.sh" @@ -318,20 +320,20 @@ run_generated_v2_trust_upgrade() ( [ ! -f "$REPO_FIXTURE/hook-result" ] ) -@test "fresh bash helpers reject v2 approval and accept explicit v3 approval" { +@test "fresh bash helpers reject v2 approval and accept current approval" { run run_generated_v2_trust_upgrade bash [ "$status" -eq 0 ] [[ "$output" == *"Untrusted .gtrconfig hooks skipped"* ]] } -@test "fresh zsh helpers reject v2 approval and accept explicit v3 approval" { +@test "fresh zsh helpers reject v2 approval and accept current approval" { require_runtime_shell zsh run run_generated_v2_trust_upgrade zsh [ "$status" -eq 0 ] [[ "$output" == *"Untrusted .gtrconfig hooks skipped"* ]] } -@test "fresh fish helpers reject v2 approval and accept explicit v3 approval" { +@test "fresh fish helpers reject v2 approval and accept current approval" { require_runtime_shell fish run run_generated_v2_trust_upgrade fish [ "$status" -eq 0 ] @@ -1167,14 +1169,14 @@ run_generated_v2_trust_upgrade() ( mkdir -p "$XDG_CACHE_HOME/gtr" for shell_name in bash zsh fish; do for func_name in gtr gwtr; do - for schema in 15 14 13 12 11 10 9 8 7 6; do + for schema in 16 15 14 13 12 11 10 9 8 7 6; do cache_file="$XDG_CACHE_HOME/gtr/init-$func_name.$shell_name" printf '# gtr-cache: version=test init=%s func=%s shell=%s\nold-cache\n' \ "$schema" "$func_name" "$shell_name" > "$cache_file" run cmd_init "$shell_name" --as "$func_name" [ "$status" -eq 0 ] [[ "$output" != *old-cache* ]] - [[ "$(head -1 "$cache_file")" == *"init=16"* ]] + [[ "$(head -1 "$cache_file")" == *"init=17"* ]] done done done @@ -1203,15 +1205,15 @@ run_documented_init_upgrade() ( if [ "$shell_name" = zsh ]; then snippet='compdef() { :; }'$'\n'"$snippet" fi - for schema in 6 7 8 9 10 11 12 13 14 15 16; do + for schema in 6 7 8 9 10 11 12 13 14 15 16 17; do version="$cli_version" - [ "$schema" != 16 ] || version=old-version + [ "$schema" != 17 ] || version=old-version printf '# gtr-cache: version=%s init=%s func=gtr shell=%s\n' \ "$version" "$schema" "$shell_name" > "$cache_file" printf '%s\n' 'echo stale > "$STALE_CACHE_MARKER"' >> "$cache_file" "$shell_name" -c "$snippet" [ ! -f "$STALE_CACHE_MARKER" ] - [[ "$(head -1 "$cache_file")" == *"version=$cli_version init=16 "* ]] + [[ "$(head -1 "$cache_file")" == *"version=$cli_version init=17 "* ]] done done ) @@ -1260,6 +1262,8 @@ run_generated_snapshot_replacement_hooks() ( REAL_GIT=$(command -v git) mkdir -p "$REPO_FIXTURE" "$fixture/bin" git -C "$REPO_FIXTURE" init --quiet + git -C "$REPO_FIXTURE" -c user.name=Fixture -c user.email=fixture@example.test \ + -c commit.gpgsign=false commit --allow-empty --quiet -m initial git config -f "$REPO_FIXTURE/.gtrconfig" hooks.postCd 'echo approved > "$REPO_ROOT/result"' source "$PROJECT_ROOT/lib/hooks.sh" _hooks_mark_trusted "$REPO_FIXTURE/.gtrconfig" diff --git a/tests/trust_followup.bats b/tests/trust_followup.bats index 3caacc0..ef32a9d 100644 --- a/tests/trust_followup.bats +++ b/tests/trust_followup.bats @@ -15,64 +15,6 @@ teardown() { teardown_integration_repo } -# Command trust deliberately does not pin delegated script bytes. Replacing -# every child of .git while retaining the directory is equivalent, for this -# boundary, to changing the checkout and refs in an already approved repository. -preserved_directory_case() { - local runtime="$1" identity key donor="$TEST_REPO/donor" - export TEST_REPO FOLLOWUP_INIT - git config -f .gtrconfig hooks.postCd 'sh "$REPO_ROOT/bootstrap.sh"' - printf 'echo original > "$REPO_ROOT/result"\n' > bootstrap.sh - _hooks_mark_trusted "$TEST_REPO/.gtrconfig" - identity=$(_hooks_repo_identity "$TEST_REPO") - key=$(_hooks_current_trust_key "$TEST_REPO/.gtrconfig") - - git init --quiet "$donor" - git -C "$donor" -c user.name=Fixture -c user.email=fixture@example.test \ - -c commit.gpgsign=false commit --allow-empty --quiet -m replacement - # All deleted entries belong to this test's disposable repository. - find "$TEST_REPO/.git" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + - cp -R "$donor/.git/." "$TEST_REPO/.git/" - printf 'echo replacement > "$REPO_ROOT/result"\n' > bootstrap.sh - [ "$(_hooks_repo_identity "$TEST_REPO")" = "$identity" ] - [ "$(_hooks_current_trust_key "$TEST_REPO/.gtrconfig")" = "$key" ] - - if [ "$runtime" = cli ]; then - run_hooks postCd REPO_ROOT="$TEST_REPO" - else - FOLLOWUP_INIT="$TEST_REPO/init.$runtime" - cmd_init "$runtime" > "$FOLLOWUP_INIT" - "$runtime" -c 'source "$FOLLOWUP_INIT"; gtr_run_post_cd_hooks "$TEST_REPO"' - fi - [ "$(cat result)" = replacement ] - rm result - git config -f .gtrconfig hooks.postCd 'echo changed > "$REPO_ROOT/result"' - if [ "$runtime" = cli ]; then - run_hooks postCd REPO_ROOT="$TEST_REPO" - else - "$runtime" -c 'source "$FOLLOWUP_INIT"; gtr_run_post_cd_hooks "$TEST_REPO"' - fi - [ ! -e result ] -} - -@test "CLI command trust retains approval when Git directory contents change in place" { - preserved_directory_case cli -} - -@test "bash command trust retains approval when Git directory contents change in place" { - preserved_directory_case bash -} - -@test "zsh command trust retains approval when Git directory contents change in place" { - command -v zsh >/dev/null || skip "zsh unavailable" - preserved_directory_case zsh -} - -@test "fish command trust retains approval when Git directory contents change in place" { - command -v fish >/dev/null || skip "fish unavailable" - preserved_directory_case fish -} - # Historical README setup bypassed the installed CLI entirely on an init=6 # cache hit. A binary upgrade cannot repair shell startup code it never runs. legacy_setup_case() { diff --git a/tests/trust_history.bats b/tests/trust_history.bats new file mode 100644 index 0000000..c4f45e6 --- /dev/null +++ b/tests/trust_history.bats @@ -0,0 +1,207 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + setup_integration_repo + export XDG_CONFIG_HOME="$TEST_REPO/private-config" + export XDG_CACHE_HOME="$TEST_REPO/private-cache" + export XDG_DATA_HOME="$TEST_REPO/private-data" + source_gtr_commands +} + +teardown() { + teardown_integration_repo +} + +run_history_hook() { + local runtime="$1" + if [ "$runtime" = cli ]; then + run_hooks postCd REPO_ROOT="$TEST_REPO" + else + "$runtime" -c 'source "$HISTORY_INIT"; gtr_run_post_cd_hooks "$TEST_REPO"' + fi +} + +# Preserve the filesystem identity and hook definitions, but replace the Git +# database with unrelated history. Only disposable repositories are modified. +preserved_directory_history_case() { + local runtime="$1" history="${2:-unrelated}" initial approved identity repo_identity donor="$TEST_REPO/donor" + export TEST_REPO HISTORY_INIT="$TEST_REPO/init.$runtime" + initial=$(git rev-parse HEAD) + git config -f .gtrconfig hooks.postCd 'sh "$REPO_ROOT/bootstrap.sh"' + printf 'echo approved > "$REPO_ROOT/result"\n' > bootstrap.sh + git add bootstrap.sh + git commit --quiet -m approved + approved=$(git rev-parse HEAD) + _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + if [ "$runtime" != cli ]; then cmd_init "$runtime" > "$HISTORY_INIT"; fi + run_history_hook "$runtime" + [ "$(cat result)" = approved ] + + # Normal updates and a branch forked before approval must keep working. + printf 'echo updated > "$REPO_ROOT/result"\n' > bootstrap.sh + git add bootstrap.sh + git commit --quiet -m updated + run_history_hook "$runtime" + [ "$(cat result)" = updated ] + git checkout --quiet -b related "$initial" + printf 'echo related > "$REPO_ROOT/result"\n' > bootstrap.sh + git add bootstrap.sh + git commit --quiet -m related + run_history_hook "$runtime" + [ "$(cat result)" = related ] + rm result + + identity=$(stat -c '%d:%i:%W:%w' .git 2>/dev/null) || identity=$(stat -f '%d:%i:%.9FB' .git) + repo_identity=$(_hooks_repo_identity "$TEST_REPO") + if [ "$history" = related ]; then + git clone --quiet --no-hardlinks "$TEST_REPO" "$donor" + else + git init --quiet "$donor" + git -C "$donor" -c user.name=Fixture -c user.email=fixture@example.test \ + -c commit.gpgsign=false commit --allow-empty --quiet -m unrelated + fi + # Keep the approved objects available: mere object existence is not continuity. + git -C "$donor" fetch --quiet "$TEST_REPO" "$approved" + if [ "$history" = unrelated ]; then + # Also preserve the object database directory to exercise the independent + # history check; old objects alone and forged ancestry must not suffice. + find "$TEST_REPO/.git" -mindepth 1 -maxdepth 1 ! -name objects -exec rm -rf -- {} + + find "$TEST_REPO/.git/objects" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + + else + find "$TEST_REPO/.git" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + + fi + cp -R "$donor/.git/." "$TEST_REPO/.git/" + printf 'echo replacement > "$REPO_ROOT/result"\n' > bootstrap.sh + local current_identity + current_identity=$(stat -c '%d:%i:%W:%w' .git 2>/dev/null) || current_identity=$(stat -f '%d:%i:%.9FB' .git) + [ "$current_identity" = "$identity" ] + if [ "$history" = unrelated ]; then + [ "$(_hooks_repo_identity "$TEST_REPO")" = "$repo_identity" ] + fi + run_history_hook "$runtime" + [ ! -e result ] + + if [ "$history" = related ]; then + _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + run_history_hook "$runtime" + [ "$(cat result)" = replacement ] + return 0 + fi + + # Repository-owned grafts and replace refs must not forge shared history. + printf '%s %s\n' "$(git rev-parse HEAD)" "$approved" > .git/info/grafts + run_history_hook "$runtime" + [ ! -e result ] + rm .git/info/grafts + git replace "$(git rev-parse HEAD)" "$approved" + run_history_hook "$runtime" + [ ! -e result ] + git replace -d "$(git rev-parse HEAD)" + + # Explicitly approving the replacement repository restores execution. + _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + run_history_hook "$runtime" + [ "$(cat result)" = replacement ] +} + +@test "CLI rejects unrelated Git contents with a preserved common directory" { + preserved_directory_history_case cli +} + +@test "bash hooks reject unrelated Git contents with a preserved common directory" { + preserved_directory_history_case bash +} + +@test "zsh hooks reject unrelated Git contents with a preserved common directory" { + command -v zsh >/dev/null || skip "zsh unavailable" + preserved_directory_history_case zsh +} + +@test "fish hooks reject unrelated Git contents with a preserved common directory" { + command -v fish >/dev/null || skip "fish unavailable" + preserved_directory_history_case fish +} + +@test "CLI rejects a replaced object database even when it retains shared history" { + preserved_directory_history_case cli related +} + +@test "bash hooks reject a replaced object database even when it retains shared history" { + preserved_directory_history_case bash related +} + +@test "zsh hooks reject a replaced object database even when it retains shared history" { + command -v zsh >/dev/null || skip "zsh unavailable" + preserved_directory_history_case zsh related +} + +@test "fish hooks reject a replaced object database even when it retains shared history" { + command -v fish >/dev/null || skip "fish unavailable" + preserved_directory_history_case fish related +} + +@test "old v3 approval cannot bypass history checks in the CLI or generated shells" { + export TEST_REPO HISTORY_INIT + git config -f .gtrconfig hooks.postCd 'echo old-approval > "$REPO_ROOT/result"' + local key runtime common_dir identity + common_dir=$(cd "$(git rev-parse --git-common-dir)" && pwd -P) + identity=$(LC_ALL=C TZ=UTC stat -c '%d:%i:%W:%w' "$common_dir" 2>/dev/null) || + identity=$(LC_ALL=C stat -f '%d:%i:%.9FB' "$common_dir") + key=$(printf 'v3\n%s\n%s\n%s\n%s\n' "$TEST_REPO" \ + "$common_dir" "$identity" "$(_hooks_file_hash "$TEST_REPO/.gtrconfig")" | + shasum -a 256 | cut -d' ' -f1) + mkdir -p "$_GTR_TRUST_DIR" + printf '%s\n' "$TEST_REPO/.gtrconfig" > "$_GTR_TRUST_DIR/$key" + for runtime in cli bash zsh fish; do + command -v "$runtime" >/dev/null || [ "$runtime" = cli ] || continue + if [ "$runtime" != cli ]; then + HISTORY_INIT="$TEST_REPO/init.$runtime" + cmd_init "$runtime" > "$HISTORY_INIT" + fi + run_history_hook "$runtime" + [ ! -e result ] + done +} + +@test "repository commit-graph cache cannot forge ancestry for approved commands" { + command -v python3 >/dev/null || skip "python3 unavailable" + git help -a | grep -q 'commit-graph' || skip "Git commit-graph unavailable" + local approved middle head + git config -f .gtrconfig hooks.postCd 'echo escaped > "$REPO_ROOT/result"' + approved=$(git rev-parse HEAD) + _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + git checkout --quiet --orphan unrelated + git commit --allow-empty --quiet -m unrelated + middle=$(git rev-parse HEAD) + git commit --allow-empty --quiet -m unrelated-child + head=$(git rev-parse HEAD) + git -c commitGraph.generationVersion=1 commit-graph write --reachable + chmod u+w .git/objects/info/commit-graph + # Forge a cached parent for an intermediate commit, preserving actual object + # bytes. Endpoint-only checks and the public graph checksum cannot detect it. + python3 - "$approved" "$middle" "$head" <<'PY' +import hashlib +from pathlib import Path +import struct +import sys + +path = Path('.git/objects/info/commit-graph') +data = bytearray(path.read_bytes()) +assert data[:5] == b'CGPH\x01' +hash_size = {1: 20, 2: 32}[data[5]] +chunks = dict(struct.unpack_from('>4sQ', data, 8 + 12 * i) for i in range(data[6])) +count = struct.unpack_from('>I', data, chunks[b'OIDF'] + 4 * 255)[0] +oids = [data[chunks[b'OIDL'] + i * hash_size:chunks[b'OIDL'] + (i + 1) * hash_size].hex() + for i in range(count)] +entry = chunks[b'CDAT'] + oids.index(sys.argv[2]) * (hash_size + 16) +struct.pack_into('>III', data, entry + hash_size, oids.index(sys.argv[1]), 0x70000000, 2 << 2) +entry = chunks[b'CDAT'] + oids.index(sys.argv[3]) * (hash_size + 16) +struct.pack_into('>I', data, entry + hash_size + 8, 3 << 2) +data[-hash_size:] = hashlib.new('sha1' if hash_size == 20 else 'sha256', data[:-hash_size]).digest() +path.write_bytes(data) +PY + run_hooks postCd REPO_ROOT="$TEST_REPO" + [ ! -e result ] +} diff --git a/tests/trust_storage.bats b/tests/trust_storage.bats index d3291cf..6bbde16 100644 --- a/tests/trust_storage.bats +++ b/tests/trust_storage.bats @@ -160,11 +160,13 @@ exercise_generated_sticky_storage() { @test "trust rejects marker symlinks, dangling links, directories and FIFOs" { _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + local original_marker + original_marker=$(cat "$TRUST_MARKER") mv "$TRUST_MARKER" "$TEST_REPO/original-marker" ln -s "$TEST_REPO/original-marker" "$TRUST_MARKER" assert_storage_rejected [ -L "$TRUST_MARKER" ] - [ "$(cat "$TEST_REPO/original-marker")" = "$TEST_REPO/.gtrconfig" ] + [ "$(cat "$TEST_REPO/original-marker")" = "$original_marker" ] rm "$TRUST_MARKER" ln -s "$TEST_REPO/missing-marker" "$TRUST_MARKER" assert_storage_rejected @@ -180,11 +182,13 @@ exercise_generated_sticky_storage() { @test "trust rejects a symlinked trust directory without modifying its target" { _hooks_mark_trusted "$TEST_REPO/.gtrconfig" + local original_marker + original_marker=$(cat "$TRUST_MARKER") mv "$_GTR_TRUST_DIR" "$TEST_REPO/original-store" ln -s "$TEST_REPO/original-store" "$_GTR_TRUST_DIR" assert_storage_rejected [ -L "$_GTR_TRUST_DIR" ] - [ "$(cat "$TRUST_MARKER")" = "$TEST_REPO/.gtrconfig" ] + [ "$(cat "$TRUST_MARKER")" = "$original_marker" ] } @test "safe config-home symlinks and relative paths remain supported" { @@ -441,13 +445,14 @@ SH # user-owned approval to the key for unreviewed definitions. Its contents and # mode remain valid, so storage ACLs must block both reads and hook execution. replay_marker_for_changed_definitions() { - local original_marker="$TRUST_MARKER" + local original_marker="$TRUST_MARKER" original_content + original_content=$(cat "$original_marker") git config -f "$TEST_REPO/.gtrconfig" hooks.postCd 'echo unreviewed > "$REPO_ROOT/result"' TRUST_MARKER=$(_hooks_current_trust_path "$TEST_REPO/.gtrconfig") [ "$TRUST_MARKER" != "$original_marker" ] chmod +a 'everyone allow search,add_file,delete_child' "$_GTR_TRUST_DIR" mv "$original_marker" "$TRUST_MARKER" - [ "$(cat "$TRUST_MARKER")" = "$TEST_REPO/.gtrconfig" ] + [ "$(cat "$TRUST_MARKER")" = "$original_content" ] } @test "macOS ACL-renamed approvals cannot execute unreviewed CLI hooks" { From 4953342c4df2134d5a70d18762d7bee4cb1650c9 Mon Sep 17 00:00:00 2001 From: Howon Lee Date: Tue, 10 Nov 2026 15:09:49 -0800 Subject: [PATCH 2/2] docs: clarify birth-time requirements for both Git directories --- README.md | 2 +- docs/configuration.md | 2 +- lib/commands/help.sh | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 17613e4..d8af6df 100644 --- a/README.md +++ b/README.md @@ -440,7 +440,7 @@ Trust markers must be regular files. Both the markers and their directory must b New trust directories are created with mode `0700` and markers with mode `0600`, regardless of your umask. Unsafe existing storage is rejected without changing its permissions. For an existing trust directory you own and have verified is not a symlink, `chmod 700 "${XDG_CONFIG_HOME:-$HOME/.config}/gtr/trusted"` repairs only that directory's permissions, not its markers or parent directories. Secure writable parent directories as well. If the store contains non-regular markers (such as directories or FIFOs), or other users could have modified it, move it aside before reviewing commands again with `git gtr trust` to create fresh approvals; changing permissions alone does not validate the old markers. -Trust requires a known filesystem birth time for the Git directory. If `stat` cannot provide it, `.gtrconfig` executable commands remain untrusted. +Trust requires known filesystem birth times for both the common Git directory and the object database directory. If `stat` cannot provide either one, `.gtrconfig` executable commands remain untrusted. ### Other Commands diff --git a/docs/configuration.md b/docs/configuration.md index b3f2beb..145a26a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -114,7 +114,7 @@ This is command-definition trust: scripts, dependencies, and tools invoked by ap Command definitions are captured once for trust verification and consumption, with NUL-delimited records preserving multiline values. The version 4 trust format invalidates older approvals; review commands again with `git gtr trust` after upgrading. -The identity includes device, inode, and birth time with nanosecond precision. If the filesystem or `stat` implementation cannot provide a known birth time, approval fails closed and `.gtrconfig` executable commands remain disabled. +Each directory's identity includes device, inode, and birth time with nanosecond precision. Both the common Git directory and the object database directory require a known birth time. If the filesystem or `stat` implementation cannot provide either one, approval fails closed and `.gtrconfig` executable commands remain disabled. --- diff --git a/lib/commands/help.sh b/lib/commands/help.sh index e143282..bd07779 100644 --- a/lib/commands/help.sh +++ b/lib/commands/help.sh @@ -502,8 +502,9 @@ Approval includes scripts and programs invoked by these commands. Their contents are not hashed or pinned; review delegated code and trust its contributors before approving. -Trust requires a known filesystem birth time for the Git directory. -Without it, .gtrconfig executable commands remain untrusted. +Trust requires known filesystem birth times for both the common Git +directory and the object database directory. If either is unavailable, +.gtrconfig executable commands remain untrusted. Hooks and defaults from your local git config (.git/config, ~/.gitconfig) are always trusted since you control those files directly.