@openclaw/msteams 2026.3.13 → 2026.5.1-beta.1
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.
- package/api.ts +3 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +2 -0
- package/config-api.ts +4 -0
- package/contract-api.ts +4 -0
- package/index.ts +15 -12
- package/openclaw.plugin.json +553 -1
- package/package.json +46 -12
- package/runtime-api.ts +73 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/setup-plugin-api.ts +3 -0
- package/src/ai-entity.ts +7 -0
- package/src/approval-auth.ts +44 -0
- package/src/attachments/bot-framework.test.ts +461 -0
- package/src/attachments/bot-framework.ts +362 -0
- package/src/attachments/download.ts +63 -19
- package/src/attachments/graph.test.ts +416 -0
- package/src/attachments/graph.ts +163 -72
- package/src/attachments/html.ts +33 -1
- package/src/attachments/payload.ts +1 -1
- package/src/attachments/remote-media.test.ts +137 -0
- package/src/attachments/remote-media.ts +75 -8
- package/src/attachments/shared.test.ts +138 -1
- package/src/attachments/shared.ts +193 -26
- package/src/attachments/types.ts +10 -0
- package/src/attachments.graph.test.ts +342 -0
- package/src/attachments.helpers.test.ts +246 -0
- package/src/attachments.test-helpers.ts +17 -0
- package/src/attachments.test.ts +163 -418
- package/src/attachments.ts +5 -5
- package/src/block-streaming-config.test.ts +61 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.actions.test.ts +742 -0
- package/src/channel.directory.test.ts +145 -4
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -0
- package/src/channel.test.ts +128 -0
- package/src/channel.ts +1077 -395
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +12 -0
- package/src/conversation-store-fs.test.ts +4 -5
- package/src/conversation-store-fs.ts +35 -51
- package/src/conversation-store-helpers.test.ts +202 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +27 -23
- package/src/conversation-store.shared.test.ts +225 -0
- package/src/conversation-store.ts +30 -0
- package/src/directory-live.test.ts +156 -0
- package/src/directory-live.ts +7 -4
- package/src/doctor.ts +27 -0
- package/src/errors.test.ts +64 -1
- package/src/errors.ts +50 -9
- package/src/feedback-reflection-prompt.ts +117 -0
- package/src/feedback-reflection-store.ts +114 -0
- package/src/feedback-reflection.test.ts +237 -0
- package/src/feedback-reflection.ts +283 -0
- package/src/file-consent-helpers.test.ts +83 -0
- package/src/file-consent-helpers.ts +64 -11
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.test.ts +363 -0
- package/src/file-consent.ts +165 -4
- package/src/graph-chat.ts +5 -3
- package/src/graph-group-management.test.ts +318 -0
- package/src/graph-group-management.ts +168 -0
- package/src/graph-members.test.ts +89 -0
- package/src/graph-members.ts +48 -0
- package/src/graph-messages.actions.test.ts +243 -0
- package/src/graph-messages.read.test.ts +391 -0
- package/src/graph-messages.search.test.ts +213 -0
- package/src/graph-messages.test-helpers.ts +50 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.test.ts +215 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.test.ts +246 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.test.ts +161 -4
- package/src/graph-upload.ts +147 -56
- package/src/graph.test.ts +516 -0
- package/src/graph.ts +233 -21
- package/src/inbound.test.ts +156 -1
- package/src/inbound.ts +101 -1
- package/src/media-helpers.ts +1 -1
- package/src/mentions.test.ts +27 -18
- package/src/mentions.ts +2 -2
- package/src/messenger.test.ts +504 -23
- package/src/messenger.ts +133 -52
- package/src/monitor-handler/access.ts +125 -0
- package/src/monitor-handler/inbound-media.test.ts +289 -0
- package/src/monitor-handler/inbound-media.ts +57 -5
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
- package/src/monitor-handler/message-handler.authz.test.ts +588 -74
- package/src/monitor-handler/message-handler.dm-media.test.ts +54 -0
- package/src/monitor-handler/message-handler.test-support.ts +100 -0
- package/src/monitor-handler/message-handler.thread-parent.test.ts +223 -0
- package/src/monitor-handler/message-handler.thread-session.test.ts +77 -0
- package/src/monitor-handler/message-handler.ts +470 -164
- package/src/monitor-handler/reaction-handler.test.ts +267 -0
- package/src/monitor-handler/reaction-handler.ts +210 -0
- package/src/monitor-handler/thread-session.ts +17 -0
- package/src/monitor-handler.adaptive-card.test.ts +162 -0
- package/src/monitor-handler.feedback-authz.test.ts +314 -0
- package/src/monitor-handler.file-consent.test.ts +281 -79
- package/src/monitor-handler.sso.test.ts +563 -0
- package/src/monitor-handler.test-helpers.ts +180 -0
- package/src/monitor-handler.ts +459 -115
- package/src/monitor-handler.types.ts +27 -0
- package/src/monitor-types.ts +1 -0
- package/src/monitor.lifecycle.test.ts +74 -10
- package/src/monitor.test.ts +35 -1
- package/src/monitor.ts +143 -46
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.test.ts +305 -0
- package/src/oauth.token.ts +158 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.test.ts +10 -11
- package/src/outbound.ts +62 -44
- package/src/pending-uploads-fs.test.ts +246 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.test.ts +173 -0
- package/src/pending-uploads.ts +34 -2
- package/src/policy.test.ts +11 -5
- package/src/policy.ts +5 -5
- package/src/polls.test.ts +106 -5
- package/src/polls.ts +15 -7
- package/src/presentation.ts +68 -0
- package/src/probe.test.ts +27 -8
- package/src/probe.ts +43 -9
- package/src/reply-dispatcher.test.ts +437 -0
- package/src/reply-dispatcher.ts +259 -73
- package/src/reply-stream-controller.test.ts +235 -0
- package/src/reply-stream-controller.ts +147 -0
- package/src/resolve-allowlist.test.ts +105 -1
- package/src/resolve-allowlist.ts +112 -7
- package/src/runtime.ts +6 -3
- package/src/sdk-types.ts +43 -3
- package/src/sdk.test.ts +666 -0
- package/src/sdk.ts +867 -16
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +1 -1
- package/src/send-context.ts +76 -9
- package/src/send.test.ts +389 -5
- package/src/send.ts +140 -32
- package/src/sent-message-cache.ts +30 -18
- package/src/session-route.ts +40 -0
- package/src/setup-core.ts +160 -0
- package/src/setup-surface.test.ts +202 -0
- package/src/setup-surface.ts +320 -0
- package/src/sso-token-store.test.ts +72 -0
- package/src/sso-token-store.ts +166 -0
- package/src/sso.ts +300 -0
- package/src/storage.ts +1 -1
- package/src/store-fs.ts +2 -2
- package/src/streaming-message.test.ts +262 -0
- package/src/streaming-message.ts +297 -0
- package/src/test-runtime.ts +1 -1
- package/src/thread-parent-context.test.ts +224 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token.test.ts +237 -50
- package/src/token.ts +162 -7
- package/src/user-agent.test.ts +86 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.test.ts +81 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
- package/CHANGELOG.md +0 -107
- package/src/file-lock.ts +0 -1
- package/src/graph-users.test.ts +0 -66
- package/src/onboarding.ts +0 -381
- package/src/polls-store.test.ts +0 -38
- package/src/revoked-context.test.ts +0 -39
- package/src/token-response.test.ts +0 -23
|
@@ -0,0 +1,363 @@
|
|
|
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
|
+
|
|
9
|
+
// Helper: a resolveFn that returns a public IP by default
|
|
10
|
+
const publicResolve = async () => ({ address: "13.107.136.10" });
|
|
11
|
+
// Helper: a resolveFn that returns a private IP
|
|
12
|
+
const privateResolve = (ip: string) => async () => ({ address: ip });
|
|
13
|
+
// Helper: a resolveFn that returns multiple addresses
|
|
14
|
+
const multiResolve = (ips: string[]) => async () => ips.map((address) => ({ address }));
|
|
15
|
+
// Helper: a resolveFn that fails
|
|
16
|
+
const failingResolve = async () => {
|
|
17
|
+
throw new Error("DNS failure");
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// ─── isPrivateOrReservedIP ───────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
describe("isPrivateOrReservedIP", () => {
|
|
23
|
+
it.each([
|
|
24
|
+
["10.0.0.1", true],
|
|
25
|
+
["10.255.255.255", true],
|
|
26
|
+
["172.16.0.1", true],
|
|
27
|
+
["172.31.255.255", true],
|
|
28
|
+
["172.15.0.1", false],
|
|
29
|
+
["172.32.0.1", false],
|
|
30
|
+
["192.168.0.1", true],
|
|
31
|
+
["192.168.255.255", true],
|
|
32
|
+
["127.0.0.1", true],
|
|
33
|
+
["127.255.255.255", true],
|
|
34
|
+
["169.254.0.1", true],
|
|
35
|
+
["169.254.169.254", true],
|
|
36
|
+
["0.0.0.0", true],
|
|
37
|
+
["8.8.8.8", false],
|
|
38
|
+
["13.107.136.10", false],
|
|
39
|
+
["52.96.0.1", false],
|
|
40
|
+
] as const)("IPv4 %s → %s", (ip, expected) => {
|
|
41
|
+
expect(isPrivateOrReservedIP(ip)).toBe(expected);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it.each([
|
|
45
|
+
["::1", true],
|
|
46
|
+
["::", true],
|
|
47
|
+
["fe80::1", true],
|
|
48
|
+
["fe80::", true],
|
|
49
|
+
["fc00::1", true],
|
|
50
|
+
["fd12:3456::1", true],
|
|
51
|
+
["2001:0db8::1", false],
|
|
52
|
+
["2620:1ec:c11::200", false],
|
|
53
|
+
// IPv4-mapped IPv6 addresses
|
|
54
|
+
["::ffff:127.0.0.1", true],
|
|
55
|
+
["::ffff:10.0.0.1", true],
|
|
56
|
+
["::ffff:192.168.1.1", true],
|
|
57
|
+
["::ffff:169.254.169.254", true],
|
|
58
|
+
["::ffff:8.8.8.8", false],
|
|
59
|
+
["::ffff:13.107.136.10", false],
|
|
60
|
+
] as const)("IPv6 %s → %s", (ip, expected) => {
|
|
61
|
+
expect(isPrivateOrReservedIP(ip)).toBe(expected);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it.each([
|
|
65
|
+
["999.999.999.999", false],
|
|
66
|
+
["256.0.0.1", false],
|
|
67
|
+
["10.0.0.256", false],
|
|
68
|
+
["-1.0.0.1", false],
|
|
69
|
+
["1.2.3.4.5", false],
|
|
70
|
+
] as const)("malformed IPv4 %s → %s", (ip, expected) => {
|
|
71
|
+
expect(isPrivateOrReservedIP(ip)).toBe(expected);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// ─── validateConsentUploadUrl ────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
describe("validateConsentUploadUrl", () => {
|
|
78
|
+
it("accepts a valid SharePoint HTTPS URL", async () => {
|
|
79
|
+
await expect(
|
|
80
|
+
validateConsentUploadUrl("https://contoso.sharepoint.com/sites/uploads/file.pdf", {
|
|
81
|
+
resolveFn: publicResolve,
|
|
82
|
+
}),
|
|
83
|
+
).resolves.toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("accepts subdomains of allowlisted domains", async () => {
|
|
87
|
+
await expect(
|
|
88
|
+
validateConsentUploadUrl(
|
|
89
|
+
"https://contoso-my.sharepoint.com/personal/user/Documents/file.docx",
|
|
90
|
+
{ resolveFn: publicResolve },
|
|
91
|
+
),
|
|
92
|
+
).resolves.toBeUndefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("accepts graph.microsoft.com", async () => {
|
|
96
|
+
await expect(
|
|
97
|
+
validateConsentUploadUrl("https://graph.microsoft.com/v1.0/me/drive/items/123/content", {
|
|
98
|
+
resolveFn: publicResolve,
|
|
99
|
+
}),
|
|
100
|
+
).resolves.toBeUndefined();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("rejects non-HTTPS URLs", async () => {
|
|
104
|
+
await expect(
|
|
105
|
+
validateConsentUploadUrl("http://contoso.sharepoint.com/file.pdf", {
|
|
106
|
+
resolveFn: publicResolve,
|
|
107
|
+
}),
|
|
108
|
+
).rejects.toThrow("must use HTTPS");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("rejects invalid URLs", async () => {
|
|
112
|
+
await expect(
|
|
113
|
+
validateConsentUploadUrl("not a url", { resolveFn: publicResolve }),
|
|
114
|
+
).rejects.toThrow("not a valid URL");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("rejects hosts not in the allowlist", async () => {
|
|
118
|
+
await expect(
|
|
119
|
+
validateConsentUploadUrl("https://evil.example.com/exfil", { resolveFn: publicResolve }),
|
|
120
|
+
).rejects.toThrow("not in the allowed domains");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("rejects an SSRF attempt with internal metadata URL", async () => {
|
|
124
|
+
await expect(
|
|
125
|
+
validateConsentUploadUrl("https://169.254.169.254/latest/meta-data/", {
|
|
126
|
+
resolveFn: publicResolve,
|
|
127
|
+
}),
|
|
128
|
+
).rejects.toThrow("not in the allowed domains");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("rejects localhost", async () => {
|
|
132
|
+
await expect(
|
|
133
|
+
validateConsentUploadUrl("https://localhost:8080/internal", { resolveFn: publicResolve }),
|
|
134
|
+
).rejects.toThrow("not in the allowed domains");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("rejects when DNS resolves to a private IPv4 (10.x)", async () => {
|
|
138
|
+
await expect(
|
|
139
|
+
validateConsentUploadUrl("https://malicious.sharepoint.com/exfil", {
|
|
140
|
+
resolveFn: privateResolve("10.0.0.1"),
|
|
141
|
+
}),
|
|
142
|
+
).rejects.toThrow("private/reserved IP");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("rejects when DNS resolves to loopback", async () => {
|
|
146
|
+
await expect(
|
|
147
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
148
|
+
resolveFn: privateResolve("127.0.0.1"),
|
|
149
|
+
}),
|
|
150
|
+
).rejects.toThrow("private/reserved IP");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("rejects when DNS resolves to link-local (169.254.x.x)", async () => {
|
|
154
|
+
await expect(
|
|
155
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
156
|
+
resolveFn: privateResolve("169.254.169.254"),
|
|
157
|
+
}),
|
|
158
|
+
).rejects.toThrow("private/reserved IP");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("rejects when DNS resolves to IPv6 loopback", async () => {
|
|
162
|
+
await expect(
|
|
163
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
164
|
+
resolveFn: privateResolve("::1"),
|
|
165
|
+
}),
|
|
166
|
+
).rejects.toThrow("private/reserved IP");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("rejects when DNS resolves to IPv4-mapped IPv6 private address", async () => {
|
|
170
|
+
await expect(
|
|
171
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
172
|
+
resolveFn: privateResolve("::ffff:10.0.0.1"),
|
|
173
|
+
}),
|
|
174
|
+
).rejects.toThrow("private/reserved IP");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("rejects when DNS resolves to IPv4-mapped IPv6 loopback", async () => {
|
|
178
|
+
await expect(
|
|
179
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
180
|
+
resolveFn: privateResolve("::ffff:127.0.0.1"),
|
|
181
|
+
}),
|
|
182
|
+
).rejects.toThrow("private/reserved IP");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("rejects when any DNS answer is private/reserved", async () => {
|
|
186
|
+
await expect(
|
|
187
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
188
|
+
resolveFn: multiResolve(["13.107.136.10", "10.0.0.1"]),
|
|
189
|
+
}),
|
|
190
|
+
).rejects.toThrow("private/reserved IP");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("accepts when all DNS answers are public", async () => {
|
|
194
|
+
await expect(
|
|
195
|
+
validateConsentUploadUrl("https://evil.sharepoint.com/path", {
|
|
196
|
+
resolveFn: multiResolve(["13.107.136.10", "52.96.0.1"]),
|
|
197
|
+
}),
|
|
198
|
+
).resolves.toBeUndefined();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("rejects when DNS resolution fails", async () => {
|
|
202
|
+
await expect(
|
|
203
|
+
validateConsentUploadUrl("https://nonexistent.sharepoint.com/path", {
|
|
204
|
+
resolveFn: failingResolve,
|
|
205
|
+
}),
|
|
206
|
+
).rejects.toThrow("Failed to resolve");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("accepts a custom allowlist", async () => {
|
|
210
|
+
await expect(
|
|
211
|
+
validateConsentUploadUrl("https://custom.example.org/file", {
|
|
212
|
+
allowlist: ["example.org"],
|
|
213
|
+
resolveFn: publicResolve,
|
|
214
|
+
}),
|
|
215
|
+
).resolves.toBeUndefined();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("rejects hosts that are suffix-tricked (e.g. notsharepoint.com)", async () => {
|
|
219
|
+
await expect(
|
|
220
|
+
validateConsentUploadUrl("https://notsharepoint.com/file", { resolveFn: publicResolve }),
|
|
221
|
+
).rejects.toThrow("not in the allowed domains");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("rejects file:// protocol", async () => {
|
|
225
|
+
await expect(
|
|
226
|
+
validateConsentUploadUrl("file:///etc/passwd", { resolveFn: publicResolve }),
|
|
227
|
+
).rejects.toThrow("must use HTTPS");
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// ─── CONSENT_UPLOAD_HOST_ALLOWLIST ───────────────────────────────────────────
|
|
232
|
+
|
|
233
|
+
describe("CONSENT_UPLOAD_HOST_ALLOWLIST", () => {
|
|
234
|
+
it("contains only Microsoft/SharePoint domains", () => {
|
|
235
|
+
for (const domain of CONSENT_UPLOAD_HOST_ALLOWLIST) {
|
|
236
|
+
expect(
|
|
237
|
+
domain.includes("microsoft") ||
|
|
238
|
+
domain.includes("sharepoint") ||
|
|
239
|
+
domain.includes("onedrive") ||
|
|
240
|
+
domain.includes("1drv") ||
|
|
241
|
+
domain.includes("live.com"),
|
|
242
|
+
).toBe(true);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("does not contain overly broad domains", () => {
|
|
247
|
+
const broad = [
|
|
248
|
+
"microsoft.com",
|
|
249
|
+
"azure.com",
|
|
250
|
+
"blob.core.windows.net",
|
|
251
|
+
"azureedge.net",
|
|
252
|
+
"trafficmanager.net",
|
|
253
|
+
];
|
|
254
|
+
for (const domain of broad) {
|
|
255
|
+
expect(CONSENT_UPLOAD_HOST_ALLOWLIST).not.toContain(domain);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// ─── uploadToConsentUrl (integration with validation) ────────────────────────
|
|
261
|
+
|
|
262
|
+
describe("uploadToConsentUrl", () => {
|
|
263
|
+
it("sends the OpenClaw User-Agent header with consent uploads", async () => {
|
|
264
|
+
const fetchFn = vi.fn(async () => new Response(null, { status: 200 }));
|
|
265
|
+
|
|
266
|
+
await uploadToConsentUrl({
|
|
267
|
+
url: "https://contoso.sharepoint.com/upload",
|
|
268
|
+
buffer: Buffer.from("hello"),
|
|
269
|
+
fetchFn,
|
|
270
|
+
validationOpts: { resolveFn: publicResolve },
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
expect(fetchFn).toHaveBeenCalledWith(
|
|
274
|
+
"https://contoso.sharepoint.com/upload",
|
|
275
|
+
expect.objectContaining({
|
|
276
|
+
method: "PUT",
|
|
277
|
+
headers: expect.objectContaining({
|
|
278
|
+
"Content-Range": "bytes 0-4/5",
|
|
279
|
+
"Content-Type": "application/octet-stream",
|
|
280
|
+
"User-Agent": expect.stringMatching(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/),
|
|
281
|
+
}),
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("blocks upload to a disallowed host", async () => {
|
|
287
|
+
const mockFetch = vi.fn();
|
|
288
|
+
await expect(
|
|
289
|
+
uploadToConsentUrl({
|
|
290
|
+
url: "https://evil.example.com/exfil",
|
|
291
|
+
buffer: Buffer.from("secret data"),
|
|
292
|
+
fetchFn: mockFetch,
|
|
293
|
+
validationOpts: { resolveFn: publicResolve },
|
|
294
|
+
}),
|
|
295
|
+
).rejects.toThrow("not in the allowed domains");
|
|
296
|
+
|
|
297
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("blocks upload to a private IP", async () => {
|
|
301
|
+
const mockFetch = vi.fn();
|
|
302
|
+
await expect(
|
|
303
|
+
uploadToConsentUrl({
|
|
304
|
+
url: "https://compromised.sharepoint.com/upload",
|
|
305
|
+
buffer: Buffer.from("data"),
|
|
306
|
+
fetchFn: mockFetch,
|
|
307
|
+
validationOpts: { resolveFn: privateResolve("10.0.0.1") },
|
|
308
|
+
}),
|
|
309
|
+
).rejects.toThrow("private/reserved IP");
|
|
310
|
+
|
|
311
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("allows upload to a valid SharePoint URL and performs PUT", async () => {
|
|
315
|
+
const mockFetch = vi.fn().mockResolvedValue({ ok: true, status: 200 });
|
|
316
|
+
const buffer = Buffer.from("file content");
|
|
317
|
+
|
|
318
|
+
await uploadToConsentUrl({
|
|
319
|
+
url: "https://contoso.sharepoint.com/sites/uploads/file.pdf",
|
|
320
|
+
buffer,
|
|
321
|
+
contentType: "application/pdf",
|
|
322
|
+
fetchFn: mockFetch,
|
|
323
|
+
validationOpts: { resolveFn: publicResolve },
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
expect(mockFetch).toHaveBeenCalledOnce();
|
|
327
|
+
const [url, opts] = mockFetch.mock.calls[0];
|
|
328
|
+
expect(url).toBe("https://contoso.sharepoint.com/sites/uploads/file.pdf");
|
|
329
|
+
expect(opts.method).toBe("PUT");
|
|
330
|
+
expect(opts.headers["Content-Type"]).toBe("application/pdf");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("throws on non-OK response after passing validation", async () => {
|
|
334
|
+
const mockFetch = vi.fn().mockResolvedValue({
|
|
335
|
+
ok: false,
|
|
336
|
+
status: 403,
|
|
337
|
+
statusText: "Forbidden",
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
await expect(
|
|
341
|
+
uploadToConsentUrl({
|
|
342
|
+
url: "https://contoso.sharepoint.com/sites/uploads/file.pdf",
|
|
343
|
+
buffer: Buffer.from("data"),
|
|
344
|
+
fetchFn: mockFetch,
|
|
345
|
+
validationOpts: { resolveFn: publicResolve },
|
|
346
|
+
}),
|
|
347
|
+
).rejects.toThrow("File upload to consent URL failed: 403 Forbidden");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("blocks HTTP (non-HTTPS) upload before fetch is called", async () => {
|
|
351
|
+
const mockFetch = vi.fn();
|
|
352
|
+
await expect(
|
|
353
|
+
uploadToConsentUrl({
|
|
354
|
+
url: "http://contoso.sharepoint.com/upload",
|
|
355
|
+
buffer: Buffer.from("data"),
|
|
356
|
+
fetchFn: mockFetch,
|
|
357
|
+
validationOpts: { resolveFn: publicResolve },
|
|
358
|
+
}),
|
|
359
|
+
).rejects.toThrow("must use HTTPS");
|
|
360
|
+
|
|
361
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
362
|
+
});
|
|
363
|
+
});
|
package/src/file-consent.ts
CHANGED
|
@@ -8,7 +8,158 @@
|
|
|
8
8
|
* - Parsing fileConsent/invoke activities
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
import { lookup } from "node:dns/promises";
|
|
12
|
+
import { buildUserAgent } from "./user-agent.js";
|
|
13
|
+
|
|
14
|
+
function normalizeLowercaseStringOrEmpty(value: unknown): string {
|
|
15
|
+
return typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Allowlist of domains that are valid targets for file consent uploads.
|
|
20
|
+
* These are the Microsoft/SharePoint domains that Teams legitimately provides
|
|
21
|
+
* as upload destinations in the FileConsentCard flow.
|
|
22
|
+
*/
|
|
23
|
+
export const CONSENT_UPLOAD_HOST_ALLOWLIST = [
|
|
24
|
+
"sharepoint.com",
|
|
25
|
+
"sharepoint.us",
|
|
26
|
+
"sharepoint.de",
|
|
27
|
+
"sharepoint.cn",
|
|
28
|
+
"sharepoint-df.com",
|
|
29
|
+
"storage.live.com",
|
|
30
|
+
"onedrive.com",
|
|
31
|
+
"1drv.ms",
|
|
32
|
+
"graph.microsoft.com",
|
|
33
|
+
"graph.microsoft.us",
|
|
34
|
+
"graph.microsoft.de",
|
|
35
|
+
"graph.microsoft.cn",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns true if the given IPv4 or IPv6 address is in a private, loopback,
|
|
40
|
+
* or link-local range that must never be reached via consent uploads.
|
|
41
|
+
*/
|
|
42
|
+
export function isPrivateOrReservedIP(ip: string): boolean {
|
|
43
|
+
// Handle IPv4-mapped IPv6 first (e.g., ::ffff:127.0.0.1, ::ffff:10.0.0.1)
|
|
44
|
+
const ipv4MappedMatch = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(ip);
|
|
45
|
+
if (ipv4MappedMatch) {
|
|
46
|
+
return isPrivateOrReservedIP(ipv4MappedMatch[1]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// IPv4 checks
|
|
50
|
+
const v4Parts = ip.split(".");
|
|
51
|
+
if (v4Parts.length === 4) {
|
|
52
|
+
const octets = v4Parts.map(Number);
|
|
53
|
+
// Validate all octets are integers in 0-255
|
|
54
|
+
if (octets.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const [a, b] = octets;
|
|
58
|
+
// 10.0.0.0/8
|
|
59
|
+
if (a === 10) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
// 172.16.0.0/12
|
|
63
|
+
if (a === 172 && b >= 16 && b <= 31) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
// 192.168.0.0/16
|
|
67
|
+
if (a === 192 && b === 168) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
// 127.0.0.0/8 (loopback)
|
|
71
|
+
if (a === 127) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
// 169.254.0.0/16 (link-local)
|
|
75
|
+
if (a === 169 && b === 254) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
// 0.0.0.0/8
|
|
79
|
+
if (a === 0) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// IPv6 checks
|
|
85
|
+
const normalized = normalizeLowercaseStringOrEmpty(ip);
|
|
86
|
+
// ::1 loopback
|
|
87
|
+
if (normalized === "::1") {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
// fe80::/10 link-local
|
|
91
|
+
if (normalized.startsWith("fe80:") || normalized.startsWith("fe80")) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
// fc00::/7 unique-local (fc00:: and fd00::)
|
|
95
|
+
if (normalized.startsWith("fc") || normalized.startsWith("fd")) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
// :: unspecified
|
|
99
|
+
if (normalized === "::") {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Validate that a consent upload URL is safe to PUT to.
|
|
108
|
+
* Checks:
|
|
109
|
+
* 1. Protocol is HTTPS
|
|
110
|
+
* 2. Hostname matches the consent upload allowlist
|
|
111
|
+
* 3. Resolved IP is not in a private/reserved range (anti-SSRF)
|
|
112
|
+
*
|
|
113
|
+
* @throws Error if the URL fails validation
|
|
114
|
+
*/
|
|
115
|
+
export async function validateConsentUploadUrl(
|
|
116
|
+
url: string,
|
|
117
|
+
opts?: {
|
|
118
|
+
allowlist?: readonly string[];
|
|
119
|
+
resolveFn?: (hostname: string) => Promise<{ address: string } | { address: string }[]>;
|
|
120
|
+
},
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
let parsed: URL;
|
|
123
|
+
try {
|
|
124
|
+
parsed = new URL(url);
|
|
125
|
+
} catch {
|
|
126
|
+
throw new Error("Consent upload URL is not a valid URL");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 1. Protocol check
|
|
130
|
+
if (parsed.protocol !== "https:") {
|
|
131
|
+
throw new Error(`Consent upload URL must use HTTPS, got ${parsed.protocol}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 2. Hostname allowlist check
|
|
135
|
+
const hostname = normalizeLowercaseStringOrEmpty(parsed.hostname);
|
|
136
|
+
const allowlist = opts?.allowlist ?? CONSENT_UPLOAD_HOST_ALLOWLIST;
|
|
137
|
+
const hostAllowed = allowlist.some(
|
|
138
|
+
(entry) => hostname === entry || hostname.endsWith(`.${entry}`),
|
|
139
|
+
);
|
|
140
|
+
if (!hostAllowed) {
|
|
141
|
+
throw new Error(`Consent upload URL hostname "${hostname}" is not in the allowed domains`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 3. DNS resolution — reject private/reserved IPs.
|
|
145
|
+
// Check all resolved addresses to avoid SSRF bypass via mixed public/private answers.
|
|
146
|
+
const resolveFn = opts?.resolveFn ?? ((name: string) => lookup(name, { all: true }));
|
|
147
|
+
let resolved: { address: string }[];
|
|
148
|
+
try {
|
|
149
|
+
const result = await resolveFn(hostname);
|
|
150
|
+
resolved = Array.isArray(result) ? result : [result];
|
|
151
|
+
} catch {
|
|
152
|
+
throw new Error(`Failed to resolve consent upload URL hostname "${hostname}"`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (const entry of resolved) {
|
|
156
|
+
if (isPrivateOrReservedIP(entry.address)) {
|
|
157
|
+
throw new Error(`Consent upload URL resolves to a private/reserved IP (${entry.address})`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface FileConsentCardParams {
|
|
12
163
|
filename: string;
|
|
13
164
|
description?: string;
|
|
14
165
|
sizeInBytes: number;
|
|
@@ -16,7 +167,7 @@ export interface FileConsentCardParams {
|
|
|
16
167
|
context?: Record<string, unknown>;
|
|
17
168
|
}
|
|
18
169
|
|
|
19
|
-
|
|
170
|
+
interface FileInfoCardParams {
|
|
20
171
|
filename: string;
|
|
21
172
|
contentUrl: string;
|
|
22
173
|
uniqueId: string;
|
|
@@ -56,7 +207,7 @@ export function buildFileInfoCard(params: FileInfoCardParams) {
|
|
|
56
207
|
};
|
|
57
208
|
}
|
|
58
209
|
|
|
59
|
-
|
|
210
|
+
interface FileConsentUploadInfo {
|
|
60
211
|
name: string;
|
|
61
212
|
uploadUrl: string;
|
|
62
213
|
contentUrl: string;
|
|
@@ -64,7 +215,7 @@ export interface FileConsentUploadInfo {
|
|
|
64
215
|
fileType: string;
|
|
65
216
|
}
|
|
66
217
|
|
|
67
|
-
|
|
218
|
+
interface FileConsentResponse {
|
|
68
219
|
action: "accept" | "decline";
|
|
69
220
|
uploadInfo?: FileConsentUploadInfo;
|
|
70
221
|
context?: Record<string, unknown>;
|
|
@@ -103,17 +254,27 @@ export function parseFileConsentInvoke(activity: {
|
|
|
103
254
|
/**
|
|
104
255
|
* Upload a file to the consent URL provided by Teams.
|
|
105
256
|
* The URL is provided in the fileConsent/invoke response after user accepts.
|
|
257
|
+
*
|
|
258
|
+
* @throws Error if the URL fails SSRF validation (non-HTTPS, disallowed host, private IP)
|
|
106
259
|
*/
|
|
107
260
|
export async function uploadToConsentUrl(params: {
|
|
108
261
|
url: string;
|
|
109
262
|
buffer: Buffer;
|
|
110
263
|
contentType?: string;
|
|
111
264
|
fetchFn?: typeof fetch;
|
|
265
|
+
/** Override for testing — custom allowlist and DNS resolver */
|
|
266
|
+
validationOpts?: {
|
|
267
|
+
allowlist?: readonly string[];
|
|
268
|
+
resolveFn?: (hostname: string) => Promise<{ address: string } | { address: string }[]>;
|
|
269
|
+
};
|
|
112
270
|
}): Promise<void> {
|
|
271
|
+
await validateConsentUploadUrl(params.url, params.validationOpts);
|
|
272
|
+
|
|
113
273
|
const fetchFn = params.fetchFn ?? fetch;
|
|
114
274
|
const res = await fetchFn(params.url, {
|
|
115
275
|
method: "PUT",
|
|
116
276
|
headers: {
|
|
277
|
+
"User-Agent": buildUserAgent(),
|
|
117
278
|
"Content-Type": params.contentType ?? "application/octet-stream",
|
|
118
279
|
"Content-Range": `bytes 0-${params.buffer.length - 1}/${params.buffer.length}`,
|
|
119
280
|
},
|
package/src/graph-chat.ts
CHANGED
|
@@ -7,8 +7,6 @@
|
|
|
7
7
|
* @see https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bots-filesv4
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import type { DriveItemProperties } from "./graph-upload.js";
|
|
11
|
-
|
|
12
10
|
/**
|
|
13
11
|
* Build a native Teams file card attachment for Bot Framework.
|
|
14
12
|
*
|
|
@@ -19,6 +17,9 @@ import type { DriveItemProperties } from "./graph-upload.js";
|
|
|
19
17
|
* @param file - DriveItem properties from getDriveItemProperties()
|
|
20
18
|
* @returns Attachment object for Bot Framework sendActivity()
|
|
21
19
|
*/
|
|
20
|
+
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
|
|
21
|
+
import type { DriveItemProperties } from "./graph-upload.js";
|
|
22
|
+
|
|
22
23
|
export function buildTeamsFileInfoCard(file: DriveItemProperties): {
|
|
23
24
|
contentType: string;
|
|
24
25
|
contentUrl: string;
|
|
@@ -39,7 +40,8 @@ export function buildTeamsFileInfoCard(file: DriveItemProperties): {
|
|
|
39
40
|
|
|
40
41
|
// Extract file extension from filename
|
|
41
42
|
const lastDot = file.name.lastIndexOf(".");
|
|
42
|
-
const fileType =
|
|
43
|
+
const fileType =
|
|
44
|
+
lastDot >= 0 ? normalizeLowercaseStringOrEmpty(file.name.slice(lastDot + 1)) : "";
|
|
43
45
|
|
|
44
46
|
return {
|
|
45
47
|
contentType: "application/vnd.microsoft.teams.card.file.info",
|