From 295af1781e6dca1cbc74d7156ad05c9a43f87156 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sun, 27 Sep 2026 11:18:17 +0000 Subject: [PATCH] Fix nickname-connect failures and add ALLOWED_SERVERS gateway allowlist - Dockerfile: install ca-certificates in the runtime stage. Its absence broke the Rust connector's native-tls HTTPS call to the TeamSpeak nickname-lookup API for every nickname-based connection (GitHub issue #11, "mgonline" report) - Node's own bundled certs masked this everywhere else in the gateway, so only that one HTTPS call failed. - tsclientlib bump: resolver.rs now surfaces the real per-step errors instead of an empty list when every resolution method fails (see that submodule's own commit for detail). - gateway: new ALLOWED_SERVERS env var lets self-hosting admins lock their instance down to specific server(s) (with *.example.com wildcard support) instead of accepting connections to any address. Documented in the README env var table. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- Dockerfile | 5 +++++ README.md | 1 + gateway/src/index.ts | 39 +++++++++++++++++++++++++++++++++++++++ tsclientlib | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 47f19dc..d4b365e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -57,6 +57,11 @@ FROM node:22-bookworm-slim AS runtime LABEL org.opencontainers.image.title="WebSpeak3" LABEL org.opencontainers.image.description="Self-hosted web client for TeamSpeak 3 servers" WORKDIR /app +# ts-connector (Rust, native-tls) needs the OS CA bundle for its HTTPS calls +# (e.g. TeamSpeak nickname lookup) - Node bundles its own certs and works +# without this, but native-tls doesn't, so it's not optional here. +RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \ + && rm -rf /var/lib/apt/lists/* COPY gateway/package*.json ./ # npm/npx are only needed to install the gateway's runtime deps; the # container never runs either afterwards, so strip them (plus npm's own diff --git a/README.md b/README.md index 408029e..fd34edc 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ All optional; add them under `environment:` in your compose override next to `PO | `STORE_DATA_FILE` | `store-themes.json` in the container's working dir | Where submitted themes (including screenshots) are stored. Mount a volume over this path if they should survive a container recreate. | | `STORE_ALLOWED_ORIGIN` | `*` | CORS origin allowed to call the store endpoint, if you want to restrict it to your own frontend's origin. | | `STORE_ADMIN_TOKEN` | unset | Bearer token that lets its holder publish under the reserved author name(s) and moderate (delete) any theme. Without it, nobody can. | +| `ALLOWED_SERVERS` | unset (any server) | Comma-separated list of server addresses this gateway is allowed to connect to (e.g. `ts.example.com,*.example.net`). A port is matched separately from the host, so listing `ts.example.com` allows any port on it. An entry starting with `*.` matches any subdomain (`*.example.com` allows `ts.example.com`, not `example.com` itself - list that separately too if it should also be reachable). Locks a self-hosted instance down to specific server(s) instead of acting as a general "connect anywhere" client; a rejected `connect` gets an error back before any connector process is even spawned. |
🛠️ Manual installation (without Docker) diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 248c823..71b1ef7 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -25,6 +25,39 @@ function parsePrivilegeKey(msg: { privilegeKey?: unknown; token?: unknown }): st return trimmed || undefined; } +// Self-hosting admins may want to lock their instance to specific server(s) +// instead of leaving it as a general-purpose "connect anywhere" client. +// Unset (the default) means unrestricted, unchanged from before this existed. +const ALLOWED_SERVERS = (process.env.ALLOWED_SERVERS ?? "") + .split(",") + .map((s) => s.trim().toLowerCase()) + .filter(Boolean); + +// Strips a trailing ":port" so "ts.example.com:9987" and "ts.example.com" +// both match a plain "ts.example.com" allowlist entry. IPv6 literals come +// wrapped in brackets ("[::1]:9987"); nicknames have neither and pass through +// as-is. Deliberately not full URL parsing - `host` here is never a URL. +function stripPort(host: string): string { + if (host.startsWith("[")) { + const end = host.indexOf("]"); + return end === -1 ? host : host.slice(1, end); + } + const i = host.lastIndexOf(":"); + return i !== -1 && /^\d+$/.test(host.slice(i + 1)) ? host.slice(0, i) : host; +} + +function isServerAllowed(host: unknown): boolean { + if (ALLOWED_SERVERS.length === 0) return true; + if (typeof host !== "string") return false; + const target = stripPort(host).trim().toLowerCase(); + return ALLOWED_SERVERS.some((entry) => { + // "*.example.com" matches any subdomain (TLS-wildcard-style: not the + // bare apex itself - list that separately if it should be allowed too). + if (entry.startsWith("*.")) return target.endsWith(entry.slice(1)); + return target === entry; + }); +} + const PORT = Number(process.env.PORT ?? 8080); // In production (Docker), the built web app lives alongside the gateway and @@ -516,6 +549,12 @@ wss.on("connection", (socket: WebSocket) => { async function handleMessage(msg: any): Promise { switch (msg.type) { case "connect": { + if (!isServerAllowed(msg.host)) { + socket.send( + JSON.stringify({ type: "error", message: "This server is not allowed by this gateway's admin." }), + ); + break; + } // Replacing a connection on the same socket: tear down the previous // connector so we don't leak processes. if (connection) { diff --git a/tsclientlib b/tsclientlib index 7361a8d..db29777 160000 --- a/tsclientlib +++ b/tsclientlib @@ -1 +1 @@ -Subproject commit 7361a8d71d3d3b0dc4588191b1753a101f6d9e5d +Subproject commit db297774c4cd6b6c6e28480d4565c3d2c29831f7