Skip to content

Commit 41665c1

Browse files
committed
fix(http): reject unsupported version pin targets
1 parent 82a9b3d commit 41665c1

5 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎docs-site/src/content/docs/reference/configuration/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids.
6767
| `adapter` | `string` | One of `openai-chat`, `openai-responses`, `anthropic`, `google`, `kiro`, `cursor`, `azure-openai` (or alias `azure`). |
6868
| `baseUrl` | `string` | Upstream API base URL. Most built-in fixed endpoints ignore a mismatch; collision-safe key presets preserve an older same-named custom destination. |
6969
| `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | Optional client-side outbound request-start pacing, separate from upstream usage, billing, and rate-limit indicators. RPM is converted to an even interval; `minIntervalMs` may impose a longer interval. Provider limits apply across all models, while `models` entries use exact upstream model IDs (for example `nvidia/llama-3.1-nemotron-ultra-253b-v1`) and can only add delay. Queue waits do not consume the upstream response-header timeout. HTTP, Responses WebSocket, and explicit adapter `fetchResponse`/`runTurn` dispatches are covered. |
70-
| `upstreamHttpVersion?` | `"auto" \| "http1.1" \| "h1" \| "http2" \| "h2"` | Pin the HTTP version used for upstream requests to this provider. Defaults to `auto`, which lets Bun negotiate. Set `http1.1` when a provider's HTTP/2 SSE stream stalls instead of delivering events — the symptom is a long-running streaming request that produces nothing and eventually times out. For Cursor, `http1.1`/`h1` selects its `RunSSE` + `BidiAppend` compatibility transport for inference and also pins live model discovery. Management `POST`/`PATCH` accept `null` to clear it back to `auto`. |
70+
| `upstreamHttpVersion?` | `"auto" \| "http1.1" \| "h1" \| "http2" \| "h2"` | Pin the HTTP version used for upstream requests to this provider. Defaults to `auto`, which lets Bun negotiate. An explicit pin requires an HTTPS target and fails locally when it cannot be honored. Set `http1.1` when a provider's HTTP/2 SSE stream stalls instead of delivering events — the symptom is a long-running streaming request that produces nothing and eventually times out. For Cursor, `http1.1`/`h1` selects its `RunSSE` + `BidiAppend` compatibility transport for inference and also pins live model discovery. Management `POST`/`PATCH` accept `null` to clear it back to `auto`. |
7171
| `responsesPath?` | `string` | Relative resource path for key-auth `openai-responses` requests. It must start with `/` and contain no scheme, query, or fragment. |
7272
| `supportsServiceTier?` | `boolean` | Tri-state `service_tier` capability fallback. `true`: fast mode may inject and caller values are preserved. `false`: the field is stripped and never injected, and exact model declarations cannot reopen it. Absent: the provider is unclassified — caller-supplied values are preserved untouched and fast mode never injects unless an exact model is enabled. The registry classifies canonical OpenAI (`true`), DeepSeek, and Volcengine Ark (`false`); set it explicitly only for custom gateways that genuinely support tiers. Chat routes additionally need provider-wide or exact-model Chat authorization. |
7373
| `modelSupportsServiceTier?` | `Record<string, boolean>` | Exact upstream model capability overrides. Exact `true` authorizes that Chat model even without `chatServiceTier`; exact `false` narrows provider defaults and Chat authorization. An explicit provider-level `supportsServiceTier: false` remains fail-closed and cannot be reopened. Undeclared models fall back to provider-wide behavior. Management `PATCH /api/providers` merges entries and accepts `null` to clear one. |

‎src/lib/upstream-http-version.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,33 @@ const UPSTREAM_HTTP_VERSION_PROTOCOL: Record<Exclude<UpstreamHttpVersion, "auto"
1717
h2: "h2",
1818
};
1919

20+
/** Raised before I/O when a fixed Bun HTTP-version pin cannot apply to the target URL. */
21+
export class UpstreamHttpVersionTargetError extends Error {
22+
readonly code = "upstream_http_version_target";
23+
24+
constructor() {
25+
super("upstream HTTP version pin requires an HTTPS target");
26+
this.name = "UpstreamHttpVersionTargetError";
27+
}
28+
}
29+
2030
/** Attach Bun's `protocol` pin for one explicit version value. */
2131
export function withUpstreamHttpVersionValue(
2232
input: Parameters<typeof globalThis.fetch>[0],
2333
init: RequestInit | undefined,
2434
version: UpstreamHttpVersion | undefined,
2535
): RequestInit | undefined {
2636
if (!version || version === "auto") return init;
27-
// Bun's protocol pin requires an https: target; local/plaintext upstreams keep
28-
// their existing transport untouched.
37+
// Bun's protocol pin requires an https: target. An explicit fixed-version request must fail
38+
// locally when the pin cannot be honored instead of silently negotiating another transport.
2939
const target = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
40+
let targetUrl: URL;
3041
try {
31-
if (new URL(target).protocol !== "https:") return init;
42+
targetUrl = new URL(target);
3243
} catch {
33-
return init;
44+
throw new UpstreamHttpVersionTargetError();
3445
}
46+
if (targetUrl.protocol !== "https:") throw new UpstreamHttpVersionTargetError();
3547
return { ...(init ?? {}), protocol: UPSTREAM_HTTP_VERSION_PROTOCOL[version] } as RequestInit;
3648
}
3749

‎src/types.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,9 @@ export interface OcxProviderConfig {
13961396
* HTTP/2 via TLS ALPN by default; some Cloudflare-fronted SSE endpoints hang on
13971397
* HTTP/2 streaming responses (issue #1668). "http1.1" / "h1" forces HTTP/1.1,
13981398
* "http2" / "h2" forces HTTP/2. Absent or "auto" keeps Bun's default negotiation
1399-
* (current behavior unchanged). Only meaningful for https: base URLs. Cursor additionally maps
1400-
* an HTTP/1.1 pin onto its RunSSE + BidiAppend compatibility transport.
1399+
* (current behavior unchanged). Explicit pins require an https: target and fail locally when the
1400+
* pin cannot be honored. Cursor additionally maps an HTTP/1.1 pin onto its RunSSE + BidiAppend
1401+
* compatibility transport.
14011402
*/
14021403
upstreamHttpVersion?: UpstreamHttpVersion;
14031404
/**

‎tests/cursor-hardening.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe("Cursor live-model discovery hardening", () => {
138138
fetch: fetchImpl,
139139
});
140140

141-
expect(result).toMatchObject({ ok: false, error: "transport", detail: expect.stringContaining("requires HTTPS") });
141+
expect(result).toEqual({ ok: false, error: "transport", detail: "Cursor discovery URL must use HTTPS" });
142142
expect(fetchCalls).toBe(0);
143143
});
144144

‎tests/upstream-http-version.test.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ describe("withUpstreamHttpVersion", () => {
5555
}
5656
});
5757

58-
test("plain-http targets are left untouched (Bun protocol requires https)", () => {
58+
test("plain-http targets fail clearly when an explicit pin cannot be honored", () => {
5959
const init = { method: "POST", headers: {} };
60-
expect(withUpstreamHttpVersion(HTTP_URL, init, provider({ upstreamHttpVersion: "http1.1" }))).toBe(init);
60+
expect(() => withUpstreamHttpVersion(
61+
HTTP_URL,
62+
init,
63+
provider({ upstreamHttpVersion: "http1.1" }),
64+
)).toThrow("upstream HTTP version pin requires an HTTPS target");
6165
});
6266

6367
test("Request objects resolve their url for the https guard", () => {
@@ -67,9 +71,13 @@ describe("withUpstreamHttpVersion", () => {
6771
expect((out as RequestInit & { protocol?: string }).protocol).toBe("http1.1");
6872
});
6973

70-
test("unparseable targets degrade to the untouched init", () => {
74+
test("unparseable targets fail clearly when a pin is configured", () => {
7175
const init = { method: "POST" };
72-
expect(withUpstreamHttpVersion("not a url", init, provider({ upstreamHttpVersion: "http1.1" }))).toBe(init);
76+
expect(() => withUpstreamHttpVersion(
77+
"not a url",
78+
init,
79+
provider({ upstreamHttpVersion: "http1.1" }),
80+
)).toThrow("upstream HTTP version pin requires an HTTPS target");
7381
});
7482

7583
test("absent init still applies the pin (providerFetch without init)", () => {
@@ -116,4 +124,19 @@ describe("providerFetch upstreamHttpVersion propagation", () => {
116124
await fetcher(HTTPS_URL);
117125
expect((seen.init as RequestInit & { protocol?: string })?.protocol).toBe("http1.1");
118126
});
127+
128+
test("a plaintext target with an explicit pin fails before the provider fetch runs", async () => {
129+
let fetchCalls = 0;
130+
const fetcher = providerFetch(provider({
131+
upstreamHttpVersion: "http1.1",
132+
fetch: (async () => {
133+
fetchCalls += 1;
134+
return new Response("must not run");
135+
}) as typeof fetch,
136+
}));
137+
138+
await expect(fetcher(HTTP_URL, { method: "POST" }))
139+
.rejects.toThrow("upstream HTTP version pin requires an HTTPS target");
140+
expect(fetchCalls).toBe(0);
141+
});
119142
});

0 commit comments

Comments
 (0)