{"thread":{"id":"66889","subject":"[PATCH] filter-branch: check hex oids in state-branch map","startedAt":"2026-11-19T19:23:16Z","lastAt":"2026-11-19T19:23:16Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558882","messageId":"20261119192316.2688525-1-dmnk@google.com","threadId":"66889","inReplyTo":null,"subject":"[PATCH] filter-branch: check hex oids in state-branch map","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:23:16Z","receivedAt":"2026-11-19T19:23:16Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"When \"--state-branch\" loads an existing \"filter.map\" blob,\ngit-filter-branch splits each \"from:to\" line (restored to \"from:to\"\norder in 4811dabed7 (filter-branch: fix commit map init from state\nbranch, 2026-10-02)) and writes \"../map/$from_commit\" without checking\nthat \"$from_commit\" or \"$to_commit\" contains valid hexadecimal object\nIDs.\n\nA malformed or untrusted state branch whose \"filter.map\" contains empty\nkeys, slashes, backslashes, or \"..\" segments writes outside\n\"$tempdir/map\" instead of failing cleanly.\n\nRead lines with \"IFS= read -r line\", and require \"$from_commit\" to be\nnon-empty lowercase hex and \"$to_commit\" to contain only lowercase hex\ndigits and spaces (allowing empty \"$to_commit\" for pruned commits)\nbefore writing \"../map/$from_commit\".\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n git-filter-branch.sh     | 14 ++++++++++++--\n t/t7003-filter-branch.sh | 19 +++++++++++++++++++\n 2 files changed, 31 insertions(+), 2 deletions(-)\n\ndiff --git a/git-filter-branch.sh b/git-filter-branch.sh\nindex 9aa07be6e1..fbf7882a70 100755\n--- a/git-filter-branch.sh\n+++ b/git-filter-branch.sh\n@@ -298,13 +298,23 @@ then\n \n \t\tgit show \"$state_commit:filter.map\" >\"$tempdir\"/filter-map ||\n \t\t\tdie \"Unable to load state from $state_branch:filter.map\"\n-\t\twhile read line\n+\t\twhile IFS= read -r line\n \t\tdo\n \t\t\tcase \"$line\" in\n \t\t\t*:*)\n \t\t\t\tfrom_commit=${line%:*}\n \t\t\t\tto_commit=${line#*:}\n-\t\t\t\techo \"$to_commit\" >../map/\"$from_commit\";;\n+\t\t\t\tcase \"$from_commit\" in\n+\t\t\t\t\"\" | *[!0-9a-f]*)\n+\t\t\t\t\tdie \"Unable to load state from $state_branch:filter.map\"\n+\t\t\t\t\t;;\n+\t\t\t\tesac\n+\t\t\t\tcase \"$to_commit\" in\n+\t\t\t\t*[!0-9a-f\\ ]*)\n+\t\t\t\t\tdie \"Unable to load state from $state_branch:filter.map\"\n+\t\t\t\t\t;;\n+\t\t\t\tesac\n+\t\t\t\tprintf '%s\\n' \"$to_commit\" >../map/\"$from_commit\";;\n \t\t\t*)\n \t\t\t\tdie \"Unable to load state from $state_branch:filter.map\";;\n \t\t\tesac\ndiff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh\nindex 801cc83e5e..cab97e0e0b 100755\n--- a/t/t7003-filter-branch.sh\n+++ b/t/t7003-filter-branch.sh\n@@ -560,4 +560,23 @@ test_expect_success 'filter-branch handles ancestor rewrite' '\n \tgit diff-tree HEAD^ ancestor\n '\n \n+test_expect_success 'state-branch rejects non-hex or path-traversing filter.map entries' '\n+\ttest_when_finished \"rm -f escaped && git update-ref -d refs/heads/bad-state\" &&\n+\tbad_blob=$(printf \"../../escaped:injected\\n\" | git hash-object -w --stdin) &&\n+\tbad_tree=$(printf \"100644 blob %s\\tfilter.map\\n\" \"$bad_blob\" | git mktree) &&\n+\tbad_commit=$(echo \"bad state\" | git commit-tree \"$bad_tree\") &&\n+\tgit update-ref refs/heads/bad-state \"$bad_commit\" &&\n+\ttest_must_fail git filter-branch -f --state-branch refs/heads/bad-state HEAD 2>err &&\n+\ttest_grep \"Unable to load state from refs/heads/bad-state:filter.map\" err &&\n+\ttest_path_is_missing escaped &&\n+\n+\thead_oid=$(git rev-parse HEAD) &&\n+\tbs_blob=$(printf \"%s\\\\\\\\:%s\\n\" \"$head_oid\" \"$head_oid\" | git hash-object -w --stdin) &&\n+\tbs_tree=$(printf \"100644 blob %s\\tfilter.map\\n\" \"$bs_blob\" | git mktree) &&\n+\tbs_commit=$(echo \"backslash state\" | git commit-tree \"$bs_tree\") &&\n+\tgit update-ref refs/heads/bad-state \"$bs_commit\" &&\n+\ttest_must_fail git filter-branch -f --state-branch refs/heads/bad-state HEAD 2>err &&\n+\ttest_grep \"Unable to load state from refs/heads/bad-state:filter.map\" err\n+'\n+\n test_done\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}