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

132 lines
4.2 KiB
C

/*
* The ack mechanism: a request is a file, an answer is a file, and any process
* may write it. Silence is not consent.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "ack.h"
#include "splinter.h"
static int checks, fails;
#define CHECK(cond, what) do { \
checks++; \
if (!(cond)) { \
fails++; \
printf("FAIL %s (line %d)\n", what, __LINE__); \
} else { \
printf("ok %s\n", what); \
} \
} while (0)
static int answer_id;
static int answer_yes;
static void *
answerer(void *arg)
{
(void)arg;
{ struct timespec ts = { 0, 300000000L }; (void)nanosleep(&ts, NULL); }
(void)sp_ack_answer(answer_id, answer_yes, "test-phone");
return NULL;
}
int
main(void)
{
char out[2048], who[64];
struct sp_buf log;
int id, yes;
pthread_t th;
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack", 1);
(void)system("rm -rf /var/tmp/splinter_test_ack");
id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5);
CHECK(id > 0, "request is written and returns an id");
/* the answer comes from another thread, like another process would */
answer_id = id;
answer_yes = 1;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 1, "an answer of yes is read back");
CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it");
id = sp_ack_request("run", "{\"command\":\"rm -rf /\"}", "destructive", 1);
answer_id = id;
answer_yes = 0;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 0, "an answer of no is read back as no");
id = sp_ack_request("run", "slow", "mutate", 1);
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
"a timeout is a no, not a yes");
CHECK(sp_ack_answer(999999, 1, "nobody") != 0,
"answering a request that does not exist fails");
id = sp_ack_request("edit", "{\"path\":\"/etc/passwd\"}", "mutate", 30);
if (sp_buf_init(&log) == 0) {
(void)sp_ack_list(&log);
snprintf(out, sizeof(out), "%s", log.p != NULL ? log.p : "");
sp_buf_free(&log);
CHECK(strstr(out, "\"path\":\"/etc/passwd\"") != NULL,
"the pending list shows what is being asked");
}
(void)sp_ack_answer(id, 0, "test");
/*
* ---- an answer belongs to one request, once, in time ----
* The id used to be the pid: a "yes" that came too late stayed on disk
* and approved the next request of the same process unseen.
*/
{
int a, b;
a = sp_ack_request("run", "first", "destructive", 1);
b = sp_ack_request("run", "second", "destructive", 1);
CHECK(a > 0 && b > 0 && a != b,
"two requests of one process get two ids");
CHECK(a != (int)getpid(), "the id is not the pid");
(void)sp_ack_answer(b, 0, "test");
(void)sp_ack_wait(b, 1, who, sizeof(who));
CHECK(sp_ack_wait(a, 1, who, sizeof(who)) == 0,
"an unanswered request times out as no");
CHECK(sp_ack_answer(a, 1, "late") != 0,
"an answer after the timeout is refused");
id = sp_ack_request("run", "third", "destructive", 1);
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
"a late yes does not approve the next request");
id = sp_ack_request("run", "fourth", "destructive", 5);
CHECK(sp_ack_answer(id, 0, "first") == 0, "the first answer counts");
CHECK(sp_ack_answer(id, 1, "second") != 0,
"a second answer to the same request is refused");
CHECK(sp_ack_wait(id, 5, who, sizeof(who)) == 0 &&
strcmp(who, "first") == 0, "the first answer wins");
}
/* ---- a directory others may write into is not trusted with a yes ---- */
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
(void)system("mkdir -p /var/tmp/splinter_test_ack_open && "
"chmod 0777 /var/tmp/splinter_test_ack_open");
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack_open", 1);
CHECK(sp_ack_request("run", "x", "destructive", 1) < 0,
"a world-writable ack directory is refused");
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
printf("\n%d checks, %d failures\n", checks, fails);
(void)system("rm -rf /var/tmp/splinter_test_ack");
return fails == 0 ? 0 : 1;
}