Skip to content

Commit 188957a

Browse files
authored
fix(gateway): require explicit credentials for probe URL overrides (#118614)
1 parent 96b4258 commit 188957a

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

‎src/commands/gateway-status.test.ts‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ async function runGatewayStatus(
315315
json?: boolean;
316316
port?: unknown;
317317
url?: string;
318+
token?: string;
319+
password?: string;
318320
ssh?: string;
319321
sshAuto?: boolean;
320322
sshIdentity?: string;
@@ -447,6 +449,7 @@ describe("gateway-status command", () => {
447449
timeout: "1000",
448450
json: true,
449451
url: "wss://remote.example:18789",
452+
token: "explicit-remote-token",
450453
});
451454

452455
expect(inspectWindowsGatewayFirewall).not.toHaveBeenCalled();
@@ -458,6 +461,132 @@ describe("gateway-status command", () => {
458461
);
459462
});
460463

464+
it.each([
465+
{
466+
source: "configured token",
467+
auth: { mode: "token", token: "configured-local-token" },
468+
env: {},
469+
options: {},
470+
},
471+
{
472+
source: "configured password",
473+
auth: { mode: "password", password: "configured-local-password" },
474+
env: {},
475+
options: {},
476+
},
477+
{
478+
source: "environment token",
479+
auth: { mode: "token" },
480+
env: { OPENCLAW_GATEWAY_TOKEN: "ambient-local-token" },
481+
options: {},
482+
},
483+
{
484+
source: "environment password",
485+
auth: { mode: "password" },
486+
env: { OPENCLAW_GATEWAY_PASSWORD: "ambient-local-password" },
487+
options: {},
488+
},
489+
{
490+
source: "whitespace token",
491+
auth: { mode: "token", token: "configured-local-token" },
492+
env: {},
493+
options: { token: " " },
494+
},
495+
{
496+
source: "whitespace password",
497+
auth: { mode: "password", password: "configured-local-password" },
498+
env: {},
499+
options: { password: " " },
500+
},
501+
{
502+
source: "explicit loopback URL",
503+
auth: { mode: "token", token: "configured-local-token" },
504+
env: {},
505+
options: { url: "ws://127.0.0.1:18991" },
506+
},
507+
])(
508+
"rejects a local $source before probing an explicit Gateway URL",
509+
async ({ auth, env, options }) => {
510+
const configuredGateway = { gateway: { mode: "local", auth } };
511+
512+
await withEnvAsync(
513+
{
514+
OPENCLAW_GATEWAY_TOKEN: undefined,
515+
OPENCLAW_GATEWAY_PASSWORD: undefined,
516+
...env,
517+
},
518+
async () => {
519+
await readBestEffortConfig.withImplementation(
520+
async () => configuredGateway as never,
521+
async () => {
522+
const { runtime } = createRuntimeCapture();
523+
await expect(
524+
runGatewayStatus(runtime, {
525+
timeout: "1000",
526+
json: true,
527+
url: "wss://attacker.example:18789",
528+
...options,
529+
}),
530+
).rejects.toMatchObject({
531+
name: "GatewayExplicitAuthRequiredError",
532+
message: expect.stringContaining(
533+
"gateway url override requires explicit credentials",
534+
),
535+
});
536+
537+
expect(readBestEffortConfig).not.toHaveBeenCalled();
538+
expect(discoverGatewayBeacons).not.toHaveBeenCalled();
539+
expect(startSshPortForward).not.toHaveBeenCalled();
540+
expect(probeGateway).not.toHaveBeenCalled();
541+
},
542+
);
543+
},
544+
);
545+
},
546+
);
547+
548+
it.each([
549+
{
550+
credential: "token",
551+
options: { token: "explicit-remote-token" },
552+
expectedAuth: { token: "explicit-remote-token", password: undefined },
553+
},
554+
{
555+
credential: "password",
556+
options: { password: "explicit-remote-password" },
557+
expectedAuth: { token: undefined, password: "explicit-remote-password" },
558+
},
559+
])(
560+
"honors an explicit $credential for an explicit Gateway URL",
561+
async ({ options, expectedAuth }) => {
562+
const explicitUrl = "wss://attacker.example:18789";
563+
readBestEffortConfig.mockResolvedValueOnce({
564+
gateway: {
565+
mode: "local",
566+
auth: { mode: "token", token: "configured-local-token" },
567+
},
568+
} as never);
569+
570+
await withEnvAsync(
571+
{
572+
OPENCLAW_GATEWAY_TOKEN: "ambient-local-token",
573+
OPENCLAW_GATEWAY_PASSWORD: "ambient-local-password",
574+
},
575+
async () => {
576+
const { runtime } = createRuntimeCapture();
577+
await runGatewayStatus(runtime, {
578+
timeout: "1000",
579+
json: true,
580+
url: explicitUrl,
581+
...options,
582+
});
583+
584+
expect(requireProbeCall(explicitUrl).auth).toEqual(expectedAuth);
585+
},
586+
);
587+
},
588+
);
589+
461590
it("includes diagnostic next steps when no gateway is reachable or discoverable", async () => {
462591
const { runtime, runtimeLogs, runtimeErrors } = createRuntimeCapture();
463592
const defaultProbeGateway = probeGateway.getMockImplementation();

‎src/commands/gateway-status.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isRich } from "../../packages/terminal-core/src/theme.js";
33
import { parseGatewayPortOption } from "../cli/gateway-port-option.js";
44
import { withProgress } from "../cli/progress.js";
55
import { readBestEffortConfig, resolveGatewayPort } from "../config/config.js";
6+
import { ensureExplicitGatewayAuth, resolveExplicitGatewayAuth } from "../gateway/call.js";
67
import { resolveWideAreaDiscoveryDomain } from "../infra/widearea-dns.js";
78
import type { RuntimeEnv } from "../runtime.js";
89
import { createLazyImportLoader } from "../shared/lazy-promise.js";
@@ -52,6 +53,12 @@ export async function gatewayStatusCommand(
5253
},
5354
runtime: RuntimeEnv,
5455
) {
56+
ensureExplicitGatewayAuth({
57+
urlOverride: opts.url?.trim(),
58+
urlOverrideSource: "cli",
59+
explicitAuth: resolveExplicitGatewayAuth(opts),
60+
errorHint: "Fix: pass --token or --password with --url.",
61+
});
5562
const startedAt = Date.now();
5663
const cfg = await readBestEffortConfig();
5764
const rich = isRich() && opts.json !== true;

0 commit comments

Comments
 (0)