git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t5551-http-fetch-smart.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | : ${HTTP_PROTO:=HTTP/1.1} |
| 4 | test_description="test smart fetching over http via http-backend ($HTTP_PROTO)" |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | . "$TEST_DIRECTORY"/lib-httpd.sh |
| 10 | |
| 11 | if ! test_have_prereq PERL_TEST_HELPERS |
| 12 | then |
| 13 | skip_all='skipping http fetch smart tests; Perl not available' |
| 14 | test_done |
| 15 | fi |
| 16 | |
| 17 | test "$HTTP_PROTO" = "HTTP/2" && enable_http2 |
| 18 | start_httpd |
| 19 | |
| 20 | # The cURL version which Debian 12 ships (v7.88.1) can fail to retry |
| 21 | # authentication after an early HTTP/2 response. This bug was introduced |
| 22 | # in cURL v7.88.0 (8c762f5998 (http2: minor buffer and error path fixes, |
| 23 | # 2023-02-08)) and fixed in v8.3.0 (https://github.com/curl/curl/pull/11756). |
| 24 | test_lazy_prereq HAVE_CURL_HTTP2_BUG - <<\EOT |
| 25 | test_have_prereq HTTP2 && |
| 26 | build_option libcurl | |
| 27 | awk -F. ' |
| 28 | ($1 == 7 && $2 >= 88) || ($1 == 8 && $2 < 3) { broken = 1 } |
| 29 | END { exit !broken } |
| 30 | ' |
| 31 | EOT |
| 32 | |
| 33 | test_expect_success HTTP2 'enable client-side http/2' ' |
| 34 | git config --global http.version HTTP/2 |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'setup repository' ' |
| 38 | git config push.default matching && |
| 39 | echo content >file && |
| 40 | git add file && |
| 41 | git commit -m one |
| 42 | ' |
| 43 | |
| 44 | test_expect_success 'create http-accessible bare repository' ' |
| 45 | mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 46 | (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 47 | git --bare init |
| 48 | ) && |
| 49 | git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 50 | git push public main:main |
| 51 | ' |
| 52 | |
| 53 | setup_askpass_helper |
| 54 | |
| 55 | test_expect_success 'clone http repository' ' |
| 56 | if test_have_prereq HTTP2 && test "$HTTPD_PROTO" = "https" |
| 57 | then |
| 58 | # ALPN lets us immediately use HTTP/2; likewise, POSTs with |
| 59 | # bodies can use it because they do not need to upgrade |
| 60 | INITIAL_PROTO=HTTP/2 |
| 61 | else |
| 62 | # either we are not using HTTP/2, or the initial |
| 63 | # request is sent via HTTP/1.1 and asks for upgrade |
| 64 | INITIAL_PROTO=HTTP/1.1 |
| 65 | fi && |
| 66 | |
| 67 | cat >exp.raw <<-EOF && |
| 68 | > GET /smart/repo.git/info/refs?service=git-upload-pack $INITIAL_PROTO |
| 69 | > accept: */* |
| 70 | > accept-encoding: ENCODINGS |
| 71 | > accept-language: ko-KR, *;q=0.9 |
| 72 | > pragma: no-cache |
| 73 | {V2} > git-protocol: version=2 |
| 74 | < $HTTP_PROTO 200 OK |
| 75 | < pragma: no-cache |
| 76 | < cache-control: no-cache, max-age=0, must-revalidate |
| 77 | < content-type: application/x-git-upload-pack-advertisement |
| 78 | > POST /smart/repo.git/git-upload-pack $INITIAL_PROTO |
| 79 | > accept-encoding: ENCODINGS |
| 80 | > content-type: application/x-git-upload-pack-request |
| 81 | > accept: application/x-git-upload-pack-result |
| 82 | > accept-language: ko-KR, *;q=0.9 |
| 83 | {V2} > git-protocol: version=2 |
| 84 | > content-length: xxx |
| 85 | < $INITIAL_PROTO 200 OK |
| 86 | < pragma: no-cache |
| 87 | < cache-control: no-cache, max-age=0, must-revalidate |
| 88 | < content-type: application/x-git-upload-pack-result |
| 89 | {V2} > POST /smart/repo.git/git-upload-pack $INITIAL_PROTO |
| 90 | {V2} > accept-encoding: ENCODINGS |
| 91 | {V2} > content-type: application/x-git-upload-pack-request |
| 92 | {V2} > accept: application/x-git-upload-pack-result |
| 93 | {V2} > accept-language: ko-KR, *;q=0.9 |
| 94 | {V2} > git-protocol: version=2 |
| 95 | {V2} > content-length: xxx |
| 96 | {V2} < $INITIAL_PROTO 200 OK |
| 97 | {V2} < pragma: no-cache |
| 98 | {V2} < cache-control: no-cache, max-age=0, must-revalidate |
| 99 | {V2} < content-type: application/x-git-upload-pack-result |
| 100 | EOF |
| 101 | |
| 102 | if test "$GIT_TEST_PROTOCOL_VERSION" = 0 |
| 103 | then |
| 104 | sed "/^{V2}/d" <exp.raw >exp |
| 105 | else |
| 106 | sed "s/^{V2} //" <exp.raw >exp |
| 107 | fi && |
| 108 | |
| 109 | GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" \ |
| 110 | git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err && |
| 111 | test_cmp file clone/file && |
| 112 | tr '\''\015'\'' Q <err | |
| 113 | perl -pe '\'' |
| 114 | s/(Send|Recv) header: ([A-Za-z0-9-]+):/ |
| 115 | "$1 header: " . lc($2) . ":" |
| 116 | /e; |
| 117 | '\'' | |
| 118 | sed -e " |
| 119 | s/Q\$// |
| 120 | /^[^<=]/d |
| 121 | /^== Info:/d |
| 122 | /^=> Send header, /d |
| 123 | /^=> Send header:$/d |
| 124 | /^<= Recv header, /d |
| 125 | /^<= Recv header:$/d |
| 126 | s/=> Send header: // |
| 127 | s/= Recv header:// |
| 128 | /^<= Recv data/d |
| 129 | /^=> Send data/d |
| 130 | /^<= Recv SSL data/d |
| 131 | /^=> Send SSL data/d |
| 132 | /^$/d |
| 133 | /^< $/d |
| 134 | |
| 135 | /^[^><]/{ |
| 136 | s/^/> / |
| 137 | } |
| 138 | |
| 139 | /^< HTTP/ { |
| 140 | s/200$/200 OK/ |
| 141 | } |
| 142 | /^< HTTP\\/1.1 101/d |
| 143 | /^[><] connection: /d |
| 144 | /^[><] upgrade: /d |
| 145 | /^> http2-settings: /d |
| 146 | |
| 147 | /^> user-agent: /d |
| 148 | /^> host: /d |
| 149 | /^> POST /,$ { |
| 150 | /^> Accept: [*]\\/[*]/d |
| 151 | } |
| 152 | s/^> content-length: .*/> content-length: xxx/ |
| 153 | /^> 00..want /d |
| 154 | /^> 00.*done/d |
| 155 | |
| 156 | /^< server: /d |
| 157 | /^< expires: /d |
| 158 | /^< date: /d |
| 159 | /^< content-length: /d |
| 160 | /^< transfer-encoding: /d |
| 161 | " >actual && |
| 162 | |
| 163 | sed -e "s/^> accept-encoding: .*/> accept-encoding: ENCODINGS/" \ |
| 164 | actual >actual.smudged && |
| 165 | test_cmp exp actual.smudged && |
| 166 | |
| 167 | grep "accept-encoding:.*gzip" actual >actual.gzip |
| 168 | ' |
| 169 | |
| 170 | test_expect_success 'fetch changes via http' ' |
| 171 | echo content >>file && |
| 172 | git commit -a -m two && |
| 173 | git push public && |
| 174 | (cd clone && git pull) && |
| 175 | test_cmp file clone/file |
| 176 | ' |
| 177 | |
| 178 | test_expect_success 'used upload-pack service' ' |
| 179 | strip_access_log >log && |
| 180 | test_grep "GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/[0-9.]* 200" log && |
| 181 | test_grep "POST /smart/repo.git/git-upload-pack HTTP/[0-9.]* 200" log |
| 182 | ' |
| 183 | |
| 184 | test_expect_success 'follow redirects (301)' ' |
| 185 | git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p |
| 186 | ' |
| 187 | |
| 188 | test_expect_success 'follow redirects (302)' ' |
| 189 | git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t |
| 190 | ' |
| 191 | |
| 192 | test_expect_success 'redirects re-root further requests' ' |
| 193 | git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited |
| 194 | ' |
| 195 | |
| 196 | test_expect_success 're-rooting dies on insane schemes' ' |
| 197 | test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane |
| 198 | ' |
| 199 | |
| 200 | test_expect_success 'clone from password-protected repository' ' |
| 201 | echo two >expect && |
| 202 | set_askpass user@host pass@host && |
| 203 | git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth && |
| 204 | expect_askpass both user%40host && |
| 205 | git --git-dir=smart-auth log -1 --format=%s >actual && |
| 206 | test_cmp expect actual |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'credential.interactive=false skips askpass' ' |
| 210 | set_askpass bogus nonsense && |
| 211 | ( |
| 212 | GIT_TRACE2_EVENT="$(pwd)/interactive-true" && |
| 213 | export GIT_TRACE2_EVENT && |
| 214 | test_must_fail git clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-true-dir && |
| 215 | test_region credential interactive interactive-true && |
| 216 | |
| 217 | GIT_TRACE2_EVENT="$(pwd)/interactive-false" && |
| 218 | export GIT_TRACE2_EVENT && |
| 219 | test_must_fail git -c credential.interactive=false \ |
| 220 | clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-false-dir && |
| 221 | test_region ! credential interactive interactive-false && |
| 222 | |
| 223 | GIT_TRACE2_EVENT="$(pwd)/interactive-never" && |
| 224 | export GIT_TRACE2_EVENT && |
| 225 | test_must_fail git -c credential.interactive=never \ |
| 226 | clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-never-dir && |
| 227 | test_region ! credential interactive interactive-never |
| 228 | ) |
| 229 | ' |
| 230 | |
| 231 | test_expect_success 'clone from auth-only-for-push repository' ' |
| 232 | echo two >expect && |
| 233 | set_askpass wrong && |
| 234 | git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth && |
| 235 | expect_askpass none && |
| 236 | git --git-dir=smart-noauth log -1 --format=%s >actual && |
| 237 | test_cmp expect actual |
| 238 | ' |
| 239 | |
| 240 | test_expect_success !HAVE_CURL_HTTP2_BUG \ |
| 241 | 'clone from auth-only-for-objects repository' ' |
| 242 | echo two >expect && |
| 243 | set_askpass user@host pass@host && |
| 244 | git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth && |
| 245 | expect_askpass both user%40host && |
| 246 | git --git-dir=half-auth log -1 --format=%s >actual && |
| 247 | test_cmp expect actual |
| 248 | ' |
| 249 | |
| 250 | test_expect_success !HAVE_CURL_HTTP2_BUG \ |
| 251 | 'no-op half-auth fetch does not require a password' ' |
| 252 | set_askpass wrong && |
| 253 | |
| 254 | # NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth" |
| 255 | # configuration with authentication required only when downloading |
| 256 | # objects and not refs, by having the HTTP server only require |
| 257 | # authentication for the "git-upload-pack" path and not "info/refs". |
| 258 | # This is not possible with protocol v2, since both objects and refs |
| 259 | # are obtained from the "git-upload-pack" path. A solution to this is |
| 260 | # to teach the server and client to be able to inline ls-refs requests |
| 261 | # as an Extra Parameter (see "git help gitformat-pack-protocol"), so that |
| 262 | # "info/refs" can serve refs, just like it does in protocol v0. |
| 263 | GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch && |
| 264 | expect_askpass none |
| 265 | ' |
| 266 | |
| 267 | test_expect_success 'redirects send auth to new location' ' |
| 268 | set_askpass user@host pass@host && |
| 269 | git -c credential.useHttpPath=true \ |
| 270 | clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth && |
| 271 | expect_askpass both user%40host auth/smart/repo.git |
| 272 | ' |
| 273 | |
| 274 | test_expect_success 'GIT_TRACE_CURL redacts auth details' ' |
| 275 | rm -rf redact-auth trace && |
| 276 | set_askpass user@host pass@host && |
| 277 | GIT_TRACE_CURL="$(pwd)/trace" git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth && |
| 278 | expect_askpass both user%40host && |
| 279 | |
| 280 | # Ensure that there is no "Basic" followed by a base64 string, but that |
| 281 | # the auth details are redacted |
| 282 | test_grep ! -i "Authorization: Basic [0-9a-zA-Z+/]" trace && |
| 283 | test_grep -i "Authorization: Basic <redacted>" trace |
| 284 | ' |
| 285 | |
| 286 | test_expect_success 'GIT_CURL_VERBOSE redacts auth details' ' |
| 287 | rm -rf redact-auth trace && |
| 288 | set_askpass user@host pass@host && |
| 289 | GIT_CURL_VERBOSE=1 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth 2>trace && |
| 290 | expect_askpass both user%40host && |
| 291 | |
| 292 | # Ensure that there is no "Basic" followed by a base64 string, but that |
| 293 | # the auth details are redacted |
| 294 | test_grep ! -i "Authorization: Basic [0-9a-zA-Z+/]" trace && |
| 295 | test_grep -i "Authorization: Basic <redacted>" trace |
| 296 | ' |
| 297 | |
| 298 | test_expect_success 'GIT_TRACE_CURL does not redact auth details if GIT_TRACE_REDACT=0' ' |
| 299 | rm -rf redact-auth trace && |
| 300 | set_askpass user@host pass@host && |
| 301 | GIT_TRACE_REDACT=0 GIT_TRACE_CURL="$(pwd)/trace" \ |
| 302 | git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth && |
| 303 | expect_askpass both user%40host && |
| 304 | |
| 305 | test_grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace |
| 306 | ' |
| 307 | |
| 308 | test_expect_success 'disable dumb http on server' ' |
| 309 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ |
| 310 | config http.getanyfile false |
| 311 | ' |
| 312 | |
| 313 | test_expect_success 'GIT_SMART_HTTP can disable smart http' ' |
| 314 | (GIT_SMART_HTTP=0 && |
| 315 | export GIT_SMART_HTTP && |
| 316 | cd clone && |
| 317 | test_must_fail git fetch) |
| 318 | ' |
| 319 | |
| 320 | test_expect_success 'invalid Content-Type rejected' ' |
| 321 | test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual && |
| 322 | test_grep "not valid:" actual |
| 323 | ' |
| 324 | |
| 325 | test_expect_success 'create namespaced refs' ' |
| 326 | test_commit namespaced && |
| 327 | git push public HEAD:refs/namespaces/ns/refs/heads/main && |
| 328 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ |
| 329 | symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/main |
| 330 | ' |
| 331 | |
| 332 | test_expect_success 'smart clone respects namespace' ' |
| 333 | git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart && |
| 334 | echo namespaced >expect && |
| 335 | git --git-dir=ns-smart/.git log -1 --format=%s >actual && |
| 336 | test_cmp expect actual |
| 337 | ' |
| 338 | |
| 339 | test_expect_success 'dumb clone via http-backend respects namespace' ' |
| 340 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ |
| 341 | config http.getanyfile true && |
| 342 | GIT_SMART_HTTP=0 git clone \ |
| 343 | "$HTTPD_URL/smart_namespace/repo.git" ns-dumb && |
| 344 | echo namespaced >expect && |
| 345 | git --git-dir=ns-dumb/.git log -1 --format=%s >actual && |
| 346 | test_cmp expect actual |
| 347 | ' |
| 348 | |
| 349 | test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' ' |
| 350 | cat >cookies.txt <<-\EOF && |
| 351 | 127.0.0.1 FALSE /smart_cookies FALSE 0 othername othervalue |
| 352 | EOF |
| 353 | sort >expect_cookies.txt <<-\EOF && |
| 354 | 127.0.0.1 FALSE /smart_cookies FALSE 0 othername othervalue |
| 355 | 127.0.0.1 FALSE /smart_cookies/repo.git FALSE 0 name value |
| 356 | 127.0.0.1 FALSE /smart_cookies/repo.git/info FALSE 0 name value |
| 357 | EOF |
| 358 | git config http.cookiefile cookies.txt && |
| 359 | git config http.savecookies true && |
| 360 | |
| 361 | test_when_finished " |
| 362 | git --git-dir=\"\$HTTPD_DOCUMENT_ROOT_PATH/repo.git\" \ |
| 363 | tag -d cookie-tag |
| 364 | " && |
| 365 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ |
| 366 | tag -m "foo" cookie-tag && |
| 367 | git fetch $HTTPD_URL/smart_cookies/repo.git cookie-tag && |
| 368 | |
| 369 | # Strip trailing slashes from cookie paths to handle output from both |
| 370 | # old curl ("/smart_cookies/") and new ("/smart_cookies"). |
| 371 | HT=" " && |
| 372 | grep "^[^#]" cookies.txt | sed "s,/$HT,$HT," | sort >cookies_clean.txt && |
| 373 | test_cmp expect_cookies.txt cookies_clean.txt |
| 374 | ' |
| 375 | |
| 376 | test_expect_success 'transfer.hiderefs works over smart-http' ' |
| 377 | test_commit hidden && |
| 378 | test_commit visible && |
| 379 | git push public HEAD^:refs/heads/a HEAD:refs/heads/b && |
| 380 | git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ |
| 381 | config transfer.hiderefs refs/heads/a && |
| 382 | git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git && |
| 383 | test_must_fail git -C hidden.git rev-parse --verify a && |
| 384 | git -C hidden.git rev-parse --verify b |
| 385 | ' |
| 386 | |
| 387 | # create an arbitrary number of tags, numbered from tag-$1 to tag-$2 |
| 388 | create_tags () { |
| 389 | rm -f marks && |
| 390 | for i in $(test_seq "$1" "$2") |
| 391 | do |
| 392 | # don't use here-doc, because it requires a process |
| 393 | # per loop iteration |
| 394 | echo "commit refs/heads/too-many-refs-$1" && |
| 395 | echo "mark :$i" && |
| 396 | echo "committer git <git@example.com> $i +0000" && |
| 397 | echo "data 0" && |
| 398 | echo "M 644 inline bla.txt" && |
| 399 | echo "data 4" && |
| 400 | echo "bla" && |
| 401 | # make every commit dangling by always |
| 402 | # rewinding the branch after each commit |
| 403 | echo "reset refs/heads/too-many-refs-$1" && |
| 404 | echo "from :$1" |
| 405 | done | git fast-import --export-marks=marks && |
| 406 | |
| 407 | # now assign tags to all the dangling commits we created above |
| 408 | tag=$(perl -e "print \"bla\" x 30") && |
| 409 | sed -e "s|^:\([^ ]*\) \(.*\)$|create refs/tags/$tag-\1 \2|" <marks >input && |
| 410 | git update-ref --stdin <input && |
| 411 | rm input |
| 412 | } |
| 413 | |
| 414 | test_expect_success 'create 2,000 tags in the repo' ' |
| 415 | git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/many-tags.git" && |
| 416 | ( |
| 417 | cd "$HTTPD_DOCUMENT_ROOT_PATH/many-tags.git" && |
| 418 | create_tags 1 2000 |
| 419 | ) |
| 420 | ' |
| 421 | |
| 422 | test_expect_success CMDLINE_LIMIT \ |
| 423 | 'clone the 2,000 tag repo to check OS command line overflow' ' |
| 424 | run_with_limited_cmdline git clone $HTTPD_URL/smart/many-tags.git too-many-refs && |
| 425 | ( |
| 426 | cd too-many-refs && |
| 427 | git for-each-ref refs/tags >actual && |
| 428 | test_line_count = 2000 actual |
| 429 | ) |
| 430 | ' |
| 431 | |
| 432 | test_expect_success 'large fetch-pack requests can be sent using chunked encoding' ' |
| 433 | GIT_TRACE_CURL=true git -c http.postbuffer=65536 \ |
| 434 | clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err && |
| 435 | { |
| 436 | test_have_prereq HTTP2 || |
| 437 | test_grep "^=> Send header: Transfer-Encoding: chunked" err |
| 438 | } |
| 439 | ' |
| 440 | |
| 441 | test_expect_success 'test allowreachablesha1inwant' ' |
| 442 | test_when_finished "rm -rf test_reachable.git" && |
| 443 | server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 444 | main_sha=$(git -C "$server" rev-parse refs/heads/main) && |
| 445 | git -C "$server" config uploadpack.allowreachablesha1inwant 1 && |
| 446 | |
| 447 | git init --bare test_reachable.git && |
| 448 | git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" && |
| 449 | git -C test_reachable.git fetch origin "$main_sha" |
| 450 | ' |
| 451 | |
| 452 | test_expect_success 'test allowreachablesha1inwant with unreachable' ' |
| 453 | test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" && |
| 454 | |
| 455 | #create unreachable sha |
| 456 | echo content >file2 && |
| 457 | git add file2 && |
| 458 | git commit -m two && |
| 459 | git push public HEAD:refs/heads/doomed && |
| 460 | git push public :refs/heads/doomed && |
| 461 | |
| 462 | server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 463 | main_sha=$(git -C "$server" rev-parse refs/heads/main) && |
| 464 | git -C "$server" config uploadpack.allowreachablesha1inwant 1 && |
| 465 | |
| 466 | git init --bare test_reachable.git && |
| 467 | git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" && |
| 468 | # Some protocol versions (e.g. 2) support fetching |
| 469 | # unadvertised objects, so restrict this test to v0. |
| 470 | test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \ |
| 471 | git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" |
| 472 | ' |
| 473 | |
| 474 | test_expect_success 'test allowanysha1inwant with unreachable' ' |
| 475 | test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" && |
| 476 | |
| 477 | #create unreachable sha |
| 478 | echo content >file2 && |
| 479 | git add file2 && |
| 480 | git commit -m two && |
| 481 | git push public HEAD:refs/heads/doomed && |
| 482 | git push public :refs/heads/doomed && |
| 483 | |
| 484 | server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && |
| 485 | main_sha=$(git -C "$server" rev-parse refs/heads/main) && |
| 486 | git -C "$server" config uploadpack.allowreachablesha1inwant 1 && |
| 487 | |
| 488 | git init --bare test_reachable.git && |
| 489 | git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" && |
| 490 | # Some protocol versions (e.g. 2) support fetching |
| 491 | # unadvertised objects, so restrict this test to v0. |
| 492 | test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \ |
| 493 | git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" && |
| 494 | |
| 495 | git -C "$server" config uploadpack.allowanysha1inwant 1 && |
| 496 | git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" |
| 497 | ' |
| 498 | |
| 499 | test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' ' |
| 500 | test_when_finished "rm -f tags" && |
| 501 | ( |
| 502 | cd "$HTTPD_DOCUMENT_ROOT_PATH/many-tags.git" && |
| 503 | create_tags 2001 50000 |
| 504 | ) && |
| 505 | git -C too-many-refs fetch -q --tags && |
| 506 | ( |
| 507 | cd "$HTTPD_DOCUMENT_ROOT_PATH/many-tags.git" && |
| 508 | create_tags 50001 100000 |
| 509 | ) && |
| 510 | git -C too-many-refs fetch -q --tags && |
| 511 | git -C too-many-refs for-each-ref refs/tags >tags && |
| 512 | test_line_count = 100000 tags |
| 513 | ' |
| 514 | |
| 515 | test_expect_success 'custom http headers' ' |
| 516 | test_must_fail git -c http.extraheader="x-magic-two: cadabra" \ |
| 517 | fetch "$HTTPD_URL/smart_headers/repo.git" && |
| 518 | git -c http.extraheader="x-magic-one: abra" \ |
| 519 | -c http.extraheader="x-magic-two: cadabra" \ |
| 520 | fetch "$HTTPD_URL/smart_headers/repo.git" && |
| 521 | git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && |
| 522 | git config -f .gitmodules submodule.sub.path sub && |
| 523 | git config -f .gitmodules submodule.sub.url \ |
| 524 | "$HTTPD_URL/smart_headers/repo.git" && |
| 525 | git submodule init sub && |
| 526 | test_must_fail git submodule update sub && |
| 527 | git -c http.extraheader="x-magic-one: abra" \ |
| 528 | -c http.extraheader="x-magic-two: cadabra" \ |
| 529 | submodule update sub |
| 530 | ' |
| 531 | |
| 532 | test_expect_success 'using fetch command in remote-curl updates refs' ' |
| 533 | SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" && |
| 534 | rm -rf "$SERVER" client && |
| 535 | |
| 536 | git init "$SERVER" && |
| 537 | test_commit -C "$SERVER" foo && |
| 538 | git -C "$SERVER" update-ref refs/heads/anotherbranch foo && |
| 539 | |
| 540 | git clone $HTTPD_URL/smart/twobranch client && |
| 541 | |
| 542 | test_commit -C "$SERVER" bar && |
| 543 | git -C client -c protocol.version=0 fetch && |
| 544 | |
| 545 | git -C "$SERVER" rev-parse main >expect && |
| 546 | git -C client rev-parse origin/main >actual && |
| 547 | test_cmp expect actual |
| 548 | ' |
| 549 | |
| 550 | test_expect_success 'fetch by SHA-1 without tag following' ' |
| 551 | SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" && |
| 552 | rm -rf "$SERVER" client && |
| 553 | |
| 554 | git init "$SERVER" && |
| 555 | test_commit -C "$SERVER" foo && |
| 556 | |
| 557 | git clone $HTTPD_URL/smart/server client && |
| 558 | |
| 559 | test_commit -C "$SERVER" bar && |
| 560 | git -C "$SERVER" rev-parse bar >bar_hash && |
| 561 | git -C client -c protocol.version=0 fetch \ |
| 562 | --no-tags origin $(cat bar_hash) |
| 563 | ' |
| 564 | |
| 565 | test_expect_success 'cookies are redacted by default' ' |
| 566 | rm -rf clone && |
| 567 | echo "Set-Cookie: Foo=1" >cookies && |
| 568 | echo "Set-Cookie: Bar=2" >>cookies && |
| 569 | GIT_TRACE_CURL=true \ |
| 570 | git -c "http.cookieFile=$(pwd)/cookies" clone \ |
| 571 | $HTTPD_URL/smart/repo.git clone 2>err && |
| 572 | test_grep -i "Cookie:.*Foo=<redacted>" err && |
| 573 | test_grep -i "Cookie:.*Bar=<redacted>" err && |
| 574 | test_grep ! -i "Cookie:.*Foo=1" err && |
| 575 | test_grep ! -i "Cookie:.*Bar=2" err |
| 576 | ' |
| 577 | |
| 578 | test_expect_success 'empty values of cookies are also redacted' ' |
| 579 | rm -rf clone && |
| 580 | echo "Set-Cookie: Foo=" >cookies && |
| 581 | GIT_TRACE_CURL=true \ |
| 582 | git -c "http.cookieFile=$(pwd)/cookies" clone \ |
| 583 | $HTTPD_URL/smart/repo.git clone 2>err && |
| 584 | test_grep -i "Cookie:.*Foo=<redacted>" err |
| 585 | ' |
| 586 | |
| 587 | test_expect_success 'GIT_TRACE_REDACT=0 disables cookie redaction' ' |
| 588 | rm -rf clone && |
| 589 | echo "Set-Cookie: Foo=1" >cookies && |
| 590 | echo "Set-Cookie: Bar=2" >>cookies && |
| 591 | GIT_TRACE_REDACT=0 GIT_TRACE_CURL=true \ |
| 592 | git -c "http.cookieFile=$(pwd)/cookies" clone \ |
| 593 | $HTTPD_URL/smart/repo.git clone 2>err && |
| 594 | test_grep -i "Cookie:.*Foo=1" err && |
| 595 | test_grep -i "Cookie:.*Bar=2" err |
| 596 | ' |
| 597 | |
| 598 | test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' ' |
| 599 | rm -rf clone && |
| 600 | GIT_TRACE_CURL=true \ |
| 601 | git clone $HTTPD_URL/smart/repo.git clone 2>err && |
| 602 | test_grep "=> Send data" err && |
| 603 | |
| 604 | rm -rf clone && |
| 605 | GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \ |
| 606 | git clone $HTTPD_URL/smart/repo.git clone 2>err && |
| 607 | test_grep ! "=> Send data" err |
| 608 | ' |
| 609 | |
| 610 | test_expect_success 'server-side error detected' ' |
| 611 | test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual && |
| 612 | test_grep "server-side error" actual |
| 613 | ' |
| 614 | |
| 615 | test_expect_success 'http auth remembers successful credentials' ' |
| 616 | rm -f .git-credentials && |
| 617 | test_config credential.helper store && |
| 618 | |
| 619 | # the first request prompts the user... |
| 620 | set_askpass user@host pass@host && |
| 621 | git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null && |
| 622 | expect_askpass both user%40host && |
| 623 | |
| 624 | # ...and the second one uses the stored value rather than |
| 625 | # prompting the user. |
| 626 | set_askpass bogus-user bogus-pass && |
| 627 | git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null && |
| 628 | expect_askpass none |
| 629 | ' |
| 630 | |
| 631 | test_expect_success 'http auth forgets bogus credentials' ' |
| 632 | # seed credential store with bogus values. In real life, |
| 633 | # this would probably come from a password which worked |
| 634 | # for a previous request. |
| 635 | rm -f .git-credentials && |
| 636 | test_config credential.helper store && |
| 637 | { |
| 638 | echo "url=$HTTPD_URL" && |
| 639 | echo "username=bogus" && |
| 640 | echo "password=bogus" |
| 641 | } | git credential approve && |
| 642 | |
| 643 | # we expect this to use the bogus values and fail, never even |
| 644 | # prompting the user... |
| 645 | set_askpass user@host pass@host && |
| 646 | test_must_fail git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null && |
| 647 | expect_askpass none && |
| 648 | |
| 649 | # ...but now we should have forgotten the bad value, causing |
| 650 | # us to prompt the user again. |
| 651 | set_askpass user@host pass@host && |
| 652 | git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null && |
| 653 | expect_askpass both user%40host |
| 654 | ' |
| 655 | |
| 656 | test_expect_success 'client falls back from v2 to v0 to match server' ' |
| 657 | GIT_TRACE_PACKET=$PWD/trace \ |
| 658 | GIT_TEST_PROTOCOL_VERSION=2 \ |
| 659 | git clone $HTTPD_URL/smart_v0/repo.git repo-v0 && |
| 660 | # check for v0; there the HEAD symref is communicated in the capability |
| 661 | # line; v2 uses a different syntax on each ref advertisement line |
| 662 | test_grep symref=HEAD:refs/heads/ trace |
| 663 | ' |
| 664 | |
| 665 | test_expect_success 'create empty http-accessible SHA-256 repository' ' |
| 666 | mkdir "$HTTPD_DOCUMENT_ROOT_PATH/sha256.git" && |
| 667 | (cd "$HTTPD_DOCUMENT_ROOT_PATH/sha256.git" && |
| 668 | git --bare init --object-format=sha256 |
| 669 | ) |
| 670 | ' |
| 671 | |
| 672 | test_expect_success 'clone empty SHA-256 repository with protocol v2' ' |
| 673 | rm -fr sha256 && |
| 674 | echo sha256 >expected && |
| 675 | git -c protocol.version=2 clone "$HTTPD_URL/smart/sha256.git" && |
| 676 | git -C sha256 rev-parse --show-object-format >actual && |
| 677 | test_cmp actual expected && |
| 678 | git ls-remote "$HTTPD_URL/smart/sha256.git" >actual && |
| 679 | test_must_be_empty actual |
| 680 | ' |
| 681 | |
| 682 | test_expect_success 'clone empty SHA-256 repository with protocol v0' ' |
| 683 | rm -fr sha256 && |
| 684 | echo sha256 >expected && |
| 685 | GIT_TRACE=1 GIT_TRACE_PACKET=1 git -c protocol.version=0 clone "$HTTPD_URL/smart/sha256.git" && |
| 686 | git -C sha256 rev-parse --show-object-format >actual && |
| 687 | test_cmp actual expected && |
| 688 | git ls-remote "$HTTPD_URL/smart/sha256.git" >actual && |
| 689 | test_must_be_empty actual |
| 690 | ' |
| 691 | |
| 692 | test_expect_success 'passing hostname resolution information works' ' |
| 693 | BOGUS_HOST=gitbogusexamplehost.invalid && |
| 694 | BOGUS_HTTPD_URL=$HTTPD_PROTO://$BOGUS_HOST:$LIB_HTTPD_PORT && |
| 695 | git -c "http.curloptResolve=$BOGUS_HOST:$LIB_HTTPD_PORT:127.0.0.1" ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null |
| 696 | ' |
| 697 | |
| 698 | # here user%40host is the URL-encoded version of user@host, |
| 699 | # which is our intentionally-odd username to catch parsing errors |
| 700 | url_user=$HTTPD_URL_USER/auth/smart/repo.git |
| 701 | url_userpass=$HTTPD_URL_USER_PASS/auth/smart/repo.git |
| 702 | url_userblank=$HTTPD_PROTO://user%40host:@$HTTPD_DEST/auth/smart/repo.git |
| 703 | message="URL .*:<redacted>@.* uses plaintext credentials" |
| 704 | |
| 705 | test_expect_success 'clone warns or fails when using username:password' ' |
| 706 | test_when_finished "rm -rf attempt*" && |
| 707 | |
| 708 | git -c transfer.credentialsInUrl=allow \ |
| 709 | clone $url_userpass attempt1 2>err && |
| 710 | test_grep ! "$message" err && |
| 711 | |
| 712 | git -c transfer.credentialsInUrl=warn \ |
| 713 | clone $url_userpass attempt2 2>err && |
| 714 | grep "warning: $message" err >warnings && |
| 715 | test_line_count -ge 1 warnings && |
| 716 | |
| 717 | test_must_fail git -c transfer.credentialsInUrl=die \ |
| 718 | clone $url_userpass attempt3 2>err && |
| 719 | grep "fatal: $message" err >warnings && |
| 720 | test_line_count -ge 1 warnings && |
| 721 | |
| 722 | test_must_fail git -c transfer.credentialsInUrl=die \ |
| 723 | clone $url_userblank attempt4 2>err && |
| 724 | grep "fatal: $message" err >warnings && |
| 725 | test_line_count -ge 1 warnings |
| 726 | ' |
| 727 | |
| 728 | test_expect_success 'clone does not detect username:password when it is https://username@domain:port/' ' |
| 729 | test_when_finished "rm -rf attempt1" && |
| 730 | |
| 731 | # we are relying on lib-httpd for url construction, so document our |
| 732 | # assumptions |
| 733 | case "$HTTPD_URL_USER" in |
| 734 | *:[0-9]*) : ok ;; |
| 735 | *) BUG "httpd url does not have port: $HTTPD_URL_USER" |
| 736 | esac && |
| 737 | |
| 738 | git -c transfer.credentialsInUrl=warn clone $url_user attempt1 2>err && |
| 739 | test_grep ! "uses plaintext credentials" err |
| 740 | ' |
| 741 | |
| 742 | test_expect_success 'fetch warns or fails when using username:password' ' |
| 743 | git -c transfer.credentialsInUrl=allow fetch $url_userpass 2>err && |
| 744 | test_grep ! "$message" err && |
| 745 | |
| 746 | git -c transfer.credentialsInUrl=warn fetch $url_userpass 2>err && |
| 747 | grep "warning: $message" err >warnings && |
| 748 | test_line_count -ge 1 warnings && |
| 749 | |
| 750 | test_must_fail git -c transfer.credentialsInUrl=die \ |
| 751 | fetch $url_userpass 2>err && |
| 752 | grep "fatal: $message" err >warnings && |
| 753 | test_line_count -ge 1 warnings && |
| 754 | |
| 755 | test_must_fail git -c transfer.credentialsInUrl=die \ |
| 756 | fetch $url_userblank 2>err && |
| 757 | grep "fatal: $message" err >warnings && |
| 758 | test_line_count -ge 1 warnings |
| 759 | ' |
| 760 | |
| 761 | |
| 762 | test_expect_success 'push warns or fails when using username:password' ' |
| 763 | git -c transfer.credentialsInUrl=allow push $url_userpass 2>err && |
| 764 | test_grep ! "$message" err && |
| 765 | |
| 766 | git -c transfer.credentialsInUrl=warn push $url_userpass 2>err && |
| 767 | grep "warning: $message" err >warnings && |
| 768 | |
| 769 | test_must_fail git -c transfer.credentialsInUrl=die \ |
| 770 | push $url_userpass 2>err && |
| 771 | grep "fatal: $message" err >warnings && |
| 772 | test_line_count -ge 1 warnings |
| 773 | ' |
| 774 | |
| 775 | test_expect_success 'no empty path components' ' |
| 776 | # In the URL, add a trailing slash, and see if git appends yet another |
| 777 | # slash. |
| 778 | git clone $HTTPD_URL/smart/repo.git/ clone-with-slash && |
| 779 | |
| 780 | strip_access_log >log && |
| 781 | test_grep ! "//" log |
| 782 | ' |
| 783 | |
| 784 | test_expect_success 'tag following always works over v0 http' ' |
| 785 | upstream=$HTTPD_DOCUMENT_ROOT_PATH/tags && |
| 786 | git init "$upstream" && |
| 787 | ( |
| 788 | cd "$upstream" && |
| 789 | git commit --allow-empty -m base && |
| 790 | git tag not-annotated && |
| 791 | git tag -m foo annotated |
| 792 | ) && |
| 793 | git init tags && |
| 794 | git -C tags -c protocol.version=0 \ |
| 795 | fetch --depth 1 $HTTPD_URL/smart/tags \ |
| 796 | refs/tags/annotated:refs/tags/annotated && |
| 797 | git -C "$upstream" for-each-ref refs/tags >expect && |
| 798 | git -C tags for-each-ref >actual && |
| 799 | test_cmp expect actual |
| 800 | ' |
| 801 | |
| 802 | test_expect_success 'ls-remote outside repo does not segfault with fetch refspec' ' |
| 803 | nongit git \ |
| 804 | -c remote.origin.url="$HTTPD_URL/smart/repo.git" \ |
| 805 | -c remote.origin.fetch=anything \ |
| 806 | ls-remote origin |
| 807 | ' |
| 808 | |
| 809 | test_done |