Skip to content

Commit a3a5bfa

Browse files
cliffhallclaude
andcommitted
review: distinguish links, cap batch size, comment truncation (PR #1657)
Address the claude[bot] review: - #1 (applied): the confirmation now reads "download or open N item(s)" and resource_links are prefixed "↗" in the summary, so a link that opens in a tab is distinguishable from an embedded file that saves to disk. - #2 (applied): cap a single ui/download-file batch at MAX_DOWNLOAD_ITEMS (20); an oversized batch is rejected (isError) before the prompt, with a warn. - #3 (declined): can't check window.open's return value to detect a blocked popup — we pass `noopener`, and per spec window.open returns null even on a successful noopener open, so null can't distinguish blocked from succeeded. - #4b (applied, comment): note the start-of-label truncation is intentional so a link's scheme+host stays visible for the trust decision. Adds tests: oversized batch rejected without confirming/acting; resource-link ↗ prefix in the summary. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BrihGWcrM9JGRyu41nzZYw
1 parent 9ffaa42 commit a3a5bfa

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎clients/web/src/components/elements/AppRenderer/createAppBridgeFactory.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,5 +669,38 @@ describe("createAppBridgeFactory", () => {
669669
// The clamped label is 80 chars max (77 + "...").
670670
expect(prompt).not.toContain(longName);
671671
});
672+
673+
it("rejects an oversized batch without confirming or acting", async () => {
674+
const confirm = stubConfirm(true);
675+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
676+
const open = vi
677+
.spyOn(window, "open")
678+
.mockImplementation(() => null as never);
679+
const bridge = await buildBridge();
680+
// 21 items exceeds the 20-item cap → rejected before the prompt.
681+
const contents = Array.from({ length: 21 }, (_, i) => ({
682+
type: "resource_link" as const,
683+
uri: `https://example.com/${i}.pdf`,
684+
}));
685+
await expect(bridge.ondownloadfile!({ contents })).resolves.toEqual({
686+
isError: true,
687+
});
688+
expect(confirm).not.toHaveBeenCalled();
689+
expect(open).not.toHaveBeenCalled();
690+
expect(warn).toHaveBeenCalledWith(
691+
expect.stringContaining("refusing download batch of 21 items"),
692+
);
693+
warn.mockRestore();
694+
});
695+
696+
it("marks resource links with a ↗ prefix in the confirmation summary", async () => {
697+
const confirm = stubConfirm(false);
698+
const bridge = await buildBridge();
699+
await bridge.ondownloadfile!({
700+
contents: [{ type: "resource_link", uri: "https://example.com/a.pdf" }],
701+
});
702+
const prompt = confirm.mock.calls[0][0] as string;
703+
expect(prompt).toContain("↗ https://example.com/a.pdf");
704+
});
672705
});
673706
});

‎clients/web/src/components/elements/AppRenderer/createAppBridgeFactory.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ function base64ToBytes(b64: string): Uint8Array<ArrayBuffer> {
106106
return bytes;
107107
}
108108

109+
/**
110+
* Upper bound on items honored from a single `ui/download-file` request. One
111+
* user approval must not fan out into an unbounded number of saves / new tabs.
112+
*/
113+
const MAX_DOWNLOAD_ITEMS = 20;
114+
109115
/**
110116
* Strip control characters and clamp length so a server-supplied filename or
111117
* URI cannot forge additional lines in the confirmation prompt or push the
@@ -115,12 +121,24 @@ function sanitizeDownloadLabel(label: string): string {
115121
// Cc = control chars (newlines, escape, etc.); Cf = format chars (bidi
116122
// overrides, zero-width joiners, BOM) — both can spoof or reflow the prompt.
117123
const cleaned = label.replace(/[\p{Cc}\p{Cf}]+/gu, " ").trim();
124+
// Keep the START of an over-long label: for a link that preserves the
125+
// scheme+host, which is what the user needs to make a trust decision.
118126
return cleaned.length > 80 ? cleaned.slice(0, 77) + "..." : cleaned;
119127
}
120128

121-
/** Human-readable label for a download item, shown in the confirmation prompt. */
122-
function describeDownloadItem(item: EmbeddedResource | ResourceLink): string {
123-
if (item.type === "resource_link") return item.uri;
129+
/**
130+
* Human-readable label for a download item, shown in the confirmation prompt.
131+
* `forPrompt` marks a resource_link with a leading "↗" so the user can tell a
132+
* link that will *open in a tab* apart from an embedded file that will *save to
133+
* disk* — the two item kinds share this "download" confirmation.
134+
*/
135+
function describeDownloadItem(
136+
item: EmbeddedResource | ResourceLink,
137+
forPrompt = false,
138+
): string {
139+
if (item.type === "resource_link") {
140+
return forPrompt ? `↗ ${item.uri}` : item.uri;
141+
}
124142
return fileNameFromUri(item.resource.uri);
125143
}
126144

@@ -259,16 +277,26 @@ export function createAppBridgeFactory(
259277
// The view asks the host to save MCP resource contents to disk (sandboxed
260278
// iframes can't download directly). Confirm with the user first — the spec
261279
// requires a host-mediated confirmation — then write each item out. A
262-
// declined prompt, an empty payload, or a thrown error all return isError.
280+
// declined prompt, an empty/oversized payload, or a thrown error all
281+
// return isError.
263282
bridge.ondownloadfile = async ({ contents }) => {
264283
if (!Array.isArray(contents) || contents.length === 0) {
265284
return { isError: true };
266285
}
286+
// Sanity cap: one approval must not fan out into an unbounded number of
287+
// downloads / new tabs. A buggy or hostile app requesting hundreds of
288+
// items is rejected outright rather than acted on.
289+
if (contents.length > MAX_DOWNLOAD_ITEMS) {
290+
console.warn(
291+
`[mcp-app] refusing download batch of ${contents.length} items (max ${MAX_DOWNLOAD_ITEMS})`,
292+
);
293+
return { isError: true };
294+
}
267295
const summary = contents
268-
.map((item) => sanitizeDownloadLabel(describeDownloadItem(item)))
296+
.map((item) => sanitizeDownloadLabel(describeDownloadItem(item, true)))
269297
.join("\n");
270298
const approved = window.confirm(
271-
`This MCP App wants to download ${contents.length} file(s):\n\n${summary}`,
299+
`This MCP App wants to download or open ${contents.length} item(s):\n\n${summary}`,
272300
);
273301
if (!approved) return { isError: true };
274302
let succeeded = 0;

0 commit comments

Comments
 (0)