Files
splinter/tests/test_builtin.c
T
lotherk b1acf1629b builtin: a read error is not the end of the file
`slurp` and `read` treated ferror() as EOF: `edit` then took the truncated
prefix for the whole file and wrote it back, and `read` silently showed
fewer lines. Both now report the error and name the path.

`search` still skips a file it cannot read - it only reports hits, and a
file it cannot open is not a hit - but it no longer claims a clean scan.

Evidence:
  edit /proc/self/mem      -> "error: /proc/self/mem: Input/output error"
                              (before: "is empty")
  read /proc/self/mem      -> "[...: read error after line 0: Input/output error]"
  edit <dir>               -> "Is a directory"
With the three ferror() guards removed, tests/test_builtin.c fails at
lines 221, 227 and the directory cases; with them the test is green.
make check: all tests passed, 21 suites. check_asan: 45 binaries clean.
2026-11-23 01:32:38 +00:00

359 lines
11 KiB
C

/*
* splinter -- builtin file tools.
*
* The tools are the part of the harness a model can actually break something
* with, so each of them is tested against the failure it is supposed to refuse:
* an ambiguous edit, a binary file, a write that is too large.
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.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[600];
static const struct sp_tool *
tool(struct sp_registry *r, const char *name)
{
const struct sp_tool *t = sp_registry_find(r, name);
CHECK(t != NULL);
return t;
}
static void
write_file(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(text, f);
fclose(f);
}
}
int
main(void)
{
struct sp_registry r;
struct sp_buf out;
char path[700], big[1600];
const char *copy;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-test-XXXXXX", tmp);
CHECK(mkdtemp(dir) != NULL);
memset(&r, 0, sizeof(r));
if (sp_buf_init(&out) != 0)
return 1;
CHECK(sp_builtin_register(&r) == 0);
/* the five tools exist and declare their class */
CHECK(tool(&r, "read")->auth == SP_AUTH_READ);
CHECK(tool(&r, "write")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "edit")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "search")->auth == SP_AUTH_READ);
CHECK(tool(&r, "list")->auth == SP_AUTH_READ);
CHECK(tool(&r, "run")->auth == SP_AUTH_DESTRUCTIVE);
snprintf(path, sizeof(path), "%s/a.txt", dir);
write_file(path, "erste zeile\nzweite zeile\ndritte zeile\n");
/* ---- read ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite zeile") != NULL);
CHECK(strstr(out.p, "1\terste") != NULL);
/* offset and limit */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"offset\":2,\"limit\":1}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite") != NULL);
CHECK(strstr(out.p, "erste") == NULL);
/* a missing file is an observation, not a crash */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s/nope\"}", dir);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "error:") != NULL);
/* ---- write ---- */
sp_buf_clear(&out);
snprintf(path, sizeof(path), "%s/neu.txt", dir);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"content\":\"hallo\"}", path);
CHECK(tool(&r, "write")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "wrote 5 bytes") != NULL);
/* ---- edit: exactly one match ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"hallo\",\"new\":\"welt\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "edited") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "welt") != NULL);
/* ---- edit: two matches are refused ---- */
write_file(path, "x x\n");
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "occurs 2 times") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "y") == NULL); /* nothing was written */
/*
* ---- a write that cannot finish must not eat the old file ----
*
* The tools used to truncate with fopen(path, "w") and then write, so a
* short write (a full disk, RLIMIT_FSIZE) left the operator with an
* empty file and no copy. RLIMIT_FSIZE makes the short write
* deterministic; SIGXFSZ is ignored so the write returns EFBIG instead
* of killing the test.
*/
{
struct rlimit rl;
struct sigaction sa, oldsa;
char safe[700], big2[1600];
snprintf(safe, sizeof(safe), "%s/keep.txt", dir);
write_file(safe, "wichtig\n");
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0);
CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0);
{
struct rlimit lim = rl;
lim.rlim_cur = 4; /* fewer bytes than the new text */
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
}
sp_buf_clear(&out);
snprintf(big2, sizeof(big2),
"{\"path\":\"%s\",\"content\":\"0123456789\"}", safe);
CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "error:") != NULL);
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
/* the original is untouched, byte for byte */
sp_buf_clear(&out);
snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", safe);
CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "wichtig") != NULL);
CHECK(strstr(out.p, "0123456789") == NULL);
/* and no debris was left behind */
snprintf(big2, sizeof(big2), "%s.tmp", safe);
CHECK(access(big2, F_OK) != 0);
}
/*
* ---- a write through a symlink writes the target, not the link ----
*
* The old fopen(path, "w") went through the link; a temp file plus
* rename() replaces the link instead, which would silently detach a
* dotfile setup. Both must agree: the link stays, the target changes.
*/
{
char real[700], link[700], big2[1600];
struct stat st;
snprintf(real, sizeof(real), "%s/real.conf", dir);
snprintf(link, sizeof(link), "%s/link.conf", dir);
write_file(real, "alt\n");
if (symlink(real, link) == 0) {
sp_buf_clear(&out);
snprintf(big2, sizeof(big2),
"{\"path\":\"%s\",\"content\":\"neu\\n\"}", link);
CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0);
CHECK(lstat(link, &st) == 0);
CHECK(S_ISLNK(st.st_mode)); /* still a link */
sp_buf_clear(&out);
snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", real);
CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "neu") != NULL); /* target written */
}
}
/*
* ---- a read error is reported, not mistaken for the end ----
*
* A directory opens fine on Linux and fails on the first read. read()
* used to print nothing at all, which is indistinguishable from an
* empty file; it now names the error. slurp() (behind edit/write) gets
* the same guard: an error there would hand edit() a truncated prefix
* to write back over the file.
*/
{
char big2[1600];
sp_buf_clear(&out);
snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", dir);
CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "read error") != NULL);
/* the same failure mid-read, not at the very first byte */
sp_buf_clear(&out);
CHECK(tool(&r, "read")->run("{\"path\":\"/proc/self/mem\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "error") != NULL ||
strstr(out.p, "read error") != NULL);
sp_buf_clear(&out);
snprintf(big2, sizeof(big2),
"{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", dir);
CHECK(tool(&r, "edit")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "error:") != NULL);
CHECK(strstr(out.p, "edited") == NULL);
/* slurp() behind edit(): the EIO must not read as "empty" */
sp_buf_clear(&out);
CHECK(tool(&r, "edit")->run(
"{\"path\":\"/proc/self/mem\",\"old\":\"x\",\"new\":\"y\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "Input/output error") != NULL);
CHECK(strstr(out.p, "is empty") == NULL);
}
/*
* ---- the file's own mode still decides ----
*
* A temp file plus rename() only needs write permission on the
* directory, so without a check a 0444 file would be replaced while
* fopen(path, "w") had always refused it. And a 0600 file must not
* become 0644 just because a tool wrote it.
*/
{
char ro[700], big2[1600];
struct stat st;
snprintf(ro, sizeof(ro), "%s/ro.txt", dir);
write_file(ro, "alt\n");
CHECK(chmod(ro, 0444) == 0);
sp_buf_clear(&out);
snprintf(big2, sizeof(big2),
"{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro);
CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "Permission denied") != NULL);
sp_buf_clear(&out);
snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", ro);
CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0);
CHECK(strstr(out.p, "alt") != NULL);
CHECK(chmod(ro, 0600) == 0);
sp_buf_clear(&out);
snprintf(big2, sizeof(big2),
"{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro);
CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0);
CHECK(stat(ro, &st) == 0);
CHECK((st.st_mode & 07777) == 0600); /* mode kept */
}
/* ---- read refuses a binary file ---- */
{
FILE *f;
char bin[700];
snprintf(bin, sizeof(bin), "%s/bin.dat", dir);
f = fopen(bin, "wb");
CHECK(f != NULL);
if (f != NULL) {
fwrite("abc\0def", 1, 7, f);
fclose(f);
}
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", bin);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "binary") != NULL);
}
/* ---- search and list ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"pattern\":\"zweite\",\"path\":\"%s\"}", dir);
CHECK(tool(&r, "search")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", dir);
CHECK(tool(&r, "list")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
/* ---- missing arguments are reported, never guessed ---- */
sp_buf_clear(&out);
CHECK(tool(&r, "read")->run("{}", &out, NULL) == 0);
CHECK(strstr(out.p, "required") != NULL);
/*
* ---- webfetch: the URL is the model's, so it is data, never shell ----
* The quote used to close curl's '...' and run the rest as a command,
* from a tool that needed nobody's consent.
*/
{
char mark[700];
struct stat st;
CHECK(tool(&r, "webfetch")->auth != SP_AUTH_READ);
snprintf(mark, sizeof(mark), "%s/pwned", dir);
snprintf(big, sizeof(big), "{\"url\":\"http://127.0.0.1:9/'; "
"touch %s; echo '\"}", mark);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run(big, &out, NULL) == 0);
CHECK(stat(mark, &st) != 0);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run("{\"url\":\"file:///etc/passwd\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "only http") != NULL);
CHECK(strstr(out.p, "root:") == NULL);
sp_buf_clear(&out);
CHECK(tool(&r, "webfetch")->run("{\"url\":\"-K/etc/passwd\"}",
&out, NULL) == 0);
CHECK(strstr(out.p, "only http") != NULL);
}
copy = out.p;
CHECK(copy != NULL);
sp_registry_free(&r);
sp_buf_free(&out);
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed: %s\n", strerror(errno));
}
if (failures == 0)
printf("test_builtin: ok\n");
return failures == 0 ? 0 : 1;
}