From 15fc06c8bfd66d5cecb869b1824d5557e54fc806 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 10:37:03 +0000 Subject: [PATCH] a2a: the Host header goes into the agent card escaped The card is built with sp_buf_printf and put the Host header straight into the url string. A Host of 127.0.0.1","evil":"x produced "supportedInterfaces":[{"url":"http://127.0.0.1","evil":"x/a2a",... which is valid JSON with an extra key of the caller's choosing in an object that ought to hold only the three interface fields. Every other client value in this file already goes through sp_json_escape (see put_ids); this one did not. Test: tests/test_a2a_server.sh asks for the card with that Host and asserts supportedInterfaces[0] carries exactly url/protocolBinding/protocolVersion and a url ending in /a2a. Red before (extra key), green after; live curl against the built binary shows the quote escaped. --- src/backend/a2aserver.c | 17 +++++++++++++++-- tests/test_a2a_server.sh | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/backend/a2aserver.c b/src/backend/a2aserver.c index 8daf6e8..0653d5e 100644 --- a/src/backend/a2aserver.c +++ b/src/backend/a2aserver.c @@ -1877,7 +1877,7 @@ serve_conn(int fd, struct sp_agent *ag, const char *token) strstr(path, "/.well-known/agent-card.json") != NULL) { const char *host = header_value(hdr, "Host"); char hostbuf[256]; - struct sp_buf card; + struct sp_buf card, esc; if (host != NULL) { size_t n = strcspn(host, "\r\n"); @@ -1889,6 +1889,17 @@ serve_conn(int fd, struct sp_agent *ag, const char *token) } else { snprintf(hostbuf, sizeof(hostbuf), "127.0.0.1"); } + /* + * The value is a header this connection sent, and it goes into + * a JSON string: a quote in Host used to end the url and add a + * key of the caller's choosing to supportedInterfaces[0]. + */ + memset(&esc, 0, sizeof(esc)); + if (sp_buf_init(&esc) != 0 || + sp_json_escape(&esc, hostbuf) != 0) { + sp_buf_free(&esc); + memset(&esc, 0, sizeof(esc)); + } memset(&card, 0, sizeof(card)); if (sp_buf_init(&card) != 0) { http_reply(fd, 500, "Internal Server Error", @@ -1903,10 +1914,12 @@ serve_conn(int fd, struct sp_agent *ag, const char *token) "\"capabilities\":{\"streaming\":true,\"pushNotifications\":true}," "\"defaultInputModes\":[\"text/plain\"]," "\"defaultOutputModes\":[\"text/plain\"]," - "\"skills\":[]}", SP_VERSION, hostbuf); + "\"skills\":[]}", SP_VERSION, + esc.p != NULL ? esc.p : ""); http_reply(fd, 200, "OK", card.p != NULL ? card.p : "{}"); sp_buf_free(&card); } + sp_buf_free(&esc); } else if (strcmp(method, "POST") == 0) { handle_rpc(fd, ag, body.p != NULL ? body.p : ""); } else { diff --git a/tests/test_a2a_server.sh b/tests/test_a2a_server.sh index cf77bc8..92d884c 100755 --- a/tests/test_a2a_server.sh +++ b/tests/test_a2a_server.sh @@ -68,6 +68,17 @@ echo "$card" | grep -q '"streaming":true' check test $? -eq 0 echo "$card" | grep -q "/a2a" check test $? -eq 0 +# the Host header lands in the card's URL: a quote in it must not reshape the +# card. Before, Host "127.0.0.1","evil":"x" produced a url of "http://127.0.0.1" +# plus a new key "evil" in supportedInterfaces[0]. +evil=$(curl -s -H 'Host: 127.0.0.1","evil":"x' \ + "http://127.0.0.1:$P/.well-known/agent-card.json") +check_that "a quote in Host does not add a key to the agent card" \ + python3 -c 'import json,sys +d = json.loads(sys.argv[1]) +iface = d["supportedInterfaces"][0] +assert set(iface) == {"url", "protocolBinding", "protocolVersion"}, iface +assert iface["url"].endswith("/a2a"), iface["url"]' "$evil" # SendMessage -> a completed task with the answer in message and artifact resp=$(post "$P" "$(send_json)")