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