Skip to content

Commit 34066f0

Browse files
peffgitster
authored andcommitted
fetch: do not consider peeled tags as advertised tips
Our filter_refs() function accidentally considers the target of a peeled tag to be advertised by the server, even though upload-pack on the server side does not consider it so. This can result in the client making a bogus fetch to the server, which will end with the server complaining "not our ref". Whereas the correct behavior is for the client to notice that the server will not allow the request and error out immediately. So as bugs go, this is not very serious (the outcome is the same either way -- the fetch fails). But it's worth making the logic here correct and consistent with other related cases (e.g., fetching an oid that the server did not mention at all). The crux of the issue comes from fdb69d3 (fetch-pack: always allow fetching of literal SHA1s, 2017-05-15). After that, the strategy of filter_refs() is basically: - for each advertised ref, try to match it with a "sought" ref provided by the user. Skip any malformed refs (which includes peeled values like "refs/tags/foo^{}"), and place any unmatched items onto the unmatched list. - if there are unmatched sought refs, then put all of the advertised tips into an oidset, including the unmatched ones. - for each sought ref, see if it's in the oidset, in which case it's legal for us to ask the server for it The problem is in the second step. Our list of unmatched refs includes the peeled refs, even though upload-pack does not allow them to be directly fetched. So the simplest fix would be to exclude them during that step. However, we can observe that the unmatched list isn't used for anything else, and is freed at the end. We can just free those malformed refs immediately. That saves us having to check each ref a second time to see if it's malformed. Note that this code only kicks in when "strict" is in effect. I.e., if we are using the v0 protocol and uploadpack.allowReachableSHA1InWant is not in effect. With v2, all oids are allowed, and we do not bother creating or consulting the oidset at all. To future-proof our test against the upcoming GIT_TEST_PROTOCOL_VERSION flag, we'll manually mark it as a v0-only test. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1027186 commit 34066f0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

fetch-pack.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,14 @@ static void filter_refs(struct fetch_pack_args *args,
573573
next = ref->next;
574574

575575
if (starts_with(ref->name, "refs/") &&
576-
check_refname_format(ref->name, 0))
577-
; /* trash */
578-
else {
576+
check_refname_format(ref->name, 0)) {
577+
/*
578+
* trash or a peeled value; do not even add it to
579+
* unmatched list
580+
*/
581+
free_one_ref(ref);
582+
continue;
583+
} else {
579584
while (i < nr_sought) {
580585
int cmp = strcmp(ref->name, sought[i]->name);
581586
if (cmp < 0)

t/t5516-fetch-push.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,17 @@ test_expect_success 'fetch follows tags by default' '
12731273
test_cmp expect actual
12741274
'
12751275

1276+
test_expect_success 'peeled advertisements are not considered ref tips' '
1277+
mk_empty testrepo &&
1278+
git -C testrepo commit --allow-empty -m one &&
1279+
git -C testrepo commit --allow-empty -m two &&
1280+
git -C testrepo tag -m foo mytag HEAD^ &&
1281+
oid=$(git -C testrepo rev-parse mytag^{commit}) &&
1282+
test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
1283+
git fetch testrepo $oid 2>err &&
1284+
test_i18ngrep "Server does not allow request for unadvertised object" err
1285+
'
1286+
12761287
test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
12771288
mk_test testrepo heads/master &&
12781289
rm -fr src dst &&

0 commit comments

Comments
 (0)