Skip to content

Commit c8f74ea

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. Signed-off-by: Elijah Newren <newren@gmail.com>
1 parent 447b679 commit c8f74ea

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
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"
@@ -1411,6 +1412,8 @@ static int tree_content_set(
14111412
die("Empty path component found in input");
14121413
if (!*slash1 && !S_ISDIR(mode) && subtree)
14131414
die("Non-directories cannot have subtrees");
1415+
if (!verify_path(p, mode))
1416+
die("invalid path '%s'", p);
14141417

14151418
if (!root->tree)
14161419
load_tree(root);
@@ -1466,8 +1469,6 @@ static int tree_content_set(
14661469
root->tree = t = grow_tree_content(t, t->entry_count);
14671470
e = new_tree_entry();
14681471
e->name = to_atom(p, n);
1469-
if (is_dot_or_dotdot(e->name->str_dat))
1470-
die("path %s contains invalid component", p);
14711472
e->versions[0].mode = 0;
14721473
oidclr(&e->versions[0].oid, the_repository->hash_algo);
14731474
t->entries[t->entry_count++] = e;

t/t9300-fast-import.sh

Lines changed: 82 additions & 2 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

t/t9350-fast-export.sh

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

0 commit comments

Comments
 (0)