threads / patch / 66889

patchfilter-branch: check hex oids in state-branch map

Subject: [PATCH] filter-branch: check hex oids in state-branch map

## tl;dr

One message between Nov 19, 2026 and Nov 19, 2026. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Dominik Maier· Nov 19, 2026, 19:23 UTC · lore

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(-)
Show changes to 2 files +31 −2

git-filter-branch.sh, t/t7003-filter-branch.sh

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

← back to recent threads