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>
430 lines
10 KiB
C
430 lines
10 KiB
C
/*
|
|
* splinter -- the MCP server.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The other direction of plugins/mcp: here splinter *is* the server, so another
|
|
* agent can use its tools over MCP.
|
|
*
|
|
* splinter --mcp-server-listen 127.0.0.1 --mcp-listen-port 8098
|
|
* SPLINTER_MCP_TOKEN=... splinter --mcp-server-listen 0.0.0.0 ...
|
|
*
|
|
* Speaks newline-delimited JSON-RPC over TCP - the same framing MCP uses on
|
|
* stdio, just on a socket. Handles initialize, tools/list and tools/call, plus
|
|
* the notifications a client sends.
|
|
*
|
|
* Authorization is the point: a read tool is served, a mutating or destructive
|
|
* one is refused unless the server was started with --autonomous. A remote agent
|
|
* does not get to run shell commands on this machine because it asked nicely. *
|
|
* Who may connect at all is the step before that. 'read' alone hands out every
|
|
* file this user can open, so an address other than loopback needs a token:
|
|
* SPLINTER_MCP_TOKEN, and every connection starts with the line
|
|
*
|
|
* AUTH <token>
|
|
*
|
|
* before any JSON-RPC; anything else closes it. Without a token the server
|
|
* refuses to listen beyond 127.0.0.0/8. There is no TLS - put it behind ssh or
|
|
* a tunnel when the network is not yours.
|
|
*/
|
|
#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/time.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
#include "mcpserver.h"
|
|
|
|
static volatile int mcp_stop;
|
|
|
|
static void
|
|
on_signal(int sig)
|
|
{
|
|
(void)sig;
|
|
mcp_stop = 1;
|
|
}
|
|
|
|
/* The object that follows "arguments" in the raw line, braces balanced. */
|
|
static const char *
|
|
slice_object(const char *at, size_t *lenp)
|
|
{
|
|
const char *start;
|
|
int depth = 0, in_str = 0, esc = 0;
|
|
|
|
start = strchr(at, '{');
|
|
if (start == NULL)
|
|
return NULL;
|
|
for (at = start; *at != '\0'; at++) {
|
|
if (in_str) {
|
|
if (esc)
|
|
esc = 0;
|
|
else if (*at == '\\')
|
|
esc = 1;
|
|
else if (*at == '"')
|
|
in_str = 0;
|
|
continue;
|
|
}
|
|
if (*at == '"')
|
|
in_str = 1;
|
|
else if (*at == '{')
|
|
depth++;
|
|
else if (*at == '}') {
|
|
depth--;
|
|
if (depth == 0) {
|
|
*lenp = (size_t)(at - start) + 1;
|
|
return start;
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
write_all(int fd, const char *buf, size_t len)
|
|
{
|
|
while (len > 0) {
|
|
ssize_t n = write(fd, buf, len);
|
|
|
|
if (n <= 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
return;
|
|
}
|
|
buf += n;
|
|
len -= (size_t)n;
|
|
}
|
|
}
|
|
|
|
static void
|
|
reply_ok(int fd, int id, const char *result)
|
|
{
|
|
struct sp_buf b;
|
|
|
|
if (sp_buf_init(&b) != 0)
|
|
return;
|
|
sp_buf_printf(&b, "{\"jsonrpc\":\"2.0\",\"id\":%d,\"result\":%s}\n",
|
|
id, result);
|
|
write_all(fd, b.p, b.len);
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
reply_error(int fd, int id, int code, const char *message)
|
|
{
|
|
struct sp_buf b;
|
|
|
|
if (sp_buf_init(&b) != 0)
|
|
return;
|
|
sp_buf_printf(&b, "{\"jsonrpc\":\"2.0\",\"id\":%d,\"error\":"
|
|
"{\"code\":%d,\"message\":\"", id, code);
|
|
(void)sp_json_escape(&b, message);
|
|
sp_buf_puts(&b, "\"}}\n");
|
|
write_all(fd, b.p, b.len);
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
reply_tools(int fd, int id, const struct sp_registry *reg)
|
|
{
|
|
struct sp_buf b;
|
|
size_t i;
|
|
|
|
if (sp_buf_init(&b) != 0)
|
|
return;
|
|
sp_buf_puts(&b, "{\"tools\":[");
|
|
for (i = 0; i < reg->len; i++) {
|
|
const struct sp_tool *t = ®->tools[i];
|
|
|
|
if (i > 0)
|
|
sp_buf_puts(&b, ",");
|
|
sp_buf_puts(&b, "{\"name\":\"");
|
|
(void)sp_json_escape(&b, t->name);
|
|
sp_buf_puts(&b, "\",\"description\":\"");
|
|
(void)sp_json_escape(&b, t->description != NULL ? t->description :
|
|
"");
|
|
sp_buf_puts(&b, "\",\"inputSchema\":");
|
|
sp_buf_puts(&b, t->params != NULL ? t->params :
|
|
"{\"type\":\"object\",\"properties\":{}}");
|
|
sp_buf_puts(&b, "}");
|
|
}
|
|
sp_buf_puts(&b, "]}");
|
|
reply_ok(fd, id, b.p);
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
call_tool(int fd, int id, const struct sp_registry *reg, const char *line,
|
|
int allow_mutate)
|
|
{
|
|
const struct sp_tool *t;
|
|
const char *name, *argstart;
|
|
struct sp_buf raw, out;
|
|
mcp_json *v;
|
|
size_t arglen = 0;
|
|
|
|
v = mcp_parse(line);
|
|
if (v == NULL) {
|
|
reply_error(fd, id, -32700, "parse error");
|
|
return;
|
|
}
|
|
name = NULL;
|
|
argstart = NULL;
|
|
{
|
|
const mcp_json *params = mcp_arg(v, "params");
|
|
const mcp_json *args;
|
|
|
|
if (params != NULL) {
|
|
name = mcp_arg_string(params, "name");
|
|
args = mcp_arg(params, "arguments");
|
|
(void)args;
|
|
}
|
|
}
|
|
if (name == NULL) {
|
|
mcp_json_free(v);
|
|
reply_error(fd, id, -32602, "params.name is required");
|
|
return;
|
|
}
|
|
t = sp_registry_find(reg, name);
|
|
if (t == NULL) {
|
|
mcp_json_free(v);
|
|
reply_error(fd, id, -32601, "unknown tool");
|
|
return;
|
|
}
|
|
if (t->auth != SP_AUTH_READ && !allow_mutate) {
|
|
mcp_json_free(v);
|
|
reply_error(fd, id, -32000, "tool needs authorization; start the "
|
|
"server with --autonomous to allow mutating calls");
|
|
return;
|
|
}
|
|
/* the arguments object, as text - tools parse their own arguments */
|
|
{
|
|
const char *p = strstr(line, "\"arguments\"");
|
|
|
|
if (p != NULL) {
|
|
const char *slice = slice_object(p, &arglen);
|
|
|
|
argstart = slice;
|
|
}
|
|
}
|
|
mcp_json_free(v);
|
|
|
|
if (sp_buf_init(&raw) != 0)
|
|
return;
|
|
if (argstart != NULL)
|
|
(void)sp_buf_add(&raw, argstart, arglen);
|
|
else
|
|
(void)sp_buf_puts(&raw, "{}");
|
|
|
|
if (sp_buf_init(&out) != 0) {
|
|
sp_buf_free(&raw);
|
|
return;
|
|
}
|
|
if (t->run(raw.p, &out, t->ctx) != 0)
|
|
sp_buf_puts(&out, "error: tool failed");
|
|
{
|
|
struct sp_buf res;
|
|
|
|
if (sp_buf_init(&res) == 0) {
|
|
sp_buf_puts(&res, "{\"content\":[{\"type\":\"text\",\"text\":\"");
|
|
(void)sp_json_escape(&res, out.p != NULL ? out.p : "");
|
|
sp_buf_puts(&res, "\"}]}");
|
|
reply_ok(fd, id, res.p);
|
|
sp_buf_free(&res);
|
|
}
|
|
}
|
|
sp_buf_free(&raw);
|
|
sp_buf_free(&out);
|
|
}
|
|
|
|
static void
|
|
handle_line(int fd, const struct sp_registry *reg, const char *line,
|
|
int allow_mutate)
|
|
{
|
|
mcp_json *v;
|
|
const char *method;
|
|
int id = -1;
|
|
|
|
v = mcp_parse(line);
|
|
if (v == NULL) {
|
|
reply_error(fd, 0, -32700, "parse error");
|
|
return;
|
|
}
|
|
method = mcp_arg_string(v, "method");
|
|
id = mcp_arg_int(v, "id", -1);
|
|
if (method == NULL) {
|
|
mcp_json_free(v);
|
|
return;
|
|
}
|
|
if (strcmp(method, "initialize") == 0) {
|
|
char res[512];
|
|
|
|
snprintf(res, sizeof(res),
|
|
"{\"protocolVersion\":\"2024-11-05\",\"capabilities\":"
|
|
"{\"tools\":{}},\"serverInfo\":{\"name\":\"splinter\","
|
|
"\"version\":\"%s\"}}", SP_VERSION);
|
|
mcp_json_free(v);
|
|
reply_ok(fd, id, res);
|
|
return;
|
|
}
|
|
if (strcmp(method, "tools/list") == 0) {
|
|
mcp_json_free(v);
|
|
reply_tools(fd, id, reg);
|
|
return;
|
|
}
|
|
if (strcmp(method, "tools/call") == 0) {
|
|
mcp_json_free(v);
|
|
call_tool(fd, id, reg, line, allow_mutate);
|
|
return;
|
|
}
|
|
mcp_json_free(v);
|
|
if (id >= 0)
|
|
reply_error(fd, id, -32601, "unknown method");
|
|
/* a notification has no id and gets no answer */
|
|
}
|
|
|
|
/* compare without stopping at the first difference: no timing hint */
|
|
static int
|
|
token_equal(const char *a, const char *b)
|
|
{
|
|
size_t la = strlen(a), lb = strlen(b), i;
|
|
unsigned char d = la != lb;
|
|
|
|
for (i = 0; i < la; i++)
|
|
d |= (unsigned char)(a[i] ^ b[i % (lb > 0 ? lb : 1)]);
|
|
return d == 0;
|
|
}
|
|
|
|
static void
|
|
serve_conn(int fd, const struct sp_registry *reg, int allow_mutate,
|
|
const char *token)
|
|
{
|
|
char line[16384];
|
|
size_t used = 0;
|
|
int authed = token == NULL;
|
|
|
|
for (;;) {
|
|
ssize_t got = read(fd, line + used, sizeof(line) - used - 1);
|
|
|
|
if (got <= 0) {
|
|
if (got < 0 && errno == EINTR)
|
|
continue;
|
|
return;
|
|
}
|
|
used += (size_t)got;
|
|
line[used] = '\0';
|
|
for (;;) {
|
|
char *nl = strchr(line, '\n');
|
|
size_t len;
|
|
|
|
if (nl == NULL)
|
|
break;
|
|
*nl = '\0';
|
|
if (!authed) {
|
|
size_t l = strlen(line);
|
|
|
|
if (l > 0 && line[l - 1] == '\r')
|
|
line[l - 1] = '\0';
|
|
if (strncmp(line, "AUTH ", 5) != 0 ||
|
|
!token_equal(line + 5, token)) {
|
|
reply_error(fd, 0, -32001,
|
|
"authentication required");
|
|
return;
|
|
}
|
|
authed = 1;
|
|
} else if (line[0] == '{')
|
|
handle_line(fd, reg, line, allow_mutate);
|
|
len = (size_t)(nl - line) + 1;
|
|
memmove(line, line + len, used - len + 1);
|
|
used -= len;
|
|
}
|
|
if (used >= sizeof(line) - 1)
|
|
used = 0; /* a line we cannot hold */
|
|
}
|
|
}
|
|
|
|
int
|
|
sp_mcp_serve(const struct sp_registry *reg, const char *ip, int port,
|
|
int allow_mutate, struct sp_buf *err)
|
|
{
|
|
int s, one = 1;
|
|
struct sockaddr_in addr;
|
|
|
|
const char *token = getenv("SPLINTER_MCP_TOKEN");
|
|
|
|
if (ip == NULL || *ip == '\0')
|
|
ip = "127.0.0.1";
|
|
if (token != NULL && *token == '\0')
|
|
token = NULL;
|
|
if (token == NULL && strncmp(ip, "127.", 4) != 0) {
|
|
sp_buf_printf(err, "mcp: refusing to serve on %s without "
|
|
"SPLINTER_MCP_TOKEN: 'read' alone would give the network "
|
|
"every file of this user", ip);
|
|
return -1;
|
|
}
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (s < 0) {
|
|
sp_buf_printf(err, "mcp: socket: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
(void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((unsigned short)port);
|
|
if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1) {
|
|
sp_buf_printf(err, "mcp: '%s' is not an address", ip);
|
|
close(s);
|
|
return -1;
|
|
}
|
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
|
sp_buf_printf(err, "mcp: bind %s:%d: %s", ip, port,
|
|
strerror(errno));
|
|
close(s);
|
|
return -1;
|
|
}
|
|
if (listen(s, 8) != 0) {
|
|
sp_buf_printf(err, "mcp: listen: %s", strerror(errno));
|
|
close(s);
|
|
return -1;
|
|
}
|
|
(void)signal(SIGINT, on_signal);
|
|
(void)signal(SIGTERM, on_signal);
|
|
/* a client that hangs up mid-reply is a closed connection, not our death */
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
|
fprintf(stderr, "splinter: MCP server on %s:%d (%zu tools, %s, %s)\n",
|
|
ip, port, reg->len, allow_mutate ? "mutating allowed" : "read only",
|
|
token != NULL ? "token required" : "loopback, no token");
|
|
|
|
while (!mcp_stop) {
|
|
int c = accept(s, NULL, NULL);
|
|
|
|
if (c < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
break;
|
|
}
|
|
{
|
|
/*
|
|
* One connection at a time, so an idle one must not hold
|
|
* the server forever: five quiet minutes and it is closed.
|
|
*/
|
|
struct timeval tv = { 300, 0 };
|
|
|
|
(void)setsockopt(c, SOL_SOCKET, SO_RCVTIMEO, &tv,
|
|
sizeof(tv));
|
|
}
|
|
serve_conn(c, reg, allow_mutate, token);
|
|
close(c);
|
|
}
|
|
close(s);
|
|
fprintf(stderr, "splinter: MCP server stopped\n");
|
|
return 0;
|
|
}
|