@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,378 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import {
3
- CONSENT_UPLOAD_HOST_ALLOWLIST,
4
- isPrivateOrReservedIP,
5
- uploadToConsentUrl,
6
- validateConsentUploadUrl,
7
- } from "./file-consent.js";
8
- import { buildUserAgent } from "./user-agent.js";
9
-
10
- // Helper: a resolveFn that returns a public IP by default
11
- const publicResolve = async () => ({ address: "13.107.136.10" });
12
- // Helper: a resolveFn that returns a private IP
13
- const privateResolve = (ip: string) => async () => ({ address: ip });
14
- // Helper: a resolveFn that returns multiple addresses
15
- const multiResolve = (ips: string[]) => async () => ips.map((address) => ({ address }));
16
- // Helper: a resolveFn that fails
17
- const failingResolve = async () => {
18
- throw new Error("DNS failure");
19
- };
20
-
21
- const firstFetchCall = (fetchFn: ReturnType<typeof vi.fn<typeof fetch>>) => {
22
- const [call] = fetchFn.mock.calls;
23
- if (!call) {
24
- throw new Error("expected fetch call");
25
- }
26
- return call;
27
- };
28
-
29
- // ─── isPrivateOrReservedIP ───────────────────────────────────────────────────
30
-
31
- describe("isPrivateOrReservedIP", () => {
32
- it.each([
33
- ["10.0.0.1", true],
34
- ["10.255.255.255", true],
35
- ["172.16.0.1", true],
36
- ["172.31.255.255", true],
37
- ["172.15.0.1", false],
38
- ["172.32.0.1", false],
39
- ["192.168.0.1", true],
40
- ["192.168.255.255", true],
41
- ["127.0.0.1", true],
42
- ["127.255.255.255", true],
43
- ["169.254.0.1", true],
44
- ["169.254.169.254", true],
45
- ["0.0.0.0", true],
46
- ["8.8.8.8", false],
47
- ["13.107.136.10", false],
48
- ["52.96.0.1", false],
49
- ] as const)("IPv4 %s → %s", (ip, expected) => {
50
- expect(isPrivateOrReservedIP(ip)).toBe(expected);
51
- });
52
-
53
- it.each([
54
- ["::1", true],
55
- ["::", true],
56
- ["fe80::1", true],
57
- ["fe80::", true],
58
- ["fc00::1", true],
59
- ["fd12:3456::1", true],
60
- ["2001:0db8::1", true],
61
- ["2620:1ec:c11::200", false],
62
- // IPv4-mapped IPv6 addresses
63
- ["::ffff:127.0.0.1", true],
64
- ["::ffff:10.0.0.1", true],
65
- ["::ffff:192.168.1.1", true],
66
- ["::ffff:169.254.169.254", true],
67
- ["::ffff:8.8.8.8", false],
68
- ["::ffff:13.107.136.10", false],
69
- ] as const)("IPv6 %s → %s", (ip, expected) => {
70
- expect(isPrivateOrReservedIP(ip)).toBe(expected);
71
- });
72
-
73
- it.each([
74
- ["999.999.999.999", true],
75
- ["256.0.0.1", true],
76
- ["10.0.0.256", true],
77
- ["-1.0.0.1", false],
78
- ["1.2.3.4.5", false],
79
- ] as const)("malformed IPv4 %s → %s", (ip, expected) => {
80
- expect(isPrivateOrReservedIP(ip)).toBe(expected);
81
- });
82
- });
83
-
84
- // ─── validateConsentUploadUrl ────────────────────────────────────────────────
85
-
86
- describe("validateConsentUploadUrl", () => {
87
- it("accepts a valid SharePoint HTTPS URL", async () => {
88
- await expect(
89
- validateConsentUploadUrl("https://contoso.sharepoint.com/sites/uploads/file.pdf", {
90
- resolveFn: publicResolve,
91
- }),
92
- ).resolves.toBeUndefined();
93
- });
94
-
95
- it("accepts subdomains of allowlisted domains", async () => {
96
- await expect(
97
- validateConsentUploadUrl(
98
- "https://contoso-my.sharepoint.com/personal/user/Documents/file.docx",
99
- { resolveFn: publicResolve },
100
- ),
101
- ).resolves.toBeUndefined();
102
- });
103
-
104
- it("accepts graph.microsoft.com", async () => {
105
- await expect(
106
- validateConsentUploadUrl("https://graph.microsoft.com/v1.0/me/drive/items/123/content", {
107
- resolveFn: publicResolve,
108
- }),
109
- ).resolves.toBeUndefined();
110
- });
111
-
112
- it("rejects non-HTTPS URLs", async () => {
113
- await expect(
114
- validateConsentUploadUrl("http://contoso.sharepoint.com/file.pdf", {
115
- resolveFn: publicResolve,
116
- }),
117
- ).rejects.toThrow("must use HTTPS");
118
- });
119
-
120
- it("rejects invalid URLs", async () => {
121
- await expect(
122
- validateConsentUploadUrl("not a url", { resolveFn: publicResolve }),
123
- ).rejects.toThrow("not a valid URL");
124
- });
125
-
126
- it("rejects hosts not in the allowlist", async () => {
127
- await expect(
128
- validateConsentUploadUrl("https://evil.example.com/exfil", { resolveFn: publicResolve }),
129
- ).rejects.toThrow("not in the allowed domains");
130
- });
131
-
132
- it("rejects an SSRF attempt with internal metadata URL", async () => {
133
- await expect(
134
- validateConsentUploadUrl("https://169.254.169.254/latest/meta-data/", {
135
- resolveFn: publicResolve,
136
- }),
137
- ).rejects.toThrow("not in the allowed domains");
138
- });
139
-
140
- it("rejects localhost", async () => {
141
- await expect(
142
- validateConsentUploadUrl("https://localhost:8080/internal", { resolveFn: publicResolve }),
143
- ).rejects.toThrow("not in the allowed domains");
144
- });
145
-
146
- it("rejects when DNS resolves to a private IPv4 (10.x)", async () => {
147
- await expect(
148
- validateConsentUploadUrl("https://malicious.sharepoint.com/exfil", {
149
- resolveFn: privateResolve("10.0.0.1"),
150
- }),
151
- ).rejects.toThrow("private/reserved IP");
152
- });
153
-
154
- it("rejects when DNS resolves to loopback", async () => {
155
- await expect(
156
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
157
- resolveFn: privateResolve("127.0.0.1"),
158
- }),
159
- ).rejects.toThrow("private/reserved IP");
160
- });
161
-
162
- it("rejects when DNS resolves to link-local (169.254.x.x)", async () => {
163
- await expect(
164
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
165
- resolveFn: privateResolve("169.254.169.254"),
166
- }),
167
- ).rejects.toThrow("private/reserved IP");
168
- });
169
-
170
- it("rejects when DNS resolves to IPv6 loopback", async () => {
171
- await expect(
172
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
173
- resolveFn: privateResolve("::1"),
174
- }),
175
- ).rejects.toThrow("private/reserved IP");
176
- });
177
-
178
- it("rejects when DNS resolves to IPv4-mapped IPv6 private address", async () => {
179
- await expect(
180
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
181
- resolveFn: privateResolve("::ffff:10.0.0.1"),
182
- }),
183
- ).rejects.toThrow("private/reserved IP");
184
- });
185
-
186
- it("rejects when DNS resolves to IPv4-mapped IPv6 loopback", async () => {
187
- await expect(
188
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
189
- resolveFn: privateResolve("::ffff:127.0.0.1"),
190
- }),
191
- ).rejects.toThrow("private/reserved IP");
192
- });
193
-
194
- it("rejects when any DNS answer is private/reserved", async () => {
195
- await expect(
196
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
197
- resolveFn: multiResolve(["13.107.136.10", "10.0.0.1"]),
198
- }),
199
- ).rejects.toThrow("private/reserved IP");
200
- });
201
-
202
- it("accepts when all DNS answers are public", async () => {
203
- await expect(
204
- validateConsentUploadUrl("https://evil.sharepoint.com/path", {
205
- resolveFn: multiResolve(["13.107.136.10", "52.96.0.1"]),
206
- }),
207
- ).resolves.toBeUndefined();
208
- });
209
-
210
- it("rejects when DNS resolution fails", async () => {
211
- await expect(
212
- validateConsentUploadUrl("https://nonexistent.sharepoint.com/path", {
213
- resolveFn: failingResolve,
214
- }),
215
- ).rejects.toThrow("Failed to resolve");
216
- });
217
-
218
- it("accepts a custom allowlist", async () => {
219
- await expect(
220
- validateConsentUploadUrl("https://custom.example.org/file", {
221
- allowlist: ["example.org"],
222
- resolveFn: publicResolve,
223
- }),
224
- ).resolves.toBeUndefined();
225
- });
226
-
227
- it("rejects hosts that are suffix-tricked (e.g. notsharepoint.com)", async () => {
228
- await expect(
229
- validateConsentUploadUrl("https://notsharepoint.com/file", { resolveFn: publicResolve }),
230
- ).rejects.toThrow("not in the allowed domains");
231
- });
232
-
233
- it("rejects file:// protocol", async () => {
234
- await expect(
235
- validateConsentUploadUrl("file:///etc/passwd", { resolveFn: publicResolve }),
236
- ).rejects.toThrow("must use HTTPS");
237
- });
238
- });
239
-
240
- // ─── CONSENT_UPLOAD_HOST_ALLOWLIST ───────────────────────────────────────────
241
-
242
- describe("CONSENT_UPLOAD_HOST_ALLOWLIST", () => {
243
- it("contains only Microsoft/SharePoint domains", () => {
244
- for (const domain of CONSENT_UPLOAD_HOST_ALLOWLIST) {
245
- expect(
246
- domain.includes("microsoft") ||
247
- domain.includes("sharepoint") ||
248
- domain.includes("onedrive") ||
249
- domain.includes("1drv") ||
250
- domain.includes("live.com"),
251
- ).toBe(true);
252
- }
253
- });
254
-
255
- it("does not contain overly broad domains", () => {
256
- const broad = [
257
- "microsoft.com",
258
- "azure.com",
259
- "blob.core.windows.net",
260
- "azureedge.net",
261
- "trafficmanager.net",
262
- ];
263
- for (const domain of broad) {
264
- expect(CONSENT_UPLOAD_HOST_ALLOWLIST).not.toContain(domain);
265
- }
266
- });
267
- });
268
-
269
- // ─── uploadToConsentUrl (integration with validation) ────────────────────────
270
-
271
- describe("uploadToConsentUrl", () => {
272
- it("sends the Klaw User-Agent header with consent uploads", async () => {
273
- const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 200 }));
274
-
275
- await uploadToConsentUrl({
276
- url: "https://contoso.sharepoint.com/upload",
277
- buffer: Buffer.from("hello"),
278
- fetchFn,
279
- validationOpts: { resolveFn: publicResolve },
280
- });
281
-
282
- expect(fetchFn).toHaveBeenCalledOnce();
283
- const [url, opts] = firstFetchCall(fetchFn);
284
- expect(url).toBe("https://contoso.sharepoint.com/upload");
285
- expect(opts?.method).toBe("PUT");
286
- expect(opts?.headers).toEqual({
287
- "Content-Range": "bytes 0-4/5",
288
- "Content-Type": "application/octet-stream",
289
- "User-Agent": buildUserAgent(),
290
- });
291
- expect(opts?.body).toEqual(new Uint8Array(Buffer.from("hello")));
292
- });
293
-
294
- it("blocks upload to a disallowed host", async () => {
295
- const mockFetch = vi.fn();
296
- await expect(
297
- uploadToConsentUrl({
298
- url: "https://evil.example.com/exfil",
299
- buffer: Buffer.from("secret data"),
300
- fetchFn: mockFetch,
301
- validationOpts: { resolveFn: publicResolve },
302
- }),
303
- ).rejects.toThrow("not in the allowed domains");
304
-
305
- expect(mockFetch).not.toHaveBeenCalled();
306
- });
307
-
308
- it("blocks upload to a private IP", async () => {
309
- const mockFetch = vi.fn();
310
- await expect(
311
- uploadToConsentUrl({
312
- url: "https://compromised.sharepoint.com/upload",
313
- buffer: Buffer.from("data"),
314
- fetchFn: mockFetch,
315
- validationOpts: { resolveFn: privateResolve("10.0.0.1") },
316
- }),
317
- ).rejects.toThrow("private/reserved IP");
318
-
319
- expect(mockFetch).not.toHaveBeenCalled();
320
- });
321
-
322
- it("allows upload to a valid SharePoint URL and performs PUT", async () => {
323
- const mockFetch = vi.fn<typeof fetch>(async () => new Response(null, { status: 200 }));
324
- const buffer = Buffer.from("file content");
325
-
326
- await uploadToConsentUrl({
327
- url: "https://contoso.sharepoint.com/sites/uploads/file.pdf",
328
- buffer,
329
- contentType: "application/pdf",
330
- fetchFn: mockFetch,
331
- validationOpts: { resolveFn: publicResolve },
332
- });
333
-
334
- expect(mockFetch).toHaveBeenCalledOnce();
335
- const [url, opts] = firstFetchCall(mockFetch);
336
- expect(url).toBe("https://contoso.sharepoint.com/sites/uploads/file.pdf");
337
- expect(opts).toEqual({
338
- method: "PUT",
339
- headers: {
340
- "User-Agent": buildUserAgent(),
341
- "Content-Type": "application/pdf",
342
- "Content-Range": "bytes 0-11/12",
343
- },
344
- body: new Uint8Array(buffer),
345
- });
346
- });
347
-
348
- it("throws on non-OK response after passing validation", async () => {
349
- const mockFetch = vi.fn().mockResolvedValue({
350
- ok: false,
351
- status: 403,
352
- statusText: "Forbidden",
353
- });
354
-
355
- await expect(
356
- uploadToConsentUrl({
357
- url: "https://contoso.sharepoint.com/sites/uploads/file.pdf",
358
- buffer: Buffer.from("data"),
359
- fetchFn: mockFetch,
360
- validationOpts: { resolveFn: publicResolve },
361
- }),
362
- ).rejects.toThrow("File upload to consent URL failed: 403 Forbidden");
363
- });
364
-
365
- it("blocks HTTP (non-HTTPS) upload before fetch is called", async () => {
366
- const mockFetch = vi.fn();
367
- await expect(
368
- uploadToConsentUrl({
369
- url: "http://contoso.sharepoint.com/upload",
370
- buffer: Buffer.from("data"),
371
- fetchFn: mockFetch,
372
- validationOpts: { resolveFn: publicResolve },
373
- }),
374
- ).rejects.toThrow("must use HTTPS");
375
-
376
- expect(mockFetch).not.toHaveBeenCalled();
377
- });
378
- });
@@ -1,223 +0,0 @@
1
- /**
2
- * FileConsentCard utilities for MS Teams large file uploads (>4MB) in personal chats.
3
- *
4
- * Teams requires user consent before the bot can upload large files. This module provides
5
- * utilities for:
6
- * - Building FileConsentCard attachments (to request upload permission)
7
- * - Building FileInfoCard attachments (to confirm upload completion)
8
- * - Parsing fileConsent/invoke activities
9
- */
10
-
11
- import { lookup } from "node:dns/promises";
12
- import { isPrivateIpAddress } from "klaw/plugin-sdk/ssrf-policy";
13
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
14
- import { buildUserAgent } from "./user-agent.js";
15
-
16
- /**
17
- * Allowlist of domains that are valid targets for file consent uploads.
18
- * These are the Microsoft/SharePoint domains that Teams legitimately provides
19
- * as upload destinations in the FileConsentCard flow.
20
- */
21
- export const CONSENT_UPLOAD_HOST_ALLOWLIST = [
22
- "sharepoint.com",
23
- "sharepoint.us",
24
- "sharepoint.de",
25
- "sharepoint.cn",
26
- "sharepoint-df.com",
27
- "storage.live.com",
28
- "onedrive.com",
29
- "1drv.ms",
30
- "graph.microsoft.com",
31
- "graph.microsoft.us",
32
- "graph.microsoft.de",
33
- "graph.microsoft.cn",
34
- ] as const;
35
-
36
- /**
37
- * Returns true if the given IPv4 or IPv6 address is private, internal, or
38
- * special-use and must never be reached via consent uploads.
39
- */
40
- export const isPrivateOrReservedIP: (ip: string) => boolean = isPrivateIpAddress;
41
-
42
- /**
43
- * Validate that a consent upload URL is safe to PUT to.
44
- * Checks:
45
- * 1. Protocol is HTTPS
46
- * 2. Hostname matches the consent upload allowlist
47
- * 3. Resolved IP is not in a private/reserved range (anti-SSRF)
48
- *
49
- * @throws Error if the URL fails validation
50
- */
51
- export async function validateConsentUploadUrl(
52
- url: string,
53
- opts?: {
54
- allowlist?: readonly string[];
55
- resolveFn?: (hostname: string) => Promise<{ address: string } | { address: string }[]>;
56
- },
57
- ): Promise<void> {
58
- let parsed: URL;
59
- try {
60
- parsed = new URL(url);
61
- } catch {
62
- throw new Error("Consent upload URL is not a valid URL");
63
- }
64
-
65
- // 1. Protocol check
66
- if (parsed.protocol !== "https:") {
67
- throw new Error(`Consent upload URL must use HTTPS, got ${parsed.protocol}`);
68
- }
69
-
70
- // 2. Hostname allowlist check
71
- const hostname = normalizeLowercaseStringOrEmpty(parsed.hostname);
72
- const allowlist = opts?.allowlist ?? CONSENT_UPLOAD_HOST_ALLOWLIST;
73
- const hostAllowed = allowlist.some(
74
- (entry) => hostname === entry || hostname.endsWith(`.${entry}`),
75
- );
76
- if (!hostAllowed) {
77
- throw new Error(`Consent upload URL hostname "${hostname}" is not in the allowed domains`);
78
- }
79
-
80
- // 3. DNS resolution — reject private/reserved IPs.
81
- // Check all resolved addresses to avoid SSRF bypass via mixed public/private answers.
82
- const resolveFn = opts?.resolveFn ?? ((name: string) => lookup(name, { all: true }));
83
- let resolved: { address: string }[];
84
- try {
85
- const result = await resolveFn(hostname);
86
- resolved = Array.isArray(result) ? result : [result];
87
- } catch {
88
- throw new Error(`Failed to resolve consent upload URL hostname "${hostname}"`);
89
- }
90
-
91
- for (const entry of resolved) {
92
- if (isPrivateOrReservedIP(entry.address)) {
93
- throw new Error(`Consent upload URL resolves to a private/reserved IP (${entry.address})`);
94
- }
95
- }
96
- }
97
-
98
- interface FileConsentCardParams {
99
- filename: string;
100
- description?: string;
101
- sizeInBytes: number;
102
- /** Custom context data to include in the card (passed back in the invoke) */
103
- context?: Record<string, unknown>;
104
- }
105
-
106
- interface FileInfoCardParams {
107
- filename: string;
108
- contentUrl: string;
109
- uniqueId: string;
110
- fileType: string;
111
- }
112
-
113
- /**
114
- * Build a FileConsentCard attachment for requesting upload permission.
115
- * Use this for files >= 4MB in personal (1:1) chats.
116
- */
117
- export function buildFileConsentCard(params: FileConsentCardParams) {
118
- return {
119
- contentType: "application/vnd.microsoft.teams.card.file.consent",
120
- name: params.filename,
121
- content: {
122
- description: params.description ?? `File: ${params.filename}`,
123
- sizeInBytes: params.sizeInBytes,
124
- acceptContext: { filename: params.filename, ...params.context },
125
- declineContext: { filename: params.filename, ...params.context },
126
- },
127
- };
128
- }
129
-
130
- /**
131
- * Build a FileInfoCard attachment for confirming upload completion.
132
- * Send this after successfully uploading the file to the consent URL.
133
- */
134
- export function buildFileInfoCard(params: FileInfoCardParams) {
135
- return {
136
- contentType: "application/vnd.microsoft.teams.card.file.info",
137
- contentUrl: params.contentUrl,
138
- name: params.filename,
139
- content: {
140
- uniqueId: params.uniqueId,
141
- fileType: params.fileType,
142
- },
143
- };
144
- }
145
-
146
- interface FileConsentUploadInfo {
147
- name: string;
148
- uploadUrl: string;
149
- contentUrl: string;
150
- uniqueId: string;
151
- fileType: string;
152
- }
153
-
154
- interface FileConsentResponse {
155
- action: "accept" | "decline";
156
- uploadInfo?: FileConsentUploadInfo;
157
- context?: Record<string, unknown>;
158
- }
159
-
160
- /**
161
- * Parse a fileConsent/invoke activity.
162
- * Returns null if the activity is not a file consent invoke.
163
- */
164
- export function parseFileConsentInvoke(activity: {
165
- name?: string;
166
- value?: unknown;
167
- }): FileConsentResponse | null {
168
- if (activity.name !== "fileConsent/invoke") {
169
- return null;
170
- }
171
-
172
- const value = activity.value as {
173
- type?: string;
174
- action?: string;
175
- uploadInfo?: FileConsentUploadInfo;
176
- context?: Record<string, unknown>;
177
- };
178
-
179
- if (value?.type !== "fileUpload") {
180
- return null;
181
- }
182
-
183
- return {
184
- action: value.action === "accept" ? "accept" : "decline",
185
- uploadInfo: value.uploadInfo,
186
- context: value.context,
187
- };
188
- }
189
-
190
- /**
191
- * Upload a file to the consent URL provided by Teams.
192
- * The URL is provided in the fileConsent/invoke response after user accepts.
193
- *
194
- * @throws Error if the URL fails SSRF validation (non-HTTPS, disallowed host, private IP)
195
- */
196
- export async function uploadToConsentUrl(params: {
197
- url: string;
198
- buffer: Buffer;
199
- contentType?: string;
200
- fetchFn?: typeof fetch;
201
- /** Override for testing — custom allowlist and DNS resolver */
202
- validationOpts?: {
203
- allowlist?: readonly string[];
204
- resolveFn?: (hostname: string) => Promise<{ address: string } | { address: string }[]>;
205
- };
206
- }): Promise<void> {
207
- await validateConsentUploadUrl(params.url, params.validationOpts);
208
-
209
- const fetchFn = params.fetchFn ?? fetch;
210
- const res = await fetchFn(params.url, {
211
- method: "PUT",
212
- headers: {
213
- "User-Agent": buildUserAgent(),
214
- "Content-Type": params.contentType ?? "application/octet-stream",
215
- "Content-Range": `bytes 0-${params.buffer.length - 1}/${params.buffer.length}`,
216
- },
217
- body: new Uint8Array(params.buffer),
218
- });
219
-
220
- if (!res.ok) {
221
- throw new Error(`File upload to consent URL failed: ${res.status} ${res.statusText}`);
222
- }
223
- }
package/src/graph-chat.ts DELETED
@@ -1,36 +0,0 @@
1
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
2
- import type { DriveItemProperties } from "./graph-upload.js";
3
-
4
- export function buildTeamsFileInfoCard(file: DriveItemProperties): {
5
- contentType: string;
6
- contentUrl: string;
7
- name: string;
8
- content: {
9
- uniqueId: string;
10
- fileType: string;
11
- };
12
- } {
13
- // Extract unique ID from eTag (remove quotes, braces, and version suffix)
14
- // Example eTag formats: "{GUID},version" or "\"{GUID},version\""
15
- const rawETag = file.eTag;
16
- const uniqueId =
17
- rawETag
18
- .replace(/^["']|["']$/g, "") // Remove outer quotes
19
- .replace(/[{}]/g, "") // Remove curly braces
20
- .split(",")[0] ?? rawETag; // Take the GUID part before comma
21
-
22
- // Extract file extension from filename
23
- const lastDot = file.name.lastIndexOf(".");
24
- const fileType =
25
- lastDot >= 0 ? normalizeLowercaseStringOrEmpty(file.name.slice(lastDot + 1)) : "";
26
-
27
- return {
28
- contentType: "application/vnd.microsoft.teams.card.file.info",
29
- contentUrl: file.webDavUrl,
30
- name: file.name,
31
- content: {
32
- uniqueId,
33
- fileType,
34
- },
35
- };
36
- }