Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: forward `serverUrl` in WS auth for apiKey verification (#13824)

Arvin Xu75626de0b3c1
diff --git a/apps/cli/man/man1/lh.1 b/apps/cli/man/man1/lh.1
index c64128860da..1fcb29b745a 100644
--- a/apps/cli/man/man1/lh.1
+++ b/apps/cli/man/man1/lh.1
@@ -1,6 +1,6 @@
.\" Code generated by `npm run man:generate`; DO NOT EDIT.
.\" Manual command details come from the Commander command tree.
-.TH LH 1 "" "@lobehub/cli 0.0.6" "User Commands"
+.TH LH 1 "" "@lobehub/cli 0.0.7" "User Commands"
.SH NAME
lh \- LobeHub CLI \- manage and connect to LobeHub services
.SH SYNOPSIS
diff --git a/apps/cli/package.json b/apps/cli/package.json
index 6e68b5f623b..edae30933b9 100644
--- a/apps/cli/package.json
+++ b/apps/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@lobehub/cli",
- "version": "0.0.6",
+ "version": "0.0.7",
"type": "module",
"bin": {
"lh": "./dist/index.js",
diff --git a/apps/cli/src/commands/agent.ts b/apps/cli/src/commands/agent.ts
index 99e57ec4b79..c902a52bc97 100644
--- a/apps/cli/src/commands/agent.ts
+++ b/apps/cli/src/commands/agent.ts
@@ -373,6 +373,7 @@ export function registerAgentCommand(program: Command) {
gatewayUrl: agentGatewayUrl,
json: options.json,
operationId,
+ serverUrl,
token,
tokenType,
verbose: options.verbose,
diff --git a/apps/cli/src/utils/agentStream.test.ts b/apps/cli/src/utils/agentStream.test.ts
index 0314e680c70..3f3212cb55a 100644
--- a/apps/cli/src/utils/agentStream.test.ts
+++ b/apps/cli/src/utils/agentStream.test.ts
@@ -279,6 +279,8 @@ describe('streamAgentEventsViaWebSocket', () => {
await flush();
const ws = capturedWs!;
+ // Note: serverUrl is not set here, and JSON.stringify drops undefined keys,
+ // so the parsed auth message will not contain a `serverUrl` field.
expect(ws.sent.map((s) => JSON.parse(s))).toEqual([
{ token: 'test-token', tokenType: 'jwt', type: 'auth' },
{ lastEventId: '', type: 'resume' },
@@ -288,10 +290,11 @@ describe('streamAgentEventsViaWebSocket', () => {
await promise;
});
- it('should send tokenType=apiKey when the caller uses an API key', async () => {
+ it('should send tokenType=apiKey and serverUrl when the caller uses an API key', async () => {
const promise = streamAgentEventsViaWebSocket({
gatewayUrl: 'https://gw.test.com',
operationId: 'op-1',
+ serverUrl: 'https://app.lobehub.com',
token: 'lh_sk_abc',
tokenType: 'apiKey',
});
@@ -299,7 +302,10 @@ describe('streamAgentEventsViaWebSocket', () => {
await flush();
const ws = capturedWs!;
+ // serverUrl is forwarded so the gateway can call back to /api/v1/users/me
+ // to verify the API key.
expect(ws.sent.map((s) => JSON.parse(s))[0]).toEqual({
+ serverUrl: 'https://app.lobehub.com',
token: 'lh_sk_abc',
tokenType: 'apiKey',
type: 'auth',
diff --git a/apps/cli/src/utils/agentStream.ts b/apps/cli/src/utils/agentStream.ts
index 6b5f732c15a..c67a33b21cb 100644
--- a/apps/cli/src/utils/agentStream.ts
+++ b/apps/cli/src/utils/agentStream.ts
@@ -20,6 +20,12 @@ interface StreamOptions {
interface WebSocketStreamOptions extends StreamOptions {
gatewayUrl: string;
operationId: string;
+ /**
+ * LobeHub server URL the gateway should call back to when verifying
+ * an apiKey token (via `/api/v1/users/me`). Required when
+ * `tokenType === 'apiKey'`; ignored for JWT.
+ */
+ serverUrl?: string;
token: string;
/**
* How the gateway should verify `token`. `jwt` is the default for
@@ -173,7 +179,7 @@ const HEARTBEAT_INTERVAL = 30_000;
export async function streamAgentEventsViaWebSocket(
options: WebSocketStreamOptions,
): Promise<void> {
- const { gatewayUrl, operationId, token, tokenType = 'jwt', ...streamOpts } = options;
+ const { gatewayUrl, operationId, serverUrl, token, tokenType = 'jwt', ...streamOpts } = options;
const wsUrl = urlJoin(
gatewayUrl.replace(/^http/, 'ws'),
`/ws?operationId=${encodeURIComponent(operationId)}`,
@@ -197,7 +203,10 @@ export async function streamAgentEventsViaWebSocket(
};
ws.onopen = () => {
- ws.send(JSON.stringify({ token, tokenType, type: 'auth' }));
+ // `serverUrl` is required so the gateway can call back to verify an
+ // apiKey token. Harmless (but unused) for JWT, so we always include it
+ // when available to match the device-gateway-client contract.
+ ws.send(JSON.stringify({ serverUrl, token, tokenType, type: 'auth' }));
};
ws.onmessage = (event) => {