fix(auth): address review — observe SSE explicit fetch, isolate test state
- The SSE branch selects a caller-supplied `eventSourceInit.fetch` as-is, so it bypassed the challenge observer and a first-time legacy SSE authorization lost the advertised URL the same way. Observing is passive, so it is applied to that fetch too while the deliberate no-proxy-wrap rule stays. Covered behaviorally against a 401-serving server. - Dropped an `as unknown as typeof fetch` from the new integration test (banned by AGENTS.md, and it erased the signature); the spy is typed as `typeof fetch` directly. The `as MCPServerConfig` beside it was unnecessary too. - The integration test reused a per-pid OAuth storage file. SDK `auth()` persists the advertised URL in its discovery state, so a rerun could satisfy discovery from that cache and pass with the plumbing removed — which would have quietly falsified the regression check. Fresh mkdtemp per run, removed in teardown. Adds observer unit tests (challenge reported, body untouched, 403 included, silent on success). Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
597e1bd25f
commit
48b393014a
1 changed files with 148 additions and 23 deletions
|
|
@ -1,5 +1,8 @@
|
|||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createAuthChallengeInterceptFetch } from "@inspector/core/mcp/node/authChallengeFetch.js";
|
||||
import {
|
||||
createAuthChallengeInterceptFetch,
|
||||
createAuthChallengeObserverFetch,
|
||||
} from "@inspector/core/mcp/node/authChallengeFetch.js";
|
||||
import { AuthChallengeError } from "@inspector/core/auth/challenge.js";
|
||||
|
||||
describe("createAuthChallengeInterceptFetch", () => {
|
||||
|
|
@ -84,3 +87,63 @@ describe("createAuthChallengeInterceptFetch", () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createAuthChallengeObserverFetch", () => {
|
||||
const METADATA_URL = "http://127.0.0.1:3001/custom/protected-resource";
|
||||
|
||||
it("reports a challenge without altering the response or its body", async () => {
|
||||
const baseFetch = vi.fn(
|
||||
async () =>
|
||||
new Response("payload", {
|
||||
status: 401,
|
||||
headers: {
|
||||
"WWW-Authenticate": `Bearer resource_metadata="${METADATA_URL}"`,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const onChallenge = vi.fn();
|
||||
const fetchFn = createAuthChallengeObserverFetch(baseFetch, onChallenge);
|
||||
|
||||
const res = await fetchFn("https://example.com/mcp");
|
||||
|
||||
expect(res.status).toBe(401);
|
||||
// The body must survive for the transport (or the interceptor above).
|
||||
expect(await res.text()).toBe("payload");
|
||||
expect(onChallenge).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ resourceMetadataUrl: METADATA_URL }),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports a 403 challenge too", async () => {
|
||||
const baseFetch = vi.fn(
|
||||
async () =>
|
||||
new Response(null, {
|
||||
status: 403,
|
||||
headers: { "WWW-Authenticate": 'Bearer error="insufficient_scope"' },
|
||||
}),
|
||||
);
|
||||
const onChallenge = vi.fn();
|
||||
|
||||
await createAuthChallengeObserverFetch(
|
||||
baseFetch,
|
||||
onChallenge,
|
||||
)("https://example.com/mcp");
|
||||
|
||||
expect(onChallenge).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ reason: "insufficient_scope" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("stays silent on a non-challenge response", async () => {
|
||||
const baseFetch = vi.fn(async () => new Response("ok", { status: 200 }));
|
||||
const onChallenge = vi.fn();
|
||||
|
||||
const res = await createAuthChallengeObserverFetch(
|
||||
baseFetch,
|
||||
onChallenge,
|
||||
)("https://example.com/mcp");
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(onChallenge).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue