Skip to content

Commit 4c8c228

Browse files
authored
fix(msteams): preserve channel thread context with supported Graph queries (#128901)
1 parent 1edbbc9 commit 4c8c228

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

‎extensions/msteams/src/graph-thread.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("fetchChannelMessage", () => {
7474
expect(result).toEqual(mockMsg);
7575
expect(fetchGraphJson).toHaveBeenCalledWith({
7676
token: "tok",
77-
path: "/teams/group-1/channels/channel-1/messages/msg-1?$select=id,from,body,createdDateTime",
77+
path: "/teams/group-1/channels/channel-1/messages/msg-1",
7878
});
7979
});
8080

@@ -92,7 +92,7 @@ describe("fetchChannelMessage", () => {
9292

9393
expect(fetchGraphJson).toHaveBeenCalledWith({
9494
token: "tok",
95-
path: "/teams/g%2F1/channels/c%2F2/messages/m%2F3?$select=id,from,body,createdDateTime",
95+
path: "/teams/g%2F1/channels/c%2F2/messages/m%2F3",
9696
});
9797
});
9898
});
@@ -176,7 +176,7 @@ describe("fetchThreadReplies", () => {
176176
expect(result).toHaveLength(2);
177177
expect(fetchGraphJson).toHaveBeenCalledWith({
178178
token: "tok",
179-
path: "/teams/group-1/channels/channel-1/messages/msg-1/replies?$top=50&$select=id,from,body,createdDateTime",
179+
path: "/teams/group-1/channels/channel-1/messages/msg-1/replies?$top=50",
180180
});
181181
});
182182

‎extensions/msteams/src/graph-thread.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function fetchChannelMessage(
3939
messageId: string,
4040
deadline?: MSTeamsRequestDeadline,
4141
): Promise<GraphThreadMessage | undefined> {
42-
const path = `/teams/${encodeURIComponent(groupId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(messageId)}?$select=id,from,body,createdDateTime`;
42+
const path = `/teams/${encodeURIComponent(groupId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(messageId)}`;
4343
try {
4444
return await fetchGraphJson<GraphThreadMessage>({
4545
token,
@@ -104,10 +104,7 @@ export async function fetchThreadReplies(
104104
deadline?: MSTeamsRequestDeadline,
105105
): Promise<GraphThreadMessage[]> {
106106
const top = Math.min(Math.max(limit, 1), 50);
107-
// NOTE: Graph replies endpoint returns oldest-first and does not support $orderby.
108-
// For threads with >50 replies, only the oldest 50 are returned. The most recent
109-
// replies (often the most relevant context) may be truncated.
110-
const path = `/teams/${encodeURIComponent(groupId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(messageId)}/replies?$top=${top}&$select=id,from,body,createdDateTime`;
107+
const path = `/teams/${encodeURIComponent(groupId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(messageId)}/replies?$top=${top}`;
111108
const res = await fetchGraphJson<GraphResponse<GraphThreadMessage>>({
112109
token,
113110
path,

‎extensions/msteams/src/graph.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ vi.mock("../runtime-api.js", async (importOriginal) => {
4444
};
4545
});
4646

47+
import { fetchChannelMessage, fetchThreadReplies } from "./graph-thread.js";
4748
import { findGraphUsersByExactIdentity, searchGraphUsers } from "./graph-users.js";
4849
import {
4950
deleteGraphRequest,
@@ -216,6 +217,39 @@ describe("msteams graph helpers", () => {
216217
expect(escapeOData("alice.o'hara")).toBe("alice.o''hara");
217218
});
218219

220+
it("keeps channel parent and reply requests within their Graph query contracts", async () => {
221+
const parent = { id: "parent-1", body: { content: "Original question" } };
222+
const reply = { id: "reply-1", body: { content: "Earlier decision" } };
223+
mockFetch(async (input: string | URL | Request) => {
224+
const url = new URL(requestUrl(input));
225+
const isReplies = url.pathname.endsWith("/replies");
226+
const unsupported = [...url.searchParams.keys()].filter(
227+
(parameter) => !isReplies || parameter !== "$top",
228+
);
229+
return jsonResponse(
230+
unsupported.length > 0
231+
? { error: { code: "BadRequest", message: "Unsupported query parameter" } }
232+
: isReplies
233+
? graphCollection(reply)
234+
: parent,
235+
unsupported.length > 0 ? { status: 400 } : undefined,
236+
);
237+
});
238+
239+
const outcomes = await Promise.allSettled([
240+
fetchChannelMessage(graphToken, "group-1", "channel-1", "parent-1"),
241+
fetchThreadReplies(graphToken, "group-1", "channel-1", "parent-1"),
242+
]);
243+
244+
expect(outcomes).toEqual([
245+
{ status: "fulfilled", value: parent },
246+
{ status: "fulfilled", value: [reply] },
247+
]);
248+
expect(fetchCallSearchParam(0, "$select")).toBeNull();
249+
expect(fetchCallSearchParam(1, "$select")).toBeNull();
250+
expect(fetchCallSearchParam(1, "$top")).toBe("50");
251+
});
252+
219253
it("lets the shared SSRF guard select the Graph transport", async () => {
220254
mockGraphCollection();
221255

0 commit comments

Comments
 (0)