Skip to content

Commit a38b88a

Browse files
wanyongstarsallyom
andauthored
fix(msteams): require dot boundary for shared-link host suffix match (#123046)
* fix(msteams): require dot boundary for shared-link host suffix match Look-alike hosts such as evil1drv.ms, notonedrive.com, or fakeonedrive.live.com satisfied the bare endsWith() check in isGraphSharedLinkUrl and were rewritten to the Graph shares endpoint. Match bare suffixes on a label boundary instead: exact host or a dot-prefixed suffix. * test(msteams): cover lookalike download routing * fix(msteams): require https for shared links --------- Co-authored-by: sallyom <[email protected]>
1 parent 08c39dd commit a38b88a

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,15 @@ describe("msteams attachments", () => {
718718
expect(tokenProvider.getAccessToken).toHaveBeenCalled();
719719
});
720720

721-
it("falls through to direct fetch for non-shared-link URLs", async () => {
722-
const directUrl = createTestUrl("direct.pdf");
723-
const fetchMock = createOkFetchMock(CONTENT_TYPE_APPLICATION_PDF, "pdf");
721+
it("keeps look-alike hosts out of Graph shares and auth fallback", async () => {
722+
const directUrl = "https://notonedrive.com/direct.pdf";
723+
const tokenProvider = createTokenProvider();
724+
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
725+
const url = resolveRequestUrl(input);
726+
return url.startsWith(GRAPH_SHARES_URL_PREFIX)
727+
? createTextResponse("unauthorized", 401)
728+
: createBufferResponse(PDF_BUFFER, CONTENT_TYPE_APPLICATION_PDF);
729+
});
724730
detectMimeMock.mockResolvedValueOnce(CONTENT_TYPE_APPLICATION_PDF);
725731
saveMediaBufferMock.mockResolvedValueOnce({
726732
id: "saved.pdf",
@@ -729,9 +735,13 @@ describe("msteams attachments", () => {
729735
contentType: CONTENT_TYPE_APPLICATION_PDF,
730736
});
731737

732-
const media = await downloadAttachmentsWithFetch(
733-
createPdfAttachments(directUrl),
734-
fetchMock,
738+
const media = await downloadMSTeamsAttachments(
739+
buildDownloadParams(createPdfAttachments(directUrl), {
740+
tokenProvider,
741+
allowHosts: ["notonedrive.com", GRAPH_HOST],
742+
authAllowHosts: [GRAPH_HOST],
743+
fetchFn: asFetchFn(fetchMock),
744+
}),
735745
);
736746

737747
expectAttachmentMediaLength(media, 1);
@@ -742,6 +752,25 @@ describe("msteams attachments", () => {
742752
// Should have hit the original host, NOT graph shares.
743753
expect(calledUrls).toContain(directUrl);
744754
expect(calledUrls.some((url) => url.startsWith(GRAPH_SHARES_URL_PREFIX))).toBe(false);
755+
expect(tokenProvider.getAccessToken).not.toHaveBeenCalled();
756+
});
757+
758+
it("rejects non-HTTPS shared-link hosts before fetch or auth fallback", async () => {
759+
const tokenProvider = createTokenProvider();
760+
const fetchMock = vi.fn(async () => createTextResponse("unauthorized", 401));
761+
762+
await downloadAttachmentsWithFetch(
763+
createPdfAttachments("http://onedrive.com/direct.pdf"),
764+
fetchMock,
765+
{
766+
tokenProvider,
767+
allowHosts: ["onedrive.com", GRAPH_HOST],
768+
authAllowHosts: [GRAPH_HOST],
769+
},
770+
{ expectFetchCalled: false },
771+
);
772+
773+
expect(tokenProvider.getAccessToken).not.toHaveBeenCalled();
745774
});
746775
});
747776

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ describe("Graph shared-link helpers", () => {
534534
["https://graph.microsoft.com/v1.0/me", false],
535535
["https://smba.trafficmanager.net/amer/v3", false],
536536
["https://example.com/file.pdf", false],
537+
["https://notonedrive.com/x", false],
538+
["https://evil1drv.ms/x", false],
539+
["https://fakeonedrive.live.com/x", false],
540+
["https://evilsharepoint.com/x", false],
541+
["http://onedrive.com/x", false],
537542
["not-a-url", false],
538543
])("isGraphSharedLinkUrl(%s) === %s", (url, expected) => {
539544
expect(isGraphSharedLinkUrl(url)).toBe(expected);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,20 @@ const GRAPH_SHARED_LINK_HOST_SUFFIXES = [
165165
* than directly.
166166
*/
167167
function isGraphSharedLinkUrl(url: string): boolean {
168-
let host: string;
168+
let parsed: URL;
169169
try {
170-
host = normalizeLowercaseStringOrEmpty(new URL(url).hostname);
170+
parsed = new URL(url);
171171
} catch {
172172
return false;
173173
}
174-
if (!host) {
174+
const host = normalizeLowercaseStringOrEmpty(parsed.hostname);
175+
if (parsed.protocol !== "https:" || !host) {
175176
return false;
176177
}
177-
return GRAPH_SHARED_LINK_HOST_SUFFIXES.some((suffix) => host === suffix || host.endsWith(suffix));
178+
// Only HTTPS URLs on a DNS label boundary may select the authenticated Graph path.
179+
return GRAPH_SHARED_LINK_HOST_SUFFIXES.some(
180+
(suffix) => host === suffix || host.endsWith(suffix.startsWith(".") ? suffix : `.${suffix}`),
181+
);
178182
}
179183

180184
/**

0 commit comments

Comments
 (0)