Skip to content

Commit cca9f19

Browse files
committed
fix(mattermost): bound null-body error response reads
1 parent 825d9a6 commit cca9f19

2 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎extensions/mattermost/src/mattermost/client.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createMattermostClient,
1616
createMattermostPost,
1717
normalizeMattermostBaseUrl,
18+
readMattermostError,
1819
updateMattermostPost,
1920
} from "./client.js";
2021

@@ -155,6 +156,38 @@ describe("normalizeMattermostBaseUrl", () => {
155156
});
156157
});
157158

159+
// ── readMattermostError ───────────────────────────────────────────────
160+
161+
describe("readMattermostError", () => {
162+
it("bounds null-body JSON errors without response.json/text", async () => {
163+
const response = new Response(null, {
164+
status: 401,
165+
headers: { "content-type": "application/json" },
166+
});
167+
const jsonSpy = vi.spyOn(response, "json").mockRejectedValue(new Error("unbounded"));
168+
const textSpy = vi.spyOn(response, "text").mockRejectedValue(new Error("unbounded"));
169+
170+
await expect(readMattermostError(response)).resolves.toBe("");
171+
172+
expect(jsonSpy).not.toHaveBeenCalled();
173+
expect(textSpy).not.toHaveBeenCalled();
174+
});
175+
176+
it("parses bounded JSON error messages from response bodies", async () => {
177+
const response = new Response(JSON.stringify({ message: "invalid token", id: "app.error" }), {
178+
status: 401,
179+
headers: { "content-type": "application/json" },
180+
});
181+
const jsonSpy = vi.spyOn(response, "json").mockRejectedValue(new Error("unbounded"));
182+
const textSpy = vi.spyOn(response, "text").mockRejectedValue(new Error("unbounded"));
183+
184+
await expect(readMattermostError(response)).resolves.toBe("invalid token");
185+
186+
expect(jsonSpy).not.toHaveBeenCalled();
187+
expect(textSpy).not.toHaveBeenCalled();
188+
});
189+
});
190+
158191
// ── createMattermostClient ───────────────────────────────────────────
159192

160193
describe("createMattermostClient", () => {
@@ -173,6 +206,29 @@ describe("createMattermostClient", () => {
173206
expect(release).toHaveBeenCalledTimes(1);
174207
});
175208

209+
it("reads guarded null-body Mattermost errors without response.json/text", async () => {
210+
const release = vi.fn(async () => {});
211+
const response = new Response(null, {
212+
status: 503,
213+
statusText: "Service Unavailable",
214+
headers: { "content-type": "application/json" },
215+
});
216+
const jsonSpy = vi.spyOn(response, "json").mockRejectedValue(new Error("unbounded"));
217+
const textSpy = vi.spyOn(response, "text").mockRejectedValue(new Error("unbounded"));
218+
fetchWithSsrFGuardMock.mockResolvedValueOnce({ response, release });
219+
const client = createMattermostClient({
220+
baseUrl: "https://chat.example.com",
221+
botToken: "test-token",
222+
});
223+
224+
await expect(client.request("/users/me")).rejects.toThrow(
225+
"Mattermost API 503 Service Unavailable: unknown error",
226+
);
227+
expect(jsonSpy).not.toHaveBeenCalled();
228+
expect(textSpy).not.toHaveBeenCalled();
229+
expect(release).toHaveBeenCalledTimes(1);
230+
});
231+
176232
it("bounds and cancels guarded Mattermost error bodies", async () => {
177233
const release = vi.fn(async () => {});
178234
const tracked = cancelTrackedResponse(`${"upstream unavailable ".repeat(512)}tail`, {

‎extensions/mattermost/src/mattermost/client.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,6 @@ async function readMattermostSuccessText(res: Response, path: string): Promise<s
105105

106106
export async function readMattermostError(res: Response): Promise<string> {
107107
const contentType = res.headers.get("content-type") ?? "";
108-
if (!res.body) {
109-
if (contentType.includes("application/json")) {
110-
const data = (await res.json()) as { message?: string } | undefined;
111-
if (data?.message) {
112-
return data.message;
113-
}
114-
return JSON.stringify(data);
115-
}
116-
return await res.text();
117-
}
118108
const text = await readResponseTextLimited(res, MATTERMOST_ERROR_BODY_LIMIT_BYTES);
119109
if (contentType.includes("application/json")) {
120110
try {

0 commit comments

Comments
 (0)