Merge branch 'cp/git-flush-is-an-env-bool'
[alt-git.git] / t / t5550-http-fetch-dumb.sh
blob4c3b32785d580fb32846306df720629aee9b3a4b
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
58 test_expect_success 'create password-protected repository' '
59 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
60 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
61 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
64 test_expect_success 'create empty remote repository' '
65 git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
66 setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
69 test_expect_success 'empty dumb HTTP repository falls back to SHA1' '
70 test_when_finished "rm -fr clone-empty" &&
71 git clone $HTTPD_URL/dumb/empty.git clone-empty &&
72 git -C clone-empty rev-parse --show-object-format >empty-format &&
73 test "$(cat empty-format)" = sha1
76 setup_askpass_helper
78 test_expect_success 'cloning password-protected repository can fail' '
79 set_askpass wrong &&
80 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
81 expect_askpass both wrong
84 test_expect_success 'http auth can use user/pass in URL' '
85 set_askpass wrong &&
86 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
87 expect_askpass none
90 test_expect_success 'http auth can use just user in URL' '
91 set_askpass wrong pass@host &&
92 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
93 expect_askpass pass user@host
96 test_expect_success 'http auth can request both user and pass' '
97 set_askpass user@host pass@host &&
98 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
99 expect_askpass both user@host
102 test_expect_success 'http auth respects credential helper config' '
103 test_config_global credential.helper "!f() {
104 cat >/dev/null
105 echo username=user@host
106 echo password=pass@host
107 }; f" &&
108 set_askpass wrong &&
109 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
110 expect_askpass none
113 test_expect_success 'http auth can get username from config' '
114 test_config_global "credential.$HTTPD_URL.username" user@host &&
115 set_askpass wrong pass@host &&
116 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
117 expect_askpass pass user@host
120 test_expect_success 'configured username does not override URL' '
121 test_config_global "credential.$HTTPD_URL.username" wrong &&
122 set_askpass wrong pass@host &&
123 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
124 expect_askpass pass user@host
127 test_expect_success 'set up repo with http submodules' '
128 git init super &&
129 set_askpass user@host pass@host &&
131 cd super &&
132 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
133 git commit -m "add submodule"
137 test_expect_success 'cmdline credential config passes to submodule via clone' '
138 set_askpass wrong pass@host &&
139 test_must_fail git clone --recursive super super-clone &&
140 rm -rf super-clone &&
142 set_askpass wrong pass@host &&
143 git -c "credential.$HTTPD_URL.username=user@host" \
144 clone --recursive super super-clone &&
145 expect_askpass pass user@host
148 test_expect_success 'cmdline credential config passes submodule via fetch' '
149 set_askpass wrong pass@host &&
150 test_must_fail git -C super-clone fetch --recurse-submodules &&
152 set_askpass wrong pass@host &&
153 git -C super-clone \
154 -c "credential.$HTTPD_URL.username=user@host" \
155 fetch --recurse-submodules &&
156 expect_askpass pass user@host
159 test_expect_success 'cmdline credential config passes submodule update' '
160 # advance the submodule HEAD so that a fetch is required
161 git commit --allow-empty -m foo &&
162 git push "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git" HEAD &&
163 sha1=$(git rev-parse HEAD) &&
164 git -C super-clone update-index --cacheinfo 160000,$sha1,sub &&
166 set_askpass wrong pass@host &&
167 test_must_fail git -C super-clone submodule update &&
169 set_askpass wrong pass@host &&
170 git -C super-clone \
171 -c "credential.$HTTPD_URL.username=user@host" \
172 submodule update &&
173 expect_askpass pass user@host
176 test_expect_success 'fetch changes via http' '
177 echo content >>file &&
178 git commit -a -m two &&
179 git push public &&
180 (cd clone && git pull) &&
181 test_cmp file clone/file
184 test_expect_success 'fetch changes via manual http-fetch' '
185 cp -R clone-tmpl clone2 &&
187 HEAD=$(git rev-parse --verify HEAD) &&
188 (cd clone2 &&
189 git http-fetch -a -w heads/main-new $HEAD $(git config remote.origin.url) &&
190 git checkout main-new &&
191 test $HEAD = $(git rev-parse --verify HEAD)) &&
192 test_cmp file clone2/file
195 test_expect_success 'manual http-fetch without -a works just as well' '
196 cp -R clone-tmpl clone3 &&
198 HEAD=$(git rev-parse --verify HEAD) &&
199 (cd clone3 &&
200 git http-fetch -w heads/main-new $HEAD $(git config remote.origin.url) &&
201 git checkout main-new &&
202 test $HEAD = $(git rev-parse --verify HEAD)) &&
203 test_cmp file clone3/file
206 test_expect_success 'http remote detects correct HEAD' '
207 git push public main:other &&
208 (cd clone &&
209 git remote set-head origin -d &&
210 git remote set-head origin -a &&
211 git symbolic-ref refs/remotes/origin/HEAD > output &&
212 echo refs/remotes/origin/main > expect &&
213 test_cmp expect output
217 test_expect_success 'fetch packed objects' '
218 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
219 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
220 git --bare repack -a -d
221 ) &&
222 git clone $HTTPD_URL/dumb/repo_pack.git
225 test_expect_success 'http-fetch --packfile' '
226 # Arbitrary hash. Use rev-parse so that we get one of the correct
227 # length.
228 ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
230 git init packfileclient &&
231 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git && ls objects/pack/pack-*.pack) &&
232 git -C packfileclient http-fetch --packfile=$ARBITRARY \
233 --index-pack-arg=index-pack --index-pack-arg=--stdin \
234 --index-pack-arg=--keep \
235 "$HTTPD_URL"/dumb/repo_pack.git/$p >out &&
237 grep -E "^keep.[0-9a-f]{16,}$" out &&
238 cut -c6- out >packhash &&
240 # Ensure that the expected files are generated
241 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).pack" &&
242 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).idx" &&
243 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).keep" &&
245 # Ensure that it has the HEAD of repo_pack, at least
246 HASH=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
247 git -C packfileclient cat-file -e "$HASH"
250 test_expect_success 'fetch notices corrupt pack' '
251 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
252 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
253 p=$(ls objects/pack/pack-*.pack) &&
254 chmod u+w $p &&
255 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
256 ) &&
257 mkdir repo_bad1.git &&
258 (cd repo_bad1.git &&
259 git --bare init &&
260 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
261 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
265 test_expect_success 'http-fetch --packfile with corrupt pack' '
266 rm -rf packfileclient &&
267 git init packfileclient &&
268 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git && ls objects/pack/pack-*.pack) &&
269 test_must_fail git -C packfileclient http-fetch --packfile \
270 "$HTTPD_URL"/dumb/repo_bad1.git/$p
273 test_expect_success 'fetch notices corrupt idx' '
274 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
275 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
276 p=$(ls objects/pack/pack-*.idx) &&
277 chmod u+w $p &&
278 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
279 ) &&
280 mkdir repo_bad2.git &&
281 (cd repo_bad2.git &&
282 git --bare init &&
283 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
284 test 0 = $(ls objects/pack | wc -l)
288 test_expect_success 'fetch can handle previously-fetched .idx files' '
289 git checkout --orphan branch1 &&
290 echo base >file &&
291 git add file &&
292 git commit -m base &&
293 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
294 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
295 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
296 git checkout -b branch2 branch1 &&
297 echo b2 >>file &&
298 git commit -a -m b2 &&
299 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
300 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
301 git --bare init clone_packed_branches.git &&
302 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
303 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
306 test_expect_success 'did not use upload-pack service' '
307 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
310 test_expect_success 'git client shows text/plain errors' '
311 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
312 grep "this is the error message" stderr
315 test_expect_success 'git client does not show html errors' '
316 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
317 ! grep "this is the error message" stderr
320 test_expect_success 'git client shows text/plain with a charset' '
321 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
322 grep "this is the error message" stderr
325 test_expect_success 'http error messages are reencoded' '
326 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
327 grep "this is the error message" stderr
330 test_expect_success 'reencoding is robust to whitespace oddities' '
331 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
332 grep "this is the error message" stderr
335 check_language () {
336 case "$2" in
338 >expect
341 echo "=> Send header: Accept-Language: $1" >expect
343 esac &&
344 GIT_TRACE_CURL=true \
345 LANGUAGE=$2 \
346 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
347 tr -d '\015' <output |
348 sort -u |
349 sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
350 test_cmp expect actual
353 test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
354 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
356 test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
357 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
358 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
359 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
360 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
361 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
363 test_expect_success 'git client sends Accept-Language with many preferred languages' '
364 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
365 ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
366 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
367 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
368 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
369 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
372 test_expect_success 'git client send an empty Accept-Language' '
373 GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
374 ! grep "^=> Send header: Accept-Language:" stderr
377 test_expect_success 'remote-http complains cleanly about malformed urls' '
378 test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
379 test_grep "url has no scheme" stderr
382 # NEEDSWORK: Writing commands to git-remote-curl can race against the latter
383 # erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
384 # learned to handle early remote helper failures more cleanly.
385 test_expect_success 'remote-http complains cleanly about empty scheme' '
386 test_must_fail ok=sigpipe git ls-remote \
387 http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
388 test_grep "url has no scheme" stderr
391 test_expect_success 'redirects can be forbidden/allowed' '
392 test_must_fail git -c http.followRedirects=false \
393 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
394 git -c http.followRedirects=true \
395 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
398 test_expect_success 'redirects are reported to stderr' '
399 # just look for a snippet of the redirected-to URL
400 test_grep /dumb/ stderr
403 test_expect_success 'non-initial redirects can be forbidden' '
404 test_must_fail git -c http.followRedirects=initial \
405 clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
406 git -c http.followRedirects=true \
407 clone $HTTPD_URL/redir-objects/repo.git redir-objects
410 test_expect_success 'http.followRedirects defaults to "initial"' '
411 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
414 # The goal is for a clone of the "evil" repository, which has no objects
415 # itself, to cause the client to fetch objects from the "victim" repository.
416 test_expect_success 'set up evil alternates scheme' '
417 victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
418 git init --bare "$victim" &&
419 git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
420 git -C "$victim" repack -ad &&
421 git -C "$victim" update-server-info &&
422 sha1=$(git -C "$victim" rev-parse HEAD) &&
424 evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
425 git init --template= --bare "$evil" &&
426 mkdir "$evil/info" &&
427 # do this by hand to avoid object existence check
428 printf "%s\\t%s\\n" $sha1 refs/heads/main >"$evil/info/refs"
431 # Here we'll just redirect via HTTP. In a real-world attack these would be on
432 # different servers, but we should reject it either way.
433 test_expect_success 'http-alternates is a non-initial redirect' '
434 echo "$HTTPD_URL/dumb/victim.git/objects" \
435 >"$evil/objects/info/http-alternates" &&
436 test_must_fail git -c http.followRedirects=initial \
437 clone $HTTPD_URL/dumb/evil.git evil-initial &&
438 git -c http.followRedirects=true \
439 clone $HTTPD_URL/dumb/evil.git evil-initial
442 # Curl supports a lot of protocols that we'd prefer not to allow
443 # http-alternates to use, but it's hard to test whether curl has
444 # accessed, say, the SMTP protocol, because we are not running an SMTP server.
445 # But we can check that it does not allow access to file://, which would
446 # otherwise allow this clone to complete.
447 test_expect_success 'http-alternates cannot point at funny protocols' '
448 echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
449 test_must_fail git -c http.followRedirects=true \
450 clone "$HTTPD_URL/dumb/evil.git" evil-file
453 test_expect_success 'http-alternates triggers not-from-user protocol check' '
454 echo "$HTTPD_URL/dumb/victim.git/objects" \
455 >"$evil/objects/info/http-alternates" &&
456 test_config_global http.followRedirects true &&
457 test_must_fail git -c protocol.http.allow=user \
458 clone $HTTPD_URL/dumb/evil.git evil-user &&
459 git -c protocol.http.allow=always \
460 clone $HTTPD_URL/dumb/evil.git evil-user
463 test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
464 git clone "$HTTPD_URL/redir-to/dumb/repo.git"
467 test_expect_success 'print HTTP error when any intermediate redirect throws error' '
468 test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
469 test_grep "unable to access.*/redir-to/502" stderr
472 test_expect_success 'fetching via http alternates works' '
473 parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
474 git init --bare "$parent" &&
475 git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
476 git -C "$parent" update-server-info &&
477 commit=$(git -C "$parent" rev-parse HEAD) &&
479 child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
480 git init --bare "$child" &&
481 echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
482 git -C "$child" update-ref HEAD $commit &&
483 git -C "$child" update-server-info &&
485 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"
488 test_done