diff --git a/Makefile b/Makefile index aed6c1c..12fda31 100644 --- a/Makefile +++ b/Makefile @@ -341,6 +341,7 @@ check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS) tests/test_junction trap 'rm -rf "$$SPLINTER_SESSION_DIR"' EXIT; \ for t in $(TESTS); do echo "== $$t"; ./$$t; done; \ echo "== tests/test_delegate_cli.sh"; sh tests/test_delegate_cli.sh; \ + echo "== tests/test_args.sh"; sh tests/test_args.sh; \ echo "== tests/test_interview.sh"; sh tests/test_interview.sh; \ echo "== tests/test_gateway.sh"; sh tests/test_gateway.sh; \ echo "== tests/test_a2a.sh"; sh tests/test_a2a.sh; \ diff --git a/docs/splinter.1 b/docs/splinter.1 index 1aa308b..5ba46e1 100644 --- a/docs/splinter.1 +++ b/docs/splinter.1 @@ -98,6 +98,8 @@ Beyond 127.x it needs .Ev SPLINTER_A2A_SERVER_TOKEN . .It Fl -a2a-listen-port Ar N A2A port (default 8099). +A port that is not a number between 1 and 65535 is refused before anything +binds. .It Fl -openai-listen Ar IP Serve an OpenAI-compatible endpoint .Pq Pa /v1/chat/completions , /v1/models @@ -111,6 +113,8 @@ Beyond loopback it needs .Pa docs/GATEWAY.md . .It Fl -openai-listen-port Ar N Gateway port (default 8097). +A port that is not a number between 1 and 65535 is refused before anything +binds. .It Fl -serve Serve the engine on a Unix socket instead of drawing a screen. Each request and event is one JSON object per line; see diff --git a/src/main.c b/src/main.c index 6613579..375332a 100644 --- a/src/main.c +++ b/src/main.c @@ -233,6 +233,30 @@ struct opts { const char *socket; }; +/* + * A TCP port from the command line. + * + * atoi() took anything: "--openai-listen-port abc" bound port 0 (a random + * port the operator was never told) and "-5" bound 65531. Both looked like a + * running server on a port nobody chose, which is worse than a refusal. + */ +static int +port_arg(const char *what, const char *s, int *out) +{ + char *end = NULL; + long v; + + errno = 0; + v = strtol(s, &end, 10); + if (errno != 0 || end == s || *end != '\0' || v < 1 || v > 65535) { + fprintf(stderr, "splinter: %s wants a port between 1 and " + "65535, not '%s'\n", what, s); + return -1; + } + *out = (int)v; + return 0; +} + /* the yes/no word of --ack; -1 for anything else */ static int ack_word(const char *w) @@ -379,7 +403,9 @@ parse_args(int argc, char **argv, struct opts *o, struct sp_agent_config *cfg) "a number\n"); return 2; } - o->mcp_port = atoi(argv[++i]); + if (port_arg("--mcp-listen-port", argv[++i], + &o->mcp_port) != 0) + return 2; } else if (!strcmp(a, "--a2a-server-listen")) { if (i + 1 >= argc) { fprintf(stderr, "splinter: --a2a-server-listen " @@ -400,14 +426,18 @@ parse_args(int argc, char **argv, struct opts *o, struct sp_agent_config *cfg) "needs a number\n"); return 2; } - o->openai_port = atoi(argv[++i]); + if (port_arg("--openai-listen-port", argv[++i], + &o->openai_port) != 0) + return 2; } else if (!strcmp(a, "--a2a-listen-port")) { if (i + 1 >= argc) { fprintf(stderr, "splinter: --a2a-listen-port needs " "a number\n"); return 2; } - o->a2a_port = atoi(argv[++i]); + if (port_arg("--a2a-listen-port", argv[++i], + &o->a2a_port) != 0) + return 2; } else if (!strcmp(a, "-c") || !strcmp(a, "--continue")) o->continue_here = 1; /* diff --git a/tests/test_args.sh b/tests/test_args.sh new file mode 100755 index 0000000..d21787d --- /dev/null +++ b/tests/test_args.sh @@ -0,0 +1,70 @@ +#!/bin/sh +# +# splinter -- the command line refuses what it cannot honour. +# +# What it guards: a port was read with atoi(), so "--openai-listen-port abc" +# bound port 0 (a random port the operator was never told) and "-5" bound +# 65531. Both started a server on a port nobody chose and said nothing. +# Now a bad port is refused before anything binds, and the message names the +# option and the value. +set -u +cd "$(dirname "$0")/.." || exit 1 +. "$(dirname "$0")/sh/port.sh" +D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-args-XXXXXX") || exit 1 +trap 'rm -rf "$D"' EXIT +fails=0 +check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; } + +# a port that is not a port: the run must fail, and say why +refuses() { # refuses