Skip to content

Commit c37cfb0

Browse files
authored
test(security): cover gateway shared auth modes (#118834)
1 parent 94410b0 commit c37cfb0

2 files changed

Lines changed: 138 additions & 2 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
title: Gateway shared auth modes
2+
3+
scenario:
4+
id: gateway-shared-auth-modes
5+
surface: security
6+
coverage:
7+
primary:
8+
- security.shared-gateway-token-password-auth
9+
- security.gateway-auth-mode
10+
objective: Prove the real Gateway enforces explicit token, password, and none authentication modes across WebSocket, HTTP, and startup boundaries.
11+
successCriteria:
12+
- WebSocket clients connect with the configured token or password and reject invalid or cross-mode credentials.
13+
- The real models HTTP endpoint accepts the configured shared secret and returns 401 for an invalid secret in token and password modes.
14+
- Missing active credentials and unauthenticated non-loopback exposure fail before the Gateway listens with actionable configuration guidance.
15+
- Explicit none mode remains available on loopback, and existing private-ingress auth behavior remains covered without claiming adjacent Tailscale or browser trust taxonomy.
16+
docsRefs:
17+
- docs/gateway/authentication.md
18+
- docs/gateway/security/index.md
19+
codeRefs:
20+
- src/gateway/auth.ts
21+
- src/gateway/server-runtime-config.ts
22+
- src/gateway/server.auth.modes.suite.ts
23+
execution:
24+
kind: vitest
25+
path: src/gateway/server.auth.modes.test.ts
26+
summary: Run the real Gateway auth-mode matrix across WebSocket, HTTP, loopback, private ingress, and startup validation.

‎src/gateway/server.auth.modes.suite.ts‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import {
1616
testTailscaleWhois,
1717
} from "./server.auth.test-helpers.js";
1818

19+
async function requestModels(port: number, secret: string): Promise<Response> {
20+
return await fetch(`http://127.0.0.1:${port}/v1/models`, {
21+
headers: {
22+
authorization: `Bearer ${secret}`,
23+
},
24+
});
25+
}
26+
1927
export function registerAuthModesSuite(): void {
2028
describe("password auth", () => {
2129
let server: Awaited<ReturnType<typeof startGatewayServer>>;
@@ -24,7 +32,7 @@ export function registerAuthModesSuite(): void {
2432
beforeAll(async () => {
2533
testState.gatewayAuth = { mode: "password", password: "secret" }; // pragma: allowlist secret
2634
port = await getFreePort();
27-
server = await startGatewayServer(port);
35+
server = await startGatewayServer(port, { openAiChatCompletionsEnabled: true });
2836
});
2937

3038
beforeEach(() => {
@@ -49,6 +57,27 @@ export function registerAuthModesSuite(): void {
4957
expect(res.error?.message ?? "").toContain("unauthorized");
5058
ws.close();
5159
});
60+
61+
test("rejects token credentials in password mode", async () => {
62+
const ws = await openWs(port);
63+
const res = await connectReq(ws, {
64+
skipDefaultAuth: true,
65+
token: "secret",
66+
});
67+
expect(res.ok).toBe(false);
68+
expect(res.error?.message ?? "").toContain("unauthorized");
69+
ws.close();
70+
});
71+
72+
test("authorizes the models HTTP endpoint with only the configured password", async () => {
73+
const authorized = await requestModels(port, "secret");
74+
expect(authorized.status).toBe(200);
75+
await authorized.body?.cancel();
76+
77+
const unauthorized = await requestModels(port, "wrong");
78+
expect(unauthorized.status).toBe(401);
79+
await unauthorized.body?.cancel();
80+
});
5281
});
5382

5483
describe("token auth", () => {
@@ -61,7 +90,7 @@ export function registerAuthModesSuite(): void {
6190
process.env.OPENCLAW_GATEWAY_TOKEN = "secret";
6291
testState.gatewayAuth = { mode: "token", token: "secret" };
6392
port = await getFreePort();
64-
server = await startGatewayServer(port);
93+
server = await startGatewayServer(port, { openAiChatCompletionsEnabled: true });
6594
});
6695

6796
beforeEach(() => {
@@ -74,6 +103,13 @@ export function registerAuthModesSuite(): void {
74103
restoreGatewayToken(prevToken);
75104
});
76105

106+
test("accepts token auth when configured", async () => {
107+
const ws = await openWs(port);
108+
const res = await connectReq(ws, { token: "secret" });
109+
expect(res.ok).toBe(true);
110+
ws.close();
111+
});
112+
77113
test("rejects invalid token", async () => {
78114
const ws = await openWs(port);
79115
const res = await connectReq(ws, { token: "wrong" });
@@ -82,6 +118,27 @@ export function registerAuthModesSuite(): void {
82118
ws.close();
83119
});
84120

121+
test("rejects password credentials in token mode", async () => {
122+
const ws = await openWs(port);
123+
const res = await connectReq(ws, {
124+
skipDefaultAuth: true,
125+
password: "secret", // pragma: allowlist secret
126+
});
127+
expect(res.ok).toBe(false);
128+
expect(res.error?.message ?? "").toContain("unauthorized");
129+
ws.close();
130+
});
131+
132+
test("authorizes the models HTTP endpoint with only the configured token", async () => {
133+
const authorized = await requestModels(port, "secret");
134+
expect(authorized.status).toBe(200);
135+
await authorized.body?.cancel();
136+
137+
const unauthorized = await requestModels(port, "wrong");
138+
expect(unauthorized.status).toBe(401);
139+
await unauthorized.body?.cancel();
140+
});
141+
85142
test("returns control ui hint when token is missing", async () => {
86143
const ws = await openWs(port, { origin: originForPort(port) });
87144
const res = await connectReq(ws, {
@@ -144,6 +201,59 @@ export function registerAuthModesSuite(): void {
144201
});
145202
});
146203

204+
describe("startup auth validation", () => {
205+
test.each([
206+
{
207+
mode: "token" as const,
208+
envKey: "OPENCLAW_GATEWAY_TOKEN" as const,
209+
expected:
210+
"gateway auth mode is token, but no token was configured (set gateway.auth.token or OPENCLAW_GATEWAY_TOKEN)",
211+
},
212+
{
213+
mode: "password" as const,
214+
envKey: "OPENCLAW_GATEWAY_PASSWORD" as const,
215+
expected: "gateway auth mode is password, but no password was configured",
216+
},
217+
])("rejects $mode mode before startup when its credential is missing", async (testCase) => {
218+
const previous = process.env[testCase.envKey];
219+
delete process.env[testCase.envKey];
220+
// Use an explicit empty override so suite-level credentials cannot satisfy
221+
// the mode under test before runtime validation runs.
222+
const auth =
223+
testCase.mode === "token"
224+
? { mode: "token" as const, token: "", allowTailscale: false }
225+
: { mode: "password" as const, password: "", allowTailscale: false };
226+
testState.gatewayAuth = auth;
227+
const port = await getFreePort();
228+
229+
try {
230+
await expect(startGatewayServer(port, { auth })).rejects.toThrow(testCase.expected);
231+
} finally {
232+
if (previous === undefined) {
233+
delete process.env[testCase.envKey];
234+
} else {
235+
process.env[testCase.envKey] = previous;
236+
}
237+
}
238+
});
239+
240+
test("rejects non-loopback exposure without effective auth before listening", async () => {
241+
testState.gatewayAuth = { mode: "none" };
242+
const port = await getFreePort();
243+
244+
await expect(
245+
startGatewayServer(port, {
246+
bind: "lan",
247+
host: "0.0.0.0",
248+
auth: { mode: "none" },
249+
controlUiEnabled: false,
250+
}),
251+
).rejects.toThrow(
252+
"without auth (set gateway.auth.token/password, or set OPENCLAW_GATEWAY_TOKEN/OPENCLAW_GATEWAY_PASSWORD",
253+
);
254+
});
255+
});
256+
147257
describe("tailscale auth", () => {
148258
let server: Awaited<ReturnType<typeof startGatewayServer>>;
149259
let port: number;

0 commit comments

Comments
 (0)