3 test_description
='check broken or malicious patterns in .git* files
7 - presence of .. in submodule names;
8 Exercise the name-checking function on a variety of names, and then give a
9 real-world setup that confirms we catch this in practice.
11 - nested submodule names
13 - symlinked .gitmodules, etc
16 .
"$TEST_DIRECTORY"/lib-pack.sh
18 test_expect_success
'setup' '
19 git config --global protocol.file.allow always
22 test_expect_success
'check names' '
23 cat >expect <<-\EOF &&
28 git submodule--helper check-name >actual <<-\EOF &&
43 test_cmp expect actual
46 test_expect_success
'create innocent subrepo' '
48 git -C innocent commit --allow-empty -m foo
51 test_expect_success
'submodule add refuses invalid names' '
53 git submodule add --name ../../modules/evil "$PWD/innocent" evil
56 test_expect_success
'add evil submodule' '
57 git submodule add "$PWD/innocent" evil &&
60 cp -r .git/modules/evil modules &&
61 write_script modules/evil/hooks/post-checkout <<-\EOF &&
62 echo >&2 "RUNNING POST CHECKOUT"
65 git config -f .gitmodules submodule.evil.update checkout &&
66 git config -f .gitmodules --rename-section \
67 submodule.evil submodule.../../modules/evil &&
72 # This step seems like it shouldn't be necessary, since the payload is
73 # contained entirely in the evil submodule. But due to the vagaries of the
74 # submodule code, checking out the evil module will fail unless ".git/modules"
75 # exists. Adding another submodule (with a name that sorts before "evil") is an
76 # easy way to make sure this is the case in the victim clone.
77 test_expect_success
'add other submodule' '
78 git submodule add "$PWD/innocent" another-module &&
79 git add another-module &&
80 git commit -am another
83 test_expect_success
'clone evil superproject' '
84 git clone --recurse-submodules . victim >output 2>&1 &&
85 ! grep "RUNNING POST CHECKOUT" output
88 test_expect_success
'fsck detects evil superproject' '
89 test_must_fail git fsck
92 test_expect_success
'transfer.fsckObjects detects evil superproject (unpack)' '
94 git init --bare dst.git &&
95 git -C dst.git config transfer.fsckObjects true &&
96 test_must_fail git push dst.git HEAD
99 test_expect_success
'transfer.fsckObjects detects evil superproject (index)' '
101 git init --bare dst.git &&
102 git -C dst.git config transfer.fsckObjects true &&
103 git -C dst.git config transfer.unpackLimit 1 &&
104 test_must_fail git push dst.git HEAD
107 # Normally our packs contain commits followed by trees followed by blobs. This
108 # reverses the order, which requires backtracking to find the context of a
109 # blob. We'll start with a fresh gitmodules-only tree to make it simpler.
110 test_expect_success
'create oddly ordered pack' '
111 git checkout --orphan odd &&
112 git rm -rf --cached . &&
113 git add .gitmodules &&
117 pack_obj $(git rev-parse HEAD:.gitmodules) &&
118 pack_obj $(git rev-parse HEAD^{tree}) &&
119 pack_obj $(git rev-parse HEAD)
121 pack_trailer odd.pack
124 test_expect_success
'transfer.fsckObjects handles odd pack (unpack)' '
126 git init --bare dst.git &&
127 test_must_fail git -C dst.git unpack-objects --strict <odd.pack
130 test_expect_success
'transfer.fsckObjects handles odd pack (index)' '
132 git init --bare dst.git &&
133 test_must_fail git -C dst.git index-pack --strict --stdin <odd.pack
136 test_expect_success
'index-pack --strict works for non-repo pack' '
138 git init --bare dst.git &&
139 cp odd.pack dst.git &&
140 test_must_fail git -C dst.git index-pack --strict odd.pack 2>output &&
141 # Make sure we fail due to bad gitmodules content, not because we
142 # could not read the blob in the first place.
143 grep gitmodulesName output
146 check_dotx_symlink
() {
147 fsck_must_fail
=test_must_fail
162 dir
=symlink-
$name-$type
164 test_expect_success
"set up repo with symlinked $name ($type)" '
169 # Make the tree directly to avoid index restrictions.
171 # Because symlinks store the target as a blob, choose
172 # a pathname that could be parsed as a .gitmodules file
173 # to trick naive non-symlink-aware checking.
174 tricky="[foo]bar=true" &&
175 content=$(git hash-object -w ../.gitmodules) &&
176 target=$(printf "$tricky" | git hash-object -w --stdin) &&
178 printf "100644 blob $content\t$tricky\n" &&
179 printf "120000 blob $target\t$path\n"
182 tree=$(git -C $dir mktree <$dir/bad-tree)
185 test_expect_success
"fsck detects symlinked $name ($type)" '
189 # Check not only that we fail, but that it is due to the
191 $fsck_must_fail git fsck 2>output &&
192 grep "$fsck_prefix.*tree $tree: ${name}Symlink" output
196 test -n "$refuse_index" &&
197 test_expect_success
"refuse to load symlinked $name into index ($type)" '
200 -c core.protectntfs \
202 read-tree $tree 2>err &&
203 grep "invalid path.*$name" err &&
204 git -C $dir ls-files -s >out &&
205 test_must_be_empty out
209 check_dotx_symlink gitmodules vanilla .gitmodules
210 check_dotx_symlink gitmodules ntfs
".gitmodules ."
211 check_dotx_symlink gitmodules hfs
".${u200c}gitmodules"
213 check_dotx_symlink
--warning gitattributes vanilla .gitattributes
214 check_dotx_symlink
--warning gitattributes ntfs
".gitattributes ."
215 check_dotx_symlink
--warning gitattributes hfs
".${u200c}gitattributes"
217 check_dotx_symlink
--warning gitignore vanilla .gitignore
218 check_dotx_symlink
--warning gitignore ntfs
".gitignore ."
219 check_dotx_symlink
--warning gitignore hfs
".${u200c}gitignore"
221 check_dotx_symlink
--warning mailmap vanilla .mailmap
222 check_dotx_symlink
--warning mailmap ntfs
".mailmap ."
223 check_dotx_symlink
--warning mailmap hfs
".${u200c}mailmap"
225 test_expect_success
'fsck detects non-blob .gitmodules' '
230 # As above, make the funny tree directly to avoid index
233 cp ../.gitmodules subdir/file &&
234 git add subdir/file &&
236 git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree &&
238 test_must_fail git fsck 2>output &&
239 test_i18ngrep gitmodulesBlob output
243 test_expect_success
'fsck detects corrupt .gitmodules' '
248 echo "[broken" >.gitmodules &&
249 git add .gitmodules &&
250 git commit -m "broken gitmodules" &&
253 test_i18ngrep gitmodulesParse output &&
254 test_i18ngrep ! "bad config" output
258 test_expect_success WINDOWS
'prevent git~1 squatting on Windows' '
259 git init squatting &&
266 git commit -m initial &&
268 modules="$(test_write_lines \
269 "[submodule \"b.\"]" "url = ." "path = c" \
270 "[submodule \"b\"]" "url = ." "path = d\\\\a" |
271 git hash-object -w --stdin)" &&
272 rev="$(git rev-parse --verify HEAD)" &&
273 hash="$(echo x | git hash-object -w --stdin)" &&
274 test_must_fail git update-index --add \
275 --cacheinfo 160000,$rev,d\\a 2>err &&
276 test_i18ngrep "Invalid path" err &&
277 git -c core.protectNTFS=false update-index --add \
278 --cacheinfo 100644,$modules,.gitmodules \
279 --cacheinfo 160000,$rev,c \
280 --cacheinfo 160000,$rev,d\\a \
281 --cacheinfo 100644,$hash,d./a/x \
282 --cacheinfo 100644,$hash,d./a/..git &&
284 git -c core.protectNTFS=false commit -m "module"
286 if test_have_prereq MINGW
288 test_must_fail git -c core.protectNTFS=false \
289 clone --recurse-submodules squatting squatting-clone 2>err &&
290 test_i18ngrep -e "directory not empty" -e "not an empty directory" err &&
291 ! grep gitdir squatting-clone/d/a/git~2
295 test_expect_success
'git dirs of sibling submodules must not be nested' '
297 test_commit -C nested nested &&
300 cat >.gitmodules <<-EOF &&
304 [submodule "hippo/hooks"]
308 git clone . thing1 &&
309 git clone . thing2 &&
310 git add .gitmodules thing1 thing2 &&
314 test_must_fail git clone --recurse-submodules nested clone 2>err &&
315 test_i18ngrep "is inside git dir" err