






















@@ -1,6 +1,6 @@
11import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22import { MatrixMediaSizeLimitError } from "../media-errors.js";
3-import { performMatrixRequest } from "./transport.js";
3+import { createMatrixGuardedFetch, performMatrixRequest } from "./transport.js";
4455const TEST_UNDICI_RUNTIME_DEPS_KEY = "__OPENCLAW_TEST_UNDICI_RUNTIME_DEPS__";
66@@ -166,3 +166,64 @@ describe("performMatrixRequest", () => {
166166);
167167});
168168});
169+170+describe("createMatrixGuardedFetch", () => {
171+beforeEach(() => {
172+vi.unstubAllGlobals();
173+clearTestUndiciRuntimeDepsOverride();
174+});
175+176+afterEach(() => {
177+clearTestUndiciRuntimeDepsOverride();
178+});
179+180+it("strips matrix-js-sdk state_after sync opt-in from /sync requests", async () => {
181+const runtimeFetch = vi.fn(
182+async (_input: RequestInfo | URL, _init?: RequestInit) =>
183+new Response("{}", {
184+status: 200,
185+headers: {
186+"content-type": "application/json",
187+},
188+}),
189+);
190+stubRuntimeFetch(runtimeFetch);
191+192+const guardedFetch = createMatrixGuardedFetch({
193+ssrfPolicy: { allowPrivateNetwork: true },
194+});
195+196+await guardedFetch(
197+"http://127.0.0.1:8008/_matrix/client/v3/sync?filter=abc&org.matrix.msc4222.use_state_after=true&timeout=30000",
198+);
199+200+expect(runtimeFetch).toHaveBeenCalledTimes(1);
201+expect(runtimeFetch.mock.calls.at(0)?.[0]).toBe(
202+"http://127.0.0.1:8008/_matrix/client/v3/sync?filter=abc&timeout=30000",
203+);
204+});
205+206+it("leaves non-sync Matrix requests unchanged", async () => {
207+const runtimeFetch = vi.fn(
208+async (_input: RequestInfo | URL, _init?: RequestInit) =>
209+new Response("{}", {
210+status: 200,
211+headers: {
212+"content-type": "application/json",
213+},
214+}),
215+);
216+stubRuntimeFetch(runtimeFetch);
217+218+const guardedFetch = createMatrixGuardedFetch({
219+ssrfPolicy: { allowPrivateNetwork: true },
220+});
221+222+const url =
223+"http://127.0.0.1:8008/_matrix/client/v3/account/whoami?org.matrix.msc4222.use_state_after=true";
224+await guardedFetch(url);
225+226+expect(runtimeFetch).toHaveBeenCalledTimes(1);
227+expect(runtimeFetch.mock.calls.at(0)?.[0]).toBe(url);
228+});
229+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。