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