Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/guardrails/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
"name": "guardrails",
"version": "0.26.0",
"version": "0.26.1",
"description": "Twelve safety guards that block secret/credential writes, hardcoded machine-specific paths, git hook-bypass attempts, irreversible git operations (force-push, reset --hard, worktree-wide checkout/restore discards), Bash file-write workarounds that circumvent Write/Edit hooks, multi-line `git commit -m` messages (an actual-newline `-m` mangles across shells; single-line `-m` passes), commit subjects and gh pr create titles that violate the repo's tracked team convention (when one is declared in .claude/source-control.md), (advisory) hallucinated CLI flags, (advisory) /plugin:skill references that do not resolve, (advisory) markdown citing a repo path the repo's own history shows was removed, (advisory, opt-in) un-throttled Workflow fan-out that risks burst 529s, and (advisory, opt-in) direct gh pr create calls bypassing this marketplace's own pull-request skill — each independently toggleable.",
"author": {
"name": "Melodic Software",
Expand Down
7 changes: 7 additions & 0 deletions plugins/guardrails/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to the `guardrails` plugin are documented here. Format follows
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this plugin uses semantic versioning.

## [0.26.1]

### Fixed

- **`block-dangerous-git` `repo_oid_width` no longer caches a width-0 failure or misdiagnoses it as a movable lease (#2227).** When `git rev-parse --show-object-format` fails, the guard now surfaces the git error, refuses to cache the failure for the rest of the invocation, and blocks with a distinct message from the abbreviation/wrong-width case — so a literal full-width SHA is not blamed on the operator when the repository's hash format could not be read.
- **`--no-force-with-lease` now clears unknown-width lease state.** A trailing negation that cancels every preceding `--force-with-lease` also resets `lease_width_unknown` and `_lease_oid_width_unknown`, so a pinned lease whose width probe failed is not incorrectly blocked after the negation.

## [0.26.0]

### Changed
Expand Down
46 changes: 38 additions & 8 deletions plugins/guardrails/hooks/block-dangerous-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ is_lease_opt() { abbrev_match "force-with-lease" "${1%%=*}" 7; }
# that is a ref name at the destination. It needs no wrapper.
#
# And it bites in the OTHER direction far more often than as a bypass: when the
# base is not a repository at all, the probe answers 0 and the guard fails closed,
# base is not a repository at all, the probe cannot read an object format and the
# guard fails closed,
# so `cd <repo> && git push --force-with-lease=main:<literal full-width sha>
# origin main` — the very form the block messages above prescribe — is DENIED
# from a session root that is not itself a repository. Fail-closed is the right
Expand All @@ -308,16 +309,26 @@ is_lease_opt() { abbrev_match "force-with-lease" "${1%%=*}" 7; }
# expectations would spawn one git per expectation.
_repo_oid_width=""
_repo_oid_width_key=""
# repo_oid_width <locating-option...> — sets $_repo_oid_width.
_repo_oid_width_err=""
_lease_oid_width_unknown=0
# repo_oid_width <locating-option...> — sets $_repo_oid_width (40/64) on success,
# clears it on failure, and sets $_repo_oid_width_err. Only a successfully read
# width is cached — a transient git failure must not poison later expectations.
# shellcheck disable=SC2329 # reached via the hook::bash_parse_segments callback chain
repo_oid_width() {
local key="$*"
local key="$*" out
[[ -n "$_repo_oid_width" && "$key" == "$_repo_oid_width_key" ]] && return 0
_repo_oid_width_key="$key"
case "$(git "$@" rev-parse --show-object-format 2>/dev/null)" in
_repo_oid_width=""
_repo_oid_width_err=""
out="$(git "$@" rev-parse --show-object-format 2>&1)" || true
case "$out" in
sha1) _repo_oid_width=40 ;;
sha256) _repo_oid_width=64 ;;
*) _repo_oid_width=0 ;;
*)
_repo_oid_width_err="${out:-could not read repository object format}"
_repo_oid_width_key=""
;;
esac
}

