Git development
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Grant Moyer <dev@grantmoyer.com>,
	 Michele Locati <michele@locati.it>,
	Patrick Steinhardt <ps@pks.im>,
	dmnk+artist@google.com,  Dominik Maier <dmnk@google.com>
Subject: [PATCH] filter-branch: check hex oids in state-branch map
Date: Thu, 19 Nov 2026 20:23:16 +0100	[thread overview]
Message-ID: <20261119192316.2688525-1-dmnk@google.com> (raw)

When "--state-branch" loads an existing "filter.map" blob,
git-filter-branch splits each "from:to" line (restored to "from:to"
order in 4811dabed7 (filter-branch: fix commit map init from state
branch, 2026-10-02)) and writes "../map/$from_commit" without checking
that "$from_commit" or "$to_commit" contains valid hexadecimal object
IDs.

A malformed or untrusted state branch whose "filter.map" contains empty
keys, slashes, backslashes, or ".." segments writes outside
"$tempdir/map" instead of failing cleanly.

Read lines with "IFS= read -r line", and require "$from_commit" to be
non-empty lowercase hex and "$to_commit" to contain only lowercase hex
digits and spaces (allowing empty "$to_commit" for pruned commits)
before writing "../map/$from_commit".

Signed-off-by: Dominik Maier <dmnk@google.com>
---
 git-filter-branch.sh     | 14 ++++++++++++--
 t/t7003-filter-branch.sh | 19 +++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index 9aa07be6e1..fbf7882a70 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -298,13 +298,23 @@ then
 
 		git show "$state_commit:filter.map" >"$tempdir"/filter-map ||
 			die "Unable to load state from $state_branch:filter.map"
-		while read line
+		while IFS= read -r line
 		do
 			case "$line" in
 			*:*)
 				from_commit=${line%:*}
 				to_commit=${line#*:}
-				echo "$to_commit" >../map/"$from_commit";;
+				case "$from_commit" in
+				"" | *[!0-9a-f]*)
+					die "Unable to load state from $state_branch:filter.map"
+					;;
+				esac
+				case "$to_commit" in
+				*[!0-9a-f\ ]*)
+					die "Unable to load state from $state_branch:filter.map"
+					;;
+				esac
+				printf '%s\n' "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
index 801cc83e5e..cab97e0e0b 100755
--- a/t/t7003-filter-branch.sh
+++ b/t/t7003-filter-branch.sh
@@ -560,4 +560,23 @@ test_expect_success 'filter-branch handles ancestor rewrite' '
 	git diff-tree HEAD^ ancestor
 '
 
+test_expect_success 'state-branch rejects non-hex or path-traversing filter.map entries' '
+	test_when_finished "rm -f escaped && git update-ref -d refs/heads/bad-state" &&
+	bad_blob=$(printf "../../escaped:injected\n" | git hash-object -w --stdin) &&
+	bad_tree=$(printf "100644 blob %s\tfilter.map\n" "$bad_blob" | git mktree) &&
+	bad_commit=$(echo "bad state" | git commit-tree "$bad_tree") &&
+	git update-ref refs/heads/bad-state "$bad_commit" &&
+	test_must_fail git filter-branch -f --state-branch refs/heads/bad-state HEAD 2>err &&
+	test_grep "Unable to load state from refs/heads/bad-state:filter.map" err &&
+	test_path_is_missing escaped &&
+
+	head_oid=$(git rev-parse HEAD) &&
+	bs_blob=$(printf "%s\\\\:%s\n" "$head_oid" "$head_oid" | git hash-object -w --stdin) &&
+	bs_tree=$(printf "100644 blob %s\tfilter.map\n" "$bs_blob" | git mktree) &&
+	bs_commit=$(echo "backslash state" | git commit-tree "$bs_tree") &&
+	git update-ref refs/heads/bad-state "$bs_commit" &&
+	test_must_fail git filter-branch -f --state-branch refs/heads/bad-state HEAD 2>err &&
+	test_grep "Unable to load state from refs/heads/bad-state:filter.map" err
+'
+
 test_done
-- 
2.56.1.750.gf70dc682cf-goog


                 reply	other threads:[~2026-11-19 19:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119192316.2688525-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=dev@grantmoyer.com \
    --cc=dmnk+artist@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=michele@locati.it \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox