Skip to content

Commit 08eb65d

Browse files
committed
fix(cursor): address CodeRabbit findings on EOF, HTTPS, and probe shape
Count createPlanRequestQuery text as assistant output before clean EOF, reject non-loopback http discovery URLs before sending the Bearer token, and return the live model count as structured data on the Cursor probe.
1 parent 6a64db1 commit 08eb65d

5 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎src/adapters/cursor/live-models.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,36 @@ const DISCOVERY_RETRY_TIMEOUT_MS = 3_000;
5050
*/
5151
export async function fetchCursorUsableModels(opts: CursorUsableModelsOptions): Promise<CursorUsableModelsResult> {
5252
if (cursorUsableModelsFetcherForTests) return cursorUsableModelsFetcherForTests(opts);
53-
const first = await fetchCursorUsableModelsOnce(opts);
53+
const resolved = resolveCursorDiscoveryBaseUrl(opts.baseUrl ?? "https://api2.cursor.sh");
54+
if (!resolved.ok) return resolved;
55+
const first = await fetchCursorUsableModelsOnce({ ...opts, baseUrl: resolved.baseUrl });
5456
if (first.ok || !RETRYABLE_DISCOVERY_ERRORS.has(first.error)) return first;
5557
await new Promise(resolve => setTimeout(resolve, 250 + Math.floor(Math.random() * 250)));
56-
return fetchCursorUsableModelsOnce({ ...opts, timeoutMs: Math.min(opts.timeoutMs ?? 8000, DISCOVERY_RETRY_TIMEOUT_MS) });
58+
return fetchCursorUsableModelsOnce({
59+
...opts,
60+
baseUrl: resolved.baseUrl,
61+
timeoutMs: Math.min(opts.timeoutMs ?? 8000, DISCOVERY_RETRY_TIMEOUT_MS),
62+
});
63+
}
64+
65+
function resolveCursorDiscoveryBaseUrl(raw: string): { ok: true; baseUrl: string } | Extract<CursorUsableModelsResult, { ok: false }> {
66+
const baseUrl = raw.replace(/\/+$/, "");
67+
let parsed: URL;
68+
try {
69+
parsed = new URL(baseUrl);
70+
} catch {
71+
return { ok: false, error: "transport", detail: "Cursor discovery URL is invalid" };
72+
}
73+
if (parsed.protocol === "https:") return { ok: true, baseUrl };
74+
// Local h2c fixtures (and an operator loopback proxy) never leave the machine.
75+
// Anything else with a Bearer token must be HTTPS, matching providerOutbound POST.
76+
if (parsed.protocol === "http:") {
77+
const host = parsed.hostname.replace(/^\[|\]$/g, "").toLowerCase();
78+
if (host === "127.0.0.1" || host === "::1" || host === "localhost" || host.endsWith(".localhost")) {
79+
return { ok: true, baseUrl };
80+
}
81+
}
82+
return { ok: false, error: "transport", detail: "Cursor discovery URL must use HTTPS" };
5783
}
5884

