Git development
 help / color / mirror / Atom feed
* [PATCH] filter-branch: check hex oids in state-branch map
@ 2026-11-19 19:23 Dominik Maier
  0 siblings, 0 replies; only message in thread
From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Grant Moyer, Michele Locati, Patrick Steinhardt,
	dmnk+artist, Dominik Maier

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19 19:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:23 [PATCH] filter-branch: check hex oids in state-branch map Dominik Maier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox