Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): prevent replay after redirected connection request

chenhongd1f9eff93a5b
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.connection.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.connection.test.ts
index 7b53faa083..ce8d499646 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.connection.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.connection.test.ts
@@ -185,6 +185,34 @@ describe('networkSlot Connection identity', () => {
expect(h.invalidate).toHaveBeenCalledWith(TOKEN_INPUT);
});
+ it.each([301, 302, 303])(
+ 'does not replay an original POST after a %s redirect downgrades it to GET before a 401',
+ async (redirectStatus) => {
+ const h = makeSlot();
+ h.fetchImpl
+ .mockResolvedValueOnce(
+ new Response(null, {
+ status: redirectStatus,
+ headers: { location: 'https://service-a.x.test/after-redirect' },
+ }),
+ )
+ .mockResolvedValueOnce(response(401));
+
+ const result = await h.slot.handleFetchRequest('plugin-a', {
+ url: URL,
+ method: 'POST',
+ body: '{"action":"create"}',
+ });
+
+ expect(result).toMatchObject({ ok: true, status: 401 });
+ expect(h.fetchImpl).toHaveBeenCalledTimes(2);
+ expect(h.fetchImpl.mock.calls.map((call) => call[1].method)).toEqual(['POST', 'GET']);
+ expect(h.getToken).toHaveBeenCalledTimes(2);
+ expect(h.invalidate).toHaveBeenCalledTimes(1);
+ expect(h.invalidate).toHaveBeenCalledWith(TOKEN_INPUT);
+ },
+ );
+
it('does not retry 403 and never retries a second 401', async () => {
const forbidden = makeSlot();
forbidden.fetchImpl.mockResolvedValue(response(403));
diff --git a/apps/desktop/src/main/cindy-brain/networkSlot.ts b/apps/desktop/src/main/cindy-brain/networkSlot.ts
index c8e5d5719a..0fd7de5372 100644
--- a/apps/desktop/src/main/cindy-brain/networkSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/networkSlot.ts
@@ -1091,8 +1091,10 @@ export class GhostNetworkSlot {
// ── 执行(重定向逐跳手动跟,每跳重验白名单 + 重算凭证注入)────────
// 外层 attempt 循环只为交换型 / oauth / Connection 凭证的 401 兜底:令牌可能被
// 服务端提前作废,作废本地缓存重换/重刷一次再整链重试;第二次仍
- // 401 就原样回给意识。
+ // 401 就原样回给意识。原始请求方法也参与判定:POST/上传即使因 3xx
+ // 降级成 GET,仍不得在 401 后重放最初的副作用请求。
let response: Response | null = null;
+ const originalRequestMethod = method;
for (let attempt = 0; attempt < 2; attempt++) {
if (attempt > 0) {
this.invalidateExchangedTokens(ghostId, net.secrets ?? []);
@@ -1204,7 +1206,10 @@ export class GhostNetworkSlot {
if (
response.status === 401
&& responseConnectionInjected.size > 0
- && !CONNECTION_RETRYABLE_METHODS.has(responseMethod)
+ && (
+ !CONNECTION_RETRYABLE_METHODS.has(responseMethod)
+ || !CONNECTION_RETRYABLE_METHODS.has(originalRequestMethod)
+ )
) {
for (const input of responseConnectionInjected.values()) {
this.deps.connectionTokens?.invalidate({