Expand All @@ -328,7 +339,11 @@ lease_expect_is_immutable() {
[[ -z "$expect" ]] && return 0
[[ "$expect" =~ ^[0-9a-fA-F]+$ ]] || return 1
repo_oid_width ${git_locating_opts[@]+"${git_locating_opts[@]}"}
((_repo_oid_width)) && ((${#expect} == _repo_oid_width))
if [[ -z "$_repo_oid_width" ]]; then
_lease_oid_width_unknown=1
return 1
fi
((${#expect} == _repo_oid_width))
}

# The repository-locating subset of git's global options, collected between the
Expand Down Expand Up @@ -612,7 +627,7 @@ alias_reexpand_admit() {
check_segment() {
local -a w=()
local nseg gi k x rest ch sub sub_idx staged worktree dry excl pos opseen
local if_includes lease_tracking lease_movable lease_seen lease_ref lease_expect
local if_includes lease_tracking lease_movable lease_width_unknown lease_seen lease_ref lease_expect
# Read by lease_expect_is_immutable further down the call chain; per-frame, so
# a recursive re-check of an alias expansion collects its own.
local -a git_locating_opts=()
Expand Down Expand Up @@ -776,6 +791,8 @@ check_segment() {
if_includes=0
lease_tracking=0
lease_movable=0
lease_width_unknown=0
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
_lease_oid_width_unknown=0
lease_seen=""
k=$((sub_idx + 1))
while ((k < nseg)); do
Expand All @@ -796,6 +813,8 @@ check_segment() {
if abbrev_match "force-with-lease" "--${x#--no-}" 7; then
lease_tracking=0
lease_movable=0
lease_width_unknown=0
_lease_oid_width_unknown=0
lease_seen=""
fi
# Consume the word here. Falling through would let
Expand Down Expand Up @@ -838,7 +857,13 @@ check_segment() {
else
lease_expect="${x#*=}"
lease_expect="${lease_expect#*:}"
lease_expect_is_immutable "$lease_expect" || lease_movable=1
if ! lease_expect_is_immutable "$lease_expect"; then
if ((_lease_oid_width_unknown)); then
lease_width_unknown=1
else
lease_movable=1
fi
fi
fi
fi
fi
Expand All @@ -850,6 +875,11 @@ check_segment() {
((dry)) && return 0
# Decided after the scan, never mid-scan: every one of these options is
# last-wins, so an early match cannot be acted on before the segment ends.
if ((lease_width_unknown)); then
block "push-lease-unsafe" \
"BLOCKED: git push --force-with-lease=<refname>:<expect> whose <expect> is a full object id, but this repository's hash format could not be determined (${_repo_oid_width_err:-git rev-parse --show-object-format failed}), so no literal object id can be validated here." \
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
"Run the push from a directory where git can read the repository's object format, or resolve the object id in a separate step and pass --force-with-lease=<refname>:<full-sha> once git can determine the hash width. Or allow via the block_dangerous_git_allow option (add push-lease-unsafe)."
fi
if ((lease_movable)); then
block "push-lease-unsafe" \
"BLOCKED: git push --force-with-lease=<refname>:<expect> whose <expect> is a name git resolves at push time (origin/main, HEAD, a tag, an abbreviated object id, or hex of the wrong width for this repository's hash format) leases against a moving target, and git-push(1) declares --force-if-includes a no-op alongside an explicit :<expect>, so nothing mitigates it." \
Expand Down
14 changes: 14 additions & 0 deletions plugins/guardrails/hooks/block-dangerous-git.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ run "git push --force-with-lease=main:<64-hex> in a SHA-1 repo (a 64-hex ref nam
run_in "$REPO_SHA256" "git push --force-with-lease=main:<64-hex> in a SHA-256 repo (full object id, allowed)" "git push --force-with-lease=main:$SHA256_OID origin main" 0
run_in "$REPO_SHA256" "git push --force-with-lease=main:<40-hex> in a SHA-256 repo (a 40-hex ref name resolves here, blocked)" "git push --force-with-lease=main:$SHA1_OID origin main" 2
run_in "$NOT_A_REPO" "git push --force-with-lease=main:<40-hex> outside a repository (width undeterminable, fail-closed block)" "git push --force-with-lease=main:$SHA1_OID origin main" 2

run_stderr_in() {
local dir="$1" label="$2" command="$3" expected="$4"
shift 4
local rc err
err="$(cd "$dir" && env "$@" bash "$HOOK" <<<"$(command_json_cwd "$command" "$dir")" 2>&1 >/dev/null)"
rc=$?
assert_exit "$label" "$expected" "$rc"
printf '%s' "$err"
}

err="$(run_stderr_in "$NOT_A_REPO" "width-undeterminable block names the git failure, not abbreviation" "git push --force-with-lease=main:$SHA1_OID origin main" 2)"
assert_contains "width-undeterminable: cites object format" "$err" "hash format could not be determined"
assert_absent "width-undeterminable: does not blame abbreviation" "$err" "abbreviated object id"
run_in "$NOT_A_REPO" "git push --force-with-lease=main: outside a repository (empty expect needs no width, allowed)" "git push --force-with-lease=main: origin main" 0
# git's repository-locating globals move the push off the invoking directory, so
# the width follows them. `-C` takes its value as a separate word — git rejects
Expand Down