5985
async function fetchCursorUsableModelsOnce(opts: CursorUsableModelsOptions): Promise<CursorUsableModelsResult> {

‎src/adapters/cursor/live-transport.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,10 @@ class LiveCursorTransport implements CursorTransport {
11061106
debugProviderDiagnostic("cursor", "interaction-query", { id: query.id, queryCase: query.query.case ?? "unknown", reply: plan.replyCase });
11071107
this.stream.write(encodeClientMessage({ message: { case: "interactionResponse", value: plan.response } }));
11081108
if (!state.terminated) {
1109-
if (plan.planText) push({ type: "text", text: plan.planText });
1109+
if (plan.planText) {
1110+
this.sawAssistantText = true;
1111+
push({ type: "text", text: plan.planText });
1112+
}
11101113
push({ type: "heartbeat" });
11111114
}
11121115
return;

‎src/server/management/provider-routes.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,12 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
747747
error: `cursor discovery ${live.error}${live.detail ? `: ${live.detail}` : ""}`,
748748
});
749749
}
750-
return jsonResponse({ ok: true, latencyMs, message: `Connected. ${live.models.length} models.` });
750+
return jsonResponse({
751+
ok: true,
752+
latencyMs,
753+
models: live.models.length,
754+
message: `Connected. ${live.models.length} models.`,
755+
});
751756
}
752757
const project = prov.project ?? snapshot?.projectId;
753758
if (antigravity && !project) {

‎tests/cursor-hardening.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { create, toBinary } from "@bufbuild/protobuf";
33
import { describe, expect, spyOn, test } from "bun:test";
44
import {
55
AgentServerMessageSchema,
6+
CreatePlanArgsSchema,
7+
CreatePlanRequestQuerySchema,
68
GetUsableModelsResponseSchema,
9+
InteractionQuerySchema,
710
KvServerMessageSchema,
811
ModelDetailsSchema,
912
TextDeltaUpdateSchema,
@@ -78,6 +81,19 @@ describe("Cursor live-model discovery hardening", () => {
7881
expect(result).toEqual({ ok: true, models: ["gpt-5.5-high"] });
7982
});
8083

84+
test("rejects a cleartext non-loopback discovery URL before connecting", async () => {
85+
const result = await fetchCursorUsableModels({
86+
apiKey: "test-token",
87+
baseUrl: "http://api2.cursor.sh",
88+
});
89+
90+
expect(result).toEqual({
91+
ok: false,
92+
error: "transport",
93+
detail: "Cursor discovery URL must use HTTPS",
94+
});
95+
});
96+
8197
test("classifies authentication failures", async () => {
8298
const result = await withDiscoveryServer(respond(401), baseUrl =>
8399
fetchCursorUsableModels({ apiKey: "bad-token", baseUrl }));
@@ -424,6 +440,57 @@ describe("Cursor live transport unexpected EOF", () => {
424440
});
425441
});
426442

443+
test("synthesizes done after createPlanRequestQuery text on clean Connect EOF", async () => {
444+
const planFrame = encodeConnectFrame(toBinary(AgentServerMessageSchema, create(AgentServerMessageSchema, {
445+
message: {
446+
case: "interactionQuery",
447+
value: create(InteractionQuerySchema, {
448+
id: 7,
449+
query: {
450+
case: "createPlanRequestQuery",
451+
value: create(CreatePlanRequestQuerySchema, {
452+
args: create(CreatePlanArgsSchema, {
453+
name: "Fix bridge",
454+
overview: "Two steps.",
455+
plan: "1. read\n2. patch",
456+
}),
457+
}),
458+
},
459+
}),
460+
},
461+
})));
462+
const connectEnd = encodeConnectFrame(new TextEncoder().encode("{}"), {
463+
flags: CONNECT_FLAG_END_STREAM,
464+
});
465+
466+
await withDiscoveryServer(stream => {
467+
stream.respond({ ":status": 200, "content-type": "application/connect+proto" });
468+
stream.end(Buffer.from(new Uint8Array([...planFrame, ...connectEnd])));
469+
}, async baseUrl => {
470+
const transport = createLiveCursorTransport({
471+
provider: { adapter: "cursor", baseUrl, apiKey: "test-token" },
472+
translatorBudget: createTestTranslatorBudget(),
473+
firstFrameTimeoutMs: 2_000,
474+
});
475+
const messages: Array<{ type: string; text?: string }> = [];
476+
try {
477+
for await (const message of transport.run({
478+
modelId: "composer-2",
479+
conversationId: "cursor_plan_eof_test",
480+
system: [],
481+
messages: [{ role: "user", content: "hello" }],
482+
})) {
483+
messages.push(message);
484+
}
485+
} finally {
486+
await transport.close?.();
487+
}
488+
489+
expect(messages.some(message => message.type === "text" && message.text?.includes("Fix bridge"))).toBe(true);
490+
expect(messages.at(-1)).toMatchObject({ type: "done" });
491+
});
492+
});
493+
427494
test("zero-frame stream end surfaces as a transport error, not success", async () => {
428495
// Real h2c peer that accepts the request stream and immediately ends it with no
429496
// response frames — the shape the WP4 reviewer reproduced as a silent success.

‎tests/provider-connection-test.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("POST /api/providers/test (WP040 connectivity probe)", () => {
7070

7171
const { body } = await probe(config, "cursor");
7272

73-
expect(body).toMatchObject({ ok: true, message: "Connected. 2 models." });
73+
expect(body).toMatchObject({ ok: true, models: 2, message: "Connected. 2 models." });
7474
expect(calls).toEqual([{ apiKey: "cursor-access-token", baseUrl: "https://api2.cursor.sh" }]);
7575
});
7676

0 commit comments

Comments
 (0)