Skip to content

Commit c582a54

Browse files
bmendonca3steipete
authored andcommitted
fix(msteams): preserve guarded dispatcher redirects
1 parent cceecc8 commit c582a54

3 files changed

Lines changed: 58 additions & 36 deletions

File tree

‎extensions/msteams/src/attachments/download.ts‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fetchWithBearerAuthScopeFallback } from "openclaw/plugin-sdk";
21
import { getMSTeamsRuntime } from "../runtime.js";
32
import { downloadAndStoreMSTeamsRemoteMedia } from "./remote-media.js";
43
import {
@@ -12,6 +11,7 @@ import {
1211
resolveRequestUrl,
1312
resolveAuthAllowedHosts,
1413
resolveAllowedHosts,
14+
safeFetch,
1515
} from "./shared.js";
1616
import type {
1717
MSTeamsAccessTokenProvider,
@@ -91,16 +91,14 @@ async function fetchWithAuthFallback(params: {
9191
tokenProvider?: MSTeamsAccessTokenProvider;
9292
fetchFn?: typeof fetch;
9393
requestInit?: RequestInit;
94+
allowHosts: string[];
9495
authAllowHosts: string[];
9596
}): Promise<Response> {
96-
return await fetchWithBearerAuthScopeFallback({
97+
const firstAttempt = await safeFetch({
9798
url: params.url,
98-
scopes: scopeCandidatesForUrl(params.url),
99-
tokenProvider: params.tokenProvider,
99+
allowHosts: params.allowHosts,
100100
fetchFn: params.fetchFn,
101101
requestInit: params.requestInit,
102-
requireHttps: true,
103-
shouldAttachAuth: (url) => isUrlAllowed(url, params.authAllowHosts),
104102
});
105103
if (firstAttempt.ok) {
106104
return firstAttempt;
@@ -116,6 +114,7 @@ async function fetchWithAuthFallback(params: {
116114
}
117115

118116
const scopes = scopeCandidatesForUrl(params.url);
117+
const fetchFn = params.fetchFn ?? fetch;
119118
for (const scope of scopes) {
120119
try {
121120
const token = await params.tokenProvider.getAccessToken(scope);
@@ -129,7 +128,6 @@ async function fetchWithAuthFallback(params: {
129128
...params.requestInit,
130129
headers: authHeaders,
131130
},
132-
resolveFn: params.resolveFn,
133131
});
134132
if (authAttempt.ok) {
135133
return authAttempt;
@@ -139,25 +137,6 @@ async function fetchWithAuthFallback(params: {
139137
// be handled by the caller's redirect/error policy.
140138
return authAttempt;
141139
}
142-
143-
const finalUrl =
144-
typeof authAttempt.url === "string" && authAttempt.url ? authAttempt.url : "";
145-
if (!finalUrl || finalUrl === params.url || !isUrlAllowed(finalUrl, params.authAllowHosts)) {
146-
continue;
147-
}
148-
const redirectedAuthAttempt = await safeFetch({
149-
url: finalUrl,
150-
allowHosts: params.allowHosts,
151-
fetchFn,
152-
requestInit: {
153-
...params.requestInit,
154-
headers: authHeaders,
155-
},
156-
resolveFn: params.resolveFn,
157-
});
158-
if (redirectedAuthAttempt.ok) {
159-
return redirectedAuthAttempt;
160-
}
161140
} catch {
162141
// Try the next scope.
163142
}
@@ -262,6 +241,7 @@ export async function downloadMSTeamsAttachments(params: {
262241
tokenProvider: params.tokenProvider,
263242
fetchFn: params.fetchFn,
264243
requestInit: init,
244+
allowHosts,
265245
authAllowHosts,
266246
}),
267247
});

‎extensions/msteams/src/attachments/shared.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
import { describe, expect, it } from "vitest";
1+
import { describe, expect, it, vi } from "vitest";
22
import {
3+
isPrivateOrReservedIP,
34
isUrlAllowed,
5+
resolveAndValidateIP,
46
resolveAllowedHosts,
57
resolveAuthAllowedHosts,
68
resolveMediaSsrfPolicy,
9+
safeFetch,
710
} from "./shared.js";
811

12+
const publicResolve = async () => ({ address: "13.107.136.10" });
13+
const privateResolve = (ip: string) => async () => ({ address: ip });
14+
const failingResolve = async () => {
15+
throw new Error("DNS failure");
16+
};
17+
18+
function mockFetchWithRedirect(redirectMap: Record<string, string>, finalBody = "ok") {
19+
return vi.fn(async (url: string, init?: RequestInit) => {
20+
const target = redirectMap[url];
21+
if (target && init?.redirect === "manual") {
22+
return new Response(null, {
23+
status: 302,
24+
headers: { location: target },
25+
});
26+
}
27+
return new Response(finalBody, { status: 200 });
28+
});
29+
}
30+
931
describe("msteams attachment allowlists", () => {
1032
it("normalizes wildcard host lists", () => {
1133
expect(resolveAllowedHosts(["*", "graph.microsoft.com"])).toEqual(["*"]);
@@ -19,6 +41,13 @@ describe("msteams attachment allowlists", () => {
1941
expect(isUrlAllowed("https://evil.example.com/file.png", allowHosts)).toBe(false);
2042
});
2143

44+
it("builds shared SSRF policy from suffix allowlist", () => {
45+
expect(resolveMediaSsrfPolicy(["sharepoint.com"])).toEqual({
46+
hostnameAllowlist: ["sharepoint.com", "*.sharepoint.com"],
47+
});
48+
expect(resolveMediaSsrfPolicy(["*"])).toBeUndefined();
49+
});
50+
2251
it.each([
2352
["999.999.999.999", true],
2453
["256.0.0.1", true],

‎extensions/msteams/src/attachments/shared.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { lookup } from "node:dns/promises";
12
import {
23
buildHostnameAllowlistPolicyFromSuffixAllowlist,
34
isHttpsUrlAllowedByHostnameSuffixAllowlist,
5+
isPrivateIpAddress,
46
normalizeHostnameSuffixAllowlist,
57
} from "openclaw/plugin-sdk";
68
import type { SsrFPolicy } from "openclaw/plugin-sdk";
@@ -268,6 +270,10 @@ export function isUrlAllowed(url: string, allowlist: string[]): boolean {
268270
return isHttpsUrlAllowedByHostnameSuffixAllowlist(url, allowlist);
269271
}
270272

273+
export function resolveMediaSsrfPolicy(allowHosts: string[]): SsrFPolicy | undefined {
274+
return buildHostnameAllowlistPolicyFromSuffixAllowlist(allowHosts);
275+
}
276+
271277
/**
272278
* Returns true if the given IPv4 or IPv6 address is in a private, loopback,
273279
* or link-local range that must never be reached from media downloads.
@@ -304,11 +310,11 @@ const MAX_SAFE_REDIRECTS = 5;
304310

305311
/**
306312
* Fetch a URL with redirect: "manual", validating each redirect target
307-
* against the hostname allowlist and DNS-resolved IP (anti-SSRF).
313+
* against the hostname allowlist and optional DNS-resolved IP (anti-SSRF).
308314
*
309315
* This prevents:
310316
* - Auto-following redirects to non-allowlisted hosts
311-
* - DNS rebinding attacks where an allowlisted domain resolves to a private IP
317+
* - DNS rebinding attacks when a lookup function is provided
312318
*/
313319
export async function safeFetch(params: {
314320
url: string;
@@ -326,14 +332,19 @@ export async function safeFetch(params: {
326332
);
327333
let currentUrl = params.url;
328334

329-
// Validate the initial URL's resolved IP
330-
try {
331-
const initialHost = new URL(currentUrl).hostname;
332-
await resolveAndValidateIP(initialHost, resolveFn);
333-
} catch {
335+
if (!isUrlAllowed(currentUrl, params.allowHosts)) {
334336
throw new Error(`Initial download URL blocked: ${currentUrl}`);
335337
}
336338

339+
if (resolveFn) {
340+
try {
341+
const initialHost = new URL(currentUrl).hostname;
342+
await resolveAndValidateIP(initialHost, resolveFn);
343+
} catch {
344+
throw new Error(`Initial download URL blocked: ${currentUrl}`);
345+
}
346+
}
347+
337348
for (let i = 0; i <= MAX_SAFE_REDIRECTS; i++) {
338349
const res = await fetchFn(currentUrl, {
339350
...params.requestInit,
@@ -369,8 +380,10 @@ export async function safeFetch(params: {
369380
}
370381

371382
// Validate redirect target's resolved IP
372-
const redirectHost = new URL(redirectUrl).hostname;
373-
await resolveAndValidateIP(redirectHost, resolveFn);
383+
if (resolveFn) {
384+
const redirectHost = new URL(redirectUrl).hostname;
385+
await resolveAndValidateIP(redirectHost, resolveFn);
386+
}
374387

375388
currentUrl = redirectUrl;
376389
}

0 commit comments

Comments
 (0)