fix: carry the proxy deadline's identity, correct the rounding note (#2319 round 9)
A deadline enforced by the backend could not arrive as an OAuthRequestTimeoutError: /api/fetch serialized it as a 500 and createRemoteFetch turned that into a plain Error, so every instanceof check downstream was false — including the one that lets a stalled probe escape withRfc8414OidcCompat. That matters most where no client-side wrapper exists to have fired first: the discovery the SDK runs from inside the transport. The route now answers 504 with a typed marker (OAUTH_TIMEOUT_WIRE_CODE plus url and timeoutMs) and createRemoteFetch reconstructs the error from it, guarded by a shape check so an upstream's own JSON cannot forge one. The route's deadline becomes a `fetchTimeoutMs` option defaulting to the shared constant, so the backstop can be proven to fire in milliseconds instead of thirty seconds of wall clock — and because a very slow authorization server is a legitimate reason to want a longer one. The rounding rationale was wrong: setTimeout accepts a fractional delay and truncates it (verified on Node 26); ERR_OUT_OF_RANGE is AbortSignal.timeout's behaviour, which this wrapper never calls. The rounding stays, for the reason that is actually true — the budget is interpolated into the message and exposed as timeoutMs, and a performance.now() budget would otherwise report 1000.4000000953674ms. Also drops a sentence in the discarded-stream test that contradicted the qualification above it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YFnM6RqB4nXqNysgUYmjmq Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
a452305204
commit
baefc96806
2 changed files with 247 additions and 20 deletions
|
|
@ -318,10 +318,12 @@ describe("withOAuthRequestTimeout", () => {
|
|||
await expect(wrapped(URL_UNDER_TEST)).rejects.toBe(boom);
|
||||
});
|
||||
|
||||
it("rounds a fractional budget, because setTimeout takes an integer", async () => {
|
||||
it("rounds a fractional budget, which is reported to the caller", async () => {
|
||||
vi.useFakeTimers();
|
||||
// A budget derived from `performance.now()` arrives fractional; a fractional
|
||||
// delay throws ERR_OUT_OF_RANGE before the request is ever made.
|
||||
// `setTimeout` would accept the fraction and truncate it. The rounding is
|
||||
// for the budget's *reported* form: a `performance.now()` subtraction would
|
||||
// otherwise name "1000.4000000953674ms" in the message and expose a
|
||||
// non-integer `timeoutMs`.
|
||||
const wrapped = withOAuthRequestTimeout(neverSettles, 1000.4);
|
||||
|
||||
const assertion = wrapped(URL_UNDER_TEST).catch((err: unknown) => err);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createRemoteFetch } from "@inspector/core/mcp/remote/createRemoteFetch.js";
|
||||
import {
|
||||
OAUTH_TIMEOUT_WIRE_CODE,
|
||||
OAuthRequestTimeoutError,
|
||||
} from "@inspector/core/auth/requestTimeout.js";
|
||||
|
||||
function ok(body: object): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
|
|
@ -241,4 +245,83 @@ describe("createRemoteFetch", () => {
|
|||
expect(signalOf(fetchFn)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("proxy-side deadline (#2319)", () => {
|
||||
function remoteReturning(status: number, body: string) {
|
||||
const fetchFn = vi.fn<typeof fetch>().mockResolvedValue(
|
||||
new Response(body, {
|
||||
status,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
return createRemoteFetch({
|
||||
baseUrl: "http://remote.example",
|
||||
fetchFn: fetchFn as unknown as typeof fetch,
|
||||
});
|
||||
}
|
||||
|
||||
it("rebuilds an OAuthRequestTimeoutError from the route's marker", async () => {
|
||||
// The one path where a timeout reaches the browser without a client-side
|
||||
// wrapper having fired first: the discovery the SDK runs from inside the
|
||||
// transport has no wrapper at all, so the backend's deadline is the only
|
||||
// one, and it arrives as an ordinary error response.
|
||||
const remoteFetch = remoteReturning(
|
||||
504,
|
||||
JSON.stringify({
|
||||
error: "proxied request to https://as.example.com/x timed out",
|
||||
code: OAUTH_TIMEOUT_WIRE_CODE,
|
||||
url: "https://as.example.com/x",
|
||||
timeoutMs: 30000,
|
||||
}),
|
||||
);
|
||||
|
||||
const err = await remoteFetch("https://as.example.com/x").catch(
|
||||
(e: unknown) => e,
|
||||
);
|
||||
|
||||
expect(err).toBeInstanceOf(OAuthRequestTimeoutError);
|
||||
const timeout = err as OAuthRequestTimeoutError;
|
||||
expect(timeout.url).toBe("https://as.example.com/x");
|
||||
expect(timeout.timeoutMs).toBe(30000);
|
||||
});
|
||||
|
||||
it("leaves an ordinary error response as a plain Error", async () => {
|
||||
const remoteFetch = remoteReturning(
|
||||
500,
|
||||
JSON.stringify({ error: "connect ECONNREFUSED" }),
|
||||
);
|
||||
|
||||
const err = await remoteFetch("https://as.example.com/x").catch(
|
||||
(e: unknown) => e,
|
||||
);
|
||||
|
||||
expect(err).not.toBeInstanceOf(OAuthRequestTimeoutError);
|
||||
expect((err as Error).message).toMatch(/Remote fetch failed \(500\)/);
|
||||
});
|
||||
|
||||
it("ignores a non-JSON error body rather than throwing on the parse", async () => {
|
||||
const remoteFetch = remoteReturning(502, "<html>bad gateway</html>");
|
||||
|
||||
const err = await remoteFetch("https://as.example.com/x").catch(
|
||||
(e: unknown) => e,
|
||||
);
|
||||
|
||||
expect((err as Error).message).toMatch(/Remote fetch failed \(502\)/);
|
||||
});
|
||||
|
||||
it("ignores a marker whose fields are the wrong shape", async () => {
|
||||
// An upstream could serve JSON of its own through a failing proxy; the
|
||||
// guard checks the field types, not just the code.
|
||||
const remoteFetch = remoteReturning(
|
||||
504,
|
||||
JSON.stringify({ code: OAUTH_TIMEOUT_WIRE_CODE, url: 5 }),
|
||||
);
|
||||
|
||||
const err = await remoteFetch("https://as.example.com/x").catch(
|
||||
(e: unknown) => e,
|
||||
);
|
||||
|
||||
expect(err).not.toBeInstanceOf(OAuthRequestTimeoutError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue