All of lore.kernel.org
 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
Subject: [PATCH v2] filter-branch: check hex oids in state-branch map
Date: Mon, 30 Nov 2026 17:22:11 +0100	[thread overview]
Message-ID: <20261130162211.3369007-1-dmnk@google.com> (raw)
In-Reply-To: <xmqqmrqyqbq3.fsf@gitster.g>

Loading a state branch in git-filter-branch writes commit-map files
using unvalidated names from filter.map, allowing entries with slashes
or ".." to write outside the temporary map directory.

When --state-branch restores filter.map, each "from:to" line is split
and written directly to "../map/$from_commit" without checking either
side.

Read entries with "IFS= read -r" and require the source ID to be
non-empty lowercase hex and the target field to contain only lowercase
hex digits and spaces (it may be empty for pruned commits, or list
multiple space-separated commits expected by $(map "$parent")).

Signed-off-by: Dominik Maier <dmnk@google.com>
---
Changes in v2:
- Note in the commit message why "$to_commit" accepts spaces for
  multi-commit mappings read via $(map "$parent") (Junio C Hamano).
- Rebase onto master (86e79b2795).
- v1: https://lore.kernel.org/git/20261119192316.2688525-1-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 9aa07be6e..fbf7882a7 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 801cc83e5..cab97e0e0 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.911.g883d8bcb20-goog


  reply	other threads:[~2026-11-30 16:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:23 [PATCH] filter-branch: check hex oids in state-branch map Dominik Maier
2026-11-24 19:19 ` Junio C Hamano
2026-11-30 16:22   ` Dominik Maier [this message]
2026-12-01  7:27     ` [PATCH v2] " Patrick Steinhardt

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=20261130162211.3369007-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.