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)")