Files
splinter/tests/test_builtin.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

208 lines
5.8 KiB
C

/*
* splinter -- builtin file tools.
*
* The tools are the part of the harness a model can actually break something
* with, so each of them is tested against the failure it is supposed to refuse:
* an ambiguous edit, a binary file, a write that is too large.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static char dir[600];
static const struct sp_tool *
tool(struct sp_registry *r, const char *name)
{
const struct sp_tool *t = sp_registry_find(r, name);
CHECK(t != NULL);
return t;
}
static void
write_file(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(text, f);
fclose(f);
}
}
int
main(void)
{
struct sp_registry r;
struct sp_buf out;
char path[700], big[1600];
const char *copy;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-test-XXXXXX", tmp);
CHECK(mkdtemp(dir) != NULL);
memset(&r, 0, sizeof(r));
if (sp_buf_init(&out) != 0)
return 1;
CHECK(sp_builtin_register(&r) == 0);
/* the five tools exist and declare their class */
CHECK(tool(&r, "read")->auth == SP_AUTH_READ);
CHECK(tool(&r, "write")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "edit")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "search")->auth == SP_AUTH_READ);
CHECK(tool(&r, "list")->auth == SP_AUTH_READ);
CHECK(tool(&r, "run")->auth == SP_AUTH_DESTRUCTIVE);
snprintf(path, sizeof(path), "%s/a.txt", dir);
write_file(path, "erste zeile\nzweite zeile\ndritte zeile\n");
/* ---- read ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite zeile") != NULL);
CHECK(strstr(out.p, "1\terste") != NULL);
/* offset and limit */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"offset\":2,\"limit\":1}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite") != NULL);
CHECK(strstr(out.p, "erste") == NULL);
/* a missing file is an observation, not a crash */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s/nope\"}", dir);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "error:") != NULL);
/* ---- write ---- */
sp_buf_clear(&out);
snprintf(path, sizeof(path), "%s/neu.txt", dir);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"content\":\"hallo\"}", path);
CHECK(tool(&r, "write")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "wrote 5 bytes") != NULL);
/* ---- edit: exactly one match ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"hallo\",\"new\":\"welt\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "edited") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "welt") != NULL);
/* ---- edit: two matches are refused ---- */
write_file(path, "x x\n");
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "occurs 2 times") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "y") == NULL); /* nothing was written */
/* ---- read refuses a binary file ---- */
{
FILE *f;
char bin[700];
snprintf(bin, sizeof(bin), "%s/bin.dat", dir);
f = fopen(bin, "wb");
CHECK(f != NULL);
if (f != NULL) {
fwrite("abc\0def", 1, 7, f);
fclose(f);
}
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", bin);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "binary") != NULL);
}
/* ---- search and list ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"pattern\":\"zweite\",\"path\":\"%s\"}", dir);
CHECK(tool(&r, "search")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", dir);
CHECK(tool(&r, "list")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
/* ---- missing arguments are reported, never guessed ---- */
sp_buf_clear(&out);
CHECK(tool(&r, "read")->run("{}", &out, NULL) == 0);
CHECK(strstr(out.p, "required") != NULL);
/*
* ---- webfetch: the URL is the model's, so it is data, never shell ----
* The quote used to close curl's '...' and run the rest as a command,
* from a tool that needed nobody's consent.
*/
{
char mark[700];
struct stat st;
CHECK(tool(&r, "webfetch")->auth != SP_AUTH_READ);
snprintf(mark, sizeof(mark), "%s/pwned", dir);
snprintf(big, sizeof(big), "{\"url\":\"http://127.0.0.1:9/'; "
"touch %s; echo '\"}", mark);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run(big, &out, NULL) == 0);
CHECK(stat(mark, &st) != 0);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run("{\"url\":\"file:///etc/passwd\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "only http") != NULL);
CHECK(strstr(out.p, "root:") == NULL);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run("{\"url\":\"-K/etc/passwd\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "only http") != NULL);
}
copy = out.p;
CHECK(copy != NULL);
sp_registry_free(&r);
sp_buf_free(&out);
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed: %s\n", strerror(errno));
}
if (failures == 0)
printf("test_builtin: ok\n");
return failures == 0 ? 0 : 1;
}