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