]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5550-http-fetch-dumb.sh
Merge branch 'jc/doc-submitting-patches-choice-of-base'
[thirdparty/git.git] / t / t5550-http-fetch-dumb.sh
1 #!/bin/sh
2
3 test_description='test dumb fetching over http via static file'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 if test_have_prereq !REFFILES
10 then
11 skip_all='skipping test; dumb HTTP protocol not supported with reftable.'
12 test_done
13 fi
14
15 . "$TEST_DIRECTORY"/lib-httpd.sh
16 start_httpd
17
18 test_expect_success 'setup repository' '
19 git config push.default matching &&
20 echo content1 >file &&
21 git add file &&
22 git commit -m one &&
23 echo content2 >file &&
24 git add file &&
25 git commit -m two
26 '
27
28 test_expect_success 'create http-accessible bare repository with loose objects' '
29 cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
30 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
31 git config core.bare true &&
32 mkdir -p hooks &&
33 write_script "hooks/post-update" <<-\EOF &&
34 exec git update-server-info
35 EOF
36 hooks/post-update
37 ) &&
38 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
39 git push public main:main
40 '
41
42 test_expect_success 'clone http repository' '
43 git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
44 cp -R clone-tmpl clone &&
45 test_cmp file clone/file
46 '
47
48 test_expect_success 'list refs from outside any repository' '
49 cat >expect <<-EOF &&
50 $(git rev-parse main) HEAD
51 $(git rev-parse main) refs/heads/main
52 EOF
53 nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
54 test_cmp expect actual
55 '
56
57 test_expect_success 'create password-protected repository' '
58 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
59 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
60 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
61 '
62
63 test_expect_success 'create empty remote repository' '
64 git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
65 (cd "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
66 mkdir -p hooks &&
67 write_script "hooks/post-update" <<-\EOF &&
68 exec git update-server-info
69 EOF
70 hooks/post-update
71 )
72 '
73
74 test_expect_success 'empty dumb HTTP repository has default hash algorithm' '
75 test_when_finished "rm -fr clone-empty" &&
76 git clone $HTTPD_URL/dumb/empty.git clone-empty &&
77 git -C clone-empty rev-parse --show-object-format >empty-format &&
78 test "$(cat empty-format)" = "$(test_oid algo)"
79 '
80
81 setup_askpass_helper
82
83 test_expect_success 'cloning password-protected repository can fail' '
84 set_askpass wrong &&
85 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
86 expect_askpass both wrong
87 '
88
89 test_expect_success 'http auth can use user/pass in URL' '
90 set_askpass wrong &&
91 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
92 expect_askpass none
93 '
94
95 test_expect_success 'http auth can use just user in URL' '
96 set_askpass wrong pass@host &&
97 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
98 expect_askpass pass user@host
99 '
100
101 test_expect_success 'http auth can request both user and pass' '
102 set_askpass user@host pass@host &&
103 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
104 expect_askpass both user@host
105 '
106
107 test_expect_success 'http auth respects credential helper config' '
108 test_config_global credential.helper "!f() {
109 cat >/dev/null
110 echo username=user@host
111 echo password=pass@host
112 }; f" &&
113 set_askpass wrong &&
114 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
115 expect_askpass none
116 '
117
118 test_expect_success 'http auth can get username from config' '
119 test_config_global "credential.$HTTPD_URL.username" user@host &&
120 set_askpass wrong pass@host &&
121 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
122 expect_askpass pass user@host
123 '
124
125 test_expect_success 'configured username does not override URL' '
126 test_config_global "credential.$HTTPD_URL.username" wrong &&
127 set_askpass wrong pass@host &&
128 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
129 expect_askpass pass user@host
130 '
131
132 test_expect_success 'set up repo with http submodules' '
133 git init super &&
134 set_askpass user@host pass@host &&
135 (
136 cd super &&
137 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
138 git commit -m "add submodule"
139 )
140 '
141
142 test_expect_success 'cmdline credential config passes to submodule via clone' '
143 set_askpass wrong pass@host &&
144 test_must_fail git clone --recursive super super-clone &&
145 rm -rf super-clone &&
146
147 set_askpass wrong pass@host &&
148 git -c "credential.$HTTPD_URL.username=user@host" \
149 clone --recursive super super-clone &&
150 expect_askpass pass user@host
151 '
152
153 test_expect_success 'cmdline credential config passes submodule via fetch' '
154 set_askpass wrong pass@host &&
155 test_must_fail git -C super-clone fetch --recurse-submodules &&
156
157 set_askpass wrong pass@host &&
158 git -C super-clone \
159 -c "credential.$HTTPD_URL.username=user@host" \
160 fetch --recurse-submodules &&
161 expect_askpass pass user@host
162 '
163
164 test_expect_success 'cmdline credential config passes submodule update' '
165 # advance the submodule HEAD so that a fetch is required
166 git commit --allow-empty -m foo &&
167 git push "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git" HEAD &&
168 sha1=$(git rev-parse HEAD) &&
169 git -C super-clone update-index --cacheinfo 160000,$sha1,sub &&
170
171 set_askpass wrong pass@host &&
172 test_must_fail git -C super-clone submodule update &&
173
174 set_askpass wrong pass@host &&
175 git -C super-clone \
176 -c "credential.$HTTPD_URL.username=user@host" \
177 submodule update &&
178 expect_askpass pass user@host
179 '
180
181 test_expect_success 'fetch changes via http' '
182 echo content >>file &&
183 git commit -a -m two &&
184 git push public &&
185 (cd clone && git pull) &&
186 test_cmp file clone/file
187 '
188
189 test_expect_success 'fetch changes via manual http-fetch' '
190 cp -R clone-tmpl clone2 &&
191
192 HEAD=$(git rev-parse --verify HEAD) &&
193 (cd clone2 &&
194 git http-fetch -a -w heads/main-new $HEAD $(git config remote.origin.url) &&
195 git checkout main-new &&
196 test $HEAD = $(git rev-parse --verify HEAD)) &&
197 test_cmp file clone2/file
198 '
199
200 test_expect_success 'manual http-fetch without -a works just as well' '
201 cp -R clone-tmpl clone3 &&
202
203 HEAD=$(git rev-parse --verify HEAD) &&
204 (cd clone3 &&
205 git http-fetch -w heads/main-new $HEAD $(git config remote.origin.url) &&
206 git checkout main-new &&
207 test $HEAD = $(git rev-parse --verify HEAD)) &&
208 test_cmp file clone3/file
209 '
210
211 test_expect_success 'http remote detects correct HEAD' '
212 git push public main:other &&
213 (cd clone &&
214 git remote set-head origin -d &&
215 git remote set-head origin -a &&
216 git symbolic-ref refs/remotes/origin/HEAD > output &&
217 echo refs/remotes/origin/main > expect &&
218 test_cmp expect output
219 )
220 '
221
222 test_expect_success 'fetch packed objects' '
223 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
224 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
225 git --bare repack -a -d
226 ) &&
227 git clone $HTTPD_URL/dumb/repo_pack.git
228 '
229
230 test_expect_success 'http-fetch --packfile' '
231 # Arbitrary hash. Use rev-parse so that we get one of the correct
232 # length.
233 ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
234
235 git init packfileclient &&
236 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git && ls objects/pack/pack-*.pack) &&
237 git -C packfileclient http-fetch --packfile=$ARBITRARY \
238 --index-pack-arg=index-pack --index-pack-arg=--stdin \
239 --index-pack-arg=--keep \
240 "$HTTPD_URL"/dumb/repo_pack.git/$p >out &&
241
242 grep "^keep.[0-9a-f]\{16,\}$" out &&
243 cut -c6- out >packhash &&
244
245 # Ensure that the expected files are generated
246 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).pack" &&
247 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).idx" &&
248 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).keep" &&
249
250 # Ensure that it has the HEAD of repo_pack, at least
251 HASH=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
252 git -C packfileclient cat-file -e "$HASH"
253 '
254
255 test_expect_success 'fetch notices corrupt pack' '
256 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
257 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
258 p=$(ls objects/pack/pack-*.pack) &&
259 chmod u+w $p &&
260 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
261 ) &&
262 mkdir repo_bad1.git &&
263 (cd repo_bad1.git &&
264 git --bare init &&
265 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
266 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
267 )
268 '
269
270 test_expect_success 'http-fetch --packfile with corrupt pack' '
271 rm -rf packfileclient &&
272 git init packfileclient &&
273 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git && ls objects/pack/pack-*.pack) &&
274 test_must_fail git -C packfileclient http-fetch --packfile \
275 "$HTTPD_URL"/dumb/repo_bad1.git/$p
276 '
277
278 test_expect_success 'fetch notices corrupt idx' '
279 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
280 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
281 p=$(ls objects/pack/pack-*.idx) &&
282 chmod u+w $p &&
283 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
284 ) &&
285 mkdir repo_bad2.git &&
286 (cd repo_bad2.git &&
287 git --bare init &&
288 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
289 test 0 = $(ls objects/pack | wc -l)
290 )
291 '
292
293 test_expect_success 'fetch can handle previously-fetched .idx files' '
294 git checkout --orphan branch1 &&
295 echo base >file &&
296 git add file &&
297 git commit -m base &&
298 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
299 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
300 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
301 git checkout -b branch2 branch1 &&
302 echo b2 >>file &&
303 git commit -a -m b2 &&
304 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
305 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
306 git --bare init clone_packed_branches.git &&
307 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
308 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
309 '
310
311 test_expect_success 'did not use upload-pack service' '
312 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
313 '
314
315 test_expect_success 'git client shows text/plain errors' '
316 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
317 grep "this is the error message" stderr
318 '
319
320 test_expect_success 'git client does not show html errors' '
321 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
322 ! grep "this is the error message" stderr
323 '
324
325 test_expect_success 'git client shows text/plain with a charset' '
326 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
327 grep "this is the error message" stderr
328 '
329
330 test_expect_success 'http error messages are reencoded' '
331 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
332 grep "this is the error message" stderr
333 '
334
335 test_expect_success 'reencoding is robust to whitespace oddities' '
336 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
337 grep "this is the error message" stderr
338 '
339
340 check_language () {
341 case "$2" in
342 '')
343 >expect
344 ;;
345 ?*)
346 echo "=> Send header: Accept-Language: $1" >expect
347 ;;
348 esac &&
349 GIT_TRACE_CURL=true \
350 LANGUAGE=$2 \
351 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
352 tr -d '\015' <output |
353 sort -u |
354 sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
355 test_cmp expect actual
356 }
357
358 test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
359 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
360
361 test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
362 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
363 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
364 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
365 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
366 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
367
368 test_expect_success 'git client sends Accept-Language with many preferred languages' '
369 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
370 ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
371 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
372 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
373 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
374 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
375 '
376
377 test_expect_success 'git client does not send an empty Accept-Language' '
378 GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
379 ! grep "^=> Send header: Accept-Language:" stderr
380 '
381
382 test_expect_success 'remote-http complains cleanly about malformed urls' '
383 test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
384 test_i18ngrep "url has no scheme" stderr
385 '
386
387 # NEEDSWORK: Writing commands to git-remote-curl can race against the latter
388 # erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
389 # learned to handle early remote helper failures more cleanly.
390 test_expect_success 'remote-http complains cleanly about empty scheme' '
391 test_must_fail ok=sigpipe git ls-remote \
392 http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
393 test_i18ngrep "url has no scheme" stderr
394 '
395
396 test_expect_success 'redirects can be forbidden/allowed' '
397 test_must_fail git -c http.followRedirects=false \
398 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
399 git -c http.followRedirects=true \
400 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
401 '
402
403 test_expect_success 'redirects are reported to stderr' '
404 # just look for a snippet of the redirected-to URL
405 test_i18ngrep /dumb/ stderr
406 '
407
408 test_expect_success 'non-initial redirects can be forbidden' '
409 test_must_fail git -c http.followRedirects=initial \
410 clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
411 git -c http.followRedirects=true \
412 clone $HTTPD_URL/redir-objects/repo.git redir-objects
413 '
414
415 test_expect_success 'http.followRedirects defaults to "initial"' '
416 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
417 '
418
419 # The goal is for a clone of the "evil" repository, which has no objects
420 # itself, to cause the client to fetch objects from the "victim" repository.
421 test_expect_success 'set up evil alternates scheme' '
422 victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
423 git init --bare "$victim" &&
424 git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
425 git -C "$victim" repack -ad &&
426 git -C "$victim" update-server-info &&
427 sha1=$(git -C "$victim" rev-parse HEAD) &&
428
429 evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
430 git init --bare "$evil" &&
431 # do this by hand to avoid object existence check
432 printf "%s\\t%s\\n" $sha1 refs/heads/main >"$evil/info/refs"
433 '
434
435 # Here we'll just redirect via HTTP. In a real-world attack these would be on
436 # different servers, but we should reject it either way.
437 test_expect_success 'http-alternates is a non-initial redirect' '
438 echo "$HTTPD_URL/dumb/victim.git/objects" \
439 >"$evil/objects/info/http-alternates" &&
440 test_must_fail git -c http.followRedirects=initial \
441 clone $HTTPD_URL/dumb/evil.git evil-initial &&
442 git -c http.followRedirects=true \
443 clone $HTTPD_URL/dumb/evil.git evil-initial
444 '
445
446 # Curl supports a lot of protocols that we'd prefer not to allow
447 # http-alternates to use, but it's hard to test whether curl has
448 # accessed, say, the SMTP protocol, because we are not running an SMTP server.
449 # But we can check that it does not allow access to file://, which would
450 # otherwise allow this clone to complete.
451 test_expect_success 'http-alternates cannot point at funny protocols' '
452 echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
453 test_must_fail git -c http.followRedirects=true \
454 clone "$HTTPD_URL/dumb/evil.git" evil-file
455 '
456
457 test_expect_success 'http-alternates triggers not-from-user protocol check' '
458 echo "$HTTPD_URL/dumb/victim.git/objects" \
459 >"$evil/objects/info/http-alternates" &&
460 test_config_global http.followRedirects true &&
461 test_must_fail git -c protocol.http.allow=user \
462 clone $HTTPD_URL/dumb/evil.git evil-user &&
463 git -c protocol.http.allow=always \
464 clone $HTTPD_URL/dumb/evil.git evil-user
465 '
466
467 test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
468 git clone "$HTTPD_URL/redir-to/dumb/repo.git"
469 '
470
471 test_expect_success 'print HTTP error when any intermediate redirect throws error' '
472 test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
473 test_i18ngrep "unable to access.*/redir-to/502" stderr
474 '
475
476 test_expect_success 'fetching via http alternates works' '
477 parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
478 git init --bare "$parent" &&
479 git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
480 git -C "$parent" update-server-info &&
481 commit=$(git -C "$parent" rev-parse HEAD) &&
482
483 child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
484 git init --bare "$child" &&
485 echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
486 git -C "$child" update-ref HEAD $commit &&
487 git -C "$child" update-server-info &&
488
489 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"
490 '
491
492 test_done