Files
splinter/tests/test_secrets.c
T
lotherk af2001bfee ui, model, cost and idle timeouts
- lcm: the sidebar widget and /status segment show the context, the number of
  compactions, messages and bytes of the last cut, and how many old tool
  outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
  sse/openai, the agent and the engine. /cost reports it with the cache share
  and splits judged from plain turns; the notes say that an upstream judge is
  not visible in the provider's usage. The status plugin and junction prefer
  the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
  bar. Notes and errors are transcript rows below the cause, never text in the
  input line or the status bar; a failed backend request is a status row and is
  retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
  once, then asks the endpoint the session is on what it serves and adds those
  models, so a router's whole menu is selectable. provider/model selectors now
  apply the right provider instead of becoming a model name. The choice is
  written to <session>.model and restored when that session is reopened; /new
  carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
  attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
  stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
  is still there before the window is extended. A long build that keeps
  reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
  plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
  restore and /model probe, cli idle timeout with a talkative and a silent
  command, run output reset, and the UI status/retry rows.

make check: all tests passed.
2026-11-17 23:21:56 +00:00

339 lines
8.9 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);
/*
* Output is life: the same one-second window is reset by every line,
* so a command that keeps talking runs past it and finishes.
*/
run_tool(&r, "run",
"{\"command\":\"for i in 1 2 3 4 5; do echo tick; "
"sleep 0.4; done; echo fertig\"}", &out);
CHECK(strstr(out.p, "fertig") != NULL);
CHECK(strstr(out.p, "timed out") == NULL);
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;
}