Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(findings): keep redirects within the configured service origin (#2426)

Michael D'Angelo28ceafdf8793
diff --git a/sdk/typescript/docs/findings-service.md b/sdk/typescript/docs/findings-service.md
index e89635ede..9fb6c2081 100644
--- a/sdk/typescript/docs/findings-service.md
+++ b/sdk/typescript/docs/findings-service.md
@@ -357,6 +357,8 @@ results, and these callbacks add no network destination or separate files.
## Compatible endpoint contract
The HTTP client appends these paths to the configured base URL, preserving any base path.
+It follows redirects within that origin (scheme, host, and port). If the service
+moves to another origin, set `--findings-url` to its new base URL.
| Method | Path | Response |
| ------ | --------------------------------------- | ------------------------------------------------------------------------ |
diff --git a/sdk/typescript/src/findings-client.ts b/sdk/typescript/src/findings-client.ts
index 4ab5a03e6..c3e42bcf4 100644
--- a/sdk/typescript/src/findings-client.ts
+++ b/sdk/typescript/src/findings-client.ts
@@ -93,7 +93,7 @@ export class FindingsClient {
url.searchParams.set("allRepositories", "true");
else url.searchParams.set("repositoryId", scope.repositoryId);
return await this.retry(async () => {
- const response = await this.request(url, { signal: this.signal });
+ const response = await this.requestEndpoint(url);
if (!response.ok) {
throw await FindingsHttpError.fromResponse(
response,
@@ -162,12 +162,49 @@ export class FindingsClient {
return new URL(path, this.url);
}
+ private async requestEndpoint(
+ url: URL,
+ post?: RequestInit,
+ ): Promise<Response> {
+ for (let redirects = 0; ; redirects++) {
+ const response = await this.request(url, {
+ ...post,
+ signal: this.signal,
+ redirect: "manual",
+ });
+ const location = response.headers.get("Location");
+ if (
+ location === null ||
+ ![301, 302, 303, 307, 308].includes(response.status)
+ )
+ return response;
+ void response.body?.cancel().catch(() => undefined);
+ this.signal?.throwIfAborted();
+ // Fetch exposes header bytes as a Latin-1 string; URL paths use UTF-8.
+ const next = new URL(
+ Buffer.from(location, "latin1").toString("utf8"),
+ url,
+ );
+ if (next.protocol !== this.url.protocol || next.host !== this.url.host)
+ throw new FindingsHttpError(
+ `Findings API redirect leaves the configured service origin (HTTP ${response.status}). Configure the final service URL.`,
+ response.status,
+ null,
+ );
+
+ // Retain Fetch's redirect limit and POST-to-GET behavior.
+ if (redirects === 20)
+ throw new TypeError("Too many findings service redirects.");
+ if ([301, 302, 303].includes(response.status)) post = undefined;
+ url = next;
+ }
+ }
+
private async post(path: string, body: unknown): Promise<unknown> {
- const response = await this.request(this.endpoint(path), {
+ const response = await this.requestEndpoint(this.endpoint(path), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
- signal: this.signal,
});
if (!response.ok) {
throw await FindingsHttpError.fromResponse(
diff --git a/sdk/typescript/tests-ts/custom-publish.test.ts b/sdk/typescript/tests-ts/custom-publish.test.ts
index bc437b1da..f5ea08a92 100644
--- a/sdk/typescript/tests-ts/custom-publish.test.ts
+++ b/sdk/typescript/tests-ts/custom-publish.test.ts
@@ -33,6 +33,7 @@ test("publishes complete sealed findings with their repository ID to a custom ba
expect(String(url)).toBe("http://synthetic.test/service/v1/bulk/findings");
expect(options).toEqual({
method: "POST",
+ redirect: "manual",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
findings: document.findings,
diff --git a/sdk/typescript/tests-ts/findings-client-retry.test.ts b/sdk/typescript/tests-ts/findings-client-retry.test.ts
index 8c936c021..edbb5eaf9 100644
--- a/sdk/typescript/tests-ts/findings-client-retry.test.ts
+++ b/sdk/typescript/tests-ts/findings-client-retry.test.ts
@@ -16,6 +16,73 @@ const errorMediaTypes = [
"Application/vnd.synthetic.findings+JSON; charset=utf-8",
];
+test.each([301, 302, 303, 307, 308])(
+ "follows a same-origin HTTP %i with Fetch's method and body",
+ async (status) => {
+ const controller = new AbortController();
+ const request = mock(async (url: URL, init: RequestInit) => {
+ expect(init.redirect).toBe("manual");
+ expect(init.signal).toBe(controller.signal);
+ if (url.pathname.endsWith("/bulk/findings")) {
+ return new Response(null, {
+ status,
+ headers: { Location: Buffer.from("../reçu").toString("latin1") },
+ });
+ }
+ expect(url.href).toBe("https://service.test/v1/re%C3%A7u");
+ expect(init.method ?? "GET").toBe(status >= 307 ? "POST" : "GET");
+ expect(init.body).toBe(
+ status >= 307
+ ? JSON.stringify({ findings: [], repositoryId: scope.repositoryId })
+ : undefined,
+ );
+ expect(new Headers(init.headers).get("content-type")).toBe(
+ status >= 307 ? "application/json" : null,
+ );
+ return Response.json([]);
+ });
+ const client = new FindingsClient(
+ "https://service.test",
+ controller.signal,
+ request,
+ );
+ await expect(client.publish([], scope.repositoryId)).resolves.toEqual([]);
+ expect(request).toHaveBeenCalledTimes(2);
+ },
+);
+
+test.each(["lookup", "publish", "groups"] as const)(
+ "%s stops when a redirect chain leaves the findings service origin",
+ async (operation) => {
+ const request = mock(async (url: URL, init: RequestInit) => {
+ expect(url.origin).toBe("https://service.test");
+ expect(init.redirect).toBe("manual");
+ return new Response(new ReadableStream(), {
+ status: 308,
+ headers: {
+ "Content-Type": "application/json",
+ Location:
+ url.pathname === "/next" ? "//service.test:8443/result" : "/next",
+ },
+ });
+ });
+ const client = new FindingsClient(
+ "https://service.test",
+ undefined,
+ request,
+ { wait: rejecting("Unexpected retry") },
+ );
+ const result =
+ operation === "lookup"
+ ? client.potentialDuplicates("synthetic", scope)
+ : operation === "publish"
+ ? client.publish([], scope.repositoryId)
+ : client.storeDedupeGroups([["a", "b"]]);
+ await expect(result).rejects.toMatchObject({ status: 308 });
+ expect(request).toHaveBeenCalledTimes(2);
+ },
+);
+
test.each(errorMediaTypes)("lookup retains %s errors", async (mediaType) => {
const message = "No current embedding exists in the requested repository. 🧪";
const client = new FindingsClient(