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