Files
splinter/tests/test_kids.c
lotherkandClaude Opus 5.5 489c5da26a fixes from the review, second round: mcp client, kids rules, plugin life, ui
Each change has a test that fails against the code before it.

Also note: 8900d7f ("telegram: refuse to start without a token") swept in
work of this round that its message does not describe - an OLLAMA_KEY no longer
overwrites the key of the operator's last exit (config.c), parallel delegates
are killed and reaped after a timeout (delegate.c), lcm/cps never leave a tool
result without its call, cps keeps working past 256 messages, Telegram ids are
parsed as 64 bit, and 'run' also hides SPLINTER_OLLAMA_KEY_* and
SPLINTER_MCP_TOKEN.

MCP client (plugins/mcp)
- fgets into char[16384] split every longer reply into unparseable pieces; a
  big tools/list or file read never arrived. A reply sharing one write with a
  notification sat in stdio's buffer, invisible to poll(). Lines are now
  buffered on the fd (up to 16 MiB). An error is the top-level "error" member,
  not the text "error" anywhere in the line. test_mcpclient.

kids
- Rules were matched against the raw JSON, so "run ls*" never matched. They
  now see the command, path or url. A 'run' command that can chain, substitute
  or redirect is never whitelisted. The glob is linear instead of exponential.
  test_kids, which also covers the approval-command quoting and the domain
  checks from the first round.

Plugin life (src/plugin.c, ABI 2)
- An init() that failed kept what it had registered (tools, gate, widget
  threads); it is now rolled back. shutdown() was never called; the host calls
  it, newest plugin first, after the widget threads have stopped.
- A widget slot is filled before it is counted, and the count is read under the
  lock, so tick threads never see a half-made slot.
- SP_PLUGIN_ABI is 2: struct sp_gate grew block_turn while the ABI stayed 1.
  test_lifecycle, with tests/plugin_lifecycle.so.

UI
- Ctrl-C and Ctrl-D cancel under the lock and wake a turn waiting for approval
  (it waited forever); Ctrl-D cancelled nothing before the join.
- A steer the turn already took is not sent again as the next message.
- KEY_RESIZE re-cuts the windows; Ctrl-L repaints instead of sending the
  half-typed line; key codes above 255 are not typed into the input.
- Markdown attributes went to stdscr while the text went to ui_hist: bold and
  code never showed. They now build on the line's role colour.
  Checked in tmux: UTF-8 input, resize to 70x20, Ctrl-L, Ctrl-D, bold.

Telegram
- Replies are split on UTF-8 boundaries; a cut umlaut made Telegram refuse the
  chunk and the rest of the answer was lost.

Build
- Seven plugins linked with an undefined $(PLUGIN_LDFLAGS): no rpath, no LIBS.
  All eleven now carry $ORIGIN/../..; objects depend on the interface headers.

Seen, not changed: sp_session_append fsyncs every line; under disk load an
answer appears up to 15 s late. Not a deadlock, and the same before this round.

make check: 26 test binaries, green, also under ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:50:33 +00:00

125 lines
3.9 KiB
C

/*
* splinter -- kid mode's gate, against the ways it was found to open.
*
* - the whitelist was matched against the raw JSON, so "run ls*" never
* matched; and a working glob over a shell command would have allowed
* "ls; anything";
* - the parent was asked through system("cmd 'Anfrage: <args>'"), so a
* quote in the model's arguments ran a command before anybody answered;
* - the domain list took "allowed.org:x@evil.example" for allowed.org.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
put(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(text, f);
fclose(f);
}
}
static int
gate(struct sp_host *h, const char *tool, int auth, const char *args)
{
char reason[256] = "";
return sp_host_gate(h, tool, auth, args, reason, sizeof(reason));
}
int
main(void)
{
char home[] = "/var/tmp/splinter-kids-XXXXXX";
char path[700], mark[700], args[1200], cmd[800];
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err;
struct stat st;
CHECK(mkdtemp(home) != NULL);
snprintf(path, sizeof(path), "%s/.splinter", home);
(void)mkdir(path, 0700);
snprintf(path, sizeof(path), "%s/.splinter/kids", home);
(void)mkdir(path, 0700);
snprintf(path, sizeof(path), "%s/.splinter/kids/allow", home);
put(path, "run ls*\n");
snprintf(path, sizeof(path), "%s/.splinter/kids/domains", home);
put(path, "allowed.org\n");
snprintf(mark, sizeof(mark), "%s/pwned", home);
(void)setenv("HOME", home, 1);
(void)unsetenv("SPLINTER_ACK_CHANNELS");
(void)unsetenv("SPLINTER_ACK_DIR");
(void)setenv("SPLINTER_KIDS_APPROVE_CMD", "exit 1", 1); /* parent says no */
(void)setenv("SPLINTER_KIDS_TIMEOUT", "2", 1);
(void)setenv("SPLINTER_KIDS_SEARCH", "approve", 1);
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
if (host == NULL)
return 1;
CHECK(sp_plugin_load(host, "plugins/kids/kids.so", &err) == 0);
/* ---- the whitelist sees the command, not the JSON ---- */
CHECK(gate(host, "run", SP_AUTH_DESTRUCTIVE,
"{\"command\":\"ls -la\"}") == 1);
/* ---- and a command that chains is a parent's decision ---- */
CHECK(gate(host, "run", SP_AUTH_DESTRUCTIVE,
"{\"command\":\"ls; rm -rf ~\"}") == 0);
CHECK(gate(host, "run", SP_AUTH_DESTRUCTIVE,
"{\"command\":\"ls $(rm -rf ~)\"}") == 0);
CHECK(gate(host, "run", SP_AUTH_DESTRUCTIVE,
"{\"command\":\"cat /etc/shadow\"}") == 0);
/* ---- asking the parent does not run the model's text ---- */
snprintf(args, sizeof(args), "{\"path\":\"x'; touch %s; echo '\"}", mark);
CHECK(gate(host, "write", SP_AUTH_MUTATE, args) == 0);
CHECK(stat(mark, &st) != 0);
/* ---- the domain list names hosts, not user names ---- */
CHECK(gate(host, "webfetch", SP_AUTH_MUTATE,
"{\"url\":\"https://allowed.org/page\"}") == 1);
CHECK(gate(host, "webfetch", SP_AUTH_MUTATE,
"{\"url\":\"https://allowed.org:x@evil.example/\"}") == 0);
CHECK(gate(host, "webfetch", SP_AUTH_MUTATE,
"{\"url\":\"https://allowed.org.evil.example/\"}") == 0);
/* two "url" keys: checked and fetched must be the same one */
CHECK(gate(host, "webfetch", SP_AUTH_MUTATE,
"{\"url\":\"https://allowed.org/\",\"url\":\"https://evil.example/\"}")
== 0 || gate(host, "webfetch", SP_AUTH_MUTATE,
"{\"url\":\"https://evil.example/\",\"url\":\"https://allowed.org/\"}")
== 0);
sp_host_free(host);
sp_buf_free(&err);
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", home);
(void)system(cmd);
if (failures == 0)
printf("test_kids: ok\n");
return failures == 0 ? 0 : 1;
}