From ab1d6acb2d4e976ad815695763d5c2701e88bd15 Mon Sep 17 00:00:00 2001 From: Towsty Date: Mon, 2 Nov 2026 22:30:26 -0600 Subject: [PATCH] Return 404 JSON for oauth-protected-resource discovery probes. Stop 302ing Claude well-known checks to /login without advertising OAuth. Co-authored-by: Cursor --- server/middleware/auth.ts | 4 +++- .../.well-known/oauth-protected-resource.get.ts | 7 +++++++ .../.well-known/oauth-protected-resource/mcp.get.ts | 7 +++++++ tests/aigen-api.test.mjs | 12 ++++++++---- 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 server/routes/.well-known/oauth-protected-resource.get.ts create mode 100644 server/routes/.well-known/oauth-protected-resource/mcp.get.ts diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index 08de7c4..7947355 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -16,7 +16,9 @@ export default defineEventHandler((event) => { path === '/login' || // Claude MCP discovery — never 302 to /login (JSON 404 handlers serve these). path === '/.well-known/oauth-authorization-server' || - path === '/.well-known/openid-configuration' + path === '/.well-known/openid-configuration' || + path === '/.well-known/oauth-protected-resource' || + path.startsWith('/.well-known/oauth-protected-resource/') ) { return } diff --git a/server/routes/.well-known/oauth-protected-resource.get.ts b/server/routes/.well-known/oauth-protected-resource.get.ts new file mode 100644 index 0000000..e7b45dd --- /dev/null +++ b/server/routes/.well-known/oauth-protected-resource.get.ts @@ -0,0 +1,7 @@ +/** Claude discovery probe — no OAuth; never redirect to /login. */ +export default defineEventHandler((event) => { + setResponseStatus(event, 404) + setResponseHeader(event, 'Content-Type', 'application/json') + setResponseHeader(event, 'Cache-Control', 'no-store') + return { error: 'not found' } +}) diff --git a/server/routes/.well-known/oauth-protected-resource/mcp.get.ts b/server/routes/.well-known/oauth-protected-resource/mcp.get.ts new file mode 100644 index 0000000..e7b45dd --- /dev/null +++ b/server/routes/.well-known/oauth-protected-resource/mcp.get.ts @@ -0,0 +1,7 @@ +/** Claude discovery probe — no OAuth; never redirect to /login. */ +export default defineEventHandler((event) => { + setResponseStatus(event, 404) + setResponseHeader(event, 'Content-Type', 'application/json') + setResponseHeader(event, 'Cache-Control', 'no-store') + return { error: 'not found' } +}) diff --git a/tests/aigen-api.test.mjs b/tests/aigen-api.test.mjs index 7546491..ef3c3d3 100644 --- a/tests/aigen-api.test.mjs +++ b/tests/aigen-api.test.mjs @@ -1,6 +1,6 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { existsSync, readFileSync } from 'node:fs' +import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' import { @@ -77,7 +77,7 @@ test('aigen routes exist, stay on the studio-2 enqueue path, and 404 on xAIGen', assert.match(middleware, /aigenApiPathAllowed/) assert.match(middleware, /\/\.well-known\/oauth-authorization-server/) assert.match(middleware, /\/\.well-known\/openid-configuration/) - assert.doesNotMatch(middleware, /oauth-protected-resource/) + assert.match(middleware, /\/\.well-known\/oauth-protected-resource/) assert.match(authPaths, /path === '\/mcp'/) assert.match(mcp, /mcpBearerOk/) assert.match(mcp, /text\/event-stream/) @@ -98,10 +98,14 @@ test('aigen routes exist, stay on the studio-2 enqueue path, and 404 on xAIGen', const as = readFileSync(join(root, 'server/routes/.well-known/oauth-authorization-server.get.ts'), 'utf8') const oidc = readFileSync(join(root, 'server/routes/.well-known/openid-configuration.get.ts'), 'utf8') + const opr = readFileSync(join(root, 'server/routes/.well-known/oauth-protected-resource.get.ts'), 'utf8') + const oprMcp = readFileSync(join(root, 'server/routes/.well-known/oauth-protected-resource/mcp.get.ts'), 'utf8') assert.match(as, /not found/) assert.match(oidc, /not found/) - assert.equal(existsSync(join(root, 'server/routes/.well-known/oauth-protected-resource.get.ts')), false) - assert.equal(existsSync(join(root, 'server/routes/.well-known/oauth-protected-resource/mcp.get.ts')), false) + assert.match(opr, /not found/) + assert.match(oprMcp, /not found/) + assert.doesNotMatch(opr, /authorization_servers/) + assert.doesNotMatch(oprMcp, /authorization_servers/) }) test('defaultLibraryFolderId prefers Inbox then the first folder', () => {