Konrad, 24.11.: "v1 geht durch ohne irgendwas. ich will auch keine approvals generell. ich will nur das der user mit ask nach sachen gefragt werden kann." Removed the whole approval apparatus, not just the prompt: - agent.c: the decision point (sp_host_gate/approve callbacks), the pause/resume path (has_pending, pending_from, sp_agent_resume), ack_approval, ask_operator and the ack stdin thread are gone. A tool runs; its exit status is the observation. sp_tool_policy (allow/deny per profile) stays - that is the operator's own configuration, not a question to the operator. - engine.c: SP_EV_APPROVAL, sp_engine_approve, the approve_pending condition variable and the `approval`/`approve` protocol commands are gone. - openai_server.c: the v1 endpoint runs gw_* tools without a gate; the autonomous flag is no longer part of sp_gateway_opts. - a2aserver.c: INPUT_REQUIRED and the resume-by-taskId path are gone; CancelTask stays. An A2A caller has no operator sitting in front of a prompt. - mcpserver.c: --autonomous no longer gates mutating tools; the flag and the config key are accepted for compatibility and documented as changing nothing. - telegram.c: tg_approve and the approval callback are gone; ask is the only dialog, and /cancel inside it still ends the turn. - front ends: protocol.c, curses.c, ui_local.c, ui_remote.c, client.c lose the approval event and the waiting flag. - ack.c stays: it is a plugin API (plugins/kids asks its parent through it), not splinter's own gate. --ack is documented as such. Tests: test_pause.c guarded the pause and now guards the opposite (a mutate and a destructive tool run, nothing is asked). test_a2a_input.sh, test_protocol.sh, test_ui.sh, test_agents_ui.sh, test_gateway.sh, test_gateway_openai.sh, test_agent.c and test_mcpsession.sh were rewritten the same way; splinter_approve must now be absent from the MCP tool list. Every rewritten test was run red against the old behaviour where that was possible. make check: all tests passed (428 s) make check-asan: all 47 test binaries clean make check-tsan: all 47 test binaries free of data races
340 lines
9.0 KiB
C
340 lines
9.0 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, NULL) == 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, NULL) == 0);
|
|
CHECK(sp_buf_init(&err) == 0);
|
|
|
|
unsetenv("SPLINTER_MCP_TOKEN");
|
|
CHECK(sp_mcp_serve(NULL, NULL, &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(NULL, NULL, &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);
|
|
}
|
|
|
|
/* ---- 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);
|
|
sp_registry_free(&r);
|
|
}
|
|
|
|
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;
|
|
}
|