Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
fix(cli): reject reset redirects before replay or fallback
Joseph Mikhail9b5fe8647686
diff --git a/docs/security/reset-outcome-reconciliation.md b/docs/security/reset-outcome-reconciliation.mdindex dfdeb4ac..0e589248 100644--- a/docs/security/reset-outcome-reconciliation.md+++ b/docs/security/reset-outcome-reconciliation.md@@ -26,6 +26,14 @@ something the CLI can infer.Local synchronous reset and the existing synchronous fallback for an absentasync **start** route remain. A 404 from **status** never invokes that fallback.++The client never follows redirects for async start, status polling, local sync+reset or legacy sync fallback. A redirected start may already have run; following+307/308 can replay its POST, while following a redirect to a 404 can incorrectly+trigger sync fallback. A refused redirect target also says nothing about whether+the initial reset ran. Redirect responses therefore require reconciliation, with+the original operation ID retained when known. Configure the direct backend+endpoint before any operator-approved recovery; do not repeat an uncertain reset.Authentication errors at start remain errors. This patch does not add tenantheaders, credentials or an authorization bypass; the remote transport mustestablish the expected verified caller identity.diff --git a/ix-cli/src/client/__tests__/reset-redirect.test.ts b/ix-cli/src/client/__tests__/reset-redirect.test.tsnew file mode 100644index 00000000..9006eee0--- /dev/null+++ b/ix-cli/src/client/__tests__/reset-redirect.test.ts@@ -0,0 +1,162 @@+// Copyright 2026 Ix Infrastructure Inc.++import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";+import type { AddressInfo } from "node:net";+import { afterEach, describe, expect, it } from "vitest";+import { IxClient } from "../api.js";+import { isBackendUnreachable } from "../../cli/errors.js";++const opId = "11111111-1111-4111-8111-111111111111";+const servers: Server[] = [];+type Seen = { method: string; path: string; body: string };++// 127.0.0.2 is loopback, but deliberately selects the remote async branch of+// IxClient. Real fetch and real servers are used; no network error is mocked.+async function fixture(reply: (request: Seen, response: ServerResponse) => void, host = "127.0.0.2") {+ const seen: Seen[] = [];+ const server = createServer((request: IncomingMessage, response: ServerResponse) => {+ let body = "";+ request.setEncoding("utf8");+ request.on("data", chunk => { body += chunk; });+ request.on("end", () => {+ const record = { method: request.method ?? "", path: request.url ?? "", body };+ seen.push(record);+ reply(record, response);+ });+ });+ servers.push(server);+ await new Promise<void>((resolve, reject) => {+ server.once("error", reject);+ server.listen(0, host, () => { server.off("error", reject); resolve(); });+ });+ return { server, seen, endpoint: `http://${host}:${(server.address() as AddressInfo).port}` };+}++function json(response: ServerResponse, body: unknown, status = 200) {+ response.writeHead(status, { "content-type": "application/json" });+ response.end(JSON.stringify(body));+}++async function close(server: Server) {+ if (!server.listening) return;+ server.closeAllConnections();+ await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()));+}++afterEach(async () => { await Promise.all(servers.splice(0).map(close)); });++describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirect", method => {+ const stem = method === "reset" ? "/v1/reset" : "/v1/reset/code";++ it("completes the direct asynchronous request and matching status", async () => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) json(response, { opId }, 202);+ else json(response, { opId, state: "done" });+ });+ await expect(new IxClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });+ expect(remote.seen).toEqual([+ { method: "POST", path: `${stem}/async`, body: "{}" },+ { method: "GET", path: `/v1/reset/status/${opId}`, body: "" },+ ]);+ });++ it("preserves a direct local synchronous reset", async () => {+ const local = await fixture((_request, response) => json(response, { ok: true }), "127.0.0.1");+ await expect(new IxClient(local.endpoint)[method]()).resolves.toMatchObject({ ok: true });+ expect(local.seen).toEqual([{ method: "POST", path: stem, body: "{}" }]);+ });++ it("preserves synchronous fallback for a direct async 404", async () => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) json(response, {}, 404);+ else json(response, { ok: true });+ });+ await expect(new IxClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });+ expect(remote.seen).toEqual([+ { method: "POST", path: `${stem}/async`, body: "{}" },+ { method: "POST", path: stem, body: "{}" },+ ]);+ });++ it("preserves unreachable classification when the initial connection is refused", async () => {+ const closed = await fixture((_request, response) => json(response, {}));+ await close(closed.server);+ const failure = await new IxClient(closed.endpoint)[method]().catch((error: unknown) => error);+ expect(closed.seen).toEqual([]);+ expect(failure).toMatchObject({ cause: { code: "ECONNREFUSED" } });+ expect(isBackendUnreachable(failure)).toBe(true);+ });++ it.each([307, 308])("does not replay the POST after a %s start response", async status => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) {+ response.writeHead(status, { location: "/redirected-reset" }); response.end();+ } else if (request.path === "/redirected-reset") json(response, { opId }, 202);+ else json(response, { opId, state: "done" });+ });+ await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");+ expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);+ });++ it.each([302, 303])("does not interpret a redirected %s/404 as an absent async route", async status => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) {+ response.writeHead(status, { location: "/missing" }); response.end();+ } else if (request.path === "/missing") json(response, {}, 404);+ else json(response, { ok: true, message: "synthetic reset" });+ });+ await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");+ expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);+ });++ it("does not classify a refused redirect target as an unsent reset", async () => {+ const closed = await fixture((_request, response) => json(response, {}));+ await close(closed.server);+ const remote = await fixture((_request, response) => {+ response.writeHead(307, { location: `${closed.endpoint}/redirected-reset` }); response.end();+ });+ const failure = await new IxClient(remote.endpoint)[method]().catch((error: unknown) => error);+ expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);+ expect(failure).toMatchObject({ name: "ResetReconciliationError" });+ expect((failure as Error).message).toContain("Do not repeat the reset");+ expect(isBackendUnreachable(failure)).toBe(false);+ });++ it("does not accept completion from a redirected status route", async () => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) json(response, { opId }, 202);+ else if (request.path === `/v1/reset/status/${opId}`) {+ response.writeHead(302, { location: "/unrelated-status" }); response.end();+ } else json(response, { opId, state: "done" });+ });+ const failure = new IxClient(remote.endpoint)[method]();+ await expect(failure).rejects.toThrow(opId);+ await expect(failure).rejects.toThrow("Do not repeat the reset");+ expect(remote.seen.map(({ method, path }) => ({ method, path }))).toEqual([+ { method: "POST", path: `${stem}/async` }, { method: "GET", path: `/v1/reset/status/${opId}` },+ ]);+ });++ it.each([307, 308])("does not replay a local synchronous POST after %s", async status => {+ const local = await fixture((request, response) => {+ if (request.path === stem) {+ response.writeHead(status, { location: "/redirected-reset" }); response.end();+ } else json(response, { ok: true, message: "synthetic reset" });+ }, "127.0.0.1");+ await expect(new IxClient(local.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");+ expect(local.seen).toEqual([{ method: "POST", path: stem, body: "{}" }]);+ });++ it("does not replay a redirected synchronous fallback", async () => {+ const remote = await fixture((request, response) => {+ if (request.path === `${stem}/async`) json(response, {}, 404);+ else if (request.path === stem) {+ response.writeHead(307, { location: "/redirected-reset" }); response.end();+ } else json(response, { ok: true, message: "synthetic reset" });+ });+ await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");+ expect(remote.seen.map(({ method, path }) => ({ method, path }))).toEqual([+ { method: "POST", path: `${stem}/async` }, { method: "POST", path: stem },+ ]);+ });+});diff --git a/ix-cli/src/client/api.ts b/ix-cli/src/client/api.tsindex c1b0238c..702b1546 100644--- a/ix-cli/src/client/api.ts+++ b/ix-cli/src/client/api.ts@@ -462,6 +462,9 @@ export class IxClient {try {beginResp = await fetch(`${this.endpoint}${asyncPath}`, {method: "POST",+ // A redirect can replay a destructive POST or hide an accepted reset+ // behind a later connection failure/404. Inspect the first response.+ redirect: "manual",headers: { "Content-Type": "application/json" },body: JSON.stringify({}),signal: AbortSignal.timeout(30 * 1000),@@ -476,6 +479,10 @@ export class IxClient {throw this.resetReconciliationError("The start response was unavailable.", undefined, error);}+ if (beginResp.status >= 300 && beginResp.status < 400) {+ await beginResp.body?.cancel().catch(() => {});+ throw this.resetReconciliationError(`The start route redirected (${beginResp.status}).`);+ }if (beginResp.status === 404) {return this.runResetSync(syncPath);}@@ -503,6 +510,7 @@ export class IxClient {try {statusResp = await fetch(`${this.endpoint}/v1/reset/status/${opId}`, {method: "GET",+ redirect: "manual",signal: AbortSignal.timeout(30 * 1000),});} catch (error) {@@ -517,6 +525,7 @@ export class IxClient {throw this.resetReconciliationError("The status is unavailable or inaccessible.", opId);}if (statusResp.status !== 200) {+ await statusResp.body?.cancel().catch(() => {});throw this.resetReconciliationError(`Status request returned ${statusResp.status}.`, opId);}let status: unknown;@@ -556,10 +565,15 @@ export class IxClient {private async runResetSync(syncPath: string): Promise<{ ok: boolean; message: string }> {const resp = await fetch(`${this.endpoint}${syncPath}`, {method: "POST",+ redirect: "manual",headers: { "Content-Type": "application/json" },body: JSON.stringify({}),signal: AbortSignal.timeout(10 * 60 * 1000),});+ if (resp.status >= 300 && resp.status < 400) {+ await resp.body?.cancel().catch(() => {});+ throw this.resetReconciliationError(`The synchronous reset route redirected (${resp.status}).`);+ }if (!resp.ok) {const text = await resp.text();throw new Error(`${resp.status}: ${text}`);