mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 19:52:53 +08:00
fix(opencode): default options-form https.request calls to https (#4749)
## Description Part of #4745 `wrapRequest` in the OpenCode transport does not know whether it wraps `http` or `https`. A request made with an options object and no `protocol` was read as `http:`, so `https.request({ hostname: "api.openai.com", path: "/v1/chat/completions", method: "POST" })` was routed with `x-headroom-base-url: http://api.openai.com`, and with `port: 443` as `http://api.openai.com:443`. The proxy accepts an `http` upstream, so the caller's `Authorization` header would be forwarded in cleartext. This PR passes the wrapped module's default protocol (`https:` for `https.request`/`https.get`, `http:` for `http`) into the URL builder. A `protocol` in the options still wins. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `plugins/opencode/src/transport.ts`: `splitNodeArgs` and `urlFromRequestOptions` take a `defaultProtocol`; `wrapRequest` passes `https:` when it wraps `https.request` and `http:` otherwise. - `plugins/opencode/src/transport.test.ts`: next to the existing `https.request` routing test, one test sends four options-form POSTs (`https.request` with and without `port: 443`, `http.request` with and without `port: 8080`) to a local stand-in for the proxy and checks the `x-headroom-base-url` it receives. ## Testing - [ ] Unit tests pass (`pytest`) - [ ] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed This is a TypeScript change in `plugins/opencode`, so the Python checks above do not apply. `vitest run` was run. `tsc --noEmit` was not run on this branch (it is a few-line change; the same toolchain was run on the sibling branches). The package has no lint or format script and the root ruff config covers Python only, so none was run. ### Test Output Fail before (`transport.ts`, `plugin.ts`, `retrieve.ts` from `origin/main` `1da8930a`, new test kept; `npx vitest run` in `plugins/opencode`, Ubuntu on WSL2, Node v24.22.2): ```text AssertionError: expected [ 'http://api.openai.com', …(3) ] to deeply equal [ 'https://api.openai.com', …(3) ] [ - "https://api.openai.com", - "https://api.openai.com", + "http://api.openai.com", + "http://api.openai.com:443", "http://api.openai.com", "http://api.openai.com:8080", ] Test Files 1 failed | 3 passed (4) Tests 1 failed | 66 passed (67) ``` Pass after (this branch): ```text Test Files 4 passed (4) Tests 67 passed (67) ``` ## Real Behavior Proof - Environment: Ubuntu on WSL2 (Windows 11 host), Node v24.22.2, vitest from `plugins/opencode/package-lock.json` (`npm ci --ignore-scripts`), no network (a local HTTP server stands in for the proxy). - Exact command / steps: `npx vitest run` in `plugins/opencode`; the new test installs the transport with the stand-in's URL and makes the four options-form POSTs above. - Observed result: see Test Output; on `origin/main` the two `https.request` calls arrive as `http://api.openai.com` and `http://api.openai.com:443`, with this change as `https://api.openai.com` for both. The `http.request` results are the same before and after. - Not tested: the real OpenCode host; a real provider; Windows; what the Headroom proxy does with the header value. ## Runtime Rollout Safety - Rollout-managed feature(s): none changed. - Minimum rollout channel: n/a (no new feature) - Stable/default behavior changed: yes, narrowly: an options-form `https.request`/`https.get` call without `protocol` now gets an `https://` base URL header instead of `http://`. URL-form calls and `http` are unchanged. - Kill switch / disable path: none added. - Unsafe override required: no - Qualification impact: none expected. - Rollback path: revert this commit. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes (see Testing) - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Additional Notes - A custom `agent` option (for example an `https.Agent` passed to `http.request`) is not used to infer the protocol; only the wrapped module decides the default. Produced with AI assistance (Claude Code) operated by me (HetCreep); the tests and proof above were run on my machine against a clone of the repository. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4749" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com> Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
JD Davis
parent
6b7b6358d5
commit
12b5fb7f2a
@@ -20140,7 +20140,7 @@ function withRoutedFetchInput(input2, init, proxy, project, excludeHosts, sessio
|
||||
}
|
||||
return [nextUrl, nextInit];
|
||||
}
|
||||
function splitNodeArgs(args) {
|
||||
function splitNodeArgs(args, defaultProtocol) {
|
||||
const callback = typeof args.at(-1) === "function" ? args.at(-1) : void 0;
|
||||
const withoutCallback = callback ? args.slice(0, -1) : args;
|
||||
const [first, second] = withoutCallback;
|
||||
@@ -20157,12 +20157,12 @@ function splitNodeArgs(args) {
|
||||
}
|
||||
if (typeof first === "object" && first !== null) {
|
||||
const requestOptions = { ...first, ...options };
|
||||
return { url: urlFromRequestOptions(requestOptions), options: requestOptions, callback };
|
||||
return { url: urlFromRequestOptions(requestOptions, defaultProtocol), options: requestOptions, callback };
|
||||
}
|
||||
return { options, callback };
|
||||
}
|
||||
function urlFromRequestOptions(options) {
|
||||
const protocol = String(options.protocol ?? "http:");
|
||||
function urlFromRequestOptions(options, defaultProtocol) {
|
||||
const protocol = String(options.protocol ?? defaultProtocol);
|
||||
if (protocol !== "http:" && protocol !== "https:") {
|
||||
return void 0;
|
||||
}
|
||||
@@ -20242,7 +20242,7 @@ function wrapRequest(originalHttpRequest, originalHttpsRequest, originalRequest)
|
||||
if (!proxy) {
|
||||
return Reflect.apply(originalRequest, this, args);
|
||||
}
|
||||
const parts = splitNodeArgs(args);
|
||||
const parts = splitNodeArgs(args, originalRequest === originalHttpsRequest ? "https:" : "http:");
|
||||
const nextOptions = routedNodeOptions(
|
||||
parts,
|
||||
proxy,
|
||||
|
||||
@@ -348,7 +348,7 @@ function withRoutedFetchInput(input, init, proxy, project, excludeHosts, session
|
||||
}
|
||||
return [nextUrl, nextInit];
|
||||
}
|
||||
function splitNodeArgs(args) {
|
||||
function splitNodeArgs(args, defaultProtocol) {
|
||||
const callback = typeof args.at(-1) === "function" ? args.at(-1) : void 0;
|
||||
const withoutCallback = callback ? args.slice(0, -1) : args;
|
||||
const [first, second] = withoutCallback;
|
||||
@@ -365,12 +365,12 @@ function splitNodeArgs(args) {
|
||||
}
|
||||
if (typeof first === "object" && first !== null) {
|
||||
const requestOptions = { ...first, ...options };
|
||||
return { url: urlFromRequestOptions(requestOptions), options: requestOptions, callback };
|
||||
return { url: urlFromRequestOptions(requestOptions, defaultProtocol), options: requestOptions, callback };
|
||||
}
|
||||
return { options, callback };
|
||||
}
|
||||
function urlFromRequestOptions(options) {
|
||||
const protocol = String(options.protocol ?? "http:");
|
||||
function urlFromRequestOptions(options, defaultProtocol) {
|
||||
const protocol = String(options.protocol ?? defaultProtocol);
|
||||
if (protocol !== "http:" && protocol !== "https:") {
|
||||
return void 0;
|
||||
}
|
||||
@@ -450,7 +450,7 @@ function wrapRequest(originalHttpRequest, originalHttpsRequest, originalRequest)
|
||||
if (!proxy) {
|
||||
return Reflect.apply(originalRequest, this, args);
|
||||
}
|
||||
const parts = splitNodeArgs(args);
|
||||
const parts = splitNodeArgs(args, originalRequest === originalHttpsRequest ? "https:" : "http:");
|
||||
const nextOptions = routedNodeOptions(
|
||||
parts,
|
||||
proxy,
|
||||
|
||||
@@ -532,6 +532,41 @@ describe("Headroom OpenCode transport", () => {
|
||||
await proxy.close();
|
||||
});
|
||||
|
||||
it("uses the module's default protocol for options-form requests without a protocol", async () => {
|
||||
const proxy = await proxyServer();
|
||||
installHeadroomTransport({ proxyUrl: proxy.url });
|
||||
|
||||
const post = (
|
||||
request: typeof https.request,
|
||||
options: Record<string, unknown>,
|
||||
): Promise<void> =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
const req = request(
|
||||
{ hostname: "api.openai.com", path: "/v1/chat/completions", method: "POST", ...options },
|
||||
(res) => {
|
||||
res.resume();
|
||||
res.on("end", resolve);
|
||||
},
|
||||
);
|
||||
req.on("error", reject);
|
||||
req.end("{}");
|
||||
});
|
||||
|
||||
await post(https.request, {});
|
||||
await post(https.request, { port: 443 });
|
||||
await post(http.request, {});
|
||||
await post(http.request, { port: 8080 });
|
||||
|
||||
expect(proxy.seen.map((seen) => seen.headers["x-headroom-base-url"])).toEqual([
|
||||
"https://api.openai.com",
|
||||
"https://api.openai.com",
|
||||
"http://api.openai.com",
|
||||
"http://api.openai.com:8080",
|
||||
]);
|
||||
|
||||
await proxy.close();
|
||||
});
|
||||
|
||||
it("normalizes Node HTTP(S) requests for /chat/completions and /responses", async () => {
|
||||
const proxy = await proxyServer("");
|
||||
installHeadroomTransport({ proxyUrl: proxy.url });
|
||||
|
||||
@@ -593,7 +593,7 @@ function withRoutedFetchInput(
|
||||
return [nextUrl, nextInit];
|
||||
}
|
||||
|
||||
function splitNodeArgs(args: unknown[]): NodeRequestParts {
|
||||
function splitNodeArgs(args: unknown[], defaultProtocol: string): NodeRequestParts {
|
||||
const callback = typeof args.at(-1) === "function" ? (args.at(-1) as (...args: unknown[]) => unknown) : undefined;
|
||||
const withoutCallback = callback ? args.slice(0, -1) : args;
|
||||
const [first, second] = withoutCallback;
|
||||
@@ -611,13 +611,15 @@ function splitNodeArgs(args: unknown[]): NodeRequestParts {
|
||||
}
|
||||
if (typeof first === "object" && first !== null) {
|
||||
const requestOptions = { ...(first as Record<string, unknown>), ...options };
|
||||
return { url: urlFromRequestOptions(requestOptions), options: requestOptions, callback };
|
||||
return { url: urlFromRequestOptions(requestOptions, defaultProtocol), options: requestOptions, callback };
|
||||
}
|
||||
return { options, callback };
|
||||
}
|
||||
|
||||
function urlFromRequestOptions(options: Record<string, unknown>): URL | undefined {
|
||||
const protocol = String(options.protocol ?? "http:");
|
||||
// An options object without `protocol` means the module's own default:
|
||||
// https.request({ hostname }) is https, http.request({ hostname }) is http.
|
||||
function urlFromRequestOptions(options: Record<string, unknown>, defaultProtocol: string): URL | undefined {
|
||||
const protocol = String(options.protocol ?? defaultProtocol);
|
||||
if (protocol !== "http:" && protocol !== "https:") {
|
||||
return undefined;
|
||||
}
|
||||
@@ -723,7 +725,7 @@ function wrapRequest(
|
||||
if (!proxy) {
|
||||
return Reflect.apply(originalRequest, this, args);
|
||||
}
|
||||
const parts = splitNodeArgs(args);
|
||||
const parts = splitNodeArgs(args, originalRequest === originalHttpsRequest ? "https:" : "http:");
|
||||
const nextOptions = routedNodeOptions(
|
||||
parts,
|
||||
proxy,
|
||||
|
||||
Reference in New Issue
Block a user