main: a port that is not a port is refused, not guessed

atoi() took anything on --mcp-listen-port, --a2a-listen-port and
--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.

port_arg() parses with strtol, requires the whole string, and refuses
anything outside 1..65535, naming the option and the value.

Test: tests/test_args.sh, new, in make check. Six bad values are refused
with the reason; a valid port still starts the gateway and says which one
it took. With the check reverted the test reports 6 failures, with it
"test_args: ok". make check: all tests passed.

docs/splinter.1: the two documented port options say so.
This commit is contained in:
lotherk committed 2026-11-23 12:37:42 +00:00
1 parent 0fc81398dc
commit 4ff2844955
4 files changed
+108 -3

No files matched your search

+1
View File
@@ -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; \
+4
View File
@@ -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
+33 -3
View File
@@ -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;
/*
+70
View File
@@ -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 <option> <value>
HOME="$D/home" ./splinter --no-config --script /dev/null "$1" "$2" \
>"$D/out" 2>"$D/err"
rc=$?
if [ "$rc" -eq 0 ]; then
echo "FAIL $1 $2 was accepted (rc=0)"
fails=$((fails + 1))
return 1
fi
if ! grep -q -- "$1 wants a port between 1 and 65535" "$D/err"; then
echo "FAIL $1 $2: the reason is missing"
sed -n '1,3p' "$D/err"
fails=$((fails + 1))
return 1
fi
return 0
}
mkdir -p "$D/home"
refuses --openai-listen-port abc
refuses --openai-listen-port -5
refuses --openai-listen-port 0
refuses --openai-listen-port 65536
refuses --a2a-listen-port ""
refuses --mcp-listen-port 8080x
# a port that IS a port still gets through: the refusal is not blanket, and
# the gateway says which port it took.
GOOD=$(free_port)
HOME="$D/home" ./splinter --no-config --model m \
--endpoint "http://127.0.0.1:1/v1/chat/completions" \
--openai-listen-port "$GOOD" --openai-listen 127.0.0.1 \
>"$D/out" 2>"$D/err" &
sleep 1
kill $! 2>/dev/null
wait $! 2>/dev/null
if grep -q "wants a port between" "$D/err"; then
echo "FAIL a valid port was refused"
fails=$((fails + 1))
fi
if ! grep -q "OpenAI gateway on http://127.0.0.1:$GOOD/v1" "$D/err"; then
echo "FAIL a valid port did not start the gateway"
sed -n '1,3p' "$D/err"
fails=$((fails + 1))
fi
if [ "$fails" -eq 0 ]; then
echo "test_args: ok"
exit 0
fi
echo "test_args: $fails failure(s)"
exit 1