3 test_description='test dumb fetching over http via static file'
5 . "$TEST_DIRECTORY"/lib-httpd.sh
8 test_expect_success 'setup repository' '
9 git config push.default matching &&
10 echo content1 >file &&
13 echo content2 >file &&
18 test_expect_success 'create http-accessible bare repository with loose objects' '
19 cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
20 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21 git config core.bare true &&
23 write_script "hooks/post-update" <<-\EOF &&
24 exec git update-server-info
28 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
29 git push public master:master
32 test_expect_success 'clone http repository' '
33 git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
34 cp -R clone-tmpl clone &&
35 test_cmp file clone/file
38 test_expect_success 'list refs from outside any repository' '
40 $(git rev-parse master) HEAD
41 $(git rev-parse master) refs/heads/master
43 nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
44 test_cmp expect actual
47 test_expect_success 'create password-protected repository' '
48 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
49 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
50 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
53 test_expect_success 'create empty remote repository' '
54 git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
55 (cd "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
57 write_script "hooks/post-update" <<-\EOF &&
58 exec git update-server-info
64 test_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)"
73 test_expect_success 'cloning password-protected repository can fail' '
75 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
76 expect_askpass both wrong
79 test_expect_success 'http auth can use user/pass in URL' '
81 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
85 test_expect_success 'http auth can use just user in URL' '
86 set_askpass wrong pass@host &&
87 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
88 expect_askpass pass user@host
91 test_expect_success 'http auth can request both user and pass' '
92 set_askpass user@host pass@host &&
93 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
94 expect_askpass both user@host
97 test_expect_success 'http auth respects credential helper config' '
98 test_config_global credential.helper "!f() {
100 echo username=user@host
101 echo password=pass@host
104 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
108 test_expect_success 'http auth can get username from config' '
109 test_config_global "credential.$HTTPD_URL.username" user@host &&
110 set_askpass wrong pass@host &&
111 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
112 expect_askpass pass user@host
115 test_expect_success 'configured username does not override URL' '
116 test_config_global "credential.$HTTPD_URL.username" wrong &&
117 set_askpass wrong pass@host &&
118 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
119 expect_askpass pass user@host
122 test_expect_success 'set up repo with http submodules' '
124 set_askpass user@host pass@host &&
127 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
128 git commit -m "add submodule"
132 test_expect_success 'cmdline credential config passes to submodule via clone' '
133 set_askpass wrong pass@host &&
134 test_must_fail git clone --recursive super super-clone &&
135 rm -rf super-clone &&
137 set_askpass wrong pass@host &&
138 git -c "credential.$HTTPD_URL.username=user@host" \
139 clone --recursive super super-clone &&
140 expect_askpass pass user@host
143 test_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 &&
147 set_askpass wrong pass@host &&
149 -c "credential.$HTTPD_URL.username=user@host" \
150 fetch --recurse-submodules &&
151 expect_askpass pass user@host
154 test_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 &&
161 set_askpass wrong pass@host &&
162 test_must_fail git -C super-clone submodule update &&
164 set_askpass wrong pass@host &&
166 -c "credential.$HTTPD_URL.username=user@host" \
168 expect_askpass pass user@host
171 test_expect_success 'fetch changes via http' '
172 echo content >>file &&
173 git commit -a -m two &&
175 (cd clone && git pull) &&
176 test_cmp file clone/file
179 test_expect_success 'fetch changes via manual http-fetch' '
180 cp -R clone-tmpl clone2 &&
182 HEAD=$(git rev-parse --verify HEAD) &&
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
190 test_expect_success 'manual http-fetch without -a works just as well' '
191 cp -R clone-tmpl clone3 &&
193 HEAD=$(git rev-parse --verify HEAD) &&
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
201 test_expect_success 'http remote detects correct HEAD' '
202 git push public master:other &&
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
212 test_expect_success 'fetch packed objects' '
213 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
214 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
215 git --bare repack -a -d
217 git clone $HTTPD_URL/dumb/repo_pack.git
220 test_expect_success 'fetch notices corrupt pack' '
221 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
222 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
223 p=$(ls objects/pack/pack-*.pack) &&
225 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
227 mkdir repo_bad1.git &&
230 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
231 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
235 test_expect_success 'fetch notices corrupt idx' '
236 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
237 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
238 p=$(ls objects/pack/pack-*.idx) &&
240 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
242 mkdir repo_bad2.git &&
245 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
246 test 0 = $(ls objects/pack | wc -l)
250 test_expect_success 'fetch can handle previously-fetched .idx files' '
251 git checkout --orphan branch1 &&
254 git commit -m base &&
255 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
256 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
257 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
258 git checkout -b branch2 branch1 &&
260 git commit -a -m b2 &&
261 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
262 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
263 git --bare init clone_packed_branches.git &&
264 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
265 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
268 test_expect_success 'did not use upload-pack service' '
269 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
272 test_expect_success 'git client shows text/plain errors' '
273 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
274 grep "this is the error message" stderr
277 test_expect_success 'git client does not show html errors' '
278 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
279 ! grep "this is the error message" stderr
282 test_expect_success 'git client shows text/plain with a charset' '
283 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
284 grep "this is the error message" stderr
287 test_expect_success 'http error messages are reencoded' '
288 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
289 grep "this is the error message" stderr
292 test_expect_success 'reencoding is robust to whitespace oddities' '
293 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
294 grep "this is the error message" stderr
303 echo "=> Send header: Accept-Language: $1" >expect
306 GIT_TRACE_CURL=true \
308 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
309 tr -d '\015' <output |
311 sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
312 test_cmp expect actual
315 test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
316 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
318 test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
319 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
320 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
321 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
322 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
323 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
325 test_expect_success 'git client sends Accept-Language with many preferred languages' '
326 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
327 ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
328 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
329 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
330 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
331 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
334 test_expect_success 'git client does not send an empty Accept-Language' '
335 GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
336 ! grep "^=> Send header: Accept-Language:" stderr
339 test_expect_success 'remote-http complains cleanly about malformed urls' '
340 test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
341 test_i18ngrep "url has no scheme" stderr
344 # NEEDSWORK: Writing commands to git-remote-curl can race against the latter
345 # erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
346 # learned to handle early remote helper failures more cleanly.
347 test_expect_success 'remote-http complains cleanly about empty scheme' '
348 test_must_fail ok=sigpipe git ls-remote \
349 http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
350 test_i18ngrep "url has no scheme" stderr
353 test_expect_success 'redirects can be forbidden/allowed' '
354 test_must_fail git -c http.followRedirects=false \
355 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
356 git -c http.followRedirects=true \
357 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
360 test_expect_success 'redirects are reported to stderr' '
361 # just look for a snippet of the redirected-to URL
362 test_i18ngrep /dumb/ stderr
365 test_expect_success 'non-initial redirects can be forbidden' '
366 test_must_fail git -c http.followRedirects=initial \
367 clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
368 git -c http.followRedirects=true \
369 clone $HTTPD_URL/redir-objects/repo.git redir-objects
372 test_expect_success 'http.followRedirects defaults to "initial"' '
373 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
376 # The goal is for a clone of the "evil" repository, which has no objects
377 # itself, to cause the client to fetch objects from the "victim" repository.
378 test_expect_success 'set up evil alternates scheme' '
379 victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
380 git init --bare "$victim" &&
381 git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
382 git -C "$victim" repack -ad &&
383 git -C "$victim" update-server-info &&
384 sha1=$(git -C "$victim" rev-parse HEAD) &&
386 evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
387 git init --bare "$evil" &&
388 # do this by hand to avoid object existence check
389 printf "%s\\t%s\\n" $sha1 refs/heads/master >"$evil/info/refs"
392 # Here we'll just redirect via HTTP. In a real-world attack these would be on
393 # different servers, but we should reject it either way.
394 test_expect_success 'http-alternates is a non-initial redirect' '
395 echo "$HTTPD_URL/dumb/victim.git/objects" \
396 >"$evil/objects/info/http-alternates" &&
397 test_must_fail git -c http.followRedirects=initial \
398 clone $HTTPD_URL/dumb/evil.git evil-initial &&
399 git -c http.followRedirects=true \
400 clone $HTTPD_URL/dumb/evil.git evil-initial
403 # Curl supports a lot of protocols that we'd prefer not to allow
404 # http-alternates to use, but it's hard to test whether curl has
405 # accessed, say, the SMTP protocol, because we are not running an SMTP server.
406 # But we can check that it does not allow access to file://, which would
407 # otherwise allow this clone to complete.
408 test_expect_success 'http-alternates cannot point at funny protocols' '
409 echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
410 test_must_fail git -c http.followRedirects=true \
411 clone "$HTTPD_URL/dumb/evil.git" evil-file
414 test_expect_success 'http-alternates triggers not-from-user protocol check' '
415 echo "$HTTPD_URL/dumb/victim.git/objects" \
416 >"$evil/objects/info/http-alternates" &&
417 test_config_global http.followRedirects true &&
418 test_must_fail git -c protocol.http.allow=user \
419 clone $HTTPD_URL/dumb/evil.git evil-user &&
420 git -c protocol.http.allow=always \
421 clone $HTTPD_URL/dumb/evil.git evil-user
424 test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
425 git clone "$HTTPD_URL/redir-to/dumb/repo.git"
428 test_expect_success 'print HTTP error when any intermediate redirect throws error' '
429 test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
430 test_i18ngrep "unable to access.*/redir-to/502" stderr
433 test_expect_success 'fetching via http alternates works' '
434 parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
435 git init --bare "$parent" &&
436 git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
437 git -C "$parent" update-server-info &&
438 commit=$(git -C "$parent" rev-parse HEAD) &&
440 child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
441 git init --bare "$child" &&
442 echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
443 git -C "$child" update-ref HEAD $commit &&
444 git -C "$child" update-server-info &&
446 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"