Skip to content

Commit 52023ff

Browse files
fix(auth): clear OAuth callback timer and reorder EMA scope persist
Clear the loopback callback timeout in runner-interactive-oauth finally so already_authorized and error paths do not hang the CLI/TUI. Check EMA step-up satisfaction against minted token scope before persisting the requested union, matching the standard OAuth path. Co-authored-by: Cursor <[email protected]>
1 parent 0635d09 commit 52023ff

4 files changed

Lines changed: 92 additions & 10 deletions

File tree

‎clients/web/src/test/core/auth/runner-interactive-oauth.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ describe("runRunnerInteractiveOAuth", () => {
8484
expect(client.completeOAuthFlow).not.toHaveBeenCalled();
8585
});
8686

87+
it("clears the callback timeout on already_authorized", async () => {
88+
vi.useFakeTimers();
89+
try {
90+
const client = mockClient({
91+
authenticate: vi.fn(async () => undefined),
92+
});
93+
const redirectUrlProvider = { redirectUrl: "" };
94+
95+
const result = await runRunnerInteractiveOAuth({
96+
client,
97+
redirectUrlProvider,
98+
callbackListen: {
99+
hostname: "127.0.0.1",
100+
port: 6276,
101+
pathname: "/oauth/callback",
102+
},
103+
callbackTimeoutMs: 60_000,
104+
createCallbackServer: () => createMockCallbackServer(handlers),
105+
});
106+
107+
expect(result).toEqual({ kind: "already_authorized" });
108+
expect(vi.getTimerCount()).toBe(0);
109+
await vi.advanceTimersByTimeAsync(60_000);
110+
} finally {
111+
vi.useRealTimers();
112+
}
113+
});
114+
87115
it("completes connect-time OAuth via authenticate and callback", async () => {
88116
const redirectUrlProvider = { redirectUrl: "" };
89117
const client = mockClient({

‎clients/web/src/test/core/mcp/oauthManager.test.ts‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,9 +1215,6 @@ describe("OAuthManager", () => {
12151215
});
12161216

12171217
it("returns satisfied for EMA insufficient_scope after user confirms", async () => {
1218-
const silentSpy = vi
1219-
.spyOn(emaFlow, "trySilentEmaAuth")
1220-
.mockResolvedValue({ status: "success" });
12211218
const params = createMockParams({
12221219
enterpriseManagedAuth: {
12231220
idp: {
@@ -1237,6 +1234,14 @@ describe("OAuthManager", () => {
12371234
token_type: "Bearer",
12381235
scope: "mcp",
12391236
});
1237+
const silentSpy = vi.spyOn(emaFlow, "trySilentEmaAuth").mockImplementation(async () => {
1238+
storageOf(params).getTokens.mockResolvedValue({
1239+
access_token: "tok",
1240+
token_type: "Bearer",
1241+
scope: "mcp weather:read",
1242+
});
1243+
return { status: "success" };
1244+
});
12401245
const manager = new OAuthManager(params);
12411246
manager.setOAuthConfig({ enterpriseManaged: true });
12421247

@@ -1257,6 +1262,51 @@ describe("OAuthManager", () => {
12571262
silentSpy.mockRestore();
12581263
});
12591264

1265+
it("does not persist union scope or return satisfied when silent EMA mint is down-scoped", async () => {
1266+
const silentSpy = vi
1267+
.spyOn(emaFlow, "trySilentEmaAuth")
1268+
.mockResolvedValue({ status: "success" });
1269+
const authUrl = new URL("https://idp.example.com/authorize?state=ema");
1270+
const startSpy = vi
1271+
.spyOn(emaFlow, "startEmaIdpAuthorization")
1272+
.mockResolvedValue(authUrl);
1273+
const params = createMockParams({
1274+
enterpriseManagedAuth: {
1275+
idp: {
1276+
issuer: "https://idp.example.com",
1277+
clientId: "app-client",
1278+
clientSecret: "secret",
1279+
},
1280+
},
1281+
});
1282+
storageOf(params).getScope.mockReturnValue("mcp");
1283+
storageOf(params).getTokens.mockResolvedValue({
1284+
access_token: "tok",
1285+
token_type: "Bearer",
1286+
scope: "mcp",
1287+
});
1288+
const manager = new OAuthManager(params);
1289+
manager.setOAuthConfig({ enterpriseManaged: true });
1290+
1291+
const outcome = await manager.handleAuthChallenge(
1292+
{
1293+
reason: "insufficient_scope",
1294+
requiredScopes: ["weather:read"],
1295+
},
1296+
{ confirmedStepUp: true },
1297+
);
1298+
1299+
expect(outcome).toEqual(
1300+
expect.objectContaining({
1301+
kind: "interactive",
1302+
authorizationUrl: authUrl,
1303+
}),
1304+
);
1305+
expect(storageOf(params).saveScope).not.toHaveBeenCalled();
1306+
silentSpy.mockRestore();
1307+
startSpy.mockRestore();
1308+
});
1309+
12601310
it("completeOAuthFlow mints EMA tokens with pending step-up union scope", async () => {
12611311
const silentSpy = vi
12621312
.spyOn(emaFlow, "trySilentEmaAuth")

‎core/auth/node/runner-interactive-oauth.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export async function runRunnerInteractiveOAuth(
5858
flowReject = reject;
5959
});
6060

61+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
62+
6163
try {
6264
const { redirectUrl } = await server.start({
6365
hostname: options.callbackListen.hostname,
@@ -85,7 +87,6 @@ export async function runRunnerInteractiveOAuth(
8587

8688
const timeoutMs =
8789
options.callbackTimeoutMs ?? DEFAULT_RUNNER_INTERACTIVE_OAUTH_TIMEOUT_MS;
88-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
8990
const waitForCallback = Promise.race([
9091
flowDone.finally(() => {
9192
if (timeoutId !== undefined) {
@@ -131,6 +132,9 @@ export async function runRunnerInteractiveOAuth(
131132

132133
return { kind: "success" };
133134
} finally {
135+
if (timeoutId !== undefined) {
136+
clearTimeout(timeoutId);
137+
}
134138
await server.stop().catch(() => {});
135139
}
136140
}

‎core/mcp/oauthManager.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,13 @@ export class OAuthManager {
602602
if (enriched.reason === "insufficient_scope") {
603603
const silent = await trySilentEmaAuth(config);
604604
if (silent.status === "success") {
605-
if (enriched.authorizationScopes?.length) {
606-
await this.oauthConfig.storage!.saveScope(
607-
this.getServerUrl(),
608-
enriched.authorizationScopes.join(" "),
609-
);
610-
}
611605
if (await this.checkAuthChallengeSatisfied(enriched)) {
606+
if (enriched.authorizationScopes?.length) {
607+
await this.oauthConfig.storage!.saveScope(
608+
this.getServerUrl(),
609+
enriched.authorizationScopes.join(" "),
610+
);
611+
}
612612
return { kind: "satisfied" };
613613
}
614614
}

0 commit comments

Comments
 (0)