serve: reject commands used as capabilities

Our table of v2 "capabilities" contains everything we might tell the client we support. But there are differences in how we expect the client to respond. Some of the entries are true capabilities (i.e., we expect the client to say "yes, I support this"), and some are ones we expect them to send as commands (with "command=ls-refs" or similar). When we receive a capability used as a command, we complain about that. But when we receive a command used as a capability (e.g., just "ls-refs" in a pkt-line by itself), we silently ignore it. This isn't really hurting anything (clients shouldn't send it, and we'll ignore it), but we can tighten up the protocol to match what we expect to happen. There are two new tests here. The first one checks a capability used as a command, which already passes. The second tests a command as a capability, which this patch fixes. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 15, 2021 at 14:36 UTC 0ab7eeccd9aea668819288c086dcdf57ca14a026
2 files changed +22 -1
serve.c
+1 -1
@@ -201,7 +201,7 @@ static int receive_client_capability(const char *key)
201 const char *value;
202 const struct protocol_capability *c = get_capability(key, &value);
203
204 - if (!c || !c->advertise(the_repository, NULL))
204 + if (!c || c->command || !c->advertise(the_repository, NULL))
205 return 0;
206
207 if (c->receive)
t/t5701-git-serve.sh
+21
@@ -72,6 +72,27 @@ test_expect_success 'request invalid command' '
72 test_i18ngrep "invalid command" err
73 '
74
75 +test_expect_success 'request capability as command' '
76 + test-tool pkt-line pack >in <<-EOF &&
77 + command=agent
78 + object-format=$(test_oid algo)
79 + 0000
80 + EOF
81 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
82 + grep invalid.command.*agent err
83 +'
84 +
85 +test_expect_success 'request command as capability' '
86 + test-tool pkt-line pack >in <<-EOF &&
87 + command=ls-refs
88 + object-format=$(test_oid algo)
89 + fetch
90 + 0000
91 + EOF
92 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
93 + grep unknown.capability err
94 +'
95 +
96 test_expect_success 'requested command is command=value' '
97 test-tool pkt-line pack >in <<-EOF &&
98 command=ls-refs=whatever