Skip to content

Commit 8f14578

Browse files
authored
fix: diagnose gateways that cannot host nodes (#125132)
* fix(doctor): expose node hosting preconditions * fix(doctor): use canonical string coercion * fix(auth): narrow identity header rejection
1 parent 7fd6d06 commit 8f14578

13 files changed

Lines changed: 277 additions & 0 deletions

‎docs/nodes/index.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ Approval note:
8686
- For direct shell/runtime file executions, OpenClaw also best-effort binds one concrete local file operand and denies the run if that file changes before execution.
8787
- If OpenClaw cannot identify exactly one concrete local file for an interpreter/runtime command, approval-backed execution is denied instead of pretending full runtime coverage. Use sandboxing, separate hosts, or an explicit trusted allowlist/full workflow for broader interpreter semantics.
8888

89+
### Gateway deployments that cannot host nodes
90+
91+
A Gateway can remain healthy for browser users while node hosting is unavailable. Run `openclaw doctor` on the Gateway before onboarding nodes, and check these preconditions:
92+
93+
- **Machine authentication:** Tailscale identity headers do not authenticate node-role connections. In `gateway.auth.mode: "trusted-proxy"`, a new node also cannot supply the proxy's user identity headers. To use a shared token, switch to token mode and configure `gateway.auth.token` with a SecretRef; trusted-proxy mode rejects mixed token configuration. A trusted-proxy Gateway can use `gateway.auth.password` only for clean loopback/direct callers. See [trusted-proxy mixed token configuration](/gateway/trusted-proxy-auth#mixed-token-configuration).
94+
- **Node onboarding URL:** With `gateway.bind: "loopback"`, configure Tailscale Serve, `gateway.remote.url`, or `plugins.entries.device-pair.config.publicUrl` before minting a join code. Otherwise `openclaw devices join-code` reports: `Gateway is only bound to loopback. Set gateway.bind=lan, enable tailscale serve, or configure plugins.entries.device-pair.config.publicUrl.`
95+
- **Edge routing:** When a reverse proxy or access edge fronts the Gateway, allow `/j/*` and `/__openclaw__/worker` through without edge identity auth. Keep WebSocket upgrade enabled for `/__openclaw__/worker`; both routes enforce their own short-lived credentials. The node's main Gateway WebSocket must also reach an auth path it can satisfy. See [worker protocol](/gateway/protocol#worker-role-and-closed-protocol).
96+
97+
Cloudflare Access service tokens (`CF-Access-Client-Id` and `CF-Access-Client-Secret`) are the intended alternative for Access-fronted machine clients, but node hosts cannot send those headers yet. Follow [node-host service-token header support](https://github.com/openclaw/openclaw/issues/125112) for that capability.
98+
8999
### Start a node host (foreground)
90100

91101
On the node machine:

‎packages/gateway-client/src/reconnect-policy.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("shouldPauseGatewayReconnect", () => {
1818
ConnectErrorDetailCodes.AUTH_RATE_LIMITED,
1919
ConnectErrorDetailCodes.AUTH_DEVICE_TOKEN_MISMATCH,
2020
ConnectErrorDetailCodes.AUTH_SCOPE_MISMATCH,
21+
"AUTH_IDENTITY_HEADER_REQUIRED",
2122
ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH,
2223
ConnectErrorDetailCodes.PAIRING_REQUIRED,
2324
ConnectErrorDetailCodes.PROTOCOL_MISMATCH,
@@ -40,6 +41,19 @@ describe("shouldPauseGatewayReconnect", () => {
4041
expect(shouldPause({ code: ConnectErrorDetailCodes.AUTH_TOKEN_MISMATCH })).toBe(false);
4142
});
4243

44+
it("keeps the identity-header pause behind a pending device-token retry", () => {
45+
expect(
46+
shouldPauseGatewayReconnect({
47+
details: { code: "AUTH_IDENTITY_HEADER_REQUIRED" },
48+
deviceTokenRetryPending: true,
49+
}),
50+
).toBe(false);
51+
});
52+
53+
it("keeps generic unauthorized failures reconnecting", () => {
54+
expect(shouldPause({ code: ConnectErrorDetailCodes.AUTH_UNAUTHORIZED })).toBe(false);
55+
});
56+
4357
it.each([undefined, {}, { code: "SOME_FUTURE_CODE" }])(
4458
"keeps reconnect active for recoverable details",
4559
(details) => {

‎packages/gateway-client/src/reconnect-policy.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const NON_RECOVERABLE_AUTH_ERRORS = new Set<string>([
1212
ConnectErrorDetailCodes.AUTH_RATE_LIMITED,
1313
ConnectErrorDetailCodes.AUTH_DEVICE_TOKEN_MISMATCH,
1414
ConnectErrorDetailCodes.AUTH_SCOPE_MISMATCH,
15+
ConnectErrorDetailCodes.AUTH_IDENTITY_HEADER_REQUIRED,
1516
ConnectErrorDetailCodes.CONTROL_UI_BUILD_MISMATCH,
1617
ConnectErrorDetailCodes.PAIRING_REQUIRED,
1718
ConnectErrorDetailCodes.CONTROL_UI_DEVICE_IDENTITY_REQUIRED,
@@ -39,6 +40,9 @@ export function shouldPauseGatewayReconnect(params: {
3940
if (code === ConnectErrorDetailCodes.AUTH_TOKEN_MISMATCH) {
4041
return params.tokenMismatchIsTerminal === true && !params.deviceTokenRetryPending;
4142
}
43+
if (code === ConnectErrorDetailCodes.AUTH_IDENTITY_HEADER_REQUIRED) {
44+
return !params.deviceTokenRetryPending;
45+
}
4246
return (
4347
NON_RECOVERABLE_AUTH_ERRORS.has(code) ||
4448
(params.protocolMismatchIsTerminal === true &&

‎packages/gateway-protocol/src/connect-error-details.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ describe("resolveAuthConnectErrorDetailCode", () => {
264264
it("maps device token scope mismatches to a dedicated auth detail", () => {
265265
expect(resolveAuthConnectErrorDetailCode("scope_mismatch")).toBe("AUTH_SCOPE_MISMATCH");
266266
});
267+
268+
it("keeps trusted-proxy identity rejection distinct from generic unauthorized auth", () => {
269+
expect(
270+
resolveAuthConnectErrorDetailCode("trusted_proxy_missing_header_cf-access-jwt-assertion"),
271+
).toBe("AUTH_IDENTITY_HEADER_REQUIRED");
272+
});
273+
274+
it("keeps non-header trusted-proxy rejection generic", () => {
275+
expect(resolveAuthConnectErrorDetailCode("trusted_proxy_local_interface_check_failed")).toBe(
276+
"AUTH_UNAUTHORIZED",
277+
);
278+
});
267279
});
268280

269281
describe("pairing connect details", () => {

‎packages/gateway-protocol/src/connect-error-details.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const ConnectErrorDetailCodes = {
3939
AUTH_TAILSCALE_PROXY_MISSING: "AUTH_TAILSCALE_PROXY_MISSING",
4040
AUTH_TAILSCALE_WHOIS_FAILED: "AUTH_TAILSCALE_WHOIS_FAILED",
4141
AUTH_TAILSCALE_IDENTITY_MISMATCH: "AUTH_TAILSCALE_IDENTITY_MISMATCH",
42+
AUTH_IDENTITY_HEADER_REQUIRED: "AUTH_IDENTITY_HEADER_REQUIRED",
4243
CONTROL_UI_BUILD_MISMATCH: "CONTROL_UI_BUILD_MISMATCH",
4344
CONTROL_UI_ORIGIN_NOT_ALLOWED: "CONTROL_UI_ORIGIN_NOT_ALLOWED",
4445
PROTOCOL_MISMATCH: "PROTOCOL_MISMATCH",
@@ -166,6 +167,9 @@ const CONNECT_PAIRING_REQUIRED_MESSAGE_BY_REASON: Readonly<
166167
export function resolveAuthConnectErrorDetailCode(
167168
reason: string | undefined,
168169
): ConnectErrorDetailCode {
170+
if (reason?.startsWith("trusted_proxy_missing_header_")) {
171+
return ConnectErrorDetailCodes.AUTH_IDENTITY_HEADER_REQUIRED;
172+
}
169173
switch (reason) {
170174
case "token_missing":
171175
return ConnectErrorDetailCodes.AUTH_TOKEN_MISSING;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Doctor node-hosting precondition tests cover browser-only auth and unreachable onboarding.
2+
import { describe, expect, it } from "vitest";
3+
import type { OpenClawConfig } from "../config/types.openclaw.js";
4+
import { collectNodeHostingPreconditionFindings } from "./doctor-node-hosting-preconditions.js";
5+
6+
function findingsFor(cfg: OpenClawConfig) {
7+
return collectNodeHostingPreconditionFindings(cfg);
8+
}
9+
10+
describe("node-hosting preconditions", () => {
11+
it.each([
12+
{
13+
name: "identity-header auth alone",
14+
cfg: {
15+
gateway: {
16+
bind: "lan",
17+
auth: { mode: "trusted-proxy" },
18+
},
19+
},
20+
requirements: ["machine-client-auth"],
21+
},
22+
{
23+
name: "loopback onboarding alone",
24+
cfg: {
25+
gateway: {
26+
bind: "loopback",
27+
auth: { mode: "token", token: "configured-token" },
28+
},
29+
},
30+
requirements: ["node-onboarding-url"],
31+
},
32+
{
33+
name: "both unavailable",
34+
cfg: {
35+
gateway: {
36+
bind: "loopback",
37+
auth: { mode: "trusted-proxy" },
38+
},
39+
},
40+
requirements: ["machine-client-auth", "node-onboarding-url"],
41+
},
42+
{
43+
name: "Tailscale identity without a shared secret",
44+
cfg: {
45+
gateway: {
46+
bind: "loopback",
47+
tailscale: { mode: "serve" },
48+
auth: { mode: "token", allowTailscale: true },
49+
},
50+
},
51+
requirements: ["machine-client-auth"],
52+
},
53+
] satisfies Array<{
54+
name: string;
55+
cfg: OpenClawConfig;
56+
requirements: string[];
57+
}>)("warns when $name", ({ cfg, requirements }) => {
58+
expect(findingsFor(cfg).map((finding) => finding.requirement)).toEqual(requirements);
59+
});
60+
61+
it("does not warn for token auth with a reachable bind", () => {
62+
expect(
63+
findingsFor({
64+
gateway: {
65+
bind: "lan",
66+
auth: {
67+
mode: "token",
68+
token: { source: "env", provider: "default", id: "GATEWAY_TOKEN" },
69+
},
70+
},
71+
}),
72+
).toEqual([]);
73+
});
74+
75+
it("gives accurate machine-auth and edge-routing remediation", () => {
76+
const findings = findingsFor({
77+
gateway: {
78+
bind: "loopback",
79+
auth: { mode: "trusted-proxy" },
80+
},
81+
});
82+
83+
expect(findings.find((finding) => finding.requirement === "machine-client-auth")?.fixHint).toBe(
84+
"Switch gateway.auth.mode to token and configure gateway.auth.token as a SecretRef so machine clients can authenticate as devices. Keep trusted-proxy only if machine clients use a clean loopback/direct gateway.auth.password path. Cloudflare Access service tokens are the alternative for Access-fronted gateways, but node-host support for CF-Access-Client-Id / CF-Access-Client-Secret is not implemented yet.",
85+
);
86+
expect(findings.find((finding) => finding.requirement === "node-onboarding-url")).toMatchObject(
87+
{
88+
message:
89+
"Gateway is only bound to loopback. Set gateway.bind=lan, enable tailscale serve, or configure plugins.entries.device-pair.config.publicUrl.",
90+
fixHint:
91+
"If an edge proxy fronts node onboarding, allow /j/* and /__openclaw__/worker without edge identity auth, and preserve WebSocket upgrade on /__openclaw__/worker. Both routes enforce their own credentials.",
92+
},
93+
);
94+
});
95+
96+
it("accepts a configured public URL for loopback onboarding", () => {
97+
expect(
98+
findingsFor({
99+
gateway: {
100+
bind: "loopback",
101+
auth: { mode: "token", token: "configured-token" },
102+
},
103+
plugins: {
104+
entries: {
105+
"device-pair": { config: { publicUrl: "wss://gateway.example" } },
106+
},
107+
},
108+
}),
109+
).toEqual([]);
110+
});
111+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Doctor node-hosting preconditions expose config combinations that leave browser auth healthy
2+
// while machine authentication or onboarding remains unavailable.
3+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
4+
import type { OpenClawConfig } from "../config/types.openclaw.js";
5+
import type { HealthFinding } from "../flows/health-checks.js";
6+
import { hasConfiguredGatewayAuthSecretInput } from "../gateway/auth-config-utils.js";
7+
8+
const CHECK_ID = "core/doctor/node-hosting-preconditions";
9+
const LOOPBACK_JOIN_CODE_MESSAGE =
10+
"Gateway is only bound to loopback. Set gateway.bind=lan, enable tailscale serve, or configure plugins.entries.device-pair.config.publicUrl.";
11+
12+
function usesIdentityHeadersWithoutMachineCredentials(cfg: OpenClawConfig): boolean {
13+
const hasToken = hasConfiguredGatewayAuthSecretInput(cfg, "gateway.auth.token");
14+
const hasPassword = hasConfiguredGatewayAuthSecretInput(cfg, "gateway.auth.password");
15+
if (hasToken || hasPassword) {
16+
return false;
17+
}
18+
if (cfg.gateway?.auth?.mode === "trusted-proxy") {
19+
return true;
20+
}
21+
return (
22+
cfg.gateway?.tailscale?.mode === "serve" &&
23+
cfg.gateway?.auth?.mode !== "password" &&
24+
cfg.gateway?.auth?.mode !== "none" &&
25+
cfg.gateway?.auth?.allowTailscale !== false
26+
);
27+
}
28+
29+
function lacksNodeOnboardingUrl(cfg: OpenClawConfig): boolean {
30+
const bind = cfg.gateway?.bind ?? "loopback";
31+
if (bind !== "loopback" && bind !== "auto") {
32+
return false;
33+
}
34+
const publicUrl = cfg.plugins?.entries?.["device-pair"]?.config?.["publicUrl"];
35+
const remoteUrl = cfg.gateway?.remote?.url;
36+
const tailscaleMode = cfg.gateway?.tailscale?.mode ?? "off";
37+
return (
38+
!normalizeOptionalString(publicUrl) &&
39+
!normalizeOptionalString(remoteUrl) &&
40+
tailscaleMode !== "serve" &&
41+
tailscaleMode !== "funnel"
42+
);
43+
}
44+
45+
/** Collects config-only warnings for node authentication, onboarding, and worker ingress. */
46+
export function collectNodeHostingPreconditionFindings(
47+
cfg: OpenClawConfig,
48+
): readonly HealthFinding[] {
49+
const findings: HealthFinding[] = [];
50+
if (usesIdentityHeadersWithoutMachineCredentials(cfg)) {
51+
findings.push({
52+
checkId: CHECK_ID,
53+
severity: "warning",
54+
message:
55+
"Gateway identity-header auth has no configured token/password path for machine clients; new node hosts cannot authenticate or become worker hosts.",
56+
path: "gateway.auth",
57+
requirement: "machine-client-auth",
58+
fixHint:
59+
"Switch gateway.auth.mode to token and configure gateway.auth.token as a SecretRef so machine clients can authenticate as devices. Keep trusted-proxy only if machine clients use a clean loopback/direct gateway.auth.password path. Cloudflare Access service tokens are the alternative for Access-fronted gateways, but node-host support for CF-Access-Client-Id / CF-Access-Client-Secret is not implemented yet.",
60+
});
61+
}
62+
if (lacksNodeOnboardingUrl(cfg)) {
63+
findings.push({
64+
checkId: CHECK_ID,
65+
severity: "warning",
66+
message: LOOPBACK_JOIN_CODE_MESSAGE,
67+
path: "gateway.bind",
68+
requirement: "node-onboarding-url",
69+
fixHint:
70+
"If an edge proxy fronts node onboarding, allow /j/* and /__openclaw__/worker without edge identity auth, and preserve WebSocket upgrade on /__openclaw__/worker. Both routes enforce their own credentials.",
71+
});
72+
}
73+
return findings;
74+
}

‎src/flows/doctor-health-contributions-initial.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ export function resolveInitialDoctorHealthContributions(params: {
9393
healthCheckIds: ["core/doctor/gateway-auth"],
9494
run: params.runGatewayAuthHealth,
9595
}),
96+
createDoctorHealthContribution({
97+
id: "doctor:node-hosting-preconditions",
98+
label: "Node hosting preconditions",
99+
healthChecks: {
100+
description: "Gateway config can authenticate and onboard node and worker hosts.",
101+
async detect(ctx) {
102+
const { collectNodeHostingPreconditionFindings } =
103+
await import("../commands/doctor-node-hosting-preconditions.js");
104+
return collectNodeHostingPreconditionFindings(ctx.cfg);
105+
},
106+
},
107+
}),
96108
createDoctorHealthContribution({
97109
id: "doctor:command-owner",
98110
label: "Command owner",

‎src/flows/doctor-health-contributions.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,7 @@ describe("doctor health contributions", () => {
23602360
expect(contributionIds).toContain("core/doctor/disk-space");
23612361
expect(contributionIds).toContain("core/doctor/whatsapp-responsiveness");
23622362
expect(contributionIds).toContain("core/doctor/device-pairing");
2363+
expect(contributionIds).toContain("core/doctor/node-hosting-preconditions");
23632364
expect(contributionIds).toContain("core/doctor/channel-plugin-blockers");
23642365
expect(contributionIds).toContain("core/doctor/channel-package-state-capabilities");
23652366
expect(contributionIds).toContain("core/doctor/channel-preview-warnings");

‎src/gateway/server/ws-connection/auth-messages.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,34 @@ describe("formatGatewayAuthFailureMessage", () => {
3636
);
3737
expect(truncateCloseReason(message)).toBe(message);
3838
});
39+
40+
it("tells rejected node hosts how to diagnose identity-header auth", () => {
41+
expect(
42+
formatGatewayAuthFailureMessage({
43+
authMode: "trusted-proxy",
44+
authProvided: "none",
45+
reason: "trusted_proxy_missing_header_cf-access-jwt-assertion",
46+
client: {
47+
id: GATEWAY_CLIENT_IDS.NODE_HOST,
48+
mode: GATEWAY_CLIENT_MODES.NODE,
49+
},
50+
}),
51+
).toBe(
52+
"gateway rejected this node: trusted-proxy identity-header authentication is required and no usable machine credential was accepted; run `openclaw doctor` on the Gateway",
53+
);
54+
});
55+
56+
it("does not describe other trusted-proxy rejection causes as missing identity headers", () => {
57+
expect(
58+
formatGatewayAuthFailureMessage({
59+
authMode: "trusted-proxy",
60+
authProvided: "none",
61+
reason: "trusted_proxy_local_interface_check_failed",
62+
client: {
63+
id: GATEWAY_CLIENT_IDS.NODE_HOST,
64+
mode: GATEWAY_CLIENT_MODES.NODE,
65+
},
66+
}),
67+
).toBe("unauthorized");
68+
});
3969
});

0 commit comments

Comments
 (0)