* [PATCH] filter-branch: check hex oids in state-branch map
@ 2026-11-19 19:23 Dominik Maier
2026-11-24 19:19 ` Junio C Hamano
0 siblings, 1 reply; 3+ messages 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] 3+ messages in thread* Re: [PATCH] filter-branch: check hex oids in state-branch map
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 ` [PATCH v2] " Dominik Maier
0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2026-11-24 19:19 UTC (permalink / raw)
To: Dominik Maier
Cc: git, Grant Moyer, Michele Locati, Patrick Steinhardt, dmnk+artist
Dominik Maier <dmnk@google.com> writes:
> 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.
Nice catch. With an empty key like ":foo", redirection to "../map/"
fails with an "Is a directory" error inside the loop without calling
die(), and keys containing "../" can escape "$tempdir/map" into the
surrounding working tree.
> 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".
Mentioning that $to_commit can be empty and the reason why is good. It
might help future readers to briefly mention why spaces are allowed in
$to_commit as well. I presume that it is because a single 'from' can be
mapped to more than one commits, and the map() function that dumps the
contents of ../map/$from_commit file is expected to be used like
for reparent in $(map "$parent"); do ... use each $reparent ...; done
without dq around $(map) call, to replace $parent (a from commit) with
these commits in the $to_commit SP delimited list?
Looks good. Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2] filter-branch: check hex oids in state-branch map
2026-11-24 19:19 ` Junio C Hamano
@ 2026-11-30 16:22 ` Dominik Maier
0 siblings, 0 replies; 3+ messages in thread
From: Dominik Maier @ 2026-11-30 16:22 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Grant Moyer, Michele Locati, Patrick Steinhardt,
dmnk+artist
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 16:22 UTC | newest]
Thread overview: 3+ messages (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
2026-11-24 19:19 ` Junio C Hamano
2026-11-30 16:22 ` [PATCH v2] " Dominik Maier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox