From 7cfef09e9bac3146a14c5f53a596bb2231b1104e Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 26 Dec 2019 15:50:31 -0800 Subject: [PATCH] filter-repo: warn users who try to use invalid path components It's hard to be exhaustive, but if users try something like: --path-rename foo/bar/baz:. or --path ../other-dir then bad things happen. In the first case, filter-repo will try to ask fast-import to create a directory named '.' and move everything from foo/bar/baz/ into it but of course '.' is a reserved directory name so we can't create it. In the second case, they are probably running from a subdirectory, but filter-repo doesn't work from a subdirectory. I hard-coded the assumption that everything was in the toplevel directory and all paths were relative from there pretty early on. So, if the user tries to use any of these components anywhere, just throw an early error. Signed-off-by: Elijah Newren --- git-filter-repo | 7 +++++++ t/t9390-filter-repo.sh | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/git-filter-repo b/git-filter-repo index 6e72fc3..7c46cf2 100755 --- a/git-filter-repo +++ b/git-filter-repo @@ -1602,6 +1602,7 @@ class GitUtils(object): class FilteringOptions(object): class AppendFilter(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): + user_path = values suffix = option_string[len('--path-'):] or 'match' if suffix.startswith('rename'): mod_type = 'rename' @@ -1612,9 +1613,15 @@ class FilteringOptions(object): raise SystemExit(_("Error: With --path-rename, if OLD_NAME and " "NEW_NAME are both non-empty and either ends " "with a slash then both must.")) + components = values[0].split(b'/') + values[1].split(b'/') else: mod_type = 'filter' match_type = suffix + components = values.split(b'/') + for illegal_path in [b'.', b'..', b'.git']: + if illegal_path in components: + raise SystemExit(_("Error: Invalid path component '%s' found in '%s'") + % (decode(illegal_path), decode(user_path))) if match_type == 'regex': values = re.compile(values) items = getattr(namespace, self.dest, []) or [] diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh index 0c0383e..2173939 100755 --- a/t/t9390-filter-repo.sh +++ b/t/t9390-filter-repo.sh @@ -1028,7 +1028,10 @@ test_expect_success 'other startup error cases and requests for help' ' test_i18ngrep "renaming globs makes no sense" err && test_must_fail git filter-repo --strip-blobs-bigger-than 3GiB 2>err && - test_i18ngrep "could not parse.*3GiB" err + test_i18ngrep "could not parse.*3GiB" err && + + test_must_fail git filter-repo --path-rename foo/bar:. 2>err && + test_i18ngrep "Invalid path component .\.. found in .foo/bar:\." err ) '