This commit is contained in:
Mahesh Sanikommu 2026-10-25 05:34:49 +00:00 • committed by GitHub
commit 9e7e469c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 3 deletions

View file

@ -214,6 +214,34 @@ describe("MCP authentication", () => {
expect(error).not.toHaveBeenCalled()
})
it("rejects an API key whose session has no organization", async () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
const error = vi.spyOn(console, "error").mockImplementation(() => {})
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue(
Response.json({
user: { id: "user_test", email: "test@example.com", name: "Test" },
org: null,
session: null,
role: null,
accessType: "full",
containerTags: null,
scope: { type: "full", permission: "write" },
}),
),
)
await expect(
validateApiKey("sm_noorg_key_0123456789abcdef", API_URL),
).resolves.toBeNull()
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
"API key rejected: session has no organization",
)
expect(error).not.toHaveBeenCalled()
})
it("rejects malformed API keys without an API request", async () => {
const fetchSpy = vi.fn()
vi.stubGlobal("fetch", fetchSpy)

View file

@ -117,7 +117,10 @@ export async function validateApiKey(
try {
const session = await fetchSession(token, apiUrl)
const organizationId = session.org?.id
if (!organizationId) return null
if (!organizationId) {
console.warn("API key rejected: session has no organization")
return null
}
const user: AuthUser = {
userId: session.user.id,

View file

@ -37,8 +37,8 @@ export const sessionInfoSchema = z
email: z.string().optional(),
name: z.string().optional(),
}),
org: z.looseObject({ id: z.string().min(1) }).optional(),
role: z.string().optional(),
org: z.looseObject({ id: z.string().min(1) }).nullish(),
role: z.string().nullish(),
accessType: z.enum(["full", "restricted"]).optional(),
containerTags: z.array(sessionNamespaceAccessSchema).nullable().optional(),
scope: sessionScopeSchema.optional(),