The eleventh batch
[git/gitster.git] / t / t5550-http-fetch-dumb.sh
blob5115b8dc230e9741882a609be5f41d4596a5512e
1 #!/bin/sh
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
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 if test_have_prereq !REFFILES
11 then
12 skip_all='skipping test; dumb HTTP protocol not supported with reftable.'
13 test_done
16 . "$TEST_DIRECTORY"/lib-httpd.sh
17 start_httpd
19 test_expect_success 'setup repository' '
20 git config push.default matching &&
21 echo content1 >file &&
22 git add file &&
23 git commit -m one &&
24 echo content2 >file &&
25 git add file &&
26 git commit -m two
29 test_expect_success 'packfile without repository does not crash' '
30 echo "fatal: not a git repository" >expect &&
31 test_must_fail nongit git http-fetch --packfile=abc 2>err &&
32 test_cmp expect err
35 setup_post_update_server_info_hook () {
36 test_hook --setup -C "$1" post-update <<-\EOF &&
37 exec git update-server-info
38 EOF
39 git -C "$1" update-server-info
42 test_expect_success 'create http-accessible bare repository with loose objects' '
43 cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
44 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config core.bare true &&
45 setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
46 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
47 git push public main:main
50 test_expect_success 'clone http repository' '
51 git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
52 cp -R clone-tmpl clone &&
53 test_cmp file clone/file
56 test_expect_success 'list refs from outside any repository' '
57 cat >expect <<-EOF &&
58 $(git rev-parse main) HEAD
59 $(git rev-parse main) refs/heads/main
60 EOF
61 nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
62 test_cmp expect actual
66 test_expect_success 'list detached HEAD from outside any repository' '
67 git clone --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
68 "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" &&
69 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" \
70 update-ref --no-deref HEAD refs/heads/main &&
71 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" update-server-info &&
72 cat >expect <<-EOF &&
73 $(git rev-parse main) HEAD
74 $(git rev-parse main) refs/heads/main
75 EOF
76 nongit git ls-remote "$HTTPD_URL/dumb/repo-detached.git" >actual &&
77 test_cmp expect actual
80 test_expect_success 'create password-protected repository' '
81 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
82 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
83 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
86 test_expect_success 'create empty remote repository' '
87 git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
88 setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
91 test_expect_success 'empty dumb HTTP repository falls back to SHA1' '
92 test_when_finished "rm -fr clone-empty" &&
93 git clone $HTTPD_URL/dumb/empty.git clone-empty &&
94 git -C clone-empty rev-parse --show-object-format >empty-format &&
95 test "$(cat empty-format)" = sha1
98 setup_askpass_helper
100 test_expect_success 'cloning password-protected repository can fail' '
101 set_askpass wrong &&
102 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
103 expect_askpass both wrong
106 test_expect_success 'http auth can use user/pass in URL' '
107 set_askpass wrong &&
108 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
109 expect_askpass none
112 test_expect_success 'http auth can use just user in URL' '
113 set_askpass wrong pass@host &&
114 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
115 expect_askpass pass user@host
118 test_expect_success 'http auth can request both user and pass' '
119 set_askpass user@host pass@host &&
120 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
121 expect_askpass both user@host
124 test_expect_success 'http auth respects credential helper config' '
125 test_config_global credential.helper "!f() {
126 cat >/dev/null
127 echo username=user@host
128 echo password=pass@host
129 }; f" &&
130 set_askpass wrong &&
131 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
132 expect_askpass none
135 test_expect_success 'http auth can get username from config' '
136 test_config_global "credential.$HTTPD_URL.username" user@host &&
137 set_askpass wrong pass@host &&
138 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
139 expect_askpass pass user@host
142 test_expect_success 'configured username does not override URL' '
143 test_config_global "credential.$HTTPD_URL.username" wrong &&
144 set_askpass wrong pass@host &&
145 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
146 expect_askpass pass user@host
149 test_expect_success 'set up repo with http submodules' '
150 git init super &&
151 set_askpass user@host pass@host &&
153 cd super &&
154 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
155 git commit -m "add submodule"
159 test_expect_success 'cmdline credential config passes to submodule via clone' '
160 set_askpass wrong pass@host &&
161 test_must_fail git clone --recursive super super-clone &&
162 rm -rf super-clone &&
164 set_askpass wrong pass@host &&
165 git -c "credential.$HTTPD_URL.username=user@host" \
166 clone --recursive super super-clone &&
167 expect_askpass pass user@host
170 test_expect_success 'cmdline credential config passes submodule via fetch' '
171 set_askpass wrong pass@host &&
172 test_must_fail git -C super-clone fetch --recurse-submodules &&
174 set_askpass wrong pass@host &&
175 git -C super-clone \
176 -c "credential.$HTTPD_URL.username=user@host" \
177 fetch --recurse-submodules &&
178 expect_askpass pass user@host
181 test_expect_success 'cmdline credential config passes submodule update' '
182 # advance the submodule HEAD so that a fetch is required
183 git commit --allow-empty -m foo &&
184 git push "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git" HEAD &&
185 sha1=$(git rev-parse HEAD) &&
186 git -C super-clone update-index --cacheinfo 160000,$sha1,sub &&
188 set_askpass wrong pass@host &&
189 test_must_fail git -C super-clone submodule update &&
191 set_askpass wrong pass@host &&
192 git -C super-clone \
193 -c "credential.$HTTPD_URL.username=user@host" \
194 submodule update &&
195 expect_askpass pass user@host
198 test_expect_success 'fetch changes via http' '
199 echo content >>file &&
200 git commit -a -m two &&
201 git push public &&
202 (cd clone && git pull) &&
203 test_cmp file clone/file
206 test_expect_success 'fetch changes via manual http-fetch' '
207 cp -R clone-tmpl clone2 &&
209 HEAD=$(git rev-parse --verify HEAD) &&
210 (cd clone2 &&
211 git http-fetch -a -w heads/main-new $HEAD $(git config remote.origin.url) &&
212 git checkout main-new &&
213 test $HEAD = $(git rev-parse --verify HEAD)) &&
214 test_cmp file clone2/file
217 test_expect_success 'manual http-fetch without -a works just as well' '
218 cp -R clone-tmpl clone3 &&
220 HEAD=$(git rev-parse --verify HEAD) &&
221 (cd clone3 &&
222 git http-fetch -w heads/main-new $HEAD $(git config remote.origin.url) &&
223 git checkout main-new &&
224 test $HEAD = $(git rev-parse --verify HEAD)) &&
225 test_cmp file clone3/file
228 test_expect_success 'http remote detects correct HEAD' '
229 git push public main:other &&
230 (cd clone &&
231 git remote set-head origin -d &&
232 git remote set-head origin -a &&
233 git symbolic-ref refs/remotes/origin/HEAD > output &&
234 echo refs/remotes/origin/main > expect &&
235 test_cmp expect output
239 test_expect_success 'fetch packed objects' '
240 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
241 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
242 git --bare repack -a -d
243 ) &&
244 git clone $HTTPD_URL/dumb/repo_pack.git
247 test_expect_success 'http-fetch --packfile' '
248 # Arbitrary hash. Use rev-parse so that we get one of the correct
249 # length.
250 ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
252 git init packfileclient &&
253 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git && ls objects/pack/pack-*.pack) &&
254 git -C packfileclient http-fetch --packfile=$ARBITRARY \
255 --index-pack-arg=index-pack --index-pack-arg=--stdin \
256 --index-pack-arg=--keep \
257 "$HTTPD_URL"/dumb/repo_pack.git/$p >out &&
259 grep -E "^keep.[0-9a-f]{16,}$" out &&
260 cut -c6- out >packhash &&
262 # Ensure that the expected files are generated
263 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).pack" &&
264 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).idx" &&
265 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).keep" &&
267 # Ensure that it has the HEAD of repo_pack, at least
268 HASH=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
269 git -C packfileclient cat-file -e "$HASH"
272 test_expect_success 'fetch notices corrupt pack' '
273 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
274 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
275 p=$(ls objects/pack/pack-*.pack) &&
276 chmod u+w $p &&
277 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
278 ) &&
279 mkdir repo_bad1.git &&
280 (cd repo_bad1.git &&
281 git --bare init &&
282 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
283 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
287 test_expect_success 'http-fetch --packfile with corrupt pack' '
288 rm -rf packfileclient &&
289 git init packfileclient &&
290 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git && ls objects/pack/pack-*.pack) &&
291 test_must_fail git -C packfileclient http-fetch --packfile \
292 "$HTTPD_URL"/dumb/repo_bad1.git/$p
295 test_expect_success 'fetch notices corrupt idx' '
296 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
297 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
298 p=$(ls objects/pack/pack-*.idx) &&
299 chmod u+w $p &&
300 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
301 ) &&
302 mkdir repo_bad2.git &&
303 (cd repo_bad2.git &&
304 git --bare init &&
305 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
306 test 0 = $(ls objects/pack | wc -l)
310 # usage: count_fetches <nr> <extension> <trace_file>
311 count_fetches () {
312 # ignore grep exit code; it may return non-zero if we are expecting no
313 # matches
314 grep "GET .*objects/pack/pack-[a-z0-9]*.$2" "$3" >trace.count
315 test_line_count = "$1" trace.count
318 test_expect_success 'fetch can handle previously-fetched .idx files' '
319 git checkout --orphan branch1 &&
320 echo base >file &&
321 git add file &&
322 git commit -m base &&
323 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
324 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
325 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
326 git checkout -b branch2 branch1 &&
327 echo b2 >>file &&
328 git commit -a -m b2 &&
329 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
330 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
331 git --bare init clone_packed_branches.git &&
332 GIT_TRACE_CURL=$PWD/one.trace git --git-dir=clone_packed_branches.git \
333 fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
334 count_fetches 2 idx one.trace &&
335 count_fetches 1 pack one.trace &&
336 GIT_TRACE_CURL=$PWD/two.trace git --git-dir=clone_packed_branches.git \
337 fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2 &&
338 count_fetches 1 idx two.trace &&
339 count_fetches 1 pack two.trace
342 test_expect_success 'did not use upload-pack service' '
343 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
346 test_expect_success 'git client shows text/plain errors' '
347 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
348 grep "this is the error message" stderr
351 test_expect_success 'git client does not show html errors' '
352 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
353 ! grep "this is the error message" stderr
356 test_expect_success 'git client shows text/plain with a charset' '
357 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
358 grep "this is the error message" stderr
361 test_expect_success ICONV 'http error messages are reencoded' '
362 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
363 grep "this is the error message" stderr
366 test_expect_success ICONV 'reencoding is robust to whitespace oddities' '
367 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
368 grep "this is the error message" stderr
371 check_language () {
372 case "$2" in
374 >expect
377 echo "=> Send header: Accept-Language: $1" >expect
379 esac &&
380 GIT_TRACE_CURL=true \
381 LANGUAGE=$2 \
382 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
383 tr -d '\015' <output |
384 sort -u |
385 sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
386 test_cmp expect actual
389 test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
390 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
392 test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
393 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
394 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
395 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
396 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
397 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
399 test_expect_success 'git client sends Accept-Language with many preferred languages' '
400 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
401 ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
402 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
403 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
404 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
405 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
408 test_expect_success 'git client send an empty Accept-Language' '
409 GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
410 ! grep "^=> Send header: Accept-Language:" stderr
413 test_expect_success 'remote-http complains cleanly about malformed urls' '
414 test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
415 test_grep "url has no scheme" stderr
418 # NEEDSWORK: Writing commands to git-remote-curl can race against the latter
419 # erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
420 # learned to handle early remote helper failures more cleanly.
421 test_expect_success 'remote-http complains cleanly about empty scheme' '
422 test_must_fail ok=sigpipe git ls-remote \
423 http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
424 test_grep "url has no scheme" stderr
427 test_expect_success 'redirects can be forbidden/allowed' '
428 test_must_fail git -c http.followRedirects=false \
429 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
430 git -c http.followRedirects=true \
431 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
434 test_expect_success 'redirects are reported to stderr' '
435 # just look for a snippet of the redirected-to URL
436 test_grep /dumb/ stderr
439 test_expect_success 'non-initial redirects can be forbidden' '
440 test_must_fail git -c http.followRedirects=initial \
441 clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
442 git -c http.followRedirects=true \
443 clone $HTTPD_URL/redir-objects/repo.git redir-objects
446 test_expect_success 'http.followRedirects defaults to "initial"' '
447 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
450 # The goal is for a clone of the "evil" repository, which has no objects
451 # itself, to cause the client to fetch objects from the "victim" repository.
452 test_expect_success 'set up evil alternates scheme' '
453 victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
454 git init --bare "$victim" &&
455 git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
456 git -C "$victim" repack -ad &&
457 git -C "$victim" update-server-info &&
458 sha1=$(git -C "$victim" rev-parse HEAD) &&
460 evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
461 git init --template= --bare "$evil" &&
462 mkdir "$evil/info" &&
463 # do this by hand to avoid object existence check
464 printf "%s\\t%s\\n" $sha1 refs/heads/main >"$evil/info/refs"
467 # Here we'll just redirect via HTTP. In a real-world attack these would be on
468 # different servers, but we should reject it either way.
469 test_expect_success 'http-alternates is a non-initial redirect' '
470 echo "$HTTPD_URL/dumb/victim.git/objects" \
471 >"$evil/objects/info/http-alternates" &&
472 test_must_fail git -c http.followRedirects=initial \
473 clone $HTTPD_URL/dumb/evil.git evil-initial &&
474 git -c http.followRedirects=true \
475 clone $HTTPD_URL/dumb/evil.git evil-initial
478 # Curl supports a lot of protocols that we'd prefer not to allow
479 # http-alternates to use, but it's hard to test whether curl has
480 # accessed, say, the SMTP protocol, because we are not running an SMTP server.
481 # But we can check that it does not allow access to file://, which would
482 # otherwise allow this clone to complete.
483 test_expect_success 'http-alternates cannot point at funny protocols' '
484 echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
485 test_must_fail git -c http.followRedirects=true \
486 clone "$HTTPD_URL/dumb/evil.git" evil-file
489 test_expect_success 'http-alternates triggers not-from-user protocol check' '
490 echo "$HTTPD_URL/dumb/victim.git/objects" \
491 >"$evil/objects/info/http-alternates" &&
492 test_config_global http.followRedirects true &&
493 test_must_fail git -c protocol.http.allow=user \
494 clone $HTTPD_URL/dumb/evil.git evil-user &&
495 git -c protocol.http.allow=always \
496 clone $HTTPD_URL/dumb/evil.git evil-user
499 test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
500 git clone "$HTTPD_URL/redir-to/dumb/repo.git"
503 test_expect_success 'print HTTP error when any intermediate redirect throws error' '
504 test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
505 test_grep "unable to access.*/redir-to/502" stderr
508 test_expect_success 'fetching via http alternates works' '
509 parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
510 git init --bare "$parent" &&
511 git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
512 git -C "$parent" update-server-info &&
513 commit=$(git -C "$parent" rev-parse HEAD) &&
515 child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
516 git init --bare "$child" &&
517 echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
518 git -C "$child" update-ref HEAD $commit &&
519 git -C "$child" update-server-info &&
521 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"
524 test_expect_success 'dumb http can fetch index v1' '
525 server=$HTTPD_DOCUMENT_ROOT_PATH/idx-v1.git &&
526 git init --bare "$server" &&
527 git -C "$server" --work-tree=. commit --allow-empty -m foo &&
528 git -C "$server" -c pack.indexVersion=1 gc &&
530 git clone "$HTTPD_URL/dumb/idx-v1.git" &&
531 git -C idx-v1 fsck
534 test_done