Skip to content

Commit d02e799

Browse files
committed
fix(gateway): preserve unattributable Tailscale routes
1 parent 1cbb16e commit d02e799

10 files changed

Lines changed: 56 additions & 234 deletions

‎docs/gateway/tailscale.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ This compatibility path does not grant managed Tailscale semantics: `gateway.aut
117117
- Tailscale Serve/Funnel requires the `tailscale` CLI installed and logged in.
118118
- `tailscale.mode: "funnel"` refuses to start unless auth mode is `password`, to avoid public exposure.
119119
- OpenClaw holds Serve/Funnel as a foreground Tailscale claim. Gateway startup succeeds only after the claim is active, and stopping or losing the Gateway releases it automatically.
120-
- When upgrading from an older OpenClaw release, startup automatically replaces the exact persistent HTTPS root route previously created for the configured Gateway port. OpenClaw does not change custom targets, extra paths, Tailscale Services, foreground routes, or a Serve/Funnel mode mismatch. If one of those configurations owns HTTPS port 443, startup explains the conflict and exits without entering a systemd restart loop; inspect `tailscale serve status`, resolve the route ownership manually, and restart the Gateway.
120+
- When upgrading from an older OpenClaw release, a persistent HTTPS root route may still own port 443. Because that route is indistinguishable from an operator-managed route, OpenClaw does not change it: startup explains the conflict and exits with status 78 instead of entering a systemd restart loop. Inspect `tailscale serve status`. If you confirm it is the stale route created by older managed Serve/Funnel, remove only its root handler with `tailscale serve --yes --https=443 --set-path=/ off` or `tailscale funnel --yes --https=443 --set-path=/ off`, then restart the Gateway. If the route is intentionally external, disable managed Tailscale ingress and use the `trustedProxies` path above.
121121
- Named Tailscale Services are not supported by managed ingress because Tailscale requires them to run as persistent background routes. Existing `gateway.tailscale.serviceName` installs must run `openclaw doctor --fix`; Doctor disables managed ingress and removes the key. Inspect the retained Service route, clear it with `tailscale serve clear <service-name>`, then enable device Serve with `gateway.tailscale.mode: "serve"` if desired.
122122
- Older releases could advertise an externally configured default HTTPS Serve route that targeted a `gateway.bind: "lan"` listener. That route does not automatically gain trusted ingress provenance. Prefer migrating to managed ingress: run `openclaw doctor` to preview an atomic change to `gateway.bind: "loopback"` plus `gateway.tailscale.mode: "serve"`; apply it with `openclaw doctor --fix`, then restart the Gateway. Doctor does not reset Tailscale state or guess how to rewrite custom Serve ports and Tailscale Services. If another service must retain ownership, use the explicit `trustedProxies` compatibility path above.
123123
- `gateway.tailscale.preserveFunnel: true` is a deprecated migration guard. It detects an externally configured `tailscale funnel` route before reapplying Serve. If that route still targets the ordinary Gateway listener, OpenClaw leaves it unchanged and warns because the route is not managed ingress. Gateway-authenticated routes work only through the explicit `trustedProxies` compatibility path above and continue to require the configured auth; plugin-authenticated webhook routes such as Google Chat and SMS keep using their own signature/auth checks. To migrate, first configure a durable `gateway.auth.password` (prefer a SecretRef) or `OPENCLAW_GATEWAY_PASSWORD`, set `gateway.auth.mode` to `password`, run `openclaw config set gateway.tailscale.mode funnel`, then `openclaw config unset gateway.tailscale.preserveFunnel`.

‎src/cli/gateway-cli/run.supervised-lock.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createServer } from "node:http";
33
import { describe, expect, it, vi } from "vitest";
44
import { GatewayLockError } from "../../infra/gateway-lock.js";
5-
import { TailscaleRouteMigrationRequiredError } from "../../infra/tailscale-route-migration-error.js";
5+
import { TailscaleRouteOwnershipConflictError } from "../../infra/tailscale-route-ownership-error.js";
66
import { OpenClawAgentDatabaseMediaMigrationRequiredError } from "../../state/openclaw-agent-db-migration-required.js";
77
import { testing } from "./run.test-support.js";
88

@@ -24,7 +24,7 @@ function createLogger() {
2424
describe("supervised gateway lock recovery", () => {
2525
it("uses exit 78 for an ambiguous persistent Tailscale route", () => {
2626
expect(
27-
testing.resolveGatewayStartupFailureExitCode(new TailscaleRouteMigrationRequiredError()),
27+
testing.resolveGatewayStartupFailureExitCode(new TailscaleRouteOwnershipConflictError()),
2828
).toBe(78);
2929
});
3030

‎src/cli/gateway-cli/run.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
formatGatewayPidList,
5656
} from "../../infra/gateway-processes.js";
5757
import type { RespawnSupervisor } from "../../infra/supervisor-markers.js";
58-
import { isTailscaleRouteMigrationRequiredError } from "../../infra/tailscale-route-migration-error.js";
58+
import { isTailscaleRouteOwnershipConflictError } from "../../infra/tailscale-route-ownership-error.js";
5959
import { setConsoleSubsystemFilter, setConsoleTimestampPrefix } from "../../logging/console.js";
6060
import { withDiagnosticPhase } from "../../logging/diagnostic-phase.js";
6161
import { createSubsystemLogger } from "../../logging/subsystem.js";
@@ -479,7 +479,7 @@ function resolveGatewayLockErrorExitCode(err: unknown): number {
479479

480480
function resolveGatewayStartupFailureExitCode(err: unknown): number {
481481
return isInvalidConfigError(err) ||
482-
isTailscaleRouteMigrationRequiredError(err) ||
482+
isTailscaleRouteOwnershipConflictError(err) ||
483483
findOpenClawAgentDatabaseMediaMigrationRequiredError(err) ||
484484
findOpenClawStateDatabaseSchemaMigrationRequiredError(err)
485485
? EXIT_CONFIG_ERROR

‎src/gateway/server-tailscale-upgrade.test.ts‎

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("managed Tailscale upgrade", () => {
3232
};
3333

3434
it.each(["serve", "funnel"] as const)(
35-
"replaces the exact legacy persistent %s route with a foreground claim",
35+
"does not infer ownership from a matching persistent %s route",
3636
async (mode) => {
3737
const env = captureEnv([
3838
"OPENCLAW_TEST_TAILSCALE_BINARY",
@@ -41,17 +41,18 @@ describe("managed Tailscale upgrade", () => {
4141
"VITEST",
4242
]);
4343
const marker = await installFixture(legacyRoute(mode === "funnel"), mode);
44+
const before = await readFile(marker, "utf8");
4445

4546
try {
46-
const cleanup = await startGatewayTailscaleExposure({
47-
tailscaleMode: mode,
48-
port: 18789,
49-
backend: { host: "127.0.0.1", port: 19000 },
50-
logTailscale: { info: () => undefined, warn: () => undefined },
51-
});
52-
53-
expect(await readFile(marker, "utf8")).toBe("cleared");
54-
await cleanup?.();
47+
await expect(
48+
startGatewayTailscaleExposure({
49+
tailscaleMode: mode,
50+
port: 18789,
51+
backend: { host: "127.0.0.1", port: 19000 },
52+
logTailscale: { info: () => undefined, warn: () => undefined },
53+
}),
54+
).rejects.toThrow("ownership OpenClaw cannot prove; it was not modified");
55+
expect(await readFile(marker, "utf8")).toBe(before);
5556
} finally {
5657
env.restore();
5758
}
@@ -82,48 +83,4 @@ describe("managed Tailscale upgrade", () => {
8283
env.restore();
8384
}
8485
});
85-
86-
it.each([
87-
["mode mismatch", legacyRoute(true)],
88-
["custom target", legacyRoute(false, 9000)],
89-
[
90-
"extra path",
91-
{
92-
...legacyRoute(),
93-
Web: {
94-
"fixture.tailnet.ts.net:443": {
95-
Handlers: {
96-
"/": { Proxy: "http://127.0.0.1:18789/" },
97-
"/other": { Proxy: "http://127.0.0.1:9000/" },
98-
},
99-
},
100-
},
101-
},
102-
],
103-
["foreground route", { ...legacyRoute(), Foreground: { session: legacyRoute() } }],
104-
["same-port Service", { ...legacyRoute(), Services: { "svc:other": legacyRoute() } }],
105-
])("leaves an ambiguous %s route unchanged and explains the conflict", async (_name, config) => {
106-
const env = captureEnv([
107-
"OPENCLAW_TEST_TAILSCALE_BINARY",
108-
"OPENCLAW_TEST_TAILSCALE_FIXTURE_MARKER",
109-
"OPENCLAW_TEST_TAILSCALE_FIXTURE_MODE",
110-
"VITEST",
111-
]);
112-
const marker = await installFixture(config, "serve");
113-
const before = await readFile(marker, "utf8");
114-
115-
try {
116-
await expect(
117-
startGatewayTailscaleExposure({
118-
tailscaleMode: "serve",
119-
port: 18789,
120-
backend: { host: "127.0.0.1", port: 19000 },
121-
logTailscale: { info: () => undefined, warn: () => undefined },
122-
}),
123-
).rejects.toThrow("cannot safely migrate");
124-
expect(await readFile(marker, "utf8")).toBe(before);
125-
} finally {
126-
env.restore();
127-
}
128-
});
12986
});

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const mocks = vi.hoisted(() => {
1414
getTailnetHostname: vi.fn<() => Promise<string | null>>(async () => null),
1515
getTailnetHostnameAfterServe: vi.fn<() => Promise<string | null>>(async () => null),
1616
hasTailscaleFunnelRouteForPort: vi.fn(async (_port: number) => false),
17-
reconcileLegacyTailscaleRoute: vi.fn(async () => false),
1817
};
1918
});
2019

@@ -23,7 +22,6 @@ vi.mock("../infra/tailscale.js", () => ({
2322
getTailnetHostname: mocks.getTailnetHostname,
2423
getTailnetHostnameAfterServe: mocks.getTailnetHostnameAfterServe,
2524
hasTailscaleFunnelRouteForPort: mocks.hasTailscaleFunnelRouteForPort,
26-
reconcileLegacyTailscaleRoute: mocks.reconcileLegacyTailscaleRoute,
2725
}));
2826

2927
import { getMcpAppChannelOrigin, prepareMcpAppChannelOrigin } from "./mcp-app-channel-origin.js";
@@ -61,7 +59,6 @@ afterEach(() => {
6159
mocks.getTailnetHostname.mockResolvedValue(null);
6260
mocks.getTailnetHostnameAfterServe.mockResolvedValue(null);
6361
mocks.hasTailscaleFunnelRouteForPort.mockResolvedValue(false);
64-
mocks.reconcileLegacyTailscaleRoute.mockResolvedValue(false);
6562
});
6663

6764
describe("startGatewayTailscaleExposure", () => {
@@ -99,28 +96,11 @@ describe("startGatewayTailscaleExposure", () => {
9996
});
10097

10198
expect(mocks.claimTailscaleRoute).toHaveBeenCalledWith("serve", MANAGED_BACKEND_PORT);
102-
expect(mocks.reconcileLegacyTailscaleRoute).toHaveBeenCalledWith("serve", 18789);
10399
expect(mocks.getTailnetHostnameAfterServe).toHaveBeenCalledOnce();
104100
expect(mocks.getTailnetHostname).not.toHaveBeenCalled();
105101
expect(mocks.hasTailscaleFunnelRouteForPort).not.toHaveBeenCalled();
106102
});
107103

108-
it("reports an automatically migrated legacy route", async () => {
109-
const logTailscale = createLogger();
110-
mocks.reconcileLegacyTailscaleRoute.mockResolvedValue(true);
111-
112-
await startGatewayTailscaleExposure({
113-
tailscaleMode: "funnel",
114-
port: 18789,
115-
logTailscale,
116-
});
117-
118-
expect(logTailscale.info).toHaveBeenCalledWith(
119-
expect.stringContaining("removed legacy persistent Tailscale funnel route"),
120-
);
121-
expect(mocks.claimTailscaleRoute).toHaveBeenCalledWith("funnel", MANAGED_BACKEND_PORT);
122-
});
123-
124104
it("fails startup when the managed route cannot be claimed", async () => {
125105
const failure = new Error("tailscale unavailable");
126106
mocks.claimTailscaleRoute.mockRejectedValue(failure);

‎src/gateway/server-tailscale.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getTailnetHostname,
77
getTailnetHostnameAfterServe,
88
hasTailscaleFunnelRouteForPort,
9-
reconcileLegacyTailscaleRoute,
109
} from "../infra/tailscale.js";
1110
import { resolveTailscalePublishedHost } from "../shared/tailscale-status.js";
1211
import type { GatewayTailscaleIngressEndpoint } from "./ingress-attribution.js";
@@ -52,11 +51,6 @@ export async function startGatewayTailscaleExposure(params: {
5251

5352
let claim: Awaited<ReturnType<typeof claimTailscaleRoute>> | undefined;
5453
try {
55-
if (await reconcileLegacyTailscaleRoute(params.tailscaleMode, params.port)) {
56-
params.logTailscale.info(
57-
`removed legacy persistent Tailscale ${params.tailscaleMode} route before claiming foreground ownership`,
58-
);
59-
}
6054
claim = await claimTailscaleRoute(params.tailscaleMode, backendTarget);
6155
const host = await (
6256
params.tailscaleMode === "serve" ? getTailnetHostnameAfterServe() : getTailnetHostname()

‎src/infra/tailscale-route-migration-error.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { asNullableObjectRecord as readRecord } from "@openclaw/normalization-core/record-coerce";
2+
3+
const TAILSCALE_ROUTE_OWNERSHIP_CONFLICT_CODE = "TAILSCALE_ROUTE_OWNERSHIP_CONFLICT";
4+
5+
export class TailscaleRouteOwnershipConflictError extends Error {
6+
readonly code = TAILSCALE_ROUTE_OWNERSHIP_CONFLICT_CODE;
7+
8+
constructor() {
9+
super(
10+
"Tailscale HTTPS port 443 is already owned by a route whose ownership OpenClaw cannot prove; it was not modified. " +
11+
"Inspect `tailscale serve status`. If it is a stale route from an older OpenClaw release, remove its root handler with " +
12+
"`tailscale serve --yes --https=443 --set-path=/ off` or `tailscale funnel --yes --https=443 --set-path=/ off`, then restart the Gateway. " +
13+
"Otherwise disable managed Tailscale ingress or reconfigure the route before restarting.",
14+
);
15+
this.name = "TailscaleRouteOwnershipConflictError";
16+
}
17+
}
18+
19+
export function isTailscaleRouteOwnershipConflictError(error: unknown): boolean {
20+
return (
21+
error instanceof TailscaleRouteOwnershipConflictError ||
22+
readRecord(error)?.code === TAILSCALE_ROUTE_OWNERSHIP_CONFLICT_CODE
23+
);
24+
}

0 commit comments

Comments
 (0)