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