Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mcp): omit session credentials from whoami

Tincf446256f399
diff --git a/packages/mcp/src/tools/get-current-user.ts b/packages/mcp/src/tools/get-current-user.ts
new file mode 100644
index 000000000..28ca9afcc
--- /dev/null
+++ b/packages/mcp/src/tools/get-current-user.ts
@@ -0,0 +1,22 @@
+import { z } from "zod";
+import type { ToolClient } from "./register.js";
+
+const currentUserSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ email: z.string(),
+ role: z.string().nullish(),
+});
+
+export async function getCurrentUser(client: ToolClient) {
+ if (client.usingApiKey) {
+ const user = await client.json("/api/user/me", { method: "GET" });
+ return currentUserSchema.parse(user);
+ }
+
+ const response = await client.json<{ user: unknown } | null>(
+ "/api/auth/get-session",
+ { method: "GET" },
+ );
+ return response === null ? null : currentUserSchema.parse(response.user);
+}
diff --git a/packages/mcp/src/tools/register.test.ts b/packages/mcp/src/tools/register.test.ts
index 9da972677..6822a3551 100644
--- a/packages/mcp/src/tools/register.test.ts
+++ b/packages/mcp/src/tools/register.test.ts
@@ -56,6 +56,9 @@ describe("registerTools", () => {
json: vi.fn().mockResolvedValue({
id: "user-1",
name: "Mohiuddin",
+ email: "[email protected]",
+ role: "user",
+ token: "unexpected-user-secret",
}),
};
@@ -70,39 +73,86 @@ describe("registerTools", () => {
expect(result?.content).toEqual([
{
type: "text",
- text: JSON.stringify({ id: "user-1", name: "Mohiuddin" }, null, 2),
+ text: JSON.stringify(
+ {
+ id: "user-1",
+ name: "Mohiuddin",
+ email: "[email protected]",
+ role: "user",
+ },
+ null,
+ 2,
+ ),
},
]);
});
- it("uses the session endpoint for whoami with device authentication", async () => {
+ it.each(["user", null, undefined])(
+ "returns only safe user fields for whoami with session authentication and role %s",
+ async (role) => {
+ const { server, tools } = createServerMock();
+ const client = {
+ usingApiKey: false,
+ json: vi.fn().mockResolvedValue({
+ user: {
+ id: "user-1",
+ name: "Mohiuddin",
+ email: "[email protected]",
+ role,
+ token: "unexpected-user-secret",
+ },
+ session: {
+ id: "session-1",
+ token: "live-session-secret",
+ expiresAt: "2026-11-08T00:00:00.000Z",
+ ipAddress: "192.0.2.1",
+ userAgent: "MCP client",
+ },
+ token: "unexpected-response-secret",
+ }),
+ };
+
+ registerTools(server as never, { client: client as never });
+
+ const result = await tools.get("whoami")?.handler({});
+
+ expect(client.json).toHaveBeenCalledWith("/api/auth/get-session", {
+ method: "GET",
+ });
+ expect(result?.isError).toBe(false);
+ expect(result?.content).toEqual([
+ {
+ type: "text",
+ text: JSON.stringify(
+ {
+ id: "user-1",
+ name: "Mohiuddin",
+ email: "[email protected]",
+ role,
+ },
+ null,
+ 2,
+ ),
+ },
+ ]);
+ },
+ );
+
+ it("returns null for whoami when there is no session", async () => {
const { server, tools } = createServerMock();
const client = {
usingApiKey: false,
- json: vi.fn().mockResolvedValue({
- user: { id: "user-1" },
- session: { id: "session-1" },
- }),
+ json: vi.fn().mockResolvedValue(null),
};
registerTools(server as never, { client: client as never });
const result = await tools.get("whoami")?.handler({});
- expect(client.json).toHaveBeenCalledWith("/api/auth/get-session", {
- method: "GET",
+ expect(result).toEqual({
+ content: [{ type: "text", text: "null" }],
+ isError: false,
});
- expect(result?.isError).toBe(false);
- expect(result?.content).toEqual([
- {
- type: "text",
- text: JSON.stringify(
- { user: { id: "user-1" }, session: { id: "session-1" } },
- null,
- 2,
- ),
- },
- ]);
});
it("builds the expected query string for list_tasks", async () => {
diff --git a/packages/mcp/src/tools/register.ts b/packages/mcp/src/tools/register.ts
index 9401c3ef9..ffa83ef85 100644
--- a/packages/mcp/src/tools/register.ts
+++ b/packages/mcp/src/tools/register.ts
@@ -2,6 +2,7 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { buildFullTaskUpdateBody } from "../kaneo/task-helpers.js";
import { errorResult, textResult } from "../utils/mcp-result.js";
+import { getCurrentUser } from "./get-current-user.js";
export type ToolClient = {
readonly usingApiKey?: boolean;
@@ -69,13 +70,7 @@ export function registerTools(
"Return the current Kaneo user for the configured authentication method.",
inputSchema: z.object({}),
},
- async () =>
- run(() =>
- client.json(
- client.usingApiKey ? "/api/user/me" : "/api/auth/get-session",
- { method: "GET" },
- ),
- ),
+ async () => run(() => getCurrentUser(client)),
);
registerTool(
diff --git a/tests/api/mcp-internal-api-url.test.ts b/tests/api/mcp-internal-api-url.test.ts
index 200f42cfb..35746c71e 100644
--- a/tests/api/mcp-internal-api-url.test.ts
+++ b/tests/api/mcp-internal-api-url.test.ts
@@ -58,7 +58,13 @@ afterEach(() => {
describe("MCP API URLs", () => {
it("advertises the public URL while fetching tools through the internal URL", async () => {
const apiFetch = vi.fn(async () =>
- Response.json({ user: { id: "test-user" } }),
+ Response.json({
+ user: {
+ id: "test-user",
+ name: "Test User",
+ email: "[email protected]",
+ },
+ }),
);
vi.stubGlobal("fetch", apiFetch);
const mcpRoutes = await loadMcpRoutes();
@@ -89,7 +95,13 @@ describe("MCP API URLs", () => {
"uses the configured internal URL %s without duplicate slashes",
async (internalApiUrl) => {
const apiFetch = vi.fn(async () =>
- Response.json({ user: { id: "test-user" } }),
+ Response.json({
+ user: {
+ id: "test-user",
+ name: "Test User",
+ email: "[email protected]",
+ },
+ }),
);
vi.stubGlobal("fetch", apiFetch);
const mcpRoutes = await loadMcpRoutes(internalApiUrl);
diff --git a/tests/api/mcp-stateless.test.ts b/tests/api/mcp-stateless.test.ts
index 9ce7f6765..423577816 100644
--- a/tests/api/mcp-stateless.test.ts
+++ b/tests/api/mcp-stateless.test.ts
@@ -13,6 +13,22 @@ vi.mock("../../apps/api/src/auth", () => ({
const protocolVersion = "2026-07-28";
+const currentUser = {
+ id: "test-user",
+ name: "Test User",
+ email: "[email protected]",
+ role: "user",
+};
+
+const sessionResponse = {
+ user: { ...currentUser, token: "unexpected-user-secret" },
+ session: {
+ id: "test-session",
+ token: "live-session-secret",
+ expiresAt: "2026-11-08T00:00:00.000Z",
+ },
+};
+
function modernRequest(
method: string,
id: number,
@@ -95,7 +111,7 @@ describe("MCP 2026-07-28 stateless HTTP", () => {
expect(new Headers(init?.headers).get("authorization")).toBe(
"Bearer test-token",
);
- return Response.json({ user: { id: "test-user" } });
+ return Response.json(sessionResponse);
},
);
vi.stubGlobal("fetch", apiFetch);
@@ -115,9 +131,11 @@ describe("MCP 2026-07-28 stateless HTTP", () => {
expect(apiFetch).toHaveBeenCalledTimes(4);
expect(responses.every((response) => response.status === 200)).toBe(true);
- expect(
- bodies.every((body) => body.result.content[0].text.includes("test-user")),
- ).toBe(true);
+ for (const body of bodies) {
+ expect(JSON.parse(body.result.content[0].text)).toEqual(currentUser);
+ expect(JSON.stringify(body)).not.toContain("live-session-secret");
+ expect(JSON.stringify(body)).not.toContain("unexpected-user-secret");
+ }
});
it("gets one task by ticket ID through the HTTP MCP server", async () => {
@@ -339,9 +357,7 @@ describe("MCP 2026-07-28 stateless HTTP", () => {
}),
});
const toolsBody = await rpcBody(tools);
- const apiFetch = vi.fn(async () =>
- Response.json({ user: { id: "test-user" } }),
- );
+ const apiFetch = vi.fn(async () => Response.json(sessionResponse));
vi.stubGlobal("fetch", apiFetch);
const call = await replicaB.default.request("/mcp", {
method: "POST",
@@ -366,7 +382,9 @@ describe("MCP 2026-07-28 stateless HTTP", () => {
expect(toolsBody.result.tools).toContainEqual(
expect.objectContaining({ name: "whoami" }),
);
- expect(callBody.result.content[0].text).toContain("test-user");
+ expect(JSON.parse(callBody.result.content[0].text)).toEqual(currentUser);
+ expect(JSON.stringify(callBody)).not.toContain("live-session-secret");
+ expect(JSON.stringify(callBody)).not.toContain("unexpected-user-secret");
expect(apiFetch).toHaveBeenCalledOnce();
expect(authMocks.getSession).toHaveBeenCalledTimes(4);
});