Files
splinter/tests/test_secrets.c
T
lotherk 8aee12e9d4 drop tmux/planner/sysinfo, tasks becomes processes, no capitalised calls
- plugins/tmux, plugins/planner and plugins/sysinfo are gone (plugin list,
  Makefile rules, tests, docs). The config no longer loads them; the stale
  .so files were removed from the install tree.
- plugins/tasks is plugins/processes: plugin name "processes", widget
  "processes", tools process_start/process_list/process_result/process_kill.
  The sidebar now lists only running work: a finished task keeps its output
  in process_result but stops taking a row forever.
- the dead planner link in the delegate (link_plan_quota -> command "tasks")
  is removed with the planner.
- tool call rows are no longer capitalised: "run  ls -l", "read  src/x.c" as
  the model sent them, not "Run "/"Read ".
- tests updated (delegate panel expects no finished row, engine/UI expect the
  lowercase call row, secrets/delegate use the processes plugin and tools).
- the user's config gets a Delegate/Scheduler section: the three providers
  carry review/plan/docs tags and keep max_parallel=2; plugin scheduler stays
  loaded so waiting jobs are retried.

make check: all tests passed.
2026-11-17 21:06:55 +00:00

329 lines
8.6 KiB
C

/*
* splinter -- secrets stay out of argv and out of the model's shell, the MCP
* server does not talk to strangers, and process_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(NULL, NULL, &r, "0.0.0.0", port, 0, 1, &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(NULL, NULL, &r, "127.0.0.1", port, 0, 1, &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);
}
/* ---- processes: process_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/processes/processes.so", &err) == 0);
run_tool(&r, "process_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, "process_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, "process_list", "{}", &out);
if (strstr(out.p, "exit") != NULL || strstr(out.p, "fertig") != NULL)
break;
(void)nanosleep(&ts, NULL);
}
run_tool(&r, "process_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;
}