Findings from a pass over the daemon, the sandbox and the surfaces. HTTP: a loopback dashboard without a token was reachable from any web page. The wildcard CORS grant and an OPTIONS branch before the token check let a cross-origin page POST into the one session, and a DNS-rebinding page could read it under its own name. Requests now need a loopback Host while no token is configured, an Origin must match the Host, and there is no CORS grant. MCP connections get a receive/send timeout instead of pinning a thread forever. CLI: --check silently ignored an unreadable or invalid config and printed a green self-test, because the load error was discarded. It now fails with the parser message, prints the sandbox plan, and only then runs the scripted turn. --self-test still works without a config. Budgets: max_turn_seconds (default 1800) cancels a turn that outlives it and records turn_timeout; the answer produced after the cancel is discarded. External chat now counts against max_impulses_per_hour instead of bypassing it. Stuck detection: kstuck_turn() reset the whole ladder whenever a turn called a mutating tool, so a loop of failing 'run' calls never escalated. The ladder now resets only after a turn whose tool signals are clean. The dashboard's stuck level is a locked snapshot instead of a read of daemon-private state. Ledger: impulse records stored the running session total as if it were that impulse's usage; they now store per-turn deltas, and turn_end logs them. Config: refuse / and .. in rootfs/state/workspace, refuse nested trees (a state inside the disposable rootfs would be deleted with it), and reject negative sizes. Event log: O_NOFOLLOW|O_CLOEXEC. Makefile: check-asan reused the same object paths, so a plain make afterwards linked uninstrumented code against sanitizer objects and failed. It now cleans before and after. Tests: test_server covers Host/Origin refusal and the OPTIONS/token check; test_krang covers the path guards and the stuck reset; test_timeout proves the wall-clock cancellation with a sleeping tool. make check is now seven suites, ASAN also green, and the root integration still passes on a fresh debootstrap.
178 lines
5.3 KiB
C
178 lines
5.3 KiB
C
/*
|
|
* krang -- offline socket test: the dashboard and MCP listeners.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include "krang.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
|
#cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
get_port(int fd)
|
|
{
|
|
struct sockaddr_in sa;
|
|
socklen_t sl = sizeof(sa);
|
|
|
|
if (getsockname(fd, (struct sockaddr *)&sa, &sl) != 0)
|
|
return -1;
|
|
return ntohs(sa.sin_port);
|
|
}
|
|
|
|
static int
|
|
roundtrip(int port, const char *request, char *out, size_t cap, int line)
|
|
{
|
|
struct sockaddr_in sa;
|
|
int fd;
|
|
size_t used = 0;
|
|
ssize_t n;
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0)
|
|
return -1;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sin_family = AF_INET;
|
|
sa.sin_port = htons((unsigned short)port);
|
|
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
if (k_write_all(fd, request, strlen(request)) != 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
while (used + 1 < cap &&
|
|
(n = read(fd, out + used, cap - used - 1)) > 0) {
|
|
used += (size_t)n;
|
|
if (line && memchr(out, '\n', used) != NULL)
|
|
break;
|
|
}
|
|
out[used] = '\0';
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct krang k;
|
|
char err[512], req[512], out[8192];
|
|
int port;
|
|
|
|
memset(&k, 0, sizeof(k));
|
|
k.log.fd = -1;
|
|
pthread_mutex_init(&k.lock, NULL);
|
|
pthread_cond_init(&k.cond, NULL);
|
|
sp_buf_init(&k.err);
|
|
kconfig_defaults(&k.cfg);
|
|
snprintf(k.cfg.dashboard_host, sizeof(k.cfg.dashboard_host),
|
|
"127.0.0.1");
|
|
k.cfg.dashboard_port = 0;
|
|
snprintf(k.cfg.mcp_host, sizeof(k.cfg.mcp_host), "127.0.0.1");
|
|
k.cfg.mcp_port = 0;
|
|
snprintf(k.st.state, sizeof(k.st.state), "sleeping");
|
|
snprintf(k.st.goal, sizeof(k.st.goal), "test the server");
|
|
k.st.started_at = k_now();
|
|
|
|
CHECK(khttp_start(&k, err, sizeof(err)) == 0);
|
|
port = get_port(k.http_fd);
|
|
CHECK(port > 0);
|
|
snprintf(req, sizeof(req),
|
|
"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "200 OK") != NULL);
|
|
CHECK(strstr(out, "\"ok\":true") != NULL);
|
|
snprintf(req, sizeof(req), "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "200 OK") != NULL);
|
|
CHECK(strstr(out, "<title>krang</title>") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: localhost\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "\"goal\":\"test the server\"") != NULL);
|
|
|
|
/* a page reached through DNS rebinding carries a foreign Host */
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: evil.example\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "403 Forbidden") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: 127.evil.example\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "403 Forbidden") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: 127.0.0.1:8096\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "200 OK") != NULL);
|
|
|
|
/* cross-origin requests are refused, same-origin ones are not */
|
|
snprintf(req, sizeof(req),
|
|
"POST /api/say HTTP/1.1\r\nHost: localhost\r\n"
|
|
"Origin: http://evil.example\r\n"
|
|
"Content-Type: application/json\r\nContent-Length: 12\r\n"
|
|
"\r\n{\"text\":\"x\"}");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "403 Forbidden") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: localhost\r\n"
|
|
"Origin: http://localhost\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "200 OK") != NULL);
|
|
|
|
/* with a token, OPTIONS is not a free pass either */
|
|
snprintf(k.cfg.token, sizeof(k.cfg.token), "s3cret");
|
|
snprintf(req, sizeof(req),
|
|
"OPTIONS /api/state HTTP/1.1\r\nHost: localhost\r\n"
|
|
"Origin: http://localhost\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "401 Unauthorized") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"GET /api/state HTTP/1.1\r\nHost: localhost\r\n"
|
|
"Authorization: Bearer s3cret\r\n\r\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
|
|
CHECK(strstr(out, "200 OK") != NULL);
|
|
k.cfg.token[0] = '\0';
|
|
khttp_stop(&k);
|
|
|
|
CHECK(kmcp_start(&k, err, sizeof(err)) == 0);
|
|
port = get_port(k.mcp_fd);
|
|
CHECK(port > 0);
|
|
snprintf(req, sizeof(req),
|
|
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 1) == 0);
|
|
CHECK(strstr(out, "krang_state") != NULL);
|
|
snprintf(req, sizeof(req),
|
|
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
|
|
"\"params\":{\"name\":\"krang_state\",\"arguments\":{}}}\n");
|
|
CHECK(roundtrip(port, req, out, sizeof(out), 1) == 0);
|
|
CHECK(strstr(out, "sleeping") != NULL);
|
|
kmcp_stop(&k);
|
|
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
if (failures != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", failures);
|
|
return 1;
|
|
}
|
|
printf("test_server: ok\n");
|
|
return 0;
|
|
}
|