Skip to content

Commit

Permalink
fast-import: disallow more path components
Browse files Browse the repository at this point in the history
Instead of just disallowing '.' and '..', make use of verify_path() to
ensure that fast-import will disallow anything we wouldn't allow into
the index, such as anything under .git/, .gitmodules as a symlink, or
a dos drive prefix on Windows.

Signed-off-by: Elijah Newren <[email protected]>
  • Loading branch information
newren committed Nov 27, 2024
1 parent 447b679 commit 486f13b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
5 changes: 3 additions & 2 deletions builtin/fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "delta.h"
#include "pack.h"
#include "path.h"
#include "read-cache-ll.h"
#include "refs.h"
#include "csum-file.h"
#include "quote.h"
Expand Down Expand Up @@ -1411,6 +1412,8 @@ static int tree_content_set(
die("Empty path component found in input");
if (!*slash1 && !S_ISDIR(mode) && subtree)
die("Non-directories cannot have subtrees");
if (!verify_path(p, mode))
die("invalid path '%s'", p);

if (!root->tree)
load_tree(root);
Expand Down Expand Up @@ -1466,8 +1469,6 @@ static int tree_content_set(
root->tree = t = grow_tree_content(t, t->entry_count);
e = new_tree_entry();
e->name = to_atom(p, n);
if (is_dot_or_dotdot(e->name->str_dat))
die("path %s contains invalid component", p);
e->versions[0].mode = 0;
oidclr(&e->versions[0].oid, the_repository->hash_algo);
t->entries[t->entry_count++] = e;
Expand Down
82 changes: 81 additions & 1 deletion t/t9300-fast-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ test_expect_success 'B: fail on invalid committer (5)' '
test_must_fail git fast-import <input
'

test_expect_success 'B: fail on invalid file path' '
test_expect_success 'B: fail on invalid file path of ..' '
cat >input <<-INPUT_END &&
blob
mark :1
Expand All @@ -542,6 +542,86 @@ test_expect_success 'B: fail on invalid file path' '
test_must_fail git fast-import <input
'

test_expect_success 'B: fail on invalid file path of .' '
cat >input <<-INPUT_END &&
blob
mark :1
data <<EOF
File contents
EOF
commit refs/heads/badpath
committer Name <email> $GIT_COMMITTER_DATE
data <<COMMIT
Commit Message
COMMIT
M 100644 :1 ./invalid-path
INPUT_END
test_when_finished "git update-ref -d refs/heads/badpath" &&
test_must_fail git fast-import <input
'

test_expect_success WINDOWS 'B: fail on invalid file path of C:' '
cat >input <<-INPUT_END &&
blob
mark :1
data <<EOF
File contents
EOF
commit refs/heads/badpath
committer Name <email> $GIT_COMMITTER_DATE
data <<COMMIT
Commit Message
COMMIT
M 100644 :1 C:/invalid-path
INPUT_END
test_when_finished "git update-ref -d refs/heads/badpath" &&
test_must_fail git fast-import <input
'

test_expect_success 'B: fail on invalid file path of .git' '
cat >input <<-INPUT_END &&
blob
mark :1
data <<EOF
File contents
EOF
commit refs/heads/badpath
committer Name <email> $GIT_COMMITTER_DATE
data <<COMMIT
Commit Message
COMMIT
M 100644 :1 .git/invalid-path
INPUT_END
test_when_finished "git update-ref -d refs/heads/badpath" &&
test_must_fail git fast-import <input
'

test_expect_success 'B: fail on invalid file path of .gitmodules' '
cat >input <<-INPUT_END &&
blob
mark :1
data <<EOF
File contents
EOF
commit refs/heads/badpath
committer Name <email> $GIT_COMMITTER_DATE
data <<COMMIT
Commit Message
COMMIT
M 120000 :1 .gitmodules
INPUT_END
test_when_finished "git update-ref -d refs/heads/badpath" &&
test_must_fail git fast-import <input
'

###
### series C
###
Expand Down

0 comments on commit 486f13b

Please sign in to comment.