Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(lobehub): keep a verified handshake when only the embed policy read fails, and CORS the 404

nirholasff3fb0b61958
diff --git a/api/lobehub/[action].js b/api/lobehub/[action].js
index b9a791bb4..70d04447c 100644
--- a/api/lobehub/[action].js
+++ b/api/lobehub/[action].js
@@ -75,23 +75,31 @@ function originHostname(value) {
// for the same reason; `verified` in the response tells the host which of the
// two answers it got.
async function lookupAgent(agentId) {
+ let row;
try {
- const [row] = await sql`
+ [row] = await sql`
SELECT id, name FROM agent_identities
WHERE id = ${agentId} AND deleted_at IS NULL
LIMIT 1
`;
- if (!row) return { verified: true, found: false, name: null, policy: null };
- return {
- verified: true,
- found: true,
- name: typeof row.name === 'string' ? row.name : null,
- policy: await readEmbedPolicy(agentId),
- };
} catch (err) {
console.warn('[lobehub/handshake] agent lookup failed, handshaking unverified:', err?.message);
return { verified: false, found: true, name: null, policy: null };
}
+ if (!row) return { verified: true, found: false, name: null, policy: null };
+
+ // The policy lives behind a second read, so it can fail on its own after the
+ // identity is already confirmed. Folding that failure into the catch above
+ // threw away an answer we had: the agent came back verified with a name, and
+ // the host was told the handshake was unverified and nameless. Degrade only
+ // the part that failed, and report the plugin's own hosts for the policy.
+ let policy = null;
+ try {
+ policy = await readEmbedPolicy(agentId);
+ } catch (err) {
+ console.warn('[lobehub/handshake] embed policy read failed, reporting plugin hosts only:', err?.message);
+ }
+ return { verified: true, found: true, name: typeof row.name === 'string' ? row.name : null, policy };
}
// Fold the agent owner's own embed policy (agent_identities.embed_policy, the
@@ -193,5 +201,11 @@ export default wrap(async (req, res) => {
});
}
+ // A misspelled action is read cross-origin like every other action here, and
+ // it is nearly always a typo in a host's integration. Answering without a
+ // CORS header hid the 404 behind an opaque browser network error, so the
+ // integrator never saw the message naming the problem. api/chat-plugin's
+ // unknown-tool branch is reachable for the same reason: it CORS-es first.
+ if (cors(req, res, { methods: 'GET,POST,OPTIONS', origins: '*' })) return;
return error(res, 404, 'not_found', 'unknown lobehub action');
});
diff --git a/tests/lobehub-plugin-endpoint.test.js b/tests/lobehub-plugin-endpoint.test.js
index 070311344..8ac2b668e 100644
--- a/tests/lobehub-plugin-endpoint.test.js
+++ b/tests/lobehub-plugin-endpoint.test.js
@@ -191,6 +191,22 @@ describe('POST /api/lobehub/handshake', () => {
expect(origins.hosts).toEqual(['bad.example']);
});
+ it('keeps a confirmed agent verified when only the policy read fails', async () => {
+ // Two reads back the handshake. When the second one fails, the first has
+ // already confirmed the agent: reporting the handshake unverified and
+ // nameless threw away an answer we held.
+ sqlMock
+ .mockResolvedValueOnce([{ id: AGENT_ID, name: 'Ada' }])
+ .mockRejectedValueOnce(new Error('connection reset'));
+ const res = await call({ action: 'handshake', method: 'POST', body: { agentId: AGENT_ID } });
+ expect(res.statusCode).toBe(200);
+ const body = res.json();
+ expect(body.verified).toBe(true);
+ expect(body.agentName).toBe('Ada');
+ expect(body.embedPolicy.origins.mode).toBe('allowlist');
+ expect(body.embedPolicy.origins.hosts).toContain('chat.lobehub.com');
+ });
+
it('degrades to an unverified handshake when the database is unreachable', async () => {
sqlMock.mockRejectedValue(new Error('connection refused'));
const res = await call({ action: 'handshake', method: 'POST', body: { agentId: AGENT_ID } });
@@ -209,9 +225,19 @@ describe('POST /api/lobehub/handshake', () => {
});
describe('unknown lobehub action', () => {
- it('404s rather than falling through', async () => {
+ it('404s rather than falling through, with a readable CORS header', async () => {
const res = await call({ action: 'bogus' });
expect(res.statusCode).toBe(404);
expect(res.json().error).toBe('not_found');
+ // A typo'd action is fetched from a browser like every valid one. Without
+ // this header the caller sees an opaque CORS failure instead of the 404
+ // that names the mistake.
+ expect(res.getHeader('access-control-allow-origin')).toBe('*');
+ });
+
+ it('answers a preflight for an unknown action instead of 404ing it', async () => {
+ const res = await call({ action: 'bogus', method: 'OPTIONS' });
+ expect(res.statusCode).toBe(204);
+ expect(res.getHeader('access-control-allow-origin')).toBe('*');
});
});