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

329 lines
8.5 KiB
C

/*
* splinter -- secrets stay out of argv and out of the model's shell, the MCP
* server does not talk to strangers, and task_kill does not kill splinter.
*
* Each block is the failure it guards against, as it was found.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "curl.h"
#include "mcpserver.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[] = "/var/tmp/splinter-secrets-XXXXXX";
static char *
slurp(const char *path)
{
static char buf[8192];
FILE *f = fopen(path, "r");
size_t n;
buf[0] = '\0';
if (f == NULL)
return buf;
n = fread(buf, 1, sizeof(buf) - 1, f);
buf[n] = '\0';
fclose(f);
return buf;
}
static void
run_tool(struct sp_registry *r, const char *name, const char *args,
struct sp_buf *out)
{
const struct sp_tool *t = sp_registry_find(r, name);
sp_buf_clear(out);
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, out, t->ctx);
}
/* ---- 'run': no keys in its environment, no terminal, no forever ---- */
static void
test_run(void)
{
struct sp_registry r;
struct sp_buf out;
time_t t0;
memset(&r, 0, sizeof(r));
CHECK(sp_builtin_register(&r) == 0);
CHECK(sp_buf_init(&out) == 0);
setenv("SPLINTER_API_KEY", "sk-geheim-1", 1);
setenv("MY_PROVIDER_KEY", "sk-geheim-2", 1);
setenv("SPLINTER_DELEGATE_KEY_4711", "sk-geheim-3", 1);
setenv("HARMLESS_VALUE", "sichtbar", 1);
CHECK(sp_secret_env_add("MY_PROVIDER_KEY") == 0);
run_tool(&r, "run", "{\"command\":\"env\"}", &out);
CHECK(strstr(out.p, "sk-geheim") == NULL);
CHECK(strstr(out.p, "HARMLESS_VALUE=sichtbar") != NULL);
CHECK(strstr(out.p, "[exit status 0]") != NULL);
/* stdin is /dev/null: cat ends at once instead of reading the terminal */
run_tool(&r, "run", "{\"command\":\"cat; echo done\"}", &out);
CHECK(strstr(out.p, "done") != NULL);
/* stderr is part of the observation */
run_tool(&r, "run", "{\"command\":\"echo oops >&2; exit 3\"}", &out);
CHECK(strstr(out.p, "oops") != NULL);
CHECK(strstr(out.p, "[exit status 3]") != NULL);
/* a command that does not end is ended */
setenv("SPLINTER_RUN_TIMEOUT", "1", 1);
t0 = time(NULL);
run_tool(&r, "run", "{\"command\":\"sleep 30 & sleep 30\"}", &out);
CHECK(strstr(out.p, "timed out") != NULL);
CHECK(time(NULL) - t0 < 10);
unsetenv("SPLINTER_RUN_TIMEOUT");
sp_buf_free(&out);
sp_registry_free(&r);
}
/* ---- curl: the key travels on stdin, never in argv ---- */
static void
test_curl(void)
{
char fake[700], argsf[700], cfgf[700], line[1600];
const char *oldpath = getenv("PATH");
const char *args[] = { "-sS", "-m", "5", NULL };
const char *hdrs[] = { "Authorization: Bearer sk-geheim-4", NULL };
struct sp_buf err, cfg;
pid_t pid;
int fd, st;
FILE *f;
char buf[256];
snprintf(argsf, sizeof(argsf), "%s/curl.args", dir);
snprintf(cfgf, sizeof(cfgf), "%s/curl.cfg", dir);
snprintf(fake, sizeof(fake), "%s/curl", dir);
f = fopen(fake, "w");
CHECK(f != NULL);
if (f == NULL)
return;
fprintf(f, "#!/bin/sh\nprintf '%%s\\n' \"$@\" > '%s'\ncat > '%s'\n"
"echo hello\n", argsf, cfgf);
fclose(f);
(void)chmod(fake, 0755);
snprintf(line, sizeof(line), "%s:%s", dir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
setenv("PATH", line, 1);
CHECK(sp_buf_init(&err) == 0);
fd = sp_curl_start(args,
"https://api.example/bot123:tok\"en/send", hdrs, 1, &pid, &err);
CHECK(fd >= 0);
if (fd >= 0) {
ssize_t n = read(fd, buf, sizeof(buf) - 1);
buf[n > 0 ? n : 0] = '\0';
close(fd);
(void)waitpid(pid, &st, 0);
CHECK(strncmp(buf, "hello", 5) == 0);
}
CHECK(strstr(slurp(argsf), "geheim") == NULL);
CHECK(strstr(slurp(argsf), "bot123") == NULL);
CHECK(strstr(slurp(argsf), "-K") != NULL);
CHECK(strstr(slurp(cfgf), "header = \"Authorization: Bearer "
"sk-geheim-4\"") != NULL);
/* a quote in the value is escaped, not the end of the string */
CHECK(strstr(slurp(cfgf), "url = \"https://api.example/bot123:tok\\\"en"
"/send\"") != NULL);
CHECK(sp_buf_init(&cfg) == 0);
CHECK(sp_curl_config_line(&cfg, "k", "a\\b\nc") == 0);
CHECK(strcmp(cfg.p, "k = \"a\\\\b\\nc\"\n") == 0);
sp_buf_free(&cfg);
sp_buf_free(&err);
setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
}
/* ---- MCP server: loopback or a token, and the token is checked ---- */
static int
mcp_exchange(int port, const char *send_text, char *reply, size_t cap)
{
struct sockaddr_in a;
struct timeval tv = { 5, 0 };
int s, tries;
size_t used = 0;
for (tries = 0; tries < 50; tries++) {
s = socket(AF_INET, SOCK_STREAM, 0);
memset(&a, 0, sizeof(a));
a.sin_family = AF_INET;
a.sin_port = htons((unsigned short)port);
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(s, (struct sockaddr *)&a, sizeof(a)) == 0)
break;
close(s);
s = -1;
{
struct timespec ts = { 0, 100 * 1000 * 1000 };
(void)nanosleep(&ts, NULL);
}
}
if (s < 0)
return -1;
(void)setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
(void)write(s, send_text, strlen(send_text));
(void)shutdown(s, SHUT_WR);
for (;;) {
ssize_t n = read(s, reply + used, cap - used - 1);
if (n <= 0)
break;
used += (size_t)n;
}
reply[used] = '\0';
close(s);
return 0;
}
static void
test_mcp(void)
{
struct sp_registry r;
struct sp_buf err;
char reply[8192];
int port = 20000 + (int)(getpid() % 20000);
pid_t srv;
memset(&r, 0, sizeof(r));
CHECK(sp_builtin_register(&r) == 0);
CHECK(sp_buf_init(&err) == 0);
unsetenv("SPLINTER_MCP_TOKEN");
CHECK(sp_mcp_serve(&r, "0.0.0.0", port, 0, &err) != 0);
CHECK(strstr(err.p, "SPLINTER_MCP_TOKEN") != NULL);
setenv("SPLINTER_MCP_TOKEN", "richtig", 1);
srv = fork();
if (srv == 0) {
struct sp_buf e;
(void)sp_buf_init(&e);
_exit(sp_mcp_serve(&r, "127.0.0.1", port, 0, &e) == 0 ? 0 : 1);
}
CHECK(srv > 0);
CHECK(mcp_exchange(port, "{\"jsonrpc\":\"2.0\",\"id\":1,"
"\"method\":\"tools/list\"}\n", reply, sizeof(reply)) == 0);
CHECK(strstr(reply, "authentication required") != NULL);
CHECK(strstr(reply, "\"read\"") == NULL);
CHECK(mcp_exchange(port, "AUTH falsch\n{\"jsonrpc\":\"2.0\",\"id\":1,"
"\"method\":\"tools/list\"}\n", reply, sizeof(reply)) == 0);
CHECK(strstr(reply, "authentication required") != NULL);
CHECK(mcp_exchange(port, "AUTH richtig\n{\"jsonrpc\":\"2.0\",\"id\":1,"
"\"method\":\"tools/list\"}\n", reply, sizeof(reply)) == 0);
CHECK(strstr(reply, "\"read\"") != NULL);
if (srv > 0) {
(void)kill(srv, SIGTERM);
(void)waitpid(srv, NULL, 0);
}
unsetenv("SPLINTER_MCP_TOKEN");
sp_buf_free(&err);
sp_registry_free(&r);
}
/* ---- tasks: task_kill stops the task, and only the task ---- */
static void
test_tasks(void)
{
struct sp_registry r;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err, out;
int i;
memset(&r, 0, sizeof(r));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&err) == 0);
CHECK(sp_buf_init(&out) == 0);
host = sp_host_new(&r, &ag, NULL);
CHECK(host != NULL);
if (host == NULL)
return;
CHECK(sp_plugin_load(host, "plugins/tasks/tasks.so", &err) == 0);
run_tool(&r, "task_start", "{\"command\":\"sleep 30\",\"label\":\"s\"}",
&out);
CHECK(strstr(out.p, "1") != NULL);
for (i = 0; i < 20; i++) { /* let the thread fork */
struct timespec ts = { 0, 50 * 1000 * 1000 };
(void)nanosleep(&ts, NULL);
}
/*
* The old code sent SIGTERM to splinter's own process group here, so
* this test would have died on the next line. An integer id, as the
* schema declares it.
*/
run_tool(&r, "task_kill", "{\"id\":1}", &out);
CHECK(strstr(out.p, "kill signal sent") != NULL);
for (i = 0; i < 40; i++) {
struct timespec ts = { 0, 50 * 1000 * 1000 };
run_tool(&r, "task_list", "{}", &out);
if (strstr(out.p, "exit") != NULL || strstr(out.p, "fertig") != NULL)
break;
(void)nanosleep(&ts, NULL);
}
run_tool(&r, "task_kill", "{\"id\":99}", &out);
CHECK(strstr(out.p, "no such task") != NULL);
sp_host_free(host);
sp_buf_free(&out);
sp_buf_free(&err);
}
int
main(void)
{
char cmd[700];
/* a background child that dies must not take the test with it */
(void)signal(SIGPIPE, SIG_IGN);
if (mkdtemp(dir) == NULL) {
perror("mkdtemp");
return 1;
}
test_run();
test_curl();
test_mcp();
test_tasks();
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
(void)system(cmd);
if (failures == 0)
printf("test_secrets: ok\n");
return failures == 0 ? 0 : 1;
}