Open History to see the last commit
1import * as NodeVM from "node:vm";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 injectMcpAppCsp,
6 injectMcpAppThemeBootstrap,
7 injectMcpAppCookieBootstrap,
8 mcpAppContentSecurityPolicy,
9 mcpAppToolCallableByApp,
10 readMcpAppCsp,
11 readMcpAppReference,
12 readMcpAppResourceUri,
13} from "./mcpApp.ts";
14
15describe("mcpAppContentSecurityPolicy", () => {
16 it("blocks the network and T3's own origin when the app declares nothing", () => {
17 const policy = mcpAppContentSecurityPolicy(undefined);
18 expect(policy).toContain("default-src 'none'");
19 expect(policy).toContain("connect-src 'none'");
20 expect(policy).toContain("frame-src 'none'");
21 expect(policy).not.toContain("'self'");
22 });
23
24 it("allows exactly the declared origins", () => {
25 const policy = mcpAppContentSecurityPolicy({
26 connectDomains: ["https://api.weather.test"],
27 resourceDomains: ["https://*.cdn.test"],
28 });
29 expect(policy).toContain("connect-src https://api.weather.test");
30 expect(policy).toContain("script-src 'unsafe-inline' https://*.cdn.test");
31 expect(policy).toContain("img-src data: blob: https://*.cdn.test");
32 });
33});
34
35describe("injectMcpAppCsp", () => {
36 it("puts the policy after the doctype and ahead of every script", () => {
37 const html = "<!DOCTYPE html><html><head><script>x()</script></head></html>";
38 const injected = injectMcpAppCsp(html, undefined);
39 expect(injected.startsWith('<!DOCTYPE html><meta http-equiv="Content-Security-Policy"')).toBe(
40 true,
41 );
42 expect(injected.indexOf("connect-src 'none'")).toBeLessThan(injected.indexOf("<script>"));
43 expect(injectMcpAppCsp("<p>hi</p>", undefined).startsWith("<!doctype html><meta")).toBe(true);
44 });
45});
46
47describe("injectMcpAppThemeBootstrap", () => {
48 it("keeps stored CSP before the bootstrap and vendor scripts after it", () => {
49 const snapshot = injectMcpAppCsp(
50 "<html><head><script>vendor()</script></head></html>",
51 undefined,
52 );
53 const html = injectMcpAppThemeBootstrap(snapshot);
54 expect(html.indexOf("Content-Security-Policy")).toBeLessThan(html.indexOf("<script>"));
55 expect(html.indexOf("window.addEventListener")).toBeLessThan(html.indexOf("vendor()"));
56 expect(html.endsWith(snapshot.slice(snapshot.indexOf("<html>")))).toBe(true);
57 });
58
59 it("applies parent initialize and theme changes while ignoring unrelated or invalid messages", () => {
60 const html = injectMcpAppThemeBootstrap(injectMcpAppCsp("<p>app</p>", undefined));
61 const script = html.slice(html.indexOf("<script>") + 8, html.indexOf("</script>"));
62 const parent = {};
63 const style = { colorScheme: "normal" };
64 let receive: ((event: { source: object; data: unknown }) => void) | undefined;
65 NodeVM.runInNewContext(script, {
66 window: {
67 parent,
68 addEventListener: (_type: string, callback: typeof receive) => {
69 receive = callback;
70 },
71 },
72 document: { documentElement: { style } },
73 });
74 const send = (data: unknown, source = parent) => receive?.({ source, data });
75 const initialized = { jsonrpc: "2.0", id: 1, result: { hostContext: { theme: "dark" } } };
76 send(initialized, {});
77 expect(style.colorScheme).toBe("normal");
78 send(initialized);
79 expect(style.colorScheme).toBe("dark");
80 for (const data of [
81 null,
82 "bad",
83 {},
84 { ...initialized, jsonrpc: "1.0" },
85 { jsonrpc: "2.0", method: "other", params: { theme: "light" } },
86 {
87 jsonrpc: "2.0",
88 method: "ui/notifications/host-context-changed",
89 params: { theme: "normal" },
90 },
91 { jsonrpc: "2.0", result: { hostContext: { theme: "light" } } },
92 ])
93 send(data);
94 expect(style.colorScheme).toBe("dark");
95 send({
96 jsonrpc: "2.0",
97 method: "ui/notifications/host-context-changed",
98 params: { theme: "light" },
99 });
100 expect(style.colorScheme).toBe("light");
101 send(initialized);
102 expect(style.colorScheme).toBe("dark");
103 });
104});
105
106describe("readMcpAppCsp", () => {
107 it("drops entries that could widen or break the policy", () => {
108 expect(
109 readMcpAppCsp({
110 connectDomains: [
111 "https://ok.test",
112 "*",
113 "'self'",
114 "https://evil.test; script-src *",
115 "http://localhost:3000",
116 "javascript:alert(1)",
117 ],
118 resourceDomains: "https://not-an-array.test",
119 }),
120 ).toEqual({ connectDomains: ["https://ok.test", "http://localhost:3000"] });
121 expect(readMcpAppCsp({ connectDomains: ["*"] })).toBeUndefined();
122 });
123});
124
125describe("readMcpAppReference", () => {
126 it("requires a ui:// resource and the owning server and tool", () => {
127 const reference = {
128 attachmentId: "thread-1-abc-html",
129 server: "weather",
130 tool: "get_weather",
131 resourceUri: "ui://weather/dashboard",
132 };
133 expect(readMcpAppReference(reference)).toEqual(reference);
134 expect(readMcpAppReference({ ...reference, resourceUri: "https://x.test" })).toBeUndefined();
135 expect(readMcpAppReference({ ...reference, server: "" })).toBeUndefined();
136 });
137});
138
139describe("tool metadata", () => {
140 it("reads the resource URI from either spelling and honors app visibility", () => {
141 expect(readMcpAppResourceUri({ ui: { resourceUri: "ui://a/b" } })).toBe("ui://a/b");
142 expect(readMcpAppResourceUri({ "ui/resourceUri": "ui://a/c" })).toBe("ui://a/c");
143 expect(readMcpAppResourceUri({ ui: { resourceUri: "https://a" } })).toBeUndefined();
144 expect(mcpAppToolCallableByApp(undefined)).toBe(true);
145 expect(mcpAppToolCallableByApp({ ui: { visibility: ["model"] } })).toBe(false);
146 expect(mcpAppToolCallableByApp({ ui: { visibility: ["model", "app"] } })).toBe(true);
147 });
148});
149
150describe("mcpAppFromToolItem", () => {
151 it("reads the app from both the stored output and its compact wire form", async () => {
152 const { compactDynamicToolOutput, mcpAppFromToolItem } = await import("./toolOutput.ts");
153 const app = {
154 attachmentId: "thread-1-abc-html",
155 server: "weather",
156 tool: "get_weather",
157 resourceUri: "ui://weather/dashboard",
158 };
159 const stored = { t3McpApp: app, result: { content: [] } };
160 const toolName = "weather.get_weather";
161 expect(mcpAppFromToolItem({ toolName, output: stored })).toEqual(app);
162 expect(mcpAppFromToolItem({ toolName, output: compactDynamicToolOutput(stored) })).toEqual(app);
163 });
164
165 it("ignores a reference naming a server or tool other than the item's own", async () => {
166 const { mcpAppFromToolItem } = await import("./toolOutput.ts");
167 const forged = {
168 t3McpApp: {
169 attachmentId: "thread-1-abc-html",
170 server: "bank",
171 tool: "transfer",
172 resourceUri: "ui://bank/app",
173 },
174 };
175 expect(mcpAppFromToolItem({ toolName: "evil.lookup", output: forged })).toBeUndefined();
176 expect(mcpAppFromToolItem({ toolName: "bank.transfer", output: forged })).toBeDefined();
177 });
178
179 it("keeps an app whose resource URI is longer than a name", async () => {
180 const { compactDynamicToolOutput } = await import("./toolOutput.ts");
181 const resourceUri = `ui://weather/${"segment/".repeat(60)}dashboard`;
182 const output = compactDynamicToolOutput({
183 t3McpApp: {
184 attachmentId: "thread-1-abc-html",
185 server: "weather",
186 tool: "get_weather",
187 resourceUri,
188 },
189 });
190 expect(output?.t3McpApp?.resourceUri).toBe(resourceUri);
191 });
192
193 it("keeps the compact app reference within the wire budget", async () => {
194 const { compactDynamicToolOutput } = await import("./toolOutput.ts");
195 const domains = Array.from(
196 { length: 32 },
197 (_, index) => `https://${"a".repeat(200)}${index}.test`,
198 );
199 const output = compactDynamicToolOutput({
200 t3McpApp: {
201 attachmentId: "thread-1-abc-html",
202 server: "weather",
203 tool: "get_weather",
204 resourceUri: "ui://weather/dashboard",
205 csp: { connectDomains: domains, resourceDomains: domains },
206 },
207 });
208 expect(new TextEncoder().encode(JSON.stringify(output)).byteLength).toBeLessThanOrEqual(8_192);
209 expect(output?.t3McpApp?.server).toBe("weather");
210 });
211});
212
213describe("MCP App cookie bootstrap", () => {
214 const vendor = `<script>document.cookie.split(";");document.cookie="analytics=1";document.root="rendered";</script>`;
215 const execute = (html: string, document: object) => {
216 for (const script of html.matchAll(/<script>([\s\S]*?)<\/script>/g)) {
217 NodeVM.runInNewContext(script[1]!, { document });
218 }
219 };
220
221 it("lets cookie-reading startup code render in an opaque document without granting storage", () => {
222 const blocked = new DOMException("Opaque origin", "SecurityError");
223 const prototype = Object.defineProperty({}, "cookie", {
224 get() {
225 throw blocked;
226 },
227 set() {
228 throw blocked;
229 },
230 });
231 const document = Object.assign(
232 Object.create(prototype) as { cookie: string; root: string; indexedDB: unknown },
233 { root: "" },
234 );
235 Object.defineProperty(document, "indexedDB", {
236 get() {
237 throw blocked;
238 },
239 });
240 const saved = injectMcpAppCsp(vendor, { connectDomains: ["https://analytics.example"] });
241 const html = injectMcpAppCookieBootstrap(saved);
242 expect(html.indexOf("Content-Security-Policy")).toBeLessThan(html.indexOf("<script>"));
243 expect(html).toContain("connect-src https://analytics.example");
244 execute(html, document);
245 expect(document.root).toBe("rendered");
246 expect(document.cookie).toBe("");
247 document.cookie = "session=secret";
248 expect(document.cookie).toBe("");
249 expect(() => document.indexedDB).toThrow(blocked);
250 expect(() => Reflect.get(prototype, "cookie")).toThrow(blocked);
251 });
252
253 it("leaves functioning cookie reads and writes unchanged", () => {
254 const document = { cookie: "existing=1", root: "" };
255 execute(injectMcpAppCookieBootstrap(vendor), document);
256 expect(document).toEqual({ cookie: "analytics=1", root: "rendered" });
257 });
258
259 it("leaves other cookie failures unchanged", () => {
260 const failure = new TypeError("Unexpected cookie failure");
261 const document = Object.defineProperty({}, "cookie", {
262 get() {
263 throw failure;
264 },
265 });
266 expect(() => execute(injectMcpAppCookieBootstrap(vendor), document)).toThrow(failure);
267 expect(() => Reflect.get(document, "cookie")).toThrow(failure);
268 });
269});