Skip to content

Commit 0d83d53

Browse files
chriscoolgitster
authored andcommitted
promisor-remote: allow a client to check fields
A previous commit allowed a server to pass additional fields through the "promisor-remote" protocol capability after the "name" and "url" fields, specifically the "partialCloneFilter" and "token" fields. Let's make it possible for a client to check if these fields match what it expects before accepting a promisor remote. We allow this by introducing a new "promisor.checkFields" configuration variable. It should contain a comma or space separated list of fields that will be checked. By limiting the protocol to specific well-defined fields, we ensure both server and client have a shared understanding of field semantics and usage. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6f37df2 commit 0d83d53

File tree

3 files changed

+155
-8
lines changed

3 files changed

+155
-8
lines changed

Documentation/config/promisor.adoc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,43 @@ promisor.acceptFromServer::
5050
lazily fetchable from this promisor remote from its responses
5151
to "fetch" and "clone" requests from the client. Name and URL
5252
comparisons are case sensitive. See linkgit:gitprotocol-v2[5].
53+
54+
promisor.checkFields::
55+
A comma or space separated list of additional remote related
56+
field names. A client will check if the values of these fields
57+
transmitted by a server correspond to the values of these
58+
fields in its own configuration before accepting a promisor
59+
remote. Currently, "partialCloneFilter" and "token" are the
60+
only supported field names.
61+
+
62+
If one of these field names (e.g., "token") is being checked for an
63+
advertised promisor remote (e.g., "foo"), three conditions must be met
64+
for the check of this specific field to pass:
65+
+
66+
1. The corresponding local configuration (e.g., `remote.foo.token`)
67+
must be set.
68+
2. The server must advertise the "token" field for remote "foo".
69+
3. The value of the locally configured `remote.foo.token` must exactly
70+
match the value advertised by the server for the "token" field.
71+
+
72+
If any of these conditions is not met for any field name listed in
73+
`promisor.checkFields`, the advertised remote "foo" will be rejected.
74+
+
75+
For the "partialCloneFilter" field, this allows the client to ensure
76+
that the server's filter matches what it expects locally, preventing
77+
inconsistencies in filtering behavior. For the "token" field, this can
78+
be used to verify that authentication credentials match expected
79+
values.
80+
+
81+
Field names are compared case-insensitively. Field values are compared
82+
case-sensitively.
83+
+
84+
The "name" and "url" fields are always checked according to the
85+
`promisor.acceptFromServer` policy, independently of this setting.
86+
+
87+
The field names and values should be passed by the server through the
88+
"promisor-remote" capability by using the `promisor.sendFields` config
89+
variable. The fields will be checked only if the
90+
`promisor.acceptFromServer` config variable is not set to "None". If
91+
set to "None", this config variable will have no effect. See
92+
linkgit:gitprotocol-v2[5].

promisor-remote.c

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,20 @@ static struct string_list *fields_sent(void)
382382
return &fields_list;
383383
}
384384

