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
9 if test_have_prereq
!REFFILES
11 skip_all
='skipping test; dumb HTTP protocol not supported with reftable.'
15 .
"$TEST_DIRECTORY"/lib-httpd.sh
18 test_expect_success
'setup repository' '
19 git config push.default matching &&
20 echo content1 >file &&
23 echo content2 >file &&
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 &&
33 write_script "hooks/post-update" <<-\EOF &&
34 exec git update-server-info
38 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
39 git push public main:main
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
48 test_expect_success
'list refs from outside any repository' '
50 $(git rev-parse main) HEAD
51 $(git rev-parse main) refs/heads/main
53 nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
54 test_cmp expect actual
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"
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" &&
67 write_script "hooks/post-update" <<-\EOF &&
68 exec git update-server-info
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)"
83 test_expect_success
'cloning password-protected repository can fail' '
85 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
86 expect_askpass both wrong
89 test_expect_success
'http auth can use user/pass in URL' '
91 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
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
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
107 test_expect_success
'http auth respects credential helper config' '
108 test_config_global credential.helper "!f() {
110 echo username=user@host
111 echo password=pass@host
114 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
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
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
132 test_expect_success
'set up repo with http submodules' '
134 set_askpass user@host pass@host &&
137 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
138 git commit -m "add submodule"
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 &&
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
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 &&
157 set_askpass wrong pass@host &&
159 -c "credential.$HTTPD_URL.username=user@host" \
160 fetch --recurse-submodules &&
161 expect_askpass pass user@host
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 &&
171 set_askpass wrong pass@host &&
172 test_must_fail git -C super-clone submodule update &&
174 set_askpass wrong pass@host &&
176 -c "credential.$HTTPD_URL.username=user@host" \
178 expect_askpass pass user@host
181 test_expect_success
'fetch changes via http' '
182 echo content >>file &&
183 git commit -a -m two &&
185 (cd clone && git pull) &&
186 test_cmp file clone/file
189 test_expect_success
'fetch changes via manual http-fetch' '
190 cp -R clone-tmpl clone2 &&
192 HEAD=$(git rev-parse --verify HEAD) &&
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
200 test_expect_success
'manual http-fetch without -a works just as well' '
201 cp -R clone-tmpl clone3 &&
203 HEAD=$(git rev-parse --verify HEAD) &&
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
211 test_expect_success
'http remote detects correct HEAD' '
212 git push public main:other &&
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
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
227 git clone $HTTPD_URL/dumb/repo_pack.git
230 test_expect_success
'http-fetch --packfile' '
231 # Arbitrary hash. Use rev-parse so that we get one of the correct
233 ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
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 &&
242 grep "^keep.[0-9a-f]\{16,\}$" out &&
243 cut -c6- out >packhash &&
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" &&
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"
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) &&
260 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
262 mkdir repo_bad1.git &&
265 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
266 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
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
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) &&
283 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
285 mkdir repo_bad2.git &&
288 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
289 test 0 = $(ls objects/pack | wc -l)
293 test_expect_success
'fetch can handle previously-fetched .idx files' '
294 git checkout --orphan branch1 &&
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 &&
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
311 test_expect_success
'did not use upload-pack service' '
312 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
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
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
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
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
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
346 echo "=> Send header: Accept-Language: $1" >expect
349 GIT_TRACE_CURL
=true \
351 git ls-remote
"$HTTPD_URL/dumb/repo.git" >output
2>&1 &&
352 tr -d '\015' <output |
354 sed -ne '/^=> Send header: Accept-Language:/ p' >actual
&&
355 test_cmp expect actual
358 test_expect_success
'git client sends Accept-Language based on LANGUAGE' '
359 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
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"'
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
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
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
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
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
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
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
415 test_expect_success
'http.followRedirects defaults to "initial"' '
416 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
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) &&
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"
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
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
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
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"
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
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) &&
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 &&
489 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"