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