Skip to content

Commit 2c23d60

Browse files
committed
fast-import: disallow more path components
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. Since a few fast-export and fast-import tests that tried to stress-test the correct handling of quoting relied on filenames that fail is_valid_win32_path(), such as spaces or periods at the end of filenames or backslashes within the filename, turn off core.protectNTFS for those tests to ensure they keep passing. Signed-off-by: Elijah Newren <newren@gmail.com>
1 parent 4a2790a commit 2c23d60

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

builtin/fast-import.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "delta.h"
1414
#include "pack.h"
1515
#include "path.h"
16+
#include "read-cache-ll.h"
1617
#include "refs.h"
1718
#include "csum-file.h"
1819
#include "quote.h"
@@ -1413,6 +1414,8 @@ static int tree_content_set(
14131414
die("Empty path component found in input");
14141415
if (!*slash1 && !S_ISDIR(mode) && subtree)
14151416
die("Non-directories cannot have subtrees");
1417+
if (!verify_path(p, mode))
1418+
die("invalid path '%s'", p);
14161419

14171420
if (!root->tree)
14181421
load_tree(root);
@@ -1468,8 +1471,6 @@ static int tree_content_set(
14681471
root->tree = t = grow_tree_content(t, t->entry_count);
14691472
e = new_tree_entry();
14701473
e->name = to_atom(p, n);
1471-
if (is_dot_or_dotdot(e->name->str_dat))
1472-
die("path %s contains invalid component", p);
14731474
e->versions[0].mode = 0;
14741475
oidclr(&e->versions[0].oid, the_repository->hash_algo);
14751476
t->entries[t->entry_count++] = e;

t/t9300-fast-import.sh

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ test_expect_success 'B: fail on invalid committer (5)' '
522522
test_must_fail git fast-import <input
523523
'
524524

525-
test_expect_success 'B: fail on invalid file path' '
525+
test_expect_success 'B: fail on invalid file path of ..' '
526526
cat >input <<-INPUT_END &&
527527
blob
528528
mark :1
@@ -542,6 +542,86 @@ test_expect_success 'B: fail on invalid file path' '
542542
test_must_fail git fast-import <input
543543
'
544544

545+
test_expect_success 'B: fail on invalid file path of .' '
546+
cat >input <<-INPUT_END &&
547+
blob
548+
mark :1
549+
data <<EOF
550+
File contents
551+
EOF
552+
553+
commit refs/heads/badpath
554+
committer Name <email> $GIT_COMMITTER_DATE
555+
data <<COMMIT
556+
Commit Message
557+
COMMIT
558+
M 100644 :1 ./invalid-path
559+
INPUT_END
560+
561+
test_when_finished "git update-ref -d refs/heads/badpath" &&
562+
test_must_fail git fast-import <input
563+
'
564+
565+
test_expect_success WINDOWS 'B: fail on invalid file path of C:' '
566+
cat >input <<-INPUT_END &&
567+
blob
568+
mark :1
569+
data <<EOF
570+
File contents
571+
EOF
572+
573+
commit refs/heads/badpath
574+
committer Name <email> $GIT_COMMITTER_DATE
575+
data <<COMMIT
576+
Commit Message
577+
COMMIT
578+
M 100644 :1 C:/invalid-path
579+
INPUT_END
580+
581+
test_when_finished "git update-ref -d refs/heads/badpath" &&
582+
test_must_fail git fast-import <input
583+
'
584+
585+
test_expect_success 'B: fail on invalid file path of .git' '
586+
cat >input <<-INPUT_END &&
587+
blob
588+
mark :1
589+
data <<EOF
590+
File contents
591+
EOF
592+
593+
commit refs/heads/badpath
594+
committer Name <email> $GIT_COMMITTER_DATE
595+
data <<COMMIT
596+
Commit Message
597+
COMMIT
598+
M 100644 :1 .git/invalid-path
599+
INPUT_END
600+
601+
test_when_finished "git update-ref -d refs/heads/badpath" &&
602+
test_must_fail git fast-import <input
603+
'
604+
605+
test_expect_success 'B: fail on invalid file path of .gitmodules' '
606+
cat >input <<-INPUT_END &&
607+
blob
608+
mark :1
609+
data <<EOF
610+
File contents
611+
EOF
612+
613+
commit refs/heads/badpath
614+
committer Name <email> $GIT_COMMITTER_DATE
615+
data <<COMMIT
616+
Commit Message
617+
COMMIT
618+
M 120000 :1 .gitmodules
619+
INPUT_END
620+
621+
test_when_finished "git update-ref -d refs/heads/badpath" &&
622+
test_must_fail git fast-import <input
623+
'
624+
545625
###
546626
### series C
547627
###
@@ -966,7 +1046,7 @@ test_expect_success 'L: verify internal tree sorting' '
9661046
:100644 100644 M ba
9671047
EXPECT_END
9681048
969-
git fast-import <input &&
1049+
git -c core.protectNTFS=false fast-import <input &&
9701050
GIT_PRINT_SHA1_ELLIPSIS="yes" git diff-tree --abbrev --raw L^ L >output &&
9711051
cut -d" " -f1,2,5 output >actual &&
9721052
test_cmp expect actual
@@ -3117,7 +3197,7 @@ test_path_eol_success () {
31173197
test_expect_success "S: paths at EOL with $test must work" '
31183198
test_when_finished "git branch -D S-path-eol" &&
31193199
3120-
git fast-import --export-marks=marks.out <<-EOF >out 2>err &&
3200+
git -c core.protectNTFS=false fast-import --export-marks=marks.out <<-EOF >out 2>err &&
31213201
blob
31223202
mark :401
31233203
data <<BLOB
@@ -3226,7 +3306,7 @@ test_path_space_success () {
32263306
test_expect_success "S: paths before space with $test must work" '
32273307
test_when_finished "git branch -D S-path-space" &&
32283308
3229-
git fast-import --export-marks=marks.out <<-EOF 2>err &&
3309+
git -c core.protectNTFS=false fast-import --export-marks=marks.out <<-EOF 2>err &&
32303310
blob
32313311
mark :401
32323312
data <<BLOB

t/t9350-fast-export.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ test_expect_success 'fast-export quotes pathnames' '
631631
git rev-list HEAD >expect &&
632632
git init result &&
633633
cd result &&
634-
git fast-import <../export.out &&
634+
git -c core.protectNTFS=false fast-import <../export.out &&
635635
git rev-list HEAD >actual &&
636636
test_cmp ../expect actual
637637
)

0 commit comments

Comments
 (0)