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