@kodelyth/msteams 2026.5.42 → 2026.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,547 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import {
3
- applyAuthorizationHeaderForUrl,
4
- encodeGraphShareId,
5
- extractInlineImageCandidates,
6
- isGraphSharedLinkUrl,
7
- isPrivateOrReservedIP,
8
- isUrlAllowed,
9
- resolveAndValidateIP,
10
- resolveAttachmentFetchPolicy,
11
- resolveAllowedHosts,
12
- resolveAuthAllowedHosts,
13
- resolveMediaSsrfPolicy,
14
- safeFetch,
15
- safeFetchWithPolicy,
16
- tryBuildGraphSharesUrlForSharedLink,
17
- } from "./shared.js";
18
-
19
- const publicResolve = async () => ({ address: "13.107.136.10" });
20
- const privateResolve = (ip: string) => async () => ({ address: ip });
21
- const failingResolve = async () => {
22
- throw new Error("DNS failure");
23
- };
24
-
25
- function mockFetchWithRedirect(redirectMap: Record<string, string>, finalBody = "ok") {
26
- return vi.fn(async (url: string, init?: RequestInit) => {
27
- const target = redirectMap[url];
28
- if (target && init?.redirect === "manual") {
29
- return new Response(null, {
30
- status: 302,
31
- headers: { location: target },
32
- });
33
- }
34
- return new Response(finalBody, { status: 200 });
35
- });
36
- }
37
-
38
- function fetchInitAt(fetchMock: ReturnType<typeof vi.fn>, index: number): unknown {
39
- const call = fetchMock.mock.calls[index];
40
- if (!call) {
41
- throw new Error(`expected fetch call ${index}`);
42
- }
43
- return call[1];
44
- }
45
-
46
- async function expectSafeFetchStatus(params: {
47
- fetchMock: ReturnType<typeof vi.fn>;
48
- url: string;
49
- allowHosts: string[];
50
- expectedStatus: number;
51
- resolveFn?: typeof publicResolve;
52
- }) {
53
- const res = await safeFetch({
54
- url: params.url,
55
- allowHosts: params.allowHosts,
56
- fetchFn: params.fetchMock as unknown as typeof fetch,
57
- resolveFn: params.resolveFn ?? publicResolve,
58
- });
59
- expect(res.status).toBe(params.expectedStatus);
60
- return res;
61
- }
62
-
63
- describe("msteams attachment allowlists", () => {
64
- it("normalizes wildcard host lists", () => {
65
- expect(resolveAllowedHosts(["*", "graph.microsoft.com"])).toEqual(["*"]);
66
- expect(resolveAuthAllowedHosts(["*", "graph.microsoft.com"])).toEqual(["*"]);
67
- });
68
-
69
- it("resolves a normalized attachment fetch policy", () => {
70
- expect(
71
- resolveAttachmentFetchPolicy({
72
- allowHosts: ["sharepoint.com"],
73
- authAllowHosts: ["graph.microsoft.com"],
74
- }),
75
- ).toEqual({
76
- allowHosts: ["sharepoint.com"],
77
- authAllowHosts: ["graph.microsoft.com"],
78
- });
79
- });
80
-
81
- it("requires https and host suffix match", () => {
82
- const allowHosts = resolveAllowedHosts(["sharepoint.com"]);
83
- expect(isUrlAllowed("https://contoso.sharepoint.com/file.png", allowHosts)).toBe(true);
84
- expect(isUrlAllowed("http://contoso.sharepoint.com/file.png", allowHosts)).toBe(false);
85
- expect(isUrlAllowed("https://evil.example.com/file.png", allowHosts)).toBe(false);
86
- });
87
-
88
- it("builds shared SSRF policy from suffix allowlist", () => {
89
- expect(resolveMediaSsrfPolicy(["sharepoint.com"])).toEqual({
90
- hostnameAllowlist: ["sharepoint.com", "*.sharepoint.com"],
91
- });
92
- expect(resolveMediaSsrfPolicy(["*"])).toBeUndefined();
93
- });
94
-
95
- it.each([
96
- ["999.999.999.999", true],
97
- ["256.0.0.1", true],
98
- ["10.0.0.256", true],
99
- ["-1.0.0.1", false],
100
- ["1.2.3.4.5", false],
101
- ["0:0:0:0:0:0:0:1", true],
102
- ] as const)("malformed/expanded %s → %s (SDK fails closed)", (ip, expected) => {
103
- expect(isPrivateOrReservedIP(ip)).toBe(expected);
104
- });
105
- });
106
-
107
- // ─── resolveAndValidateIP ────────────────────────────────────────────────────
108
-
109
- describe("resolveAndValidateIP", () => {
110
- it("accepts a hostname resolving to a public IP", async () => {
111
- const ip = await resolveAndValidateIP("teams.sharepoint.com", publicResolve);
112
- expect(ip).toBe("13.107.136.10");
113
- });
114
-
115
- it("rejects a hostname resolving to 10.x.x.x", async () => {
116
- await expect(resolveAndValidateIP("evil.test", privateResolve("10.0.0.1"))).rejects.toThrow(
117
- "private/reserved IP",
118
- );
119
- });
120
-
121
- it("rejects a hostname resolving to 169.254.169.254", async () => {
122
- await expect(
123
- resolveAndValidateIP("evil.test", privateResolve("169.254.169.254")),
124
- ).rejects.toThrow("private/reserved IP");
125
- });
126
-
127
- it("rejects a hostname resolving to loopback", async () => {
128
- await expect(resolveAndValidateIP("evil.test", privateResolve("127.0.0.1"))).rejects.toThrow(
129
- "private/reserved IP",
130
- );
131
- });
132
-
133
- it("rejects a hostname resolving to IPv6 loopback", async () => {
134
- await expect(resolveAndValidateIP("evil.test", privateResolve("::1"))).rejects.toThrow(
135
- "private/reserved IP",
136
- );
137
- });
138
-
139
- it("throws on DNS resolution failure", async () => {
140
- await expect(resolveAndValidateIP("nonexistent.test", failingResolve)).rejects.toThrow(
141
- "DNS resolution failed",
142
- );
143
- });
144
- });
145
-
146
- // ─── safeFetch ───────────────────────────────────────────────────────────────
147
-
148
- describe("safeFetch", () => {
149
- it("fetches a URL directly when no redirect occurs", async () => {
150
- const fetchMock = vi.fn(async (_url: string, _init?: RequestInit) => {
151
- return new Response("ok", { status: 200 });
152
- });
153
- await expectSafeFetchStatus({
154
- fetchMock,
155
- url: "https://teams.sharepoint.com/file.pdf",
156
- allowHosts: ["sharepoint.com"],
157
- expectedStatus: 200,
158
- });
159
- expect(fetchMock).toHaveBeenCalledOnce();
160
- // Should have used redirect: "manual"
161
- expect(fetchInitAt(fetchMock, 0)).toHaveProperty("redirect", "manual");
162
- });
163
-
164
- it("follows a redirect to an allowlisted host with public IP", async () => {
165
- const fetchMock = mockFetchWithRedirect({
166
- "https://teams.sharepoint.com/file.pdf": "https://cdn.sharepoint.com/storage/file.pdf",
167
- });
168
- await expectSafeFetchStatus({
169
- fetchMock,
170
- url: "https://teams.sharepoint.com/file.pdf",
171
- allowHosts: ["sharepoint.com"],
172
- expectedStatus: 200,
173
- });
174
- expect(fetchMock).toHaveBeenCalledTimes(2);
175
- });
176
-
177
- it("returns the redirect response when dispatcher is provided by an outer guard", async () => {
178
- const redirectedTo = "https://cdn.sharepoint.com/storage/file.pdf";
179
- const fetchMock = mockFetchWithRedirect({
180
- "https://teams.sharepoint.com/file.pdf": redirectedTo,
181
- });
182
- const res = await safeFetch({
183
- url: "https://teams.sharepoint.com/file.pdf",
184
- allowHosts: ["sharepoint.com"],
185
- fetchFn: fetchMock as unknown as typeof fetch,
186
- requestInit: { dispatcher: {} } as RequestInit,
187
- resolveFn: publicResolve,
188
- });
189
- expect(res.status).toBe(302);
190
- expect(res.headers.get("location")).toBe(redirectedTo);
191
- expect(fetchMock).toHaveBeenCalledOnce();
192
- });
193
-
194
- it("still enforces allowlist checks before returning dispatcher-mode redirects", async () => {
195
- const fetchMock = mockFetchWithRedirect({
196
- "https://teams.sharepoint.com/file.pdf": "https://evil.example.com/steal",
197
- });
198
- await expect(
199
- safeFetch({
200
- url: "https://teams.sharepoint.com/file.pdf",
201
- allowHosts: ["sharepoint.com"],
202
- fetchFn: fetchMock as unknown as typeof fetch,
203
- requestInit: { dispatcher: {} } as RequestInit,
204
- resolveFn: publicResolve,
205
- }),
206
- ).rejects.toThrow("blocked by allowlist");
207
- expect(fetchMock).toHaveBeenCalledOnce();
208
- });
209
-
210
- it("blocks a redirect to a non-allowlisted host", async () => {
211
- const fetchMock = mockFetchWithRedirect({
212
- "https://teams.sharepoint.com/file.pdf": "https://evil.example.com/steal",
213
- });
214
- await expect(
215
- safeFetch({
216
- url: "https://teams.sharepoint.com/file.pdf",
217
- allowHosts: ["sharepoint.com"],
218
- fetchFn: fetchMock as unknown as typeof fetch,
219
- resolveFn: publicResolve,
220
- }),
221
- ).rejects.toThrow("blocked by allowlist");
222
- // Should not have fetched the evil URL
223
- expect(fetchMock).toHaveBeenCalledTimes(1);
224
- });
225
-
226
- it("blocks a redirect to an allowlisted host that resolves to a private IP (DNS rebinding)", async () => {
227
- let callCount = 0;
228
- const rebindingResolve = async () => {
229
- callCount++;
230
- // First call (initial URL) resolves to public IP
231
- if (callCount === 1) {
232
- return { address: "13.107.136.10" };
233
- }
234
- // Second call (redirect target) resolves to private IP
235
- return { address: "169.254.169.254" };
236
- };
237
-
238
- const fetchMock = mockFetchWithRedirect({
239
- "https://teams.sharepoint.com/file.pdf": "https://evil.trafficmanager.net/metadata",
240
- });
241
- await expect(
242
- safeFetch({
243
- url: "https://teams.sharepoint.com/file.pdf",
244
- allowHosts: ["sharepoint.com", "trafficmanager.net"],
245
- fetchFn: fetchMock as unknown as typeof fetch,
246
- resolveFn: rebindingResolve,
247
- }),
248
- ).rejects.toThrow("private/reserved IP");
249
- expect(fetchMock).toHaveBeenCalledTimes(1);
250
- });
251
-
252
- it("blocks when the initial URL resolves to a private IP", async () => {
253
- const fetchMock = vi.fn();
254
- await expect(
255
- safeFetch({
256
- url: "https://evil.sharepoint.com/file.pdf",
257
- allowHosts: ["sharepoint.com"],
258
- fetchFn: fetchMock as unknown as typeof fetch,
259
- resolveFn: privateResolve("10.0.0.1"),
260
- }),
261
- ).rejects.toThrow("Initial download URL blocked");
262
- expect(fetchMock).not.toHaveBeenCalled();
263
- });
264
-
265
- it("blocks private hosts with the default resolver", async () => {
266
- const fetchMock = vi.fn();
267
- await expect(
268
- safeFetch({
269
- url: "https://localhost/file.pdf",
270
- allowHosts: ["localhost"],
271
- fetchFn: fetchMock as unknown as typeof fetch,
272
- }),
273
- ).rejects.toThrow("Initial download URL blocked");
274
- expect(fetchMock).not.toHaveBeenCalled();
275
- });
276
-
277
- it("blocks when initial URL DNS resolution fails", async () => {
278
- const fetchMock = vi.fn();
279
- await expect(
280
- safeFetch({
281
- url: "https://nonexistent.sharepoint.com/file.pdf",
282
- allowHosts: ["sharepoint.com"],
283
- fetchFn: fetchMock as unknown as typeof fetch,
284
- resolveFn: failingResolve,
285
- }),
286
- ).rejects.toThrow("Initial download URL blocked");
287
- expect(fetchMock).not.toHaveBeenCalled();
288
- });
289
-
290
- it("follows multiple redirects when all are valid", async () => {
291
- const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
292
- if (url === "https://a.sharepoint.com/1" && init?.redirect === "manual") {
293
- return new Response(null, {
294
- status: 302,
295
- headers: { location: "https://b.sharepoint.com/2" },
296
- });
297
- }
298
- if (url === "https://b.sharepoint.com/2" && init?.redirect === "manual") {
299
- return new Response(null, {
300
- status: 302,
301
- headers: { location: "https://c.sharepoint.com/3" },
302
- });
303
- }
304
- return new Response("final", { status: 200 });
305
- });
306
-
307
- const res = await safeFetch({
308
- url: "https://a.sharepoint.com/1",
309
- allowHosts: ["sharepoint.com"],
310
- fetchFn: fetchMock as unknown as typeof fetch,
311
- resolveFn: publicResolve,
312
- });
313
- expect(res.status).toBe(200);
314
- expect(fetchMock).toHaveBeenCalledTimes(3);
315
- });
316
-
317
- it("throws on too many redirects", async () => {
318
- let counter = 0;
319
- const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
320
- if (init?.redirect === "manual") {
321
- counter++;
322
- return new Response(null, {
323
- status: 302,
324
- headers: { location: `https://loop${counter}.sharepoint.com/x` },
325
- });
326
- }
327
- return new Response("ok", { status: 200 });
328
- });
329
-
330
- await expect(
331
- safeFetch({
332
- url: "https://start.sharepoint.com/x",
333
- allowHosts: ["sharepoint.com"],
334
- fetchFn: fetchMock as unknown as typeof fetch,
335
- resolveFn: publicResolve,
336
- }),
337
- ).rejects.toThrow("Too many redirects");
338
- });
339
-
340
- it("blocks redirect to HTTP (non-HTTPS)", async () => {
341
- const fetchMock = mockFetchWithRedirect({
342
- "https://teams.sharepoint.com/file": "http://internal.sharepoint.com/file",
343
- });
344
- await expect(
345
- safeFetch({
346
- url: "https://teams.sharepoint.com/file",
347
- allowHosts: ["sharepoint.com"],
348
- fetchFn: fetchMock as unknown as typeof fetch,
349
- resolveFn: publicResolve,
350
- }),
351
- ).rejects.toThrow("blocked by allowlist");
352
- });
353
-
354
- it("strips authorization across redirects outside auth allowlist", async () => {
355
- const seenAuth: string[] = [];
356
- const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
357
- const auth = new Headers(init?.headers).get("authorization") ?? "";
358
- seenAuth.push(`${url}|${auth}`);
359
- if (url === "https://teams.sharepoint.com/file.pdf") {
360
- return new Response(null, {
361
- status: 302,
362
- headers: { location: "https://cdn.sharepoint.com/storage/file.pdf" },
363
- });
364
- }
365
- return new Response("ok", { status: 200 });
366
- });
367
-
368
- const headers = new Headers({ Authorization: "Bearer secret" });
369
- const res = await safeFetch({
370
- url: "https://teams.sharepoint.com/file.pdf",
371
- allowHosts: ["sharepoint.com"],
372
- authorizationAllowHosts: ["graph.microsoft.com"],
373
- fetchFn: fetchMock as unknown as typeof fetch,
374
- requestInit: { headers },
375
- resolveFn: publicResolve,
376
- });
377
- expect(res.status).toBe(200);
378
- expect(seenAuth[0]).toContain("Bearer secret");
379
- expect(seenAuth[1]).toMatch(/\|$/);
380
- });
381
- });
382
-
383
- describe("attachment fetch auth helpers", () => {
384
- it("sets and clears authorization header by auth allowlist", () => {
385
- const headers = new Headers();
386
- applyAuthorizationHeaderForUrl({
387
- headers,
388
- url: "https://graph.microsoft.com/v1.0/me",
389
- authAllowHosts: ["graph.microsoft.com"],
390
- bearerToken: "token-1",
391
- });
392
- expect(headers.get("authorization")).toBe("Bearer token-1");
393
-
394
- applyAuthorizationHeaderForUrl({
395
- headers,
396
- url: "https://evil.example.com/collect",
397
- authAllowHosts: ["graph.microsoft.com"],
398
- bearerToken: "token-1",
399
- });
400
- expect(headers.get("authorization")).toBeNull();
401
- });
402
-
403
- it("safeFetchWithPolicy forwards policy allowlists", async () => {
404
- const fetchMock = vi.fn(async (_url: string, _init?: RequestInit) => {
405
- return new Response("ok", { status: 200 });
406
- });
407
- const res = await safeFetchWithPolicy({
408
- url: "https://teams.sharepoint.com/file.pdf",
409
- policy: resolveAttachmentFetchPolicy({
410
- allowHosts: ["sharepoint.com"],
411
- authAllowHosts: ["graph.microsoft.com"],
412
- }),
413
- fetchFn: fetchMock as unknown as typeof fetch,
414
- resolveFn: publicResolve,
415
- });
416
- expect(res.status).toBe(200);
417
- expect(fetchMock).toHaveBeenCalledOnce();
418
- });
419
- });
420
-
421
- describe("Graph shared-link helpers", () => {
422
- it.each([
423
- ["https://contoso.sharepoint.com/personal/user/Documents/report.pdf", true],
424
- ["https://contoso.sharepoint.us/sites/team/file.docx", true],
425
- ["https://contoso.sharepoint.cn/file", true],
426
- ["https://tenant-my.sharepoint.com/:b:/g/personal/file", true],
427
- ["https://1drv.ms/b/s!AkxYabc", true],
428
- ["https://onedrive.live.com/view.aspx?resid=ABC", true],
429
- ["https://onedrive.com/share/abc", true],
430
- ["https://graph.microsoft.com/v1.0/me", false],
431
- ["https://smba.trafficmanager.net/amer/v3", false],
432
- ["https://example.com/file.pdf", false],
433
- ["not-a-url", false],
434
- ])("isGraphSharedLinkUrl(%s) === %s", (url, expected) => {
435
- expect(isGraphSharedLinkUrl(url)).toBe(expected);
436
- });
437
-
438
- it("encodeGraphShareId uses u! + base64url without padding", () => {
439
- // Graph docs example: encoding "https://onedrive.live.com/redir?resid=..."
440
- // should yield u!aHR0cHM6... (base64url, no '+', '/', or trailing '=').
441
- const url = "https://contoso.sharepoint.com/sites/a/Shared Documents/file.pdf";
442
- const shareId = encodeGraphShareId(url);
443
- expect(shareId.startsWith("u!")).toBe(true);
444
- const encoded = shareId.slice(2);
445
- // base64url alphabet is A-Z, a-z, 0-9, '-', '_' (no padding).
446
- expect(encoded).toMatch(/^[A-Za-z0-9_-]+$/);
447
- // Round-trip check: decoding yields the original URL.
448
- const decoded = Buffer.from(encoded, "base64url").toString("utf8");
449
- expect(decoded).toBe(url);
450
- });
451
-
452
- it("encodeGraphShareId swaps '+' and '/' for '-' and '_'", () => {
453
- // A URL whose standard base64 contains '+' and '/' chars.
454
- // Choose an input that base64 encodes with those characters.
455
- const url = "https://host.sharepoint.com/sites/path?x=???";
456
- const shareId = encodeGraphShareId(url);
457
- const encoded = shareId.slice(2);
458
- expect(encoded).not.toContain("+");
459
- expect(encoded).not.toContain("/");
460
- expect(encoded).not.toContain("=");
461
- });
462
-
463
- it("tryBuildGraphSharesUrlForSharedLink rewrites SharePoint URLs", () => {
464
- const url = "https://contoso.sharepoint.com/personal/user/Documents/report.pdf";
465
- const result = tryBuildGraphSharesUrlForSharedLink(url);
466
- expect(result).toBe(
467
- `https://graph.microsoft.com/v1.0/shares/${encodeGraphShareId(url)}/driveItem/content`,
468
- );
469
- });
470
-
471
- it("tryBuildGraphSharesUrlForSharedLink rewrites OneDrive URLs", () => {
472
- const url = "https://1drv.ms/b/s!AkxYabcdefg";
473
- const result = tryBuildGraphSharesUrlForSharedLink(url);
474
- expect(result).toBe(
475
- `https://graph.microsoft.com/v1.0/shares/${encodeGraphShareId(url)}/driveItem/content`,
476
- );
477
- });
478
-
479
- it("tryBuildGraphSharesUrlForSharedLink returns undefined for non-shared URLs", () => {
480
- expect(
481
- tryBuildGraphSharesUrlForSharedLink("https://graph.microsoft.com/v1.0/me"),
482
- ).toBeUndefined();
483
- expect(tryBuildGraphSharesUrlForSharedLink("https://example.com/file.pdf")).toBeUndefined();
484
- expect(tryBuildGraphSharesUrlForSharedLink("not-a-url")).toBeUndefined();
485
- });
486
- });
487
-
488
- describe("msteams inline image limits", () => {
489
- const smallPngDataUrl = "data:image/png;base64,aGVsbG8="; // "hello" (5 bytes)
490
-
491
- it("rejects inline data images above per-image limit", () => {
492
- const attachments = [
493
- {
494
- contentType: "text/html",
495
- content: `<img src="${smallPngDataUrl}" />`,
496
- },
497
- ];
498
- const out = extractInlineImageCandidates(attachments, { maxInlineBytes: 4 });
499
- expect(out).toStrictEqual([]);
500
- });
501
-
502
- it("accepts inline data images within limit", () => {
503
- const attachments = [
504
- {
505
- contentType: "text/html",
506
- content: `<img src="${smallPngDataUrl}" />`,
507
- },
508
- ];
509
- const out = extractInlineImageCandidates(attachments, { maxInlineBytes: 10 });
510
- expect(out.length).toBe(1);
511
- expect(out[0]?.kind).toBe("data");
512
- if (out[0]?.kind === "data") {
513
- expect(out[0].data.byteLength).toBeGreaterThan(0);
514
- expect(out[0].contentType).toBe("image/png");
515
- }
516
- });
517
-
518
- it("rejects inline data images with malformed base64 padding", () => {
519
- const attachments = [
520
- {
521
- contentType: "text/html",
522
- content: `<img src="data:image/png;base64,aGV=sbG8=" />`,
523
- },
524
- ];
525
- const out = extractInlineImageCandidates(attachments, { maxInlineBytes: 10 });
526
- expect(out).toStrictEqual([]);
527
- });
528
-
529
- it("enforces cumulative inline size limit across attachments", () => {
530
- const attachments = [
531
- {
532
- contentType: "text/html",
533
- content: `<img src="${smallPngDataUrl}" />`,
534
- },
535
- {
536
- contentType: "text/html",
537
- content: `<img src="${smallPngDataUrl}" />`,
538
- },
539
- ];
540
- const out = extractInlineImageCandidates(attachments, {
541
- maxInlineBytes: 10,
542
- maxInlineTotalBytes: 6,
543
- });
544
- expect(out.length).toBe(1);
545
- expect(out[0]?.kind).toBe("data");
546
- });
547
- });