#!/bin/sh # splinter -- the A2A server, driven by curl. # # What it guards: the agent card, SendMessage -> completed Task with the # answer, GetTask from the in-memory store, a refusal for streaming, and the # bearer token that is required beyond loopback (or when configured). set -u cd "$(dirname "$0")/.." || exit 1 . "$(dirname "$0")/sh/port.sh" D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-srv-XXXXXX") || exit 1 SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR P=$(free_port) P2=$((P + 1)) P3=$((P + 2)) P4=$((P + 3)) P5=$((P + 4)) S1= S2= S3= S4= W= cleanup() { [ -n "$S1" ] && kill "$S1" 2>/dev/null [ -n "$S2" ] && kill "$S2" 2>/dev/null [ -n "$S3" ] && kill "$S3" 2>/dev/null [ -n "$S4" ] && kill "$S4" 2>/dev/null [ -n "$W" ] && kill "$W" 2>/dev/null rm -rf "$D" } trap cleanup EXIT mkdir -p "$D/home" printf 'answer hallo von splinter\n%.0s' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > "$D/model.txt" fails=0 check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; } check_that() { # $1 = description, $2.. = command d=$1; shift if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi } wait_port() { for i in $(seq 1 50); do curl -s -o /dev/null "http://127.0.0.1:$1/" && return 0 sleep 0.2 done return 1 } post() { # post [extra curl args...] port=$1; body=$2; shift 2 curl -s -H 'Content-Type: application/json' "$@" \ --data "$body" "http://127.0.0.1:$port/a2a" } send_json() { printf '%s' '{"jsonrpc":"2.0","id":"1","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"text":"hallo"}]}}}' } HOME="$D/home" ./splinter --no-config --script "$D/model.txt" \ --a2a-server-listen 127.0.0.1 --a2a-listen-port "$P" \ >"$D/out1" 2>"$D/err1" & S1=$! check wait_port "$P" python3 -I tests/fake_webhook.py "$P3" "$D/hook.log" & W=$! check wait_port "$P3" # the agent card names the JSON-RPC interface card=$(curl -s "http://127.0.0.1:$P/.well-known/agent-card.json") echo "$card" | grep -q '"protocolBinding":"JSONRPC"' check test $? -eq 0 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)") echo "$resp" | grep -q '"state":"TASK_STATE_COMPLETED"' check test $? -eq 0 echo "$resp" | grep -q 'hallo von splinter' check test $? -eq 0 # GetTask returns the same task id=$(printf '%s' "$resp" | python3 -c \ 'import json,sys; print(json.load(sys.stdin)["result"]["task"]["id"])') resp2=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"2\",\"method\":\"GetTask\",\"params\":{\"id\":\"$id\"}}") echo "$resp2" | grep -q 'hallo von splinter' check test $? -eq 0 echo "$resp2" | grep -q '"result":{"id":"task-' check test $? -eq 0 # streaming: SSE with an artifact and a completed status resp3=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"3\",\"method\":\"SendStreamingMessage\",\"params\":{\"message\":{\"messageId\":\"m\",\"role\":\"ROLE_USER\",\"parts\":[{\"text\":\"hallo\"}]}}}") echo "$resp3" | grep -q 'artifactUpdate' check test $? -eq 0 echo "$resp3" | grep -q 'hallo von splinter' check test $? -eq 0 echo "$resp3" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 # a completed task cannot be canceled: the spec's TaskNotCancelable (-32002) resp6=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"6\",\"method\":\"CancelTask\",\"params\":{\"id\":\"$id\"}}") echo "$resp6" | grep -q '"code":-32002' check test $? -eq 0 # push-config CRUD: create, get, list, delete, then get again resp9=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"9\",\"method\":\"CreateTaskPushNotificationConfig\",\"params\":{\"taskId\":\"$id\",\"url\":\"http://127.0.0.1:$P3/hook2\",\"token\":\"t2\",\"authentication\":{\"scheme\":\"Bearer\",\"credentials\":\"c2\"}}}") echo "$resp9" | grep -q '"id":"cfg-' check test $? -eq 0 cid=$(printf '%s' "$resp9" | python3 -c 'import json,sys; print(json.load(sys.stdin)["result"]["id"])') check test -n "$cid" resp9=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"9\",\"method\":\"GetTaskPushNotificationConfig\",\"params\":{\"taskId\":\"$id\",\"pushNotificationConfigId\":\"$cid\"}}") echo "$resp9" | grep -q 'hook2' check test $? -eq 0 resp9=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"9\",\"method\":\"ListTaskPushNotificationConfigs\",\"params\":{\"taskId\":\"$id\"}}") echo "$resp9" | grep -q '"totalSize":1' check test $? -eq 0 resp9=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"9\",\"method\":\"DeleteTaskPushNotificationConfig\",\"params\":{\"taskId\":\"$id\",\"pushNotificationConfigId\":\"$cid\"}}") echo "$resp9" | grep -q '"result":{}' check test $? -eq 0 resp9=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"9\",\"method\":\"GetTaskPushNotificationConfig\",\"params\":{\"taskId\":\"$id\",\"pushNotificationConfigId\":\"$cid\"}}") echo "$resp9" | grep -q '"code":-32001' check test $? -eq 0 # a binary part is accepted: the bytes land in the session's inbox and the # model is told the path (a text model cannot read a PNG, the agent can) b64=$(printf 'PNGDATA' | python3 -c 'import base64,sys; print(base64.b64encode(sys.stdin.buffer.read()).decode())') resp13=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"13\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m13\",\"role\":\"ROLE_USER\",\"parts\":[{\"file\":{\"name\":\"pic.png\",\"mimeType\":\"image/png\",\"bytes\":\"$b64\"}},{\"text\":\"sieh dir das an\"}]}}}") echo "$resp13" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 found=$(ls "$D"/home/.splinter/inbox/*pic.png 2>/dev/null | head -1) check_that "the binary part lands in the inbox" test -n "$found" check_that "the bytes are the bytes" sh -c "printf PNGDATA | cmp - '$found'" check_that "the model is told the path" grep -rq 'file pic.png at' "$D/home/.splinter" # the raw shape (base64 in "raw") is the same thing resp14=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"14\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m14\",\"role\":\"ROLE_USER\",\"parts\":[{\"raw\":\"$b64\",\"mediaType\":\"image/png\",\"metadata\":{\"filename\":\"raw.png\"}},{\"text\":\"und das\"}]}}}") echo "$resp14" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 found=$(ls "$D"/home/.splinter/inbox/*raw.png 2>/dev/null | head -1) check_that "a raw part lands in the inbox" test -n "$found" # a binary part without a session and without an inbox still must not be lost # silently: a text/plain raw part is still text resp15=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"15\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m15\",\"role\":\"ROLE_USER\",\"parts\":[{\"raw\":\"$b64\",\"mediaType\":\"text/plain\"}]}}}") echo "$resp15" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 # SubscribeToTask: the stored state, once, as an SSE status update resp16=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"16\",\"method\":\"SubscribeToTask\",\"params\":{\"id\":\"$id\"}}") echo "$resp16" | grep -q 'statusUpdate' check test $? -eq 0 echo "$resp16" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 echo "$resp16" | grep -q 'hallo von splinter' check test $? -eq 0 resp16=$(post "$P" '{"jsonrpc":"2.0","id":"16","method":"SubscribeToTask","params":{"id":"nope"}}') echo "$resp16" | grep -q '"code":-32001' check test $? -eq 0 # ListTasks: the stored task, and an empty list for another context resp7=$(post "$P" '{"jsonrpc":"2.0","id":"7","method":"ListTasks","params":{}}') echo "$resp7" | grep -q "$id" check test $? -eq 0 resp8=$(post "$P" '{"jsonrpc":"2.0","id":"8","method":"ListTasks","params":{"contextId":"other"}}') echo "$resp8" | grep -q '"tasks":\[\]' check test $? -eq 0 # a task that does not exist is a JSON-RPC error, not an empty answer resp4=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"4\",\"method\":\"GetTask\",\"params\":{\"id\":\"nope\"}}") echo "$resp4" | grep -q '"code":-32001' check test $? -eq 0 # a text/plain raw part is accepted; a url part is refused, not fetched resp10=$(post "$P" '{"jsonrpc":"2.0","id":"10","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"raw":"aGFsbG8=","mediaType":"text/plain"}]}}}') echo "$resp10" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 resp11=$(post "$P" '{"jsonrpc":"2.0","id":"11","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"url":"http://example/x"}]}}}') echo "$resp11" | grep -q '"code":-32005' check test $? -eq 0 # a text/plain URL on loopback is fetched; the file server sees the GET resp14=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"14\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m\",\"role\":\"ROLE_USER\",\"parts\":[{\"url\":\"http://127.0.0.1:$P3/file.txt\",\"mediaType\":\"text/plain\"}]}}}") echo "$resp14" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 grep -q 'get /file.txt' "$D/hook.log" check test $? -eq 0 # a push config posts the completed task to the loopback webhook resp12=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"12\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m\",\"role\":\"ROLE_USER\",\"parts\":[{\"text\":\"push\"}]},\"configuration\":{\"taskPushNotificationConfig\":{\"url\":\"http://127.0.0.1:$P3/hook\",\"token\":\"t1\",\"authentication\":{\"scheme\":\"Bearer\",\"credentials\":\"secret\"}}}}}") echo "$resp12" | grep -q 'TASK_STATE_COMPLETED' check test $? -eq 0 for i in $(seq 1 50); do grep -q 'hallo von splinter' "$D/hook.log" 2>/dev/null && break sleep 0.2 done check_that "the push carries the answer" grep -q 'hallo von splinter' "$D/hook.log" check_that "the push carries the bearer credential" \ grep -q 'auth=Bearer secret' "$D/hook.log" check_that "the push carries its token" grep -q 'token=t1' "$D/hook.log" # a push to a host that is not allowlisted is refused before the turn resp13=$(post "$P" '{"jsonrpc":"2.0","id":"13","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"text":"push"}]},"configuration":{"taskPushNotificationConfig":{"url":"http://example.com/hook"}}}}') echo "$resp13" | grep -q '"code":-32003' check test $? -eq 0 # userinfo in front of an allowed host is not that host (SSRF) resp15=$(post "$P" '{"jsonrpc":"2.0","id":"15","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"text":"push"}]},"configuration":{"taskPushNotificationConfig":{"url":"http://localhost:x@169.254.169.254/hook"}}}}') printf '%s' "$resp15" | grep -q '"code":-32003' check_that "userinfo push url refused" test $? -eq 0 # the reply carries the top-level id, not the first "id" in the body resp16=$(post "$P" '{"jsonrpc":"2.0","params":{"id":"inner"},"method":"GetTask","id":77}') printf '%s' "$resp16" | grep -q '"id":77,' check_that "top-level id answered" test $? -eq 0 resp17=$(post "$P" '{"jsonrpc":"2.0","method":"GetTask","params":{"id":"x"},"id":"a\"b"}') printf '%s' "$resp17" | grep -qF '"id":"a\"b"' check_that "string id escaped" test $? -eq 0 # a contextId that would be stored cut short is refused longctx=$(printf 'c%.0s' $(seq 1 200)) resp18=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"18\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m\",\"role\":\"ROLE_USER\",\"contextId\":\"$longctx\",\"parts\":[{\"text\":\"x\"}]}}}") printf '%s' "$resp18" | grep -q 'contextId too long' check_that "long contextId refused" test $? -eq 0 # ---- the client half against the server half ---- # discovery (the agent card names /a2a) plus a blocking turn, then the # streaming path: both sides of the protocol in one process pair cardurl="http://127.0.0.1:$P/.well-known/agent-card.json" out=$(HOME="$D/home" SPLINTER_A2A_CARD="$cardurl" SPLINTER_A2A_TIMEOUT=20 \ ./splinter --no-config --plugin plugins/a2a/a2a.so --transport a2a \ --no-stream -p "hallo" 2>&1) echo "$out" | grep -q 'hallo von splinter' check test $? -eq 0 out=$(HOME="$D/home" SPLINTER_A2A_CARD="$cardurl" SPLINTER_A2A_TIMEOUT=20 \ ./splinter --no-config --plugin plugins/a2a/a2a.so --transport a2a \ -p "hallo" 2>&1) echo "$out" | grep -q 'hallo von splinter' check test $? -eq 0 # ---- token: without it a request is 401, with it the turn runs ---- HOME="$D/home" SPLINTER_A2A_SERVER_TOKEN=tok ./splinter --no-config \ --script "$D/model.txt" --a2a-server-listen 127.0.0.1 \ --a2a-listen-port "$P2" >"$D/out2" 2>"$D/err2" & S2=$! check wait_port "$P2" code=$(curl -s -o /dev/null -w '%{http_code}' -H 'Content-Type: application/json' \ --data "$(send_json)" "http://127.0.0.1:$P2/a2a") check test "$code" = 401 resp5=$(post "$P2" "$(send_json)" -H 'Authorization: Bearer tok') echo "$resp5" | grep -q 'hallo von splinter' check test $? -eq 0 # ---- a client that goes quiet must not hold the server ---- # The accept loop serves one connection at a time. With a five minute read # timeout, one socket that opened and said nothing blocked every other client # for five minutes. HOME="$D/home" SPLINTER_A2A_READ_TIMEOUT=2 ./splinter --no-config \ --script "$D/model.txt" --a2a-server-listen 127.0.0.1 \ --a2a-listen-port "$P4" >"$D/out4" 2>"$D/err4" & S3=$! check wait_port "$P4" python3 -I tests/slow_a2a_client.py "$P4" /a2a > "$D/slow.txt" 2>&1 check test $? -eq 0 if ! grep -q 'second client answered' "$D/slow.txt"; then echo "FAIL a quiet client held the A2A server" cat "$D/slow.txt" fails=$((fails + 1)) fi # ---- a background process started by a tool must not hold the port ---- # The `run` tool forks /bin/sh; the child inherited the server's descriptors. # A command that leaves something behind ("sleep 300 &") kept the LISTEN socket # open after splinter exited, so the port stayed bound and the next start died # with "Address already in use". The listen socket is created with # SOCK_CLOEXEC and the accepted one gets FD_CLOEXEC, so no exec inherits them. # The child writes its own pid down: killing by name ("pkill -f sleep") would # reach a sleep that belongs to something else on this machine. cat > "$D/bg.txt" </dev/null 2>&1 & echo \$! > $D/bgpid; echo started"} answer fertig MDL HOME="$D/home" ./splinter --no-config --autonomous --script "$D/bg.txt" \ --a2a-server-listen 127.0.0.1 --a2a-listen-port "$P5" \ >"$D/out5" 2>"$D/err5" & S4=$! check wait_port "$P5" post "$P5" "$(send_json)" > /dev/null sleep 1 kill "$S4" 2>/dev/null wait "$S4" 2>/dev/null S4= sleep 1 check_that "a tool's background child does not keep the A2A port bound" \ sh -c "! ss -ltn 2>/dev/null | grep -q ':$P5 '" HOME="$D/home" ./splinter --no-config --script "$D/model.txt" \ --a2a-server-listen 127.0.0.1 --a2a-listen-port "$P5" \ >"$D/out6" 2>"$D/err6" & S4=$! sleep 1 check_that "the port can be taken again after a tool left a child behind" \ sh -c "! grep -q 'Address already in use' '$D/err6'" kill "$S4" 2>/dev/null S4= [ -f "$D/bgpid" ] && kill "$(cat "$D/bgpid")" 2>/dev/null if [ "$fails" -eq 0 ]; then echo "test_a2a_server: ok"; exit 0; fi exit 1