Sync with 2.42.2
[git.git] / t / t7450-bad-git-dotfiles.sh
blob5b845e899bf17caec8e26cff4fb329e8701fb77b
1 #!/bin/sh
3 test_description='check broken or malicious patterns in .git* files
5 Such as:
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_PASSES_SANITIZE_LEAK=true
17 . ./test-lib.sh
18 . "$TEST_DIRECTORY"/lib-pack.sh
20 test_expect_success 'setup' '
21 git config --global protocol.file.allow always
24 test_expect_success 'check names' '
25 cat >expect <<-\EOF &&
26 valid
27 valid/with/paths
28 EOF
30 test-tool submodule check-name >actual <<-\EOF &&
31 valid
32 valid/with/paths
34 ../foo
35 /../foo
36 ..\foo
37 \..\foo
38 foo/..
39 foo/../
40 foo\..
41 foo\..\
42 foo/../bar
43 EOF
45 test_cmp expect actual
48 test_expect_success 'create innocent subrepo' '
49 git init innocent &&
50 git -C innocent commit --allow-empty -m foo
53 test_expect_success 'submodule add refuses invalid names' '
54 test_must_fail \
55 git submodule add --name ../../modules/evil "$PWD/innocent" evil
58 test_expect_success 'add evil submodule' '
59 git submodule add "$PWD/innocent" evil &&
61 mkdir modules &&
62 cp -r .git/modules/evil modules &&
63 write_script modules/evil/hooks/post-checkout <<-\EOF &&
64 echo >&2 "RUNNING POST CHECKOUT"
65 EOF
67 git config -f .gitmodules submodule.evil.update checkout &&
68 git config -f .gitmodules --rename-section \
69 submodule.evil submodule.../../modules/evil &&
70 git add modules &&
71 git commit -am evil
74 # This step seems like it shouldn't be necessary, since the payload is
75 # contained entirely in the evil submodule. But due to the vagaries of the
76 # submodule code, checking out the evil module will fail unless ".git/modules"
77 # exists. Adding another submodule (with a name that sorts before "evil") is an
78 # easy way to make sure this is the case in the victim clone.
79 test_expect_success 'add other submodule' '
80 git submodule add "$PWD/innocent" another-module &&
81 git add another-module &&
82 git commit -am another
85 test_expect_success 'clone evil superproject' '
86 git clone --recurse-submodules . victim >output 2>&1 &&
87 ! grep "RUNNING POST CHECKOUT" output
90 test_expect_success 'fsck detects evil superproject' '
91 test_must_fail git fsck
94 test_expect_success 'transfer.fsckObjects detects evil superproject (unpack)' '
95 rm -rf dst.git &&
96 git init --bare dst.git &&
97 git -C dst.git config transfer.fsckObjects true &&
98 test_must_fail git push dst.git HEAD
101 test_expect_success 'transfer.fsckObjects detects evil superproject (index)' '
102 rm -rf dst.git &&
103 git init --bare dst.git &&
104 git -C dst.git config transfer.fsckObjects true &&
105 git -C dst.git config transfer.unpackLimit 1 &&
106 test_must_fail git push dst.git HEAD
109 # Normally our packs contain commits followed by trees followed by blobs. This
110 # reverses the order, which requires backtracking to find the context of a
111 # blob. We'll start with a fresh gitmodules-only tree to make it simpler.
112 test_expect_success 'create oddly ordered pack' '
113 git checkout --orphan odd &&
114 git rm -rf --cached . &&
115 git add .gitmodules &&
116 git commit -m odd &&
118 pack_header 3 &&
119 pack_obj $(git rev-parse HEAD:.gitmodules) &&
120 pack_obj $(git rev-parse HEAD^{tree}) &&
121 pack_obj $(git rev-parse HEAD)
122 } >odd.pack &&
123 pack_trailer odd.pack
126 test_expect_success 'transfer.fsckObjects handles odd pack (unpack)' '
127 rm -rf dst.git &&
128 git init --bare dst.git &&
129 test_must_fail git -C dst.git unpack-objects --strict <odd.pack
132 test_expect_success 'transfer.fsckObjects handles odd pack (index)' '
133 rm -rf dst.git &&
134 git init --bare dst.git &&
135 test_must_fail git -C dst.git index-pack --strict --stdin <odd.pack
138 test_expect_success 'index-pack --strict works for non-repo pack' '
139 rm -rf dst.git &&
140 git init --bare dst.git &&
141 cp odd.pack dst.git &&
142 test_must_fail git -C dst.git index-pack --strict odd.pack 2>output &&
143 # Make sure we fail due to bad gitmodules content, not because we
144 # could not read the blob in the first place.
145 grep gitmodulesName output
148 check_dotx_symlink () {
149 fsck_must_fail=test_must_fail
150 fsck_prefix=error
151 refuse_index=t
152 case "$1" in
153 --warning)
154 fsck_must_fail=
155 fsck_prefix=warning
156 refuse_index=
157 shift
159 esac
161 name=$1
162 type=$2
163 path=$3
164 dir=symlink-$name-$type
166 test_expect_success "set up repo with symlinked $name ($type)" '
167 git init $dir &&
169 cd $dir &&
171 # Make the tree directly to avoid index restrictions.
173 # Because symlinks store the target as a blob, choose
174 # a pathname that could be parsed as a .gitmodules file
175 # to trick naive non-symlink-aware checking.
176 tricky="[foo]bar=true" &&
177 content=$(git hash-object -w ../.gitmodules) &&
178 target=$(printf "$tricky" | git hash-object -w --stdin) &&
180 printf "100644 blob $content\t$tricky\n" &&
181 printf "120000 blob $target\t$path\n"
182 } >bad-tree
183 ) &&
184 tree=$(git -C $dir mktree <$dir/bad-tree)
187 test_expect_success "fsck detects symlinked $name ($type)" '
189 cd $dir &&
191 # Check not only that we fail, but that it is due to the
192 # symlink detector
193 $fsck_must_fail git fsck 2>output &&
194 grep "$fsck_prefix.*tree $tree: ${name}Symlink" output
198 test -n "$refuse_index" &&
199 test_expect_success "refuse to load symlinked $name into index ($type)" '
200 test_must_fail \
201 git -C $dir \
202 -c core.protectntfs \
203 -c core.protecthfs \
204 read-tree $tree 2>err &&
205 grep "invalid path.*$name" err &&
206 git -C $dir ls-files -s >out &&
207 test_must_be_empty out
211 check_dotx_symlink gitmodules vanilla .gitmodules
212 check_dotx_symlink gitmodules ntfs ".gitmodules ."
213 check_dotx_symlink gitmodules hfs ".${u200c}gitmodules"
215 check_dotx_symlink --warning gitattributes vanilla .gitattributes
216 check_dotx_symlink --warning gitattributes ntfs ".gitattributes ."
217 check_dotx_symlink --warning gitattributes hfs ".${u200c}gitattributes"
219 check_dotx_symlink --warning gitignore vanilla .gitignore
220 check_dotx_symlink --warning gitignore ntfs ".gitignore ."
221 check_dotx_symlink --warning gitignore hfs ".${u200c}gitignore"
223 check_dotx_symlink --warning mailmap vanilla .mailmap
224 check_dotx_symlink --warning mailmap ntfs ".mailmap ."
225 check_dotx_symlink --warning mailmap hfs ".${u200c}mailmap"
227 test_expect_success 'fsck detects non-blob .gitmodules' '
228 git init non-blob &&
230 cd non-blob &&
232 # As above, make the funny tree directly to avoid index
233 # restrictions.
234 mkdir subdir &&
235 cp ../.gitmodules subdir/file &&
236 git add subdir/file &&
237 git commit -m ok &&
238 git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree &&
240 test_must_fail git fsck 2>output &&
241 test_grep gitmodulesBlob output
245 test_expect_success 'fsck detects corrupt .gitmodules' '
246 git init corrupt &&
248 cd corrupt &&
250 echo "[broken" >.gitmodules &&
251 git add .gitmodules &&
252 git commit -m "broken gitmodules" &&
254 git fsck 2>output &&
255 test_grep gitmodulesParse output &&
256 test_grep ! "bad config" output
260 test_expect_success WINDOWS 'prevent git~1 squatting on Windows' '
261 git init squatting &&
263 cd squatting &&
264 mkdir a &&
265 touch a/..git &&
266 git add a/..git &&
267 test_tick &&
268 git commit -m initial &&
270 modules="$(test_write_lines \
271 "[submodule \"b.\"]" "url = ." "path = c" \
272 "[submodule \"b\"]" "url = ." "path = d\\\\a" |
273 git hash-object -w --stdin)" &&
274 rev="$(git rev-parse --verify HEAD)" &&
275 hash="$(echo x | git hash-object -w --stdin)" &&
276 test_must_fail git update-index --add \
277 --cacheinfo 160000,$rev,d\\a 2>err &&
278 test_grep "Invalid path" err &&
279 git -c core.protectNTFS=false update-index --add \
280 --cacheinfo 100644,$modules,.gitmodules \
281 --cacheinfo 160000,$rev,c \
282 --cacheinfo 160000,$rev,d\\a \
283 --cacheinfo 100644,$hash,d./a/x \
284 --cacheinfo 100644,$hash,d./a/..git &&
285 test_tick &&
286 git -c core.protectNTFS=false commit -m "module"
287 ) &&
288 if test_have_prereq MINGW
289 then
290 test_must_fail git -c core.protectNTFS=false \
291 clone --recurse-submodules squatting squatting-clone 2>err &&
292 test_grep -e "directory not empty" -e "not an empty directory" err &&
293 ! grep gitdir squatting-clone/d/a/git~2
297 test_expect_success 'setup submodules with nested git dirs' '
298 git init nested &&
299 test_commit -C nested nested &&
301 cd nested &&
302 cat >.gitmodules <<-EOF &&
303 [submodule "hippo"]
304 url = .
305 path = thing1
306 [submodule "hippo/hooks"]
307 url = .
308 path = thing2
310 git clone . thing1 &&
311 git clone . thing2 &&
312 git add .gitmodules thing1 thing2 &&
313 test_tick &&
314 git commit -m nested
318 test_expect_success 'git dirs of sibling submodules must not be nested' '
319 test_must_fail git clone --recurse-submodules nested clone 2>err &&
320 test_grep "is inside git dir" err
323 test_expect_success 'submodule git dir nesting detection must work with parallel cloning' '
324 test_must_fail git clone --recurse-submodules --jobs=2 nested clone_parallel 2>err &&
325 cat err &&
326 grep -E "(already exists|is inside git dir|not a git repository)" err &&
328 test_path_is_missing .git/modules/hippo/HEAD ||
329 test_path_is_missing .git/modules/hippo/hooks/HEAD
333 test_expect_success 'checkout -f --recurse-submodules must not use a nested gitdir' '
334 git clone nested nested_checkout &&
336 cd nested_checkout &&
337 git submodule init &&
338 git submodule update thing1 &&
339 mkdir -p .git/modules/hippo/hooks/refs &&
340 mkdir -p .git/modules/hippo/hooks/objects/info &&
341 echo "../../../../objects" >.git/modules/hippo/hooks/objects/info/alternates &&
342 echo "ref: refs/heads/master" >.git/modules/hippo/hooks/HEAD
343 ) &&
344 test_must_fail git -C nested_checkout checkout -f --recurse-submodules HEAD 2>err &&
345 cat err &&
346 grep "is inside git dir" err &&
347 test_path_is_missing nested_checkout/thing2/.git
350 test_done