385+
static struct string_list *fields_checked(void)
386+
{
387+
static struct string_list fields_list = STRING_LIST_INIT_NODUP;
388+
static int initialized = 0;
389+
390+
if (!initialized) {
391+
fields_list.cmp = strcasecmp;
392+
fields_from_config(&fields_list, "promisor.checkFields");
393+
initialized = 1;
394+
}
395+
396+
return &fields_list;
397+
}
398+
385399
/*
386400
* Struct for promisor remotes involved in the "promisor-remote"
387401
* protocol capability.
@@ -527,6 +541,61 @@ enum accept_promisor {
527541
ACCEPT_ALL
528542
};
529543

544+
static int match_field_against_config(const char *field, const char *value,
545+
struct promisor_info *config_info)
546+
{
547+
if (config_info->filter && !strcasecmp(field, promisor_field_filter))
548+
return !strcmp(config_info->filter, value);
549+
else if (config_info->token && !strcasecmp(field, promisor_field_token))
550+
return !strcmp(config_info->token, value);
551+
552+
return 0;
553+
}
554+
555+
static int all_fields_match(struct promisor_info *advertised,
556+
struct string_list *config_info,
557+
int in_list)
558+
{
559+
struct string_list* fields = fields_checked();
560+
struct string_list_item *item_checked;
561+
562+
for_each_string_list_item(item_checked, fields) {
563+
int match = 0;
564+
const char *field = item_checked->string;
565+
const char *value = NULL;
566+
struct string_list_item *item;
567+
568+
if (!strcasecmp(field, promisor_field_filter))
569+
value = advertised->filter;
570+
else if (!strcasecmp(field, promisor_field_token))
571+
value = advertised->token;
572+
573+
if (!value)
574+
return 0;
575+
576+
if (in_list) {
577+
for_each_string_list_item(item, config_info) {
578+
struct promisor_info *p = item->util;
579+
if (match_field_against_config(field, value, p)) {
580+
match = 1;
581+
break;
582+
}
583+
}
584+
} else {
585+
item = string_list_lookup(config_info, advertised->name);
586+
if (item) {
587+
struct promisor_info *p = item->util;
588+
match = match_field_against_config(field, value, p);
589+
}
590+
}
591+
592+
if (!match)
593+
return 0;
594+
}
595+
596+
return 1;
597+
}
598+
530599
static int should_accept_remote(enum accept_promisor accept,
531600
struct promisor_info *advertised,
532601
struct string_list *config_info)
@@ -537,7 +606,7 @@ static int should_accept_remote(enum accept_promisor accept,
537606
const char *remote_url = advertised->url;
538607

539608
if (accept == ACCEPT_ALL)
540-
return 1;
609+
return all_fields_match(advertised, config_info, 1);
541610

542611
/* Get config info for that promisor remote */
543612
item = string_list_lookup(config_info, remote_name);
@@ -549,7 +618,7 @@ static int should_accept_remote(enum accept_promisor accept,
549618
p = item->util;
550619

551620
if (accept == ACCEPT_KNOWN_NAME)
552-
return 1;
621+
return all_fields_match(advertised, config_info, 0);
553622

554623
if (accept != ACCEPT_KNOWN_URL)
555624
BUG("Unhandled 'enum accept_promisor' value '%d'", accept);
@@ -564,7 +633,7 @@ static int should_accept_remote(enum accept_promisor accept,
564633
remote_name);
565634

566635
if (!strcmp(p->url, remote_url))
567-
return 1;
636+
return all_fields_match(advertised, config_info, 0);
568637

569638
warning(_("known remote named '%s' but with URL '%s' instead of '%s'"),
570639
remote_name, p->url, remote_url);
@@ -596,6 +665,10 @@ static struct promisor_info *parse_one_advertised_remote(struct strbuf *remote_i
596665
info->name = value;
597666
else if (!strcmp(elem, "url"))
598667
info->url = value;
668+
else if (!strcasecmp(elem, promisor_field_filter))
669+
info->filter = value;
670+
else if (!strcasecmp(elem, promisor_field_token))
671+
info->token = value;
599672
else
600673
free(value);
601674
}
@@ -638,11 +711,6 @@ static void filter_promisor_remote(struct repository *repo,
638711
if (accept == ACCEPT_NONE)
639712
return;
640713

641-
if (accept != ACCEPT_ALL) {
642-
promisor_config_info_list(repo, &config_info, NULL);
643-
string_list_sort(&config_info);
644-
}
645-
646714
/* Parse remote info received */
647715

648716
remotes = strbuf_split_str(info, ';', 0);
@@ -657,6 +725,11 @@ static void filter_promisor_remote(struct repository *repo,
657725
if (!advertised)
658726
continue;
659727

728+
if (!config_info.nr) {
729+
promisor_config_info_list(repo, &config_info, fields_checked());
730+
string_list_sort(&config_info);
731+
}
732+
660733
if (should_accept_remote(accept, advertised, &config_info))
661734
strvec_push(accepted, advertised->name);
662735

t/t5710-promisor-remote-capability.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,40 @@ test_expect_success "clone with promisor.sendFields" '
326326
check_missing_objects server 1 "$oid"
327327
'
328328

329+
test_expect_success "clone with promisor.checkFields" '
330+
git -C server config promisor.advertise true &&
331+
test_when_finished "rm -rf client" &&
332+
333+
git -C server remote add otherLop "https://invalid.invalid" &&
334+
git -C server config remote.otherLop.token "fooBar" &&
335+
git -C server config remote.otherLop.stuff "baz" &&
336+
git -C server config remote.otherLop.partialCloneFilter "blob:limit=10k" &&
337+
test_when_finished "git -C server remote remove otherLop" &&
338+
test_config -C server promisor.sendFields "partialCloneFilter, token" &&
339+
test_when_finished "rm trace" &&
340+
341+
# Clone from server to create a client
342+
GIT_TRACE_PACKET="$(pwd)/trace" GIT_NO_LAZY_FETCH=0 git clone \
343+
-c remote.lop.promisor=true \
344+
-c remote.lop.fetch="+refs/heads/*:refs/remotes/lop/*" \
345+
-c remote.lop.url="file://$(pwd)/lop" \
346+
-c remote.lop.partialCloneFilter="blob:none" \
347+
-c promisor.acceptfromserver=All \
348+
-c promisor.checkFields=partialcloneFilter \
349+
--no-local --filter="blob:limit=5k" server client &&
350+
351+
# Check that fields are properly transmitted
352+
ENCODED_URL=$(echo "file://$(pwd)/lop" | sed -e "s/ /%20/g") &&
353+
PR1="name=lop,url=$ENCODED_URL,partialCloneFilter=blob:none" &&
354+
PR2="name=otherLop,url=https://invalid.invalid,partialCloneFilter=blob:limit=10k,token=fooBar" &&
355+
test_grep "clone< promisor-remote=$PR1;$PR2" trace &&
356+
test_grep "clone> promisor-remote=lop" trace &&
357+
test_grep ! "clone> promisor-remote=lop;otherLop" trace &&
358+
359+
# Check that the largest object is still missing on the server
360+
check_missing_objects server 1 "$oid"
361+
'
362+
329363
test_expect_success "clone with promisor.advertise set to 'true' but don't delete the client" '
330364
git -C server config promisor.advertise true &&
331365

0 commit comments

Comments
 (0)