Skip to content

Commit a4901b6

Browse files
authored
fix(gateway): preserve managed Tailscale ingress compatibility (#126519)
* fix(gateway): accept tailnet peers on managed Funnel * fix(gateway): release Tailscale claim on interrupt * test(gateway): cover Funnel tailnet ingress * docs: remove Tailscale changelog entry
1 parent adc6084 commit a4901b6

7 files changed

Lines changed: 120 additions & 21 deletions

‎docs/gateway/tailscale.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ When a bindable Tailnet IPv4 is present, the Gateway also requires `http://127.0
7676

7777
Prefer `OPENCLAW_GATEWAY_PASSWORD` over committing a password to disk.
7878

79+
The Funnel URL also remains usable from devices inside the tailnet. Tailscale marks public
80+
requests as Funnel traffic but sends tailnet peers through its Serve identity path instead;
81+
OpenClaw recognizes both paths on its dedicated listener and still requires the configured
82+
Funnel password.
83+
7984
## CLI examples
8085

8186
```bash
@@ -128,7 +133,8 @@ This compatibility path does not grant managed Tailscale semantics: `gateway.aut
128133
### Tailscale prerequisites and limits
129134

130135
- Serve requires HTTPS enabled for your tailnet; the CLI prompts if it is missing.
131-
- Serve injects Tailscale identity headers; Funnel does not.
136+
- Tailnet Serve traffic injects Tailscale identity headers. Public Funnel traffic uses a Funnel
137+
marker instead, while tailnet access to the same Funnel URL follows the Serve identity path.
132138
- OpenClaw-managed Serve/Funnel proxy to a dedicated `127.0.0.1:<ephemeral-port>` listener while ordinary local clients keep the configured Gateway port. Startup fails closed rather than sharing listener provenance, and the foreground claim releases the route when its Gateway owner disappears.
133139
- Funnel requires Tailscale v1.38.3+, MagicDNS, HTTPS enabled, and a funnel node attribute.
134140
- Funnel only supports ports `443`, `8443`, and `10000` over TLS.

‎src/gateway/auth.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,33 @@ describe("gateway auth", () => {
768768
).resolves.toMatchObject({ ok: true, method: "password" });
769769
});
770770

771+
it("uses password auth for tailnet peers on managed Funnel", async () => {
772+
const req = createTailscaleForwardedReq(false);
773+
markGatewayIngressTransport(req, { kind: "managed-tailscale", mode: "funnel" });
774+
775+
await expect(
776+
authorizeWsControlUiGatewayConnect({
777+
auth: { mode: "password", password: "secret", allowTailscale: false },
778+
connectAuth: { password: "secret" },
779+
req,
780+
}),
781+
).resolves.toMatchObject({ ok: true, method: "password" });
782+
});
783+
784+
it("requires the Funnel password when Tailscale header auth is explicitly enabled", async () => {
785+
const req = createTailscaleForwardedReq(false);
786+
markGatewayIngressTransport(req, { kind: "managed-tailscale", mode: "funnel" });
787+
788+
await expect(
789+
authorizeWsControlUiGatewayConnect({
790+
auth: { mode: "password", password: "secret", allowTailscale: true },
791+
connectAuth: null,
792+
tailscaleWhois: createTailscaleWhois(),
793+
req,
794+
}),
795+
).resolves.toMatchObject({ ok: false, reason: "password_missing" });
796+
});
797+
771798
it("allows an origin-less same-origin image through the profile avatar surface", async () => {
772799
const limiter = createLimiterSpy();
773800
const req = createTailscaleForwardedReq();

‎src/gateway/ingress-attribution.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,19 @@ describe("gateway ingress attribution", () => {
136136
});
137137
});
138138

139-
it("requires the Tailscale Funnel marker on the managed Funnel listener", async () => {
139+
it("attributes unmarked tailnet traffic to the managed Funnel policy", async () => {
140+
const req = request({ forwardedFor: "100.64.0.10", login: "[email protected]" });
141+
markGatewayIngressTransport(req, { kind: "managed-tailscale", mode: "funnel" });
142+
143+
expect(prepareGatewayIngressAttribution({ req })).toMatchObject({
144+
kind: "tailscale-funnel",
145+
clientIp: "100.64.0.10",
146+
});
147+
});
148+
149+
it("rejects an invalid marker on the managed Funnel listener", async () => {
140150
const req = request({ forwardedFor: "203.0.113.10" });
151+
req.headers["tailscale-funnel-request"] = "?0";
141152
markGatewayIngressTransport(req, { kind: "managed-tailscale", mode: "funnel" });
142153

143154
expect(prepareGatewayIngressAttribution({ req })).toMatchObject({

‎src/gateway/ingress-attribution.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function resolveManagedTailscaleIngress(params: {
162162
}
163163
const funnelMarker = headerValue(req.headers?.["tailscale-funnel-request"]);
164164
if (mode === "funnel") {
165-
return funnelMarker === "?1"
165+
return !funnelMarker || funnelMarker === "?1"
166166
? attributed("tailscale-funnel", clientIp)
167167
: unattributableProxy(remoteAddress);
168168
}

‎src/gateway/server-runtime-state.tailscale.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe("managed Tailscale gateway ingress", () => {
145145
expect(managed.status).toBe(200);
146146
});
147147

148-
it("requires the Funnel marker on the dedicated Funnel listener", async () => {
148+
it("accepts tailnet and public ingress on the dedicated Funnel listener", async () => {
149149
const runtime = await createGatewayRuntimeStateForTest(undefined, {
150150
tailscaleMode: "funnel",
151151
getReadiness: () => ({ ready: true, failing: [], uptimeMs: 1 }),
@@ -174,9 +174,16 @@ describe("managed Tailscale gateway ingress", () => {
174174
path: "/ready",
175175
headers: { ...baseHeaders, "tailscale-funnel-request": "?1" },
176176
});
177+
const malformedMarker = await requestStatus({
178+
host: endpoint.host,
179+
port: endpoint.port,
180+
path: "/ready",
181+
headers: { ...baseHeaders, "tailscale-funnel-request": "true" },
182+
});
177183

178-
expect(missingMarker.status).toBe(403);
184+
expect(missingMarker.status).toBe(200);
179185
expect(marked.status).toBe(200);
186+
expect(malformedMarker.status).toBe(403);
180187
});
181188

182189
it("rejects external Funnel ingress when gateway auth is disabled", async () => {

‎src/infra/tailscale-route-owner.worker.test.ts‎

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ import {
88
} from "./tailscale-route-owner-protocol.js";
99
import { runTailscaleRouteOwner } from "./tailscale-route-owner.worker.js";
1010

11+
function spawnRouteOwnerFixture() {
12+
const workerPath = fileURLToPath(new URL("./tailscale-route-owner.worker.ts", import.meta.url));
13+
const fixturePath = fileURLToPath(
14+
new URL("../../test/fixtures/tailscale-foreground-fixture.mjs", import.meta.url),
15+
);
16+
const worker = fork(
17+
workerPath,
18+
[
19+
TAILSCALE_ROUTE_OWNER_ARG,
20+
JSON.stringify({ argv: [fixturePath, "serve", "--yes", "--bg=false", "18789"] }),
21+
],
22+
{ execArgv: ["--import", "tsx"], stdio: ["ignore", "ignore", "ignore", "ipc"] },
23+
);
24+
const messages: TailscaleRouteOwnerMessage[] = [];
25+
worker.on("message", (message: TailscaleRouteOwnerMessage) => messages.push(message));
26+
return { messages, worker };
27+
}
28+
1129
describe("Tailscale route owner", () => {
1230
it("reports readiness and terminates the foreground claim when its owner stops", async () => {
1331
const messages: TailscaleRouteOwnerMessage[] = [];
@@ -49,22 +67,7 @@ describe("Tailscale route owner", () => {
4967
it.runIf(process.platform !== "win32")(
5068
"terminates the claim when the Gateway IPC owner disappears",
5169
async () => {
52-
const workerPath = fileURLToPath(
53-
new URL("./tailscale-route-owner.worker.ts", import.meta.url),
54-
);
55-
const fixturePath = fileURLToPath(
56-
new URL("../../test/fixtures/tailscale-foreground-fixture.mjs", import.meta.url),
57-
);
58-
const worker = fork(
59-
workerPath,
60-
[
61-
TAILSCALE_ROUTE_OWNER_ARG,
62-
JSON.stringify({ argv: [fixturePath, "serve", "--yes", "--bg=false", "18789"] }),
63-
],
64-
{ execArgv: ["--import", "tsx"], stdio: ["ignore", "ignore", "ignore", "ipc"] },
65-
);
66-
const messages: TailscaleRouteOwnerMessage[] = [];
67-
worker.on("message", (message: TailscaleRouteOwnerMessage) => messages.push(message));
70+
const { messages, worker } = spawnRouteOwnerFixture();
6871
try {
6972
await vi.waitFor(() => {
7073
expect(messages).toContainEqual({ type: "ready" });
@@ -84,4 +87,44 @@ describe("Tailscale route owner", () => {
8487
}
8588
},
8689
);
90+
91+
it.runIf(process.platform !== "win32")(
92+
"terminates the claim before exiting on an interactive interrupt",
93+
async () => {
94+
const { messages, worker } = spawnRouteOwnerFixture();
95+
let routePid: number | undefined;
96+
try {
97+
await vi.waitFor(() => {
98+
expect(messages).toContainEqual({ type: "ready" });
99+
});
100+
const spawned = messages.find((message) => message.type === "spawned");
101+
if (!spawned) {
102+
throw new Error("route owner did not report its claim process");
103+
}
104+
routePid = spawned.pid;
105+
const exit = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>(
106+
(resolve) => {
107+
worker.once("exit", (code, signal) => resolve({ code, signal }));
108+
},
109+
);
110+
worker.kill("SIGINT");
111+
112+
await expect(exit).resolves.toEqual({ code: 0, signal: null });
113+
await vi.waitFor(() => {
114+
expect(() => process.kill(spawned.pid, 0)).toThrow();
115+
});
116+
} finally {
117+
if (worker.exitCode === null && worker.signalCode === null) {
118+
worker.kill("SIGKILL");
119+
}
120+
if (routePid) {
121+
try {
122+
process.kill(routePid, "SIGKILL");
123+
} catch {
124+
// Already released by the worker.
125+
}
126+
}
127+
}
128+
},
129+
);
87130
});

‎src/infra/tailscale-route-owner.worker.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ export function runTailscaleRouteOwner(
137137
if (process.argv[2] === TAILSCALE_ROUTE_OWNER_ARG) {
138138
try {
139139
const owner = runTailscaleRouteOwner(parseStart(process.argv[3]));
140+
// Terminal signals reach this worker with the Gateway process group. Drain the
141+
// detached route child first or the HTTPS port remains claimed after Gateway exit.
142+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) {
143+
process.once(signal, owner.stop);
144+
}
140145
process.once("disconnect", owner.stop);
141146
process.once("message", (message: unknown) => {
142147
if (isRecord(message) && message.type === "stop") {

0 commit comments

Comments
 (0)