Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 7927f51

Browse files
committed
Merge branch 'sp/smart-http-content-type-check' into maint
* sp/smart-http-content-type-check: http_request: reset "type" strbuf before adding t5551: fix expected error output Verify Content-Type from smart HTTP servers
2 parents d49f9f1 + 3443db5 commit 7927f51

File tree

8 files changed

+59
-17
lines changed

8 files changed

+59
-17
lines changed

http-push.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ static int remote_exists(const char *path)
15601560

15611561
sprintf(url, "%s%s", repo->url, path);
15621562

1563-
switch (http_get_strbuf(url, NULL, 0)) {
1563+
switch (http_get_strbuf(url, NULL, NULL, 0)) {
15641564
case HTTP_OK:
15651565
ret = 1;
15661566
break;
@@ -1584,7 +1584,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
15841584
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
15851585
sprintf(url, "%s%s", repo->url, path);
15861586

1587-
if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1587+
if (http_get_strbuf(url, NULL, &buffer, 0) != HTTP_OK)
15881588
die("Couldn't get %s for remote symref\n%s", url,
15891589
curl_errorstr);
15901590
free(url);

http.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ int handle_curl_result(struct slot_results *results)
788788
#define HTTP_REQUEST_STRBUF 0
789789
#define HTTP_REQUEST_FILE 1
790790

791-
static int http_request(const char *url, void *result, int target, int options)
791+
static int http_request(const char *url, struct strbuf *type,
792+
void *result, int target, int options)
792793
{
793794
struct active_request_slot *slot;
794795
struct slot_results results;
@@ -838,24 +839,37 @@ static int http_request(const char *url, void *result, int target, int options)
838839
ret = HTTP_START_FAILED;
839840
}
840841

842+
if (type) {
843+
char *t;
844+
strbuf_reset(type);
845+
curl_easy_getinfo(slot->curl, CURLINFO_CONTENT_TYPE, &t);
846+
if (t)
847+
strbuf_addstr(type, t);
848+
}
849+
841850
curl_slist_free_all(headers);
842851
strbuf_release(&buf);
843852

844853
return ret;
845854
}
846855

847-
static int http_request_reauth(const char *url, void *result, int target,
856+
static int http_request_reauth(const char *url,
857+
struct strbuf *type,
858+
void *result, int target,
848859
int options)
849860
{
850-
int ret = http_request(url, result, target, options);
861+
int ret = http_request(url, type, result, target, options);
851862
if (ret != HTTP_REAUTH)
852863
return ret;
853-
return http_request(url, result, target, options);
864+
return http_request(url, type, result, target, options);
854865
}
855866

856-
int http_get_strbuf(const char *url, struct strbuf *result, int options)
867+
int http_get_strbuf(const char *url,
868+
struct strbuf *type,
869+
struct strbuf *result, int options)
857870
{
858-
return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
871+
return http_request_reauth(url, type, result,
872+
HTTP_REQUEST_STRBUF, options);
859873
}
860874

861875
/*
@@ -878,7 +892,7 @@ static int http_get_file(const char *url, const char *filename, int options)
878892
goto cleanup;
879893
}
880894

881-
ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
895+
ret = http_request_reauth(url, NULL, result, HTTP_REQUEST_FILE, options);
882896
fclose(result);
883897

884898
if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
@@ -904,7 +918,7 @@ int http_fetch_ref(const char *base, struct ref *ref)
904918
int ret = -1;
905919

906920
url = quote_ref_url(base, ref->name);
907-
if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
921+
if (http_get_strbuf(url, NULL, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
908922
strbuf_rtrim(&buffer);
909923
if (buffer.len == 40)
910924
ret = get_sha1_hex(buffer.buf, ref->old_sha1);
@@ -997,7 +1011,7 @@ int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
9971011
strbuf_addstr(&buf, "objects/info/packs");
9981012
url = strbuf_detach(&buf, NULL);
9991013

1000-
ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
1014+
ret = http_get_strbuf(url, NULL, &buf, HTTP_NO_CACHE);
10011015
if (ret != HTTP_OK)
10021016
goto cleanup;
10031017

http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extern char *get_remote_object_url(const char *url, const char *hex,
132132
*
133133
* If the result pointer is NULL, a HTTP HEAD request is made instead of GET.
134134
*/
135-
int http_get_strbuf(const char *url, struct strbuf *result, int options);
135+
int http_get_strbuf(const char *url, struct strbuf *content_type, struct strbuf *result, int options);
136136

137137
/*
138138
* Prints an error message using error() containing url and curl_errorstr,

remote-curl.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ static void free_discovery(struct discovery *d)
9292

9393
static struct discovery* discover_refs(const char *service)
9494
{
95+
struct strbuf exp = STRBUF_INIT;
96+
struct strbuf type = STRBUF_INIT;
9597
struct strbuf buffer = STRBUF_INIT;
9698
struct discovery *last = last_discovery;
9799
char *refs_url;
@@ -113,7 +115,7 @@ static struct discovery* discover_refs(const char *service)
113115
}
114116
refs_url = strbuf_detach(&buffer, NULL);
115117

116-
http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
118+
http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
117119
switch (http_ret) {
118120
case HTTP_OK:
119121
break;
@@ -132,17 +134,20 @@ static struct discovery* discover_refs(const char *service)
132134
last->buf_alloc = strbuf_detach(&buffer, &last->len);
133135
last->buf = last->buf_alloc;
134136

135-
if (maybe_smart && 5 <= last->len && last->buf[4] == '#') {
136-
/* smart HTTP response; validate that the service
137+
strbuf_addf(&exp, "application/x-%s-advertisement", service);
138+
if (maybe_smart &&
139+
(5 <= last->len && last->buf[4] == '#') &&
140+
!strbuf_cmp(&exp, &type)) {
141+
/*
142+
* smart HTTP response; validate that the service
137143
* pkt-line matches our request.
138144
*/
139-
struct strbuf exp = STRBUF_INIT;
140-
141145
if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
142146
die("%s has invalid packet header", refs_url);
143147
if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
144148
strbuf_setlen(&buffer, buffer.len - 1);
145149

150+
strbuf_reset(&exp);
146151
strbuf_addf(&exp, "# service=%s", service);
147152
if (strbuf_cmp(&exp, &buffer))
148153
die("invalid server response; got '%s'", buffer.buf);
@@ -160,6 +165,8 @@ static struct discovery* discover_refs(const char *service)
160165
}
161166

162167
free(refs_url);
168+
strbuf_release(&exp);
169+
strbuf_release(&type);
163170
strbuf_release(&buffer);
164171
last_discovery = last;
165172
return last;

t/lib-httpd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fi
8080
prepare_httpd() {
8181
mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
8282
cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
83+
cp "$TEST_PATH"/broken-smart-http.sh "$HTTPD_ROOT_PATH"
8384

8485
ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
8586

t/lib-httpd/apache.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ Alias /auth/dumb/ www/auth/dumb/
6262
SetEnv GIT_COMMITTER_EMAIL [email protected]
6363
</LocationMatch>
6464
ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
65+
ScriptAlias /broken_smart/ broken-smart-http.sh/
6566
<Directory ${GIT_EXEC_PATH}>
6667
Options FollowSymlinks
6768
</Directory>
69+
<Files broken-smart-http.sh>
70+
Options ExecCGI
71+
</Files>
6872
<Files ${GIT_EXEC_PATH}/git-http-backend>
6973
Options ExecCGI
7074
</Files>

t/lib-httpd/broken-smart-http.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
printf "Content-Type: text/%s\n" "html"
3+
echo
4+
printf "%s\n" "001e# service=git-upload-pack"
5+
printf "%s" "0000"
6+
printf "%s%c%s%s\n" \
7+
"00a58681d9f286a48b08f37b3a095330da16689e3693 HEAD" \
8+
0 \
9+
" include-tag multi_ack_detailed multi_ack ofs-delta" \
10+
" side-band side-band-64k thin-pack no-progress shallow no-done "
11+
printf "%s" "0000"

t/t5551-http-fetch.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ test_expect_success 'GIT_SMART_HTTP can disable smart http' '
157157
test_must_fail git fetch)
158158
'
159159

160+
test_expect_success 'invalid Content-Type rejected' '
161+
test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual
162+
grep "not valid:" actual
163+
'
164+
160165
test -n "$GIT_TEST_LONG" && test_set_prereq EXPENSIVE
161166

162167
test_expect_success EXPENSIVE 'create 50,000 tags in the repo' '

0 commit comments

Comments
 (0)