Skip to content

Commit 050ef36

Browse files
peffjrn
authored andcommitted
remote-curl: rewrite base url from info/refs redirects
For efficiency and security reasons, an earlier commit in this series taught http_get_* to re-write the base url based on redirections we saw while making a specific request. This commit wires that option into the info/refs request, meaning that a redirect from http://example.com/foo.git/info/refs to https://example.com/bar.git/info/refs will behave as if "https://example.com/bar.git" had been provided to git in the first place. The tests bear some explanation. We introduce two new hierearchies into the httpd test config: 1. Requests to /smart-redir-limited will work only for the initial info/refs request, but not any subsequent requests. As a result, we can confirm whether the client is re-rooting its requests after the initial contact, since otherwise it will fail (it will ask for "repo.git/git-upload-pack", which is not redirected). 2. Requests to smart-redir-auth will redirect, and require auth after the redirection. Since we are using the redirected base for further requests, we also update the credential struct, in order not to mislead the user (or credential helpers) about which credential is needed. We can therefore check the GIT_ASKPASS prompts to make sure we are prompting for the new location. Because we have neither multiple servers nor https support in our test setup, we can only redirect between paths, meaning we need to turn on credential.useHttpPath to see the difference. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent b227bbc commit 050ef36

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

remote-curl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
188188
struct strbuf type = STRBUF_INIT;
189189
struct strbuf buffer = STRBUF_INIT;
190190
struct strbuf refs_url = STRBUF_INIT;
191+
struct strbuf effective_url = STRBUF_INIT;
191192
struct discovery *last = last_discovery;
192193
int http_ret, maybe_smart = 0;
193194
struct http_get_options options;
@@ -209,6 +210,8 @@ static struct discovery* discover_refs(const char *service, int for_push)
209210

210211
memset(&options, 0, sizeof(options));
211212
options.content_type = &type;
213+
options.effective_url = &effective_url;
214+
options.base_url = &url;
212215
options.no_cache = 1;
213216
options.keep_error = 1;
214217

@@ -268,6 +271,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
268271
strbuf_release(&refs_url);
269272
strbuf_release(&exp);
270273
strbuf_release(&type);
274+
strbuf_release(&effective_url);
271275
strbuf_release(&buffer);
272276
last_discovery = last;
273277
return last;

t/lib-httpd.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ set_askpass() {
187187
}
188188

189189
expect_askpass() {
190-
dest=$HTTPD_DEST
190+
dest=$HTTPD_DEST${3+/$3}
191+
191192
{
192193
case "$1" in
193194
none)

t/lib-httpd/apache.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ ScriptAlias /broken_smart/ broken-smart-http.sh/
102102
RewriteEngine on
103103
RewriteRule ^/smart-redir-perm/(.*)$ /smart/$1 [R=301]
104104
RewriteRule ^/smart-redir-temp/(.*)$ /smart/$1 [R=302]
105+
RewriteRule ^/smart-redir-auth/(.*)$ /auth/smart/$1 [R=301]
106+
RewriteRule ^/smart-redir-limited/(.*)/info/refs$ /smart/$1/info/refs [R=301]
105107

106108
<IfDefine SSL>
107109
LoadModule ssl_module modules/mod_ssl.so

t/t5551-http-fetch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ test_expect_success 'follow redirects (302)' '
113113
git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
114114
'
115115

116+
test_expect_success 'redirects re-root further requests' '
117+
git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
118+
'
119+
116120
test_expect_success 'clone from password-protected repository' '
117121
echo two >expect &&
118122
set_askpass user@host &&
@@ -146,6 +150,13 @@ test_expect_success 'no-op half-auth fetch does not require a password' '
146150
expect_askpass none
147151
'
148152

153+
test_expect_success 'redirects send auth to new location' '
154+
set_askpass user@host &&
155+
git -c credential.useHttpPath=true \
156+
clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
157+
expect_askpass both user@host auth/smart/repo.git
158+
'
159+
149160
test_expect_success 'disable dumb http on server' '
150161
git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
151162
config http.getanyfile false

0 commit comments

Comments
 (0)