@kodelyth/msteams 2026.5.39 → 2026.5.42
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/dist/api.js +3 -0
- package/dist/channel-BvTXHuGs.js +1161 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-NssGKZm5.js +650 -0
- package/dist/config-schema-Btk-XCOd.js +43 -0
- package/dist/contract-api.js +2 -0
- package/dist/graph-users-D-gKCguI.js +1411 -0
- package/dist/index.js +22 -0
- package/dist/oauth-BUxlphX3.js +114 -0
- package/dist/oauth.token-ebId9946.js +116 -0
- package/dist/probe-Cj2KsAGF.js +2190 -0
- package/dist/runtime-api-BL4DOWXD.js +28 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-Bo7kdUrT.js +35 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-plugin-api.js +64 -0
- package/dist/setup-surface-COTQDcTQ.js +531 -0
- package/dist/src-tvpsGYPV.js +4226 -0
- package/dist/test-api.js +2 -0
- package/index.ts +20 -0
- package/klaw.plugin.json +2 -726
- package/package.json +4 -4
- package/runtime-api.ts +66 -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 +506 -0
- package/src/attachments/bot-framework.ts +348 -0
- package/src/attachments/download.ts +328 -0
- package/src/attachments/graph.test.ts +441 -0
- package/src/attachments/graph.ts +489 -0
- package/src/attachments/html.ts +122 -0
- package/src/attachments/payload.ts +14 -0
- package/src/attachments/remote-media.test.ts +187 -0
- package/src/attachments/remote-media.ts +86 -0
- package/src/attachments/shared.test.ts +547 -0
- package/src/attachments/shared.ts +655 -0
- package/src/attachments/types.ts +47 -0
- package/src/attachments.graph.test.ts +414 -0
- package/src/attachments.helpers.test.ts +245 -0
- package/src/attachments.test-helpers.ts +17 -0
- package/src/attachments.test.ts +754 -0
- package/src/attachments.ts +18 -0
- package/src/block-streaming-config.test.ts +61 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.actions.test.ts +797 -0
- package/src/channel.directory.test.ts +176 -0
- package/src/channel.message-adapter.test.ts +227 -0
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -0
- package/src/channel.test.ts +136 -0
- package/src/channel.ts +1176 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +40 -0
- package/src/conversation-store-fs.test.ts +81 -0
- package/src/conversation-store-fs.ts +149 -0
- package/src/conversation-store-helpers.test.ts +202 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +51 -0
- package/src/conversation-store.shared.test.ts +260 -0
- package/src/conversation-store.ts +71 -0
- package/src/directory-live.test.ts +156 -0
- package/src/directory-live.ts +111 -0
- package/src/doctor.ts +27 -0
- package/src/errors.test.ts +154 -0
- package/src/errors.ts +270 -0
- package/src/feedback-reflection-prompt.ts +117 -0
- package/src/feedback-reflection-store.ts +113 -0
- package/src/feedback-reflection.test.ts +237 -0
- package/src/feedback-reflection.ts +268 -0
- package/src/file-consent-helpers.test.ts +328 -0
- package/src/file-consent-helpers.ts +115 -0
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.test.ts +378 -0
- package/src/file-consent.ts +223 -0
- package/src/graph-chat.ts +36 -0
- package/src/graph-group-management.test.ts +332 -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 +253 -0
- package/src/graph-messages.read.test.ts +391 -0
- package/src/graph-messages.search.test.ts +227 -0
- package/src/graph-messages.test-helpers.ts +50 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.test.ts +222 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.test.ts +252 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.test.ts +253 -0
- package/src/graph-upload.ts +531 -0
- package/src/graph-users.ts +29 -0
- package/src/graph.test.ts +540 -0
- package/src/graph.ts +308 -0
- package/src/inbound.test.ts +221 -0
- package/src/inbound.ts +148 -0
- package/src/index.ts +4 -0
- package/src/media-helpers.test.ts +220 -0
- package/src/media-helpers.ts +105 -0
- package/src/mentions.test.ts +254 -0
- package/src/mentions.ts +114 -0
- package/src/messenger.test.ts +961 -0
- package/src/messenger.ts +608 -0
- package/src/monitor-handler/access.ts +136 -0
- package/src/monitor-handler/inbound-media.test.ts +314 -0
- package/src/monitor-handler/inbound-media.ts +180 -0
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
- package/src/monitor-handler/message-handler.authz.test.ts +739 -0
- package/src/monitor-handler/message-handler.dm-media.test.ts +54 -0
- package/src/monitor-handler/message-handler.test-support.ts +99 -0
- package/src/monitor-handler/message-handler.thread-parent.test.ts +225 -0
- package/src/monitor-handler/message-handler.thread-session.test.ts +132 -0
- package/src/monitor-handler/message-handler.ts +1003 -0
- package/src/monitor-handler/reaction-handler.test.ts +325 -0
- package/src/monitor-handler/reaction-handler.ts +122 -0
- package/src/monitor-handler/thread-session.ts +30 -0
- package/src/monitor-handler.adaptive-card.test.ts +158 -0
- package/src/monitor-handler.feedback-authz.test.ts +357 -0
- package/src/monitor-handler.file-consent.test.ts +443 -0
- package/src/monitor-handler.sso.test.ts +576 -0
- package/src/monitor-handler.test-helpers.ts +181 -0
- package/src/monitor-handler.ts +538 -0
- package/src/monitor-handler.types.ts +27 -0
- package/src/monitor-types.ts +6 -0
- package/src/monitor.lifecycle.test.ts +457 -0
- package/src/monitor.test.ts +119 -0
- package/src/monitor.ts +476 -0
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.test.ts +350 -0
- package/src/oauth.token.ts +162 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.test.ts +400 -0
- package/src/outbound.ts +198 -0
- package/src/pending-uploads-fs.test.ts +261 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.test.ts +186 -0
- package/src/pending-uploads.ts +121 -0
- package/src/policy.test.ts +156 -0
- package/src/policy.ts +245 -0
- package/src/polls-store-memory.ts +32 -0
- package/src/polls.test.ts +169 -0
- package/src/polls.ts +312 -0
- package/src/presentation.ts +93 -0
- package/src/probe.test.ts +79 -0
- package/src/probe.ts +132 -0
- package/src/reply-dispatcher.test.ts +543 -0
- package/src/reply-dispatcher.ts +523 -0
- package/src/reply-stream-controller.test.ts +424 -0
- package/src/reply-stream-controller.ts +334 -0
- package/src/resolve-allowlist.test.ts +253 -0
- package/src/resolve-allowlist.ts +309 -0
- package/src/revoked-context.ts +17 -0
- package/src/runtime.ts +12 -0
- package/src/sdk-types.ts +59 -0
- package/src/sdk.test.ts +727 -0
- package/src/sdk.ts +916 -0
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +7 -0
- package/src/send-context.test.ts +93 -0
- package/src/send-context.ts +269 -0
- package/src/send.test.ts +588 -0
- package/src/send.ts +697 -0
- package/src/sent-message-cache.test.ts +106 -0
- package/src/sent-message-cache.ts +174 -0
- package/src/session-route.ts +40 -0
- package/src/setup-core.ts +162 -0
- package/src/setup-surface.test.ts +175 -0
- package/src/setup-surface.ts +319 -0
- package/src/sso-token-store.test.ts +74 -0
- package/src/sso-token-store.ts +166 -0
- package/src/sso.ts +300 -0
- package/src/storage.ts +25 -0
- package/src/store-fs.ts +42 -0
- package/src/streaming-message.test.ts +323 -0
- package/src/streaming-message.ts +327 -0
- package/src/test-runtime.ts +16 -0
- package/src/thread-parent-context.test.ts +224 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token-response.ts +11 -0
- package/src/token.test.ts +268 -0
- package/src/token.ts +194 -0
- package/src/user-agent.test.ts +121 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.test.ts +104 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-config-api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/test-api.js +0 -7
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import type { Request, Response } from "express";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import type { KlawConfig, RuntimeEnv } from "../runtime-api.js";
|
|
5
|
+
import type { MSTeamsConversationStore } from "./conversation-store.js";
|
|
6
|
+
import type { MSTeamsActivityHandler, MSTeamsMessageHandlerDeps } from "./monitor-handler.js";
|
|
7
|
+
import type { MSTeamsPollStore } from "./polls.js";
|
|
8
|
+
|
|
9
|
+
type FakeServer = EventEmitter & {
|
|
10
|
+
close: (callback?: (err?: Error | null) => void) => void;
|
|
11
|
+
setTimeout: (msecs: number) => FakeServer;
|
|
12
|
+
requestTimeout: number;
|
|
13
|
+
headersTimeout: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type MSTeamsChannelResolution = {
|
|
17
|
+
input: string;
|
|
18
|
+
resolved: boolean;
|
|
19
|
+
teamId?: string;
|
|
20
|
+
channelId?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type MSTeamsUserResolution = {
|
|
24
|
+
input: string;
|
|
25
|
+
resolved: boolean;
|
|
26
|
+
id?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type ResolveMSTeamsChannelAllowlistMock = (params: {
|
|
30
|
+
cfg: unknown;
|
|
31
|
+
entries: string[];
|
|
32
|
+
}) => Promise<MSTeamsChannelResolution[]>;
|
|
33
|
+
|
|
34
|
+
type ResolveMSTeamsUserAllowlistMock = (params: {
|
|
35
|
+
cfg: unknown;
|
|
36
|
+
entries: string[];
|
|
37
|
+
}) => Promise<MSTeamsUserResolution[]>;
|
|
38
|
+
|
|
39
|
+
type RegisterMSTeamsHandlersMock = (
|
|
40
|
+
handler: MSTeamsActivityHandler,
|
|
41
|
+
deps: MSTeamsMessageHandlerDeps,
|
|
42
|
+
) => MSTeamsActivityHandler;
|
|
43
|
+
|
|
44
|
+
const expressControl = vi.hoisted(() => ({
|
|
45
|
+
mode: { value: "listening" as "listening" | "error" },
|
|
46
|
+
apps: [] as Array<{
|
|
47
|
+
use: ReturnType<typeof vi.fn>;
|
|
48
|
+
post: ReturnType<typeof vi.fn>;
|
|
49
|
+
listen: ReturnType<typeof vi.fn>;
|
|
50
|
+
}>,
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
const isDangerousNameMatchingEnabled = vi.hoisted(() => vi.fn());
|
|
54
|
+
|
|
55
|
+
vi.mock("../runtime-api.js", () => ({
|
|
56
|
+
DEFAULT_WEBHOOK_MAX_BODY_BYTES: 1024 * 1024,
|
|
57
|
+
isDangerousNameMatchingEnabled,
|
|
58
|
+
normalizeSecretInputString: (value: unknown) =>
|
|
59
|
+
typeof value === "string" && value.trim() ? value.trim() : undefined,
|
|
60
|
+
hasConfiguredSecretInput: (value: unknown) =>
|
|
61
|
+
typeof value === "string" && value.trim().length > 0,
|
|
62
|
+
normalizeResolvedSecretInputString: (params: { value?: unknown }) =>
|
|
63
|
+
typeof params?.value === "string" && params.value.trim() ? params.value.trim() : undefined,
|
|
64
|
+
keepHttpServerTaskAlive: vi.fn(
|
|
65
|
+
async (params: { abortSignal?: AbortSignal; onAbort?: () => Promise<void> | void }) => {
|
|
66
|
+
await new Promise<void>((resolve) => {
|
|
67
|
+
if (params.abortSignal?.aborted) {
|
|
68
|
+
resolve();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
params.abortSignal?.addEventListener("abort", () => resolve(), { once: true });
|
|
72
|
+
});
|
|
73
|
+
await params.onAbort?.();
|
|
74
|
+
},
|
|
75
|
+
),
|
|
76
|
+
mergeAllowlist: (params: { existing?: string[]; additions?: string[] }) =>
|
|
77
|
+
Array.from(new Set([...(params.existing ?? []), ...(params.additions ?? [])])),
|
|
78
|
+
summarizeMapping: vi.fn(),
|
|
79
|
+
}));
|
|
80
|
+
|
|
81
|
+
vi.mock("express", () => {
|
|
82
|
+
const json = vi.fn(() => {
|
|
83
|
+
return (_req: unknown, _res: unknown, next?: (err?: unknown) => void) => {
|
|
84
|
+
next?.();
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const factory = () => ({
|
|
89
|
+
use: vi.fn(),
|
|
90
|
+
post: vi.fn(),
|
|
91
|
+
listen: vi.fn((_port: number) => {
|
|
92
|
+
const server = new EventEmitter() as FakeServer;
|
|
93
|
+
server.setTimeout = vi.fn((_msecs: number) => server);
|
|
94
|
+
server.requestTimeout = 0;
|
|
95
|
+
server.headersTimeout = 0;
|
|
96
|
+
server.close = (callback?: (err?: Error | null) => void) => {
|
|
97
|
+
queueMicrotask(() => {
|
|
98
|
+
server.emit("close");
|
|
99
|
+
callback?.(null);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
queueMicrotask(() => {
|
|
103
|
+
if (expressControl.mode.value === "error") {
|
|
104
|
+
server.emit("error", new Error("listen EADDRINUSE"));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
server.emit("listening");
|
|
108
|
+
});
|
|
109
|
+
return server;
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const wrappedFactory = () => {
|
|
114
|
+
const app = factory();
|
|
115
|
+
expressControl.apps.push(app);
|
|
116
|
+
return app;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
default: wrappedFactory,
|
|
121
|
+
json,
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const registerMSTeamsHandlers = vi.hoisted(() =>
|
|
126
|
+
vi.fn<RegisterMSTeamsHandlersMock>((handler) => handler),
|
|
127
|
+
);
|
|
128
|
+
const createMSTeamsAdapter = vi.hoisted(() =>
|
|
129
|
+
vi.fn(() => ({
|
|
130
|
+
process: vi.fn(async () => {}),
|
|
131
|
+
})),
|
|
132
|
+
);
|
|
133
|
+
const jwtValidate = vi.hoisted(() => vi.fn().mockResolvedValue(true));
|
|
134
|
+
const loadMSTeamsSdkWithAuth = vi.hoisted(() =>
|
|
135
|
+
vi.fn(async () => ({
|
|
136
|
+
sdk: {
|
|
137
|
+
ActivityHandler: function ActivityHandler() {},
|
|
138
|
+
MsalTokenProvider: function MsalTokenProvider() {},
|
|
139
|
+
authorizeJWT:
|
|
140
|
+
() => (_req: unknown, _res: unknown, next: ((err?: unknown) => void) | undefined) =>
|
|
141
|
+
next?.(),
|
|
142
|
+
},
|
|
143
|
+
authConfig: {},
|
|
144
|
+
})),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
vi.mock("./monitor-handler.js", () => ({
|
|
148
|
+
registerMSTeamsHandlers,
|
|
149
|
+
}));
|
|
150
|
+
|
|
151
|
+
const resolveAllowlistMocks = vi.hoisted(() => ({
|
|
152
|
+
resolveMSTeamsChannelAllowlist: vi.fn<ResolveMSTeamsChannelAllowlistMock>(async () => []),
|
|
153
|
+
resolveMSTeamsUserAllowlist: vi.fn<ResolveMSTeamsUserAllowlistMock>(async () => []),
|
|
154
|
+
}));
|
|
155
|
+
|
|
156
|
+
vi.mock("./resolve-allowlist.js", () => ({
|
|
157
|
+
resolveMSTeamsChannelAllowlist: resolveAllowlistMocks.resolveMSTeamsChannelAllowlist,
|
|
158
|
+
resolveMSTeamsUserAllowlist: resolveAllowlistMocks.resolveMSTeamsUserAllowlist,
|
|
159
|
+
}));
|
|
160
|
+
|
|
161
|
+
vi.mock("./sdk.js", () => ({
|
|
162
|
+
createMSTeamsAdapter: () => createMSTeamsAdapter(),
|
|
163
|
+
loadMSTeamsSdkWithAuth: () => loadMSTeamsSdkWithAuth(),
|
|
164
|
+
createMSTeamsTokenProvider: () => ({
|
|
165
|
+
getAccessToken: vi.fn().mockResolvedValue("mock-token"),
|
|
166
|
+
}),
|
|
167
|
+
createBotFrameworkJwtValidator: vi.fn().mockResolvedValue({
|
|
168
|
+
validate: jwtValidate,
|
|
169
|
+
}),
|
|
170
|
+
}));
|
|
171
|
+
|
|
172
|
+
vi.mock("./runtime.js", () => ({
|
|
173
|
+
getMSTeamsRuntime: () => ({
|
|
174
|
+
logging: {
|
|
175
|
+
getChildLogger: () => ({
|
|
176
|
+
info: vi.fn(),
|
|
177
|
+
error: vi.fn(),
|
|
178
|
+
debug: vi.fn(),
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
channel: {
|
|
182
|
+
text: {
|
|
183
|
+
resolveTextChunkLimit: () => 4000,
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
}),
|
|
187
|
+
}));
|
|
188
|
+
|
|
189
|
+
import { monitorMSTeamsProvider } from "./monitor.js";
|
|
190
|
+
|
|
191
|
+
function createConfig(port: number): KlawConfig {
|
|
192
|
+
return {
|
|
193
|
+
channels: {
|
|
194
|
+
msteams: {
|
|
195
|
+
enabled: true,
|
|
196
|
+
appId: "app-id",
|
|
197
|
+
appPassword: "app-password", // pragma: allowlist secret
|
|
198
|
+
tenantId: "tenant-id",
|
|
199
|
+
webhook: {
|
|
200
|
+
port,
|
|
201
|
+
path: "/api/messages",
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
} as KlawConfig;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function updateMSTeamsConfig(
|
|
209
|
+
cfg: KlawConfig,
|
|
210
|
+
patch: NonNullable<NonNullable<KlawConfig["channels"]>["msteams"]>,
|
|
211
|
+
): void {
|
|
212
|
+
const msteams = cfg.channels?.msteams;
|
|
213
|
+
if (!cfg.channels || !msteams) {
|
|
214
|
+
throw new Error("Expected Microsoft Teams config fixture");
|
|
215
|
+
}
|
|
216
|
+
cfg.channels.msteams = {
|
|
217
|
+
...msteams,
|
|
218
|
+
...patch,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function createRuntime(): RuntimeEnv {
|
|
223
|
+
return {
|
|
224
|
+
log: vi.fn(),
|
|
225
|
+
error: vi.fn(),
|
|
226
|
+
exit: (code: number): never => {
|
|
227
|
+
throw new Error(`exit ${code}`);
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function createStores() {
|
|
233
|
+
return {
|
|
234
|
+
conversationStore: {} as MSTeamsConversationStore,
|
|
235
|
+
pollStore: {} as MSTeamsPollStore,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function readMockCallArg(mock: ReturnType<typeof vi.fn>, callIndex: number, argIndex: number) {
|
|
240
|
+
const call = mock.mock.calls[callIndex];
|
|
241
|
+
if (!call) {
|
|
242
|
+
throw new Error(`expected mock call #${callIndex + 1}`);
|
|
243
|
+
}
|
|
244
|
+
if (argIndex >= call.length) {
|
|
245
|
+
throw new Error(`expected mock call #${callIndex + 1} argument #${argIndex + 1}`);
|
|
246
|
+
}
|
|
247
|
+
return call[argIndex];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function requireRegisteredMSTeamsConfig(): KlawConfig {
|
|
251
|
+
const registered = readMockCallArg(registerMSTeamsHandlers, 0, 1) as { cfg?: KlawConfig };
|
|
252
|
+
if (!registered?.cfg) {
|
|
253
|
+
throw new Error("expected registered MSTeams handler config");
|
|
254
|
+
}
|
|
255
|
+
return registered.cfg;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
describe("monitorMSTeamsProvider lifecycle", () => {
|
|
259
|
+
afterEach(() => {
|
|
260
|
+
vi.clearAllMocks();
|
|
261
|
+
expressControl.mode.value = "listening";
|
|
262
|
+
expressControl.apps.length = 0;
|
|
263
|
+
isDangerousNameMatchingEnabled.mockReset().mockReturnValue(false);
|
|
264
|
+
resolveAllowlistMocks.resolveMSTeamsChannelAllowlist.mockReset().mockResolvedValue([]);
|
|
265
|
+
resolveAllowlistMocks.resolveMSTeamsUserAllowlist.mockReset().mockResolvedValue([]);
|
|
266
|
+
jwtValidate.mockReset().mockResolvedValue(true);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("stays active until aborted", async () => {
|
|
270
|
+
const abort = new AbortController();
|
|
271
|
+
const stores = createStores();
|
|
272
|
+
const task = monitorMSTeamsProvider({
|
|
273
|
+
cfg: createConfig(0),
|
|
274
|
+
runtime: createRuntime(),
|
|
275
|
+
abortSignal: abort.signal,
|
|
276
|
+
conversationStore: stores.conversationStore,
|
|
277
|
+
pollStore: stores.pollStore,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const early = await Promise.race([
|
|
281
|
+
task.then(() => "resolved"),
|
|
282
|
+
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 50)),
|
|
283
|
+
]);
|
|
284
|
+
expect(early).toBe("pending");
|
|
285
|
+
|
|
286
|
+
abort.abort();
|
|
287
|
+
const result = await task;
|
|
288
|
+
if (!result.app) {
|
|
289
|
+
throw new Error("expected Teams monitor app after startup abort");
|
|
290
|
+
}
|
|
291
|
+
await expect(result.shutdown()).resolves.toBeUndefined();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("rejects startup when webhook port is already in use", async () => {
|
|
295
|
+
expressControl.mode.value = "error";
|
|
296
|
+
await expect(
|
|
297
|
+
monitorMSTeamsProvider({
|
|
298
|
+
cfg: createConfig(3978),
|
|
299
|
+
runtime: createRuntime(),
|
|
300
|
+
abortSignal: new AbortController().signal,
|
|
301
|
+
conversationStore: createStores().conversationStore,
|
|
302
|
+
pollStore: createStores().pollStore,
|
|
303
|
+
}),
|
|
304
|
+
).rejects.toThrow(/EADDRINUSE/);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("runs JWT validation before JSON body parsing", async () => {
|
|
308
|
+
const abort = new AbortController();
|
|
309
|
+
const task = monitorMSTeamsProvider({
|
|
310
|
+
cfg: createConfig(0),
|
|
311
|
+
runtime: createRuntime(),
|
|
312
|
+
abortSignal: abort.signal,
|
|
313
|
+
conversationStore: createStores().conversationStore,
|
|
314
|
+
pollStore: createStores().pollStore,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
await vi.waitFor(() => {
|
|
318
|
+
expect(expressControl.apps.length).toBeGreaterThan(0);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const app = expressControl.apps.at(-1);
|
|
322
|
+
if (!app) {
|
|
323
|
+
throw new Error("expected Express app to be created");
|
|
324
|
+
}
|
|
325
|
+
expect(app.use).toHaveBeenCalledTimes(4);
|
|
326
|
+
|
|
327
|
+
const jsonMiddleware = vi.mocked((await import("express")).json).mock.results[0]?.value;
|
|
328
|
+
if (typeof jsonMiddleware !== "function") {
|
|
329
|
+
throw new Error("expected Express JSON middleware");
|
|
330
|
+
}
|
|
331
|
+
expect(readMockCallArg(app.use, 1, 0)).not.toBe(jsonMiddleware);
|
|
332
|
+
expect(readMockCallArg(app.use, 2, 0)).toBe(jsonMiddleware);
|
|
333
|
+
|
|
334
|
+
const jwtMiddleware = readMockCallArg(app.use, 1, 0) as (
|
|
335
|
+
req: Request,
|
|
336
|
+
res: Response,
|
|
337
|
+
next: (err?: unknown) => void,
|
|
338
|
+
) => void;
|
|
339
|
+
const next = vi.fn();
|
|
340
|
+
jwtMiddleware(
|
|
341
|
+
{ headers: { authorization: "Bearer token" } } as Request,
|
|
342
|
+
{
|
|
343
|
+
status: vi.fn().mockReturnThis(),
|
|
344
|
+
json: vi.fn(),
|
|
345
|
+
} as unknown as Response,
|
|
346
|
+
next,
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
await vi.waitFor(() => {
|
|
350
|
+
expect(jwtValidate).toHaveBeenCalledWith("Bearer token");
|
|
351
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
abort.abort();
|
|
355
|
+
await task;
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("does not resolve user allowlists by display name unless name matching is enabled", async () => {
|
|
359
|
+
const abort = new AbortController();
|
|
360
|
+
const cfg = createConfig(0);
|
|
361
|
+
updateMSTeamsConfig(cfg, {
|
|
362
|
+
allowFrom: ["Alice", "user:40a1a0ed-4ff2-4164-a219-55518990c197"],
|
|
363
|
+
groupAllowFrom: ["Bob", "msteams:user:50a1a0ed-4ff2-4164-a219-55518990c198"],
|
|
364
|
+
teams: {
|
|
365
|
+
Product: {
|
|
366
|
+
channels: {
|
|
367
|
+
Roadmap: {},
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
resolveAllowlistMocks.resolveMSTeamsChannelAllowlist.mockResolvedValueOnce([
|
|
373
|
+
{
|
|
374
|
+
input: "Product/Roadmap",
|
|
375
|
+
resolved: true,
|
|
376
|
+
teamId: "team-id",
|
|
377
|
+
channelId: "channel-id",
|
|
378
|
+
},
|
|
379
|
+
]);
|
|
380
|
+
|
|
381
|
+
const task = monitorMSTeamsProvider({
|
|
382
|
+
cfg,
|
|
383
|
+
runtime: createRuntime(),
|
|
384
|
+
abortSignal: abort.signal,
|
|
385
|
+
conversationStore: createStores().conversationStore,
|
|
386
|
+
pollStore: createStores().pollStore,
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
await vi.waitFor(() => {
|
|
390
|
+
expect(registerMSTeamsHandlers).toHaveBeenCalled();
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
expect(resolveAllowlistMocks.resolveMSTeamsUserAllowlist).not.toHaveBeenCalled();
|
|
394
|
+
expect(resolveAllowlistMocks.resolveMSTeamsChannelAllowlist).toHaveBeenCalledWith({
|
|
395
|
+
cfg,
|
|
396
|
+
entries: ["Product/Roadmap"],
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const registeredCfg = requireRegisteredMSTeamsConfig();
|
|
400
|
+
expect(registeredCfg.channels?.msteams?.allowFrom).toEqual([
|
|
401
|
+
"Alice",
|
|
402
|
+
"user:40a1a0ed-4ff2-4164-a219-55518990c197",
|
|
403
|
+
"40a1a0ed-4ff2-4164-a219-55518990c197",
|
|
404
|
+
]);
|
|
405
|
+
expect(registeredCfg.channels?.msteams?.groupAllowFrom).toEqual([
|
|
406
|
+
"Bob",
|
|
407
|
+
"msteams:user:50a1a0ed-4ff2-4164-a219-55518990c198",
|
|
408
|
+
"50a1a0ed-4ff2-4164-a219-55518990c198",
|
|
409
|
+
]);
|
|
410
|
+
|
|
411
|
+
abort.abort();
|
|
412
|
+
await task;
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("resolves user allowlists when name matching is enabled", async () => {
|
|
416
|
+
isDangerousNameMatchingEnabled.mockReturnValue(true);
|
|
417
|
+
resolveAllowlistMocks.resolveMSTeamsUserAllowlist
|
|
418
|
+
.mockResolvedValueOnce([{ input: "Alice", resolved: true, id: "alice-aad" }])
|
|
419
|
+
.mockResolvedValueOnce([{ input: "Bob", resolved: true, id: "bob-aad" }]);
|
|
420
|
+
|
|
421
|
+
const abort = new AbortController();
|
|
422
|
+
const cfg = createConfig(0);
|
|
423
|
+
updateMSTeamsConfig(cfg, {
|
|
424
|
+
dangerouslyAllowNameMatching: true,
|
|
425
|
+
allowFrom: ["Alice"],
|
|
426
|
+
groupAllowFrom: ["Bob"],
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
const task = monitorMSTeamsProvider({
|
|
430
|
+
cfg,
|
|
431
|
+
runtime: createRuntime(),
|
|
432
|
+
abortSignal: abort.signal,
|
|
433
|
+
conversationStore: createStores().conversationStore,
|
|
434
|
+
pollStore: createStores().pollStore,
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
await vi.waitFor(() => {
|
|
438
|
+
expect(registerMSTeamsHandlers).toHaveBeenCalled();
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
expect(resolveAllowlistMocks.resolveMSTeamsUserAllowlist).toHaveBeenNthCalledWith(1, {
|
|
442
|
+
cfg,
|
|
443
|
+
entries: ["Alice"],
|
|
444
|
+
});
|
|
445
|
+
expect(resolveAllowlistMocks.resolveMSTeamsUserAllowlist).toHaveBeenNthCalledWith(2, {
|
|
446
|
+
cfg,
|
|
447
|
+
entries: ["Bob"],
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const registeredCfg = requireRegisteredMSTeamsConfig();
|
|
451
|
+
expect(registeredCfg.channels?.msteams?.allowFrom).toEqual(["Alice", "alice-aad"]);
|
|
452
|
+
expect(registeredCfg.channels?.msteams?.groupAllowFrom).toEqual(["Bob", "bob-aad"]);
|
|
453
|
+
|
|
454
|
+
abort.abort();
|
|
455
|
+
await task;
|
|
456
|
+
});
|
|
457
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import type { Server } from "node:http";
|
|
3
|
+
import { createConnection, type AddressInfo } from "node:net";
|
|
4
|
+
import express from "express";
|
|
5
|
+
import { describe, expect, it } from "vitest";
|
|
6
|
+
import { applyMSTeamsWebhookTimeouts } from "./webhook-timeouts.js";
|
|
7
|
+
|
|
8
|
+
async function closeServer(server: Server): Promise<void> {
|
|
9
|
+
await new Promise<void>((resolve) => {
|
|
10
|
+
server.close(() => resolve());
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function waitForSlowBodySocketClose(port: number, timeoutMs: number): Promise<number> {
|
|
15
|
+
return new Promise<number>((resolve, reject) => {
|
|
16
|
+
const startedAt = Date.now();
|
|
17
|
+
const socket = createConnection({ host: "127.0.0.1", port }, () => {
|
|
18
|
+
socket.write("POST /api/messages HTTP/1.1\r\n");
|
|
19
|
+
socket.write("Host: localhost\r\n");
|
|
20
|
+
socket.write("Content-Type: application/json\r\n");
|
|
21
|
+
socket.write("Content-Length: 1048576\r\n");
|
|
22
|
+
socket.write("\r\n");
|
|
23
|
+
socket.write('{"type":"message"');
|
|
24
|
+
});
|
|
25
|
+
socket.on("error", () => {
|
|
26
|
+
// ECONNRESET is expected once the server drops the socket.
|
|
27
|
+
});
|
|
28
|
+
const failTimer = setTimeout(() => {
|
|
29
|
+
socket.destroy();
|
|
30
|
+
reject(new Error(`socket stayed open for ${timeoutMs}ms`));
|
|
31
|
+
}, timeoutMs);
|
|
32
|
+
socket.on("close", () => {
|
|
33
|
+
clearTimeout(failTimer);
|
|
34
|
+
resolve(Date.now() - startedAt);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("msteams monitor webhook hardening", () => {
|
|
40
|
+
it("applies default timeouts and header clamp", async () => {
|
|
41
|
+
const app = express();
|
|
42
|
+
const server = app.listen(0, "127.0.0.1");
|
|
43
|
+
await once(server, "listening");
|
|
44
|
+
try {
|
|
45
|
+
applyMSTeamsWebhookTimeouts(server);
|
|
46
|
+
|
|
47
|
+
expect(server.timeout).toBe(30_000);
|
|
48
|
+
expect(server.requestTimeout).toBe(30_000);
|
|
49
|
+
expect(server.headersTimeout).toBe(15_000);
|
|
50
|
+
} finally {
|
|
51
|
+
await closeServer(server);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("applies explicit webhook timeout values", async () => {
|
|
56
|
+
const app = express();
|
|
57
|
+
const server = app.listen(0, "127.0.0.1");
|
|
58
|
+
await once(server, "listening");
|
|
59
|
+
try {
|
|
60
|
+
applyMSTeamsWebhookTimeouts(server, {
|
|
61
|
+
inactivityTimeoutMs: 3210,
|
|
62
|
+
requestTimeoutMs: 6543,
|
|
63
|
+
headersTimeoutMs: 9876,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(server.timeout).toBe(3210);
|
|
67
|
+
expect(server.requestTimeout).toBe(6543);
|
|
68
|
+
expect(server.headersTimeout).toBe(6543);
|
|
69
|
+
} finally {
|
|
70
|
+
await closeServer(server);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("clamps headers timeout when explicit value exceeds request timeout", async () => {
|
|
75
|
+
const app = express();
|
|
76
|
+
const server = app.listen(0, "127.0.0.1");
|
|
77
|
+
await once(server, "listening");
|
|
78
|
+
try {
|
|
79
|
+
applyMSTeamsWebhookTimeouts(server, {
|
|
80
|
+
inactivityTimeoutMs: 12_000,
|
|
81
|
+
requestTimeoutMs: 9_000,
|
|
82
|
+
headersTimeoutMs: 15_000,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(server.timeout).toBe(12_000);
|
|
86
|
+
expect(server.requestTimeout).toBe(9_000);
|
|
87
|
+
expect(server.headersTimeout).toBe(9_000);
|
|
88
|
+
} finally {
|
|
89
|
+
await closeServer(server);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("drops slow-body webhook requests within configured inactivity timeout", async () => {
|
|
94
|
+
const app = express();
|
|
95
|
+
app.use(express.json({ limit: "1mb" }));
|
|
96
|
+
app.use((_req, res, _next) => {
|
|
97
|
+
res.status(401).end("unauthorized");
|
|
98
|
+
});
|
|
99
|
+
app.post("/api/messages", (_req, res) => {
|
|
100
|
+
res.end("ok");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const server = app.listen(0, "127.0.0.1");
|
|
104
|
+
await once(server, "listening");
|
|
105
|
+
try {
|
|
106
|
+
applyMSTeamsWebhookTimeouts(server, {
|
|
107
|
+
inactivityTimeoutMs: 400,
|
|
108
|
+
requestTimeoutMs: 1500,
|
|
109
|
+
headersTimeoutMs: 1500,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const port = (server.address() as AddressInfo).port;
|
|
113
|
+
const closedMs = await waitForSlowBodySocketClose(port, 3000);
|
|
114
|
+
expect(closedMs).toBeLessThan(2500);
|
|
115
|
+
} finally {
|
|
116
|
+
await closeServer(server